ebook img

Python All-In-One for Dummies PDF

703 Pages·2019·40.89 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Python All-In-One for Dummies

Python A L L - I N - O N E by John Shovic and Alan Simpson Python All-in-One For Dummies® Published by: John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030-5774, www.wiley.com Copyright © 2019 by John Wiley & Sons, Inc., Hoboken, New Jersey Published simultaneously in Canada No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without the prior written permission of the Publisher. Requests to the Publisher for permission should be addressed to the Permissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-6008, or online at www.wiley.com/go/permissions. Trademarks: Wiley, For Dummies, the Dummies Man logo, Dummies.com, Making Everything Easier, and related trade dress are trademarks or registered trademarks of John Wiley & Sons, Inc. and may not be used without written permission. All other trademarks are the property of their respective owners. John Wiley & Sons, Inc. is not associated with any product or vendor mentioned in this book. LIMIT OF LIABILITY/DISCLAIMER OF WARRANTY: THE PUBLISHER AND THE AUTHOR MAKE NO REPRESENTATIONS OR WARRANTIES WITH RESPECT TO THE ACCURACY OR COMPLETENESS OF THE CONTENTS OF THIS WORK AND SPECIFICALLY DISCLAIM ALL WARRANTIES, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE. NO WARRANTY MAY BE CREATED OR EXTENDED BY SALES OR PROMOTIONAL MATERIALS. THE ADVICE AND STRATEGIES CONTAINED HEREIN MAY NOT BE SUITABLE FOR EVERY SITUATION. THIS WORK IS SOLD WITH THE UNDERSTANDING THAT THE PUBLISHER IS NOT ENGAGED IN RENDERING LEGAL, ACCOUNTING, OR OTHER PROFESSIONAL SERVICES. IF PROFESSIONAL ASSISTANCE IS REQUIRED, THE SERVICES OF A COMPETENT PROFESSIONAL PERSON SHOULD BE SOUGHT. NEITHER THE PUBLISHER NOR THE AUTHOR SHALL BE LIABLE FOR DAMAGES ARISING HEREFROM. THE FACT THAT AN ORGANIZATION OR WEBSITE IS REFERRED TO IN THIS WORK AS A CITATION AND/OR A POTENTIAL SOURCE OF FURTHER INFORMATION DOES NOT MEAN THAT THE AUTHOR OR THE PUBLISHER ENDORSES THE INFORMATION THE ORGANIZATION OR WEBSITE MAY PROVIDE OR RECOMMENDATIONS IT MAY MAKE. FURTHER, READERS SHOULD BE AWARE THAT INTERNET WEBSITES LISTED IN THIS WORK MAY HAVE CHANGED OR DISAPPEARED BETWEEN 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 within the U.S. at 877-762-2974, outside the U.S. at 317-572-3993, or fax 317-572-4002. For technical support, please visit https://hub.wiley.com/community/support/dummies. Wiley publishes in a variety of print and electronic formats and by print-on-demand. Some material included with standard print versions of this book may not be included in e-books or in print-on-demand. If this book refers to media such as a CD or DVD that is not included in the version you purchased, you may download this material at http://booksupport.wiley.com. For more information about Wiley products, visit www.wiley.com. Library of Congress Control Number: 2019937504 ISBN 978-1-119-55759-3 (pbk); ISBN 978-1-119-55767-8 (ebk); ISBN 978-1-119-55761-6 (ebk) Manufactured in the United States of America 10 9 8 7 6 5 4 3 2 1 Contents at a Glance Python All-in-One Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Book 1: Getting Started with Python . . . . . . . . . . . . . . . . . . . . . . . . . . .5 CHAPTER 1: Starting with Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7 CHAPTER 2: Interactive Mode, Getting Help, Writing Apps . . . . . . . . . . . . . . . . . . . . . . 27 CHAPTER 3: Python Elements and Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 CHAPTER 4: Building Your First Python Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 Book 2: Understanding Python Building Blocks . . . . . . . . . . . . . 83 CHAPTER 1: Working with Numbers, Text, and Dates . . . . . . . . . . . . . . . . . . . . . . . . . . 85 CHAPTER 2: Controlling the Action . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 CHAPTER 3: Speeding Along with Lists and Tuples . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147 CHAPTER 4: Cruising Massive Data with Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . 169 CHAPTER 5: Wrangling Bigger Chunks of Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193 CHAPTER 6: Doing Python with Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213 CHAPTER 7: Sidestepping Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247 Book 3: Working with Python Libraries . . . . . . . . . . . . . . . . . . . . . . 265 CHAPTER 1: Working with External Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267 CHAPTER 2: Juggling JSON Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303 CHAPTER 3: Interacting with the Internet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323 CHAPTER 4: Libraries, Packages, and Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339 Book 4: Using Artificial Intelligence in Python . . . . . . . . . . . . . 353 CHAPTER 1: Exploring Artificial Intelligence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355 CHAPTER 2: Building a Neural Network in Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365 CHAPTER 3: Doing Machine Learning in Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393 CHAPTER 4: Exploring More AI in Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 415 Book 5: Doing Data Science with Python . . . . . . . . . . . . . . . . . . . . 427 CHAPTER 1: The Five Areas of Data Science . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 .29 CHAPTER 2: Exploring Big Data with Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 437 CHAPTER 3: Using Big Data from the Google Cloud . . . . . . . . . . . . . . . . . . . . . . . . . . . 451 Book 6: Talking to Hardware with Python . . . . . . . . . . . . . . . . . . 469 CHAPTER 1: Introduction to Physical Computing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 471 CHAPTER 2: No Soldering! Grove Connectors for Building Things . . . . . . . . . . . . . . 487 CHAPTER 3: Sensing the World with Python: The World of I2C . . . . . . . . . . . . . . . . . 505 CHAPTER 4: Making Things Move with Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 537 Book 7: Building Robots with Python . . . . . . . . . . . . . . . . . . . . . . . . 565 CHAPTER 1: Introduction to Robotics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 567 CHAPTER 2: Building Your First Python Robot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 575 CHAPTER 3: Programming Your Robot Rover in Python . . . . . . . . . . . . . . . . . . . . . . . 595 CHAPTER 4: Using Artificial Intelligence in Robotics . . . . . . . . . . . . . . . . . . . . . . . . . . . 623 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 647 Table of Contents INTRODUCTION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 About This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Foolish Assumptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Icons Used in This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Beyond the Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Where to Go from Here . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 BOOK 1: GETTING STARTED WITH PYTHON . . . . . . . . . . . . . . . . . . 5 CHAPTER 1: Starting with Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7 Why Python Is Hot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Choosing the Right Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Tools for Success . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 An excellent, free learning environment . . . . . . . . . . . . . . . . . . . . . . 12 Installing Anaconda and VS Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Writing Python in VS Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Choosing your Python interpreter . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Writing some Python code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 Getting back to VS Code Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 Using Jupyter Notebook for Coding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 CHAPTER 2: Interactive Mode, Getting Help, Writing Apps . . . . . . 27 Using Python Interactive Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 Opening Terminal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 Getting your Python version . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 Going into the Python Interpreter . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 Entering commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 Using Python’s built-in help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 Exiting interactive help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 Searching for specific help topics online . . . . . . . . . . . . . . . . . . . . . . 33 Lots of free cheat sheets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 Creating a Python Development Workspace . . . . . . . . . . . . . . . . . . . . . . 34 Creating a Folder for your Python Code . . . . . . . . . . . . . . . . . . . . . . . . . 37 Typing, Editing, and Debugging Python Code . . . . . . . . . . . . . . . . . . . . . 39 Writing Python code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 Saving your code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 Running Python in VS Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 Simple debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 The VS Code Python debugger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 Table of Contents v Writing Code in a Jupyter Notebook . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 Creating a folder for Jupyter Notebook . . . . . . . . . . . . . . . . . . . . . . . 45 Creating and saving a Jupyter notebook . . . . . . . . . . . . . . . . . . . . . . 46 Typing and running code in a notebook . . . . . . . . . . . . . . . . . . . . . . 46 Adding some Markdown text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 Saving and opening notebooks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 CHAPTER 3: Python Elements and Syntax . . . . . . . . . . . . . . . . . . . . . . . . . .49 The Zen of Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 Object-Oriented Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 Indentations Count, Big Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 Using Python Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 Syntax for importing modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 Using an alias with modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 CHAPTER 4: Building Your First Python Application . . . . . . . . . . . . . . 61 Open the Python App File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 Typing and Using Python Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 Understanding Python Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 Words (strings) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 True/false Booleans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 Doing Work with Python Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 Arithmetic operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 Comparison operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 Boolean operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 Creating and Using Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 Creating valid variable names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 Creating variables in code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 Manipulating variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 Saving your work . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 Running your Python app in VS Code . . . . . . . . . . . . . . . . . . . . . . . . . 76 What Syntax Is and Why It Matters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 Putting Code Together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 BOOK 2: UNDERSTANDING PYTHON BUILDING BLOCKS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 CHAPTER 1: Working with Numbers, Text, and Dates . . . . . . . . . . . . 85 Calculating Numbers with Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 Still More Math Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 Formatting Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 Formatting with f-strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 Showing dollar amounts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 Formatting percent numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 vi Python All-in-One For Dummies Making multiline format strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 Formatting width and alignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 Grappling with Weirder Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 Binary, octal, and hexadecimal numbers . . . . . . . . . . . . . . . . . . . . . . 98 Complex numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 Manipulating Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100 Concatenating strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 Getting the length of a string . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 Working with common string operators . . . . . . . . . . . . . . . . . . . . . 102 Manipulating strings with methods . . . . . . . . . . . . . . . . . . . . . . . . . 105 Uncovering Dates and Times . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 Working with dates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 Working with times . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 Calculating timespans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114 Accounting for Time Zones . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118 Working with Time Zones . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120 CHAPTER 2: Controlling the Action . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 Main Operators for Controlling the Action . . . . . . . . . . . . . . . . . . . . . . 125 Making Decisions with if . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126 Adding else to your if login . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .130 Handling multiple else’s with elif . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131 Ternary operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133 Repeating a Process with for . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134 Looping through numbers in a range . . . . . . . . . . . . . . . . . . . . . . . 134 Looping through a string . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136 Looping through a list . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137 Bailing out of a loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138 Looping with continue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140 Nesting loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140 Looping with while . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141 Starting while loops over with continue . . . . . . . . . . . . . . . . . . . . . . 143 Breaking while loops with break . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144 CHAPTER 3: Speeding Along with Lists and Tuples . . . . . . . . . . . . . . . 147 Defining and Using Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147 Referencing list items by position . . . . . . . . . . . . . . . . . . . . . . . . . . .148 Looping through a list . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150 Seeing whether a list contains an item . . . . . . . . . . . . . . . . . . . . . . .150 Getting the length of a list . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151 Adding an item to the end of a list . . . . . . . . . . . . . . . . . . . . . . . . . . 151 Inserting an item into a list . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152 Changing an item in a list . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153 Combining lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153 Table of Contents vii Removing list items . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .154 Clearing out a list . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .156 Counting how many times an item appears in a list . . . . . . . . . . . 157 Finding an list item’s index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158 Alphabetizing and sorting lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .159 Reversing a list . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161 Copying a list . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162 What’s a Tuple and Who Cares? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163 Working with Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165 CHAPTER 4: Cruising Massive Data with Dictionaries . . . . . . . . . . . 169 Creating a Data Dictionary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171 Accessing dictionary data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .172 Getting the length of a dictionary . . . . . . . . . . . . . . . . . . . . . . . . . . . 174 Seeing whether a key exists in a dictionary . . . . . . . . . . . . . . . . . . .175 Getting dictionary data with get() . . . . . . . . . . . . . . . . . . . . . . . . . . . 176 Changing the value of a key . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177 Adding or changing dictionary data . . . . . . . . . . . . . . . . . . . . . . . . . 177 Looping through a Dictionary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179 Data Dictionary Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181 Copying a Dictionary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182 Deleting Dictionary Items . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182 Using pop() with Data Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . .184 Fun with Multi-Key Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .186 Using the mysterious fromkeys and setdefault methods . . . . . . . 188 Nesting Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190 CHAPTER 5: Wrangling Bigger Chunks of Code . . . . . . . . . . . . . . . . . . . 193 Creating a Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194 Commenting a Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195 Passing Information to a Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196 Defining optional parameters with defaults . . . . . . . . . . . . . . . . . . 198 Passing multiple values to a function . . . . . . . . . . . . . . . . . . . . . . . . 199 Using keyword arguments (kwargs) . . . . . . . . . . . . . . . . . . . . . . . . . 200 Passing multiple values in a list . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202 Passing in an arbitrary number of arguments . . . . . . . . . . . . . . . . 204 Returning Values from Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205 Unmasking Anonymous Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . .206 CHAPTER 6: Doing Python with Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213 Mastering Classes and Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213 Creating a Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216 How a Class Creates an Instance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217 viii Python All-in-One For Dummies Giving an Object Its Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .218 Creating an instance from a class . . . . . . . . . . . . . . . . . . . . . . . . . . . 219 Changing the value of an attribute . . . . . . . . . . . . . . . . . . . . . . . . . .222 Defining attributes with default values . . . . . . . . . . . . . . . . . . . . . . 222 Giving a Class Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224 Passing parameters to methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226 Calling a class method by class name . . . . . . . . . . . . . . . . . . . . . . . 227 Using class variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .228 Using class methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230 Using static methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232 Understanding Class Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234 Creating the base (main) class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236 Defining a subclass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237 Overriding a default value from a subclass . . . . . . . . . . . . . . . . . . . 239 Adding extra parameters from a subclass . . . . . . . . . . . . . . . . . . . . 239 Calling a base class method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .242 Using the same name twice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .243 CHAPTER 7: Sidestepping Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247 Understanding Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247 Handling Errors Gracefully . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251 Being Specific about Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252 Keeping Your App from Crashing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253 Adding an else to the Mix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255 Using try . . . except . . . else . . . finally . . . . . . . . . . . . . . . . . . . . . . . . . . 257 Raising Your Own Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259 BOOK 3: WORKING WITH PYTHON LIBRARIES . . . . . . . . . . . . 265 CHAPTER 1: Working with External Files . . . . . . . . . . . . . . . . . . . . . . . . . . 267 Understanding Text and Binary Files . . . . . . . . . . . . . . . . . . . . . . . . . . . 267 Opening and Closing Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269 Reading a File’s Contents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276 Looping through a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277 Looping with readlines() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .277 Looping with readline() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279 Appending versus overwriting files . . . . . . . . . . . . . . . . . . . . . . . . . . 280 Using tell() to determine the pointer location . . . . . . . . . . . . . . . . . 281 Moving the pointer with seek() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283 Reading and Copying a Binary File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283 Conquering CSV Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286 Opening a CSV file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288 Converting strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290 Table of Contents ix

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.