Beginning Programming With Python For Dummies , 2nd Edition

3y ago
74 Views
11 Downloads
8.61 MB
411 Pages
Last View : 26d ago
Last Download : 3m ago
Upload by : Gannon Casey
Transcription

BeginningProgrammingwith Python 2nd Editionby John Paul Mueller

Beginning Programming with Python For Dummies , 2nd EditionPublished by: John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030-5774, www.wiley.comCopyright 2018 by John Wiley & Sons, Inc., Hoboken, New JerseyPublished simultaneously in CanadaNo part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by anymeans, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections107 or 108 of the 1976 United States Copyright Act, without the prior written permission of the Publisher. Requests tothe Publisher for permission should be addressed to the Permissions Department, John Wiley & Sons, Inc., 111 RiverStreet, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-6008, or online at http://www.wiley.com/go/permissions.Trademarks: Wiley, For Dummies, the Dummies Man logo, Dummies.com, Making Everything Easier, and relatedtrade dress are trademarks or registered trademarks of John Wiley & Sons, Inc. and may not be used without writtenpermission. Python is a registered trademark of Python Software Foundation Corporation. All other trademarks are theproperty of their respective owners. John Wiley & Sons, Inc. is not associated with any product or vendor mentioned inthis book.LIMIT OF LIABILITY/DISCLAIMER OF WARRANTY: THE PUBLISHER AND THE AUTHOR MAKE NOREPRESENTATIONS OR WARRANTIES WITH RESPECT TO THE ACCURACY OR COMPLETENESS OF THE CONTENTSOF THIS WORK AND SPECIFICALLY DISCLAIM ALL WARRANTIES, INCLUDING WITHOUT LIMITATION WARRANTIESOF FITNESS FOR A PARTICULAR PURPOSE. NO WARRANTY MAY BE CREATED OR EXTENDED BY SALES ORPROMOTIONAL MATERIALS. THE ADVICE AND STRATEGIES CONTAINED HEREIN MAY NOT BE SUITABLE FOREVERY SITUATION. THIS WORK IS SOLD WITH THE UNDERSTANDING THAT THE PUBLISHER IS NOT ENGAGEDIN RENDERING LEGAL, ACCOUNTING, OR OTHER PROFESSIONAL SERVICES. IF PROFESSIONAL ASSISTANCE ISREQUIRED, THE SERVICES OF A COMPETENT PROFESSIONAL PERSON SHOULD BE SOUGHT. NEITHER THEPUBLISHER NOR THE AUTHOR SHALL BE LIABLE FOR DAMAGES ARISING HEREFROM. THE FACT THAT ANORGANIZATION OR WEBSITE IS REFERRED TO IN THIS WORK AS A CITATION AND/OR A POTENTIAL SOURCE OFFURTHER INFORMATION DOES NOT MEAN THAT THE AUTHOR OR THE PUBLISHER ENDORSES THE INFORMATIONTHE ORGANIZATION OR WEBSITE MAY PROVIDE OR RECOMMENDATIONS IT MAY MAKE. FURTHER, READERSSHOULD BE AWARE THAT INTERNET WEBSITES LISTED IN THIS WORK MAY HAVE CHANGED OR DISAPPEAREDBETWEEN WHEN THIS WORK WAS WRITTEN AND WHEN IT IS READ.For general information on our other products and services, please contact our Customer Care Department withinthe U.S. at 877-762-2974, outside the U.S. at 317-572-3993, or fax 317-572-4002. For technical support, please es.Wiley publishes in a variety of print and electronic formats and by print-on-demand. Some material included withstandard print versions of this book may not be included in e-books or in print-on-demand. If this book refers tomedia such as a CD or DVD that is not included in the version you purchased, you may download this material athttp://booksupport.wiley.com. For more information about Wiley products, visit www.wiley.com.Library of Congress Control Number: 2017964018ISBN 978-1-119-45789-3; ISBN 978-1-119-45787-9 (ebk); ISBN 978-1-119-45790-9 (ebk)Manufactured in the United States of America10 9 8 7 6 5 4 3 2 1

Contents at a GlanceIntroduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1Part 1: Getting Started with Python. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5CHAPTER 1:CHAPTER 2:CHAPTER 3:CHAPTER 4:CHAPTER 5:Talking to Your Computer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7Getting Your Own Copy of Python. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21Interacting with Python. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37Writing Your First Application. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55Working with Anaconda. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83Part 2: Talking the Talk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .101Storing and Modifying Information. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .CHAPTER 7: Managing Information. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .CHAPTER 8: Making Decisions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .CHAPTER 9: Performing Repetitive Tasks. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .CHAPTER 10: Dealing with Errors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .103113135151165Part 3: Performing Common Tasks. . . . . . . . . . . . . . . . . . . . . . . . . . .195Interacting with Packages. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .CHAPTER 12: Working with Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .CHAPTER 13: Managing Lists. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .CHAPTER 14: Collecting All Sorts of Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .CHAPTER 15: Creating and Using Classes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .197225243261281Part 4: Performing Advanced Tasks. . . . . . . . . . . . . . . . . . . . . . . . . .303CHAPTER 6:CHAPTER 11:Storing Data in Files. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305CHAPTER 17: Sending an Email. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323CHAPTER 16:Part 5: The Part of Tens. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .341Ten Amazing Programming Resources. . . . . . . . . . . . . . . . . . . . . . . . . . .Ten Ways to Make a Living with Python. . . . . . . . . . . . . . . . . . . . . . . . . .CHAPTER 20: Ten Tools That Enhance Your Python Experience. . . . . . . . . . . . . . . . . .CHAPTER 21: Ten (Plus) Libraries You Need to Know About. . . . . . . . . . . . . . . . . . . . .343353361371Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .379CHAPTER 18:CHAPTER 19:

Table of ContentsINTRODUCTION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1About This Book. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Foolish Assumptions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Icons Used in This Book. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Beyond the Book. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Where to Go from Here . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .12334PART 1: GETTING STARTED WITH PYTHON. . . . . . . . . . . . . . . . . . . . 5CHAPTER 1:Talking to Your Computer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7Understanding Why You Want to Talk to Your Computer. . . . . . . . . . . . 8Knowing that an Application is a Form of Communication. . . . . . . . . . . 9Thinking about procedures you use daily . . . . . . . . . . . . . . . . . . . . . . 9Writing procedures down. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10Seeing applications as being like any other procedure. . . . . . . . . . 11Understanding that computers take things literally. . . . . . . . . . . . . 11Defining What an Application Is . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11Understanding that computers use a special language . . . . . . . . . 12Helping humans speak to the computer. . . . . . . . . . . . . . . . . . . . . . 12Understanding Why Python Is So Cool. . . . . . . . . . . . . . . . . . . . . . . . . . . 14Unearthing the reasons for using Python . . . . . . . . . . . . . . . . . . . . . 14Deciding how you can personally benefit from Python. . . . . . . . . . 15Discovering which organizations use Python . . . . . . . . . . . . . . . . . . 16Finding useful Python applications. . . . . . . . . . . . . . . . . . . . . . . . . . . 17Comparing Python to other languages . . . . . . . . . . . . . . . . . . . . . . . 18CHAPTER 2:Getting Your Own Copy of Python. . . . . . . . . . . . . . . . . . . . . 21Downloading the Version You Need. . . . . . . . . . . . . . . . . . . . . . . . . . . . .Installing Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Working with Windows. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Working with the Mac. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Working with Linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Accessing Python on Your Machine . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Using Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Using the Mac . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Using Linux. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Testing Your Installation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Table of Contents21242527283132343535v

CHAPTER 3:Interacting with Python. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37Opening the Command Line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Starting Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Using the command line to your advantage. . . . . . . . . . . . . . . . . . .Using Python environment variables to your advantage . . . . . . . .Typing a Command. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Telling the computer what to do. . . . . . . . . . . . . . . . . . . . . . . . . . . . .Telling the computer you’re done. . . . . . . . . . . . . . . . . . . . . . . . . . . .Seeing the result . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Using Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Getting into help mode. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Asking for help. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Leaving help mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Obtaining help directly. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Closing the Command Line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .CHAPTER 4:3838394143434444464647495051Writing Your First Application. . . . . . . . . . . . . . . . . . . . . . . . . . 55Understanding Why IDEs Are Important. . . . . . . . . . . . . . . . . . . . . . . . . 56Creating bet

vi Beginning Programming with Python For Dummies CHAPTER 3: Interacting with Python. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 Opening the .

Related Documents:

Python Programming for the Absolute Beginner Second Edition. CONTENTS CHAPTER 1 GETTING STARTED: THE GAME OVER PROGRAM 1 Examining the Game Over Program 2 Introducing Python 3 Python Is Easy to Use 3 Python Is Powerful 3 Python Is Object Oriented 4 Python Is a "Glue" Language 4 Python Runs Everywhere 4 Python Has a Strong Community 4 Python Is Free and Open Source 5 Setting Up Python on .

Python 2 versus Python 3 - the great debate Installing Python Setting up the Python interpreter About virtualenv Your first virtual environment Your friend, the console How you can run a Python program Running Python scripts Running the Python interactive shell Running Python as a service Running Python as a GUI application How is Python code .

Python is readable 5 Python is complete—"batteries included" 6 Python is cross-platform 6 Python is free 6 1.3 What Python doesn't do as well 7 Python is not the fastest language 7 Python doesn't have the most libraries 8 Python doesn't check variable types at compile time 8 1.4 Why learn Python 3? 8 1.5 Summary 9

site "Python 2.x is legacy, Python 3.x is the present and future of the language". In addition, "Python 3 eliminates many quirks that can unnecessarily trip up beginning programmers". However, note that Python 2 is currently still rather widely used. Python 2 and 3 are about 90% similar. Hence if you learn Python 3, you will likely

Bruksanvisning för bilstereo . Bruksanvisning for bilstereo . Instrukcja obsługi samochodowego odtwarzacza stereo . Operating Instructions for Car Stereo . 610-104 . SV . Bruksanvisning i original

A Python Book A Python Book: Beginning Python, Advanced Python, and Python Exercises Author: Dave Kuhlman Contact: dkuhlman@davekuhlman.org

There are currently two versions of Python in use; Python 2 and Python 3. Python 3 is not backward compatible with Python 2. A lot of the imported modules were only available in Python 2 for quite some time, leading to a slow adoption of Python 3. However, this not really an issue anymore. Support for Python 2 will end in 2020.

Python Programming - This is a textbook in Python Programming with lots of Practical Examples and Exercises. You will learn the necessary foundation for basic programming with focus on Python. Python for Science and Engineering - This is a textbook in Python Programming with lots of Examples, Exercises, and Practical Applications