📄 Page
1
(This page has no text content)
📄 Page
2
(This page has no text content)
📄 Page
3
PRAISE FOR PYTHON CRASH COURSE “It has been interesting to see No Starch Press producing future classics that should be alongside the more traditional programming books. Python Crash Course is one of those books.” —Greg Laden, ScienceBlogs “Deals with some rather complex projects and lays them out in a consistent, logical, and pleasant manner that draws the reader into the subject.” —Full CirCle Magazine “Well presented with good explanations of the code snippets. The book works with you, one small step at a time, building more complex code, explaining what’s going on all the way.” —FlickThrough Reviews “Learning Python with Python Crash Course was an extremely positive experi- ence! A great choice if you’re new to Python.” —Mikke Goes Coding “Does what it says on the tin, and does it really well. . . . Presents a large number of useful exercises as well as three challenging and entertaining projects.” —RealPython.com “A fast-paced but comprehensive introduction to programming with Python, Python Crash Course is another superb book to add to your library and help you finally master Python.” —TutorialEdge.net “A brilliant option for complete beginners without any coding experience. If you’re looking for a solid, uncomplicated intro to this very deep language, I have to recommend this book.” —WhatPixel.com “Contains literally everything you need to know about Python and even more.” —FireBearStudio.com “While Python Crash Course uses Python to teach you to code, it also teaches clean programming skills that apply to most other languages.” —Great Lakes Geek
📄 Page
4
(This page has no text content)
📄 Page
5
San Francisco P Y T H O N C R A S H C O U R S E 3 R D E D I T I O N A H a n d s - O n , P r o j e c t - B a s e d I n t r o d u c t i o n t o P r o g r a m m i n g by Er ic Matthes
📄 Page
6
PYTHON CRASH COURSE, 3RD EDITION. Copyright © 2023 by Eric Matthes. All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. First printing 26 25 24 23 22 1 2 3 4 5 ISBN-13: 978-1-7185-0270-3 (print) ISBN-13: 978-1-7185-0271-0 (ebook) Publisher: William Pollock Managing Editor: Jill Franklin Production Editor: Jennifer Kepler Developmental Editor: Eva Morrow Cover Illustrator: Josh Ellingson Interior Design: Octopod Studios Technical Reviewer: Kenneth Love Copyeditor: Doug McNair Compositor: Jeff Lytle, Happenstance Type-O-Rama Proofreader: Scout Festa For information on distribution, bulk sales, corporate sales, or translations, please contact No Starch Press, Inc. directly at info@nostarch.com or: No Starch Press, Inc. 245 8th Street, San Francisco, CA 94103 phone: 1.415.863.9900 www.nostarch.com The Library of Congress has catalogued the first edition as follows: Matthes, Eric, 1972- Python crash course : a hands-on, project-based introduction to programming / by Eric Matthes. pages cm Includes index. Summary: "A project-based introduction to programming in Python, with exercises. Covers general programming concepts, Python fundamentals, and problem solving. Includes three projects - how to create a simple video game, use data visualization techniques to make graphs and charts, and build an interactive web application"-- Provided by publisher. ISBN 978-1-59327-603-4 -- ISBN 1-59327-603-6 1. Python (Computer program language) I. Title. QA76.73.P98M38 2015 005.13'3--dc23 2015018135 No Starch Press and the No Starch Press logo are registered trademarks of No Starch Press, Inc. Other product and company names mentioned herein may be the trademarks of their respective owners. Rather than use a trademark symbol with every occurrence of a trademarked name, we are using the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The information in this book is distributed on an “As Is” basis, without warranty. While every precaution has been taken in the preparation of this work, neither the author nor No Starch Press, Inc. shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in it.
📄 Page
7
For my father, who always made time to answer my questions about programming, and for Ever, who is just beginning to ask me his questions
📄 Page
8
(This page has no text content)
📄 Page
9
About the Author Eric Matthes was a high school math and science teacher for 25 years, and he taught introductory Python classes whenever he could find a way to fit them into the curriculum. Eric is a full-time writer and programmer now, and he is involved in a number of open source projects. His projects have a diverse range of goals, from helping predict landslide activity in mountain- ous regions to simplifying the process of deploying Django projects. When he’s not writing or programming, he enjoys climbing mountains and spend- ing time with his family. About the Technical Reviewer Kenneth Love lives in the Pacific Northwest with their family and cats. Kenneth is a longtime Python programmer, open source contributor, teacher, and conference speaker.
📄 Page
10
(This page has no text content)
📄 Page
11
B R I E F C O N T E N T S Preface to the Third Edition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvii Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxi Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxiii PART I: BASICS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1 Chapter 1: Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Chapter 2: Variables and Simple Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 Chapter 3: Introducing Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 Chapter 4: Working with Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 Chapter 5: if Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 Chapter 6: Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 Chapter 7: User Input and while Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 Chapter 8: Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 Chapter 9: Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157 Chapter 10: Files and Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183 Chapter 11: Testing Your Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209 PART II: PROJECTS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .225 Chapter 12: A Ship That Fires Bullets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227 Chapter 13: Aliens! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255 Chapter 14: Scoring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277 Chapter 15: Generating Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301 Chapter 16: Downloading Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329 Chapter 17: Working with APIs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355
📄 Page
12
x Brief Contents Chapter 18: Getting Started with Django . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373 Chapter 19: User Accounts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 403 Chapter 20: Styling and Deploying an App . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 433 Appendix A: Installation and Troubleshooting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463 Appendix B: Text Editors and IDEs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 469 Appendix C: Getting Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 477 Appendix D: Using Git for Version Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 483 Appendix E: Troubleshooting Deployments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 493 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 503
📄 Page
13
C O N T E N T S I N D E T A I L PREFACE TO THE THIRD EDITION xxvii ACKNOWLEDGMENTS xxxi INTRODUCTION xxxiii Who Is This Book For? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxiv What Can You Expect to Learn? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxiv Online Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxv Why Python? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxvi PART I: BASICS 1 1 GETTING STARTED 3 Setting Up Your Programming Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Python Versions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Running Snippets of Python Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 About the VS Code Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Python on Different Operating Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Python on Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Python on macOS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Python on Linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Running a Hello World Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Installing the Python Extension for VS Code . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Running hello_world .py . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Troubleshooting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Running Python Programs from a Terminal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 On Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 On macOS and Linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Exercise 1-1: python.org . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Exercise 1-2: Hello World Typos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Exercise 1-3: Infinite Skills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 2 VARIABLES AND SIMPLE DATA TYPES 15 What Really Happens When You Run hello_world .py . . . . . . . . . . . . . . . . . . . . . . . . . 15 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 Naming and Using Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Avoiding Name Errors When Using Variables . . . . . . . . . . . . . . . . . . . . . . . 17 Variables Are Labels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 Exercise 2-1: Simple Message . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Exercise 2-2: Simple Messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
📄 Page
14
xii Contents in Detail Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Changing Case in a String with Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 Using Variables in Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 Adding Whitespace to Strings with Tabs or Newlines . . . . . . . . . . . . . . . . . . 21 Stripping Whitespace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 Removing Prefixes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 Avoiding Syntax Errors with Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 Exercise 2-3: Personal Message . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 Exercise 2-4: Name Cases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 Exercise 2-5: Famous Quote . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 Exercise 2-6: Famous Quote 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 Exercise 2-7: Stripping Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .25 Exercise 2-8: File Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 Integers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 Floats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 Integers and Floats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 Underscores in Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 Multiple Assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 Exercise 2-9: Number Eight . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .29 Exercise 2-10: Favorite Number . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 How Do You Write Comments? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 What Kinds of Comments Should You Write? . . . . . . . . . . . . . . . . . . . . . . . . 29 Exercise 2-11: Adding Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 The Zen of Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 Exercise 2-12: Zen of Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 3 INTRODUCING LISTS 33 What Is a List? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 Accessing Elements in a List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 Index Positions Start at 0, Not 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 Using Individual Values from a List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 Exercise 3-1: Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 Exercise 3-2: Greetings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 Exercise 3-3: Your Own List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 Modifying, Adding, and Removing Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 Modifying Elements in a List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 Adding Elements to a List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 Removing Elements from a List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 Exercise 3-4: Guest List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 Exercise 3-5: Changing Guest List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 Exercise 3-6: More Guests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 Exercise 3-7: Shrinking Guest List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 Organizing a List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 Sorting a List Permanently with the sort() Method . . . . . . . . . . . . . . . . . . . . . . 43 Sorting a List Temporarily with the sorted() Function . . . . . . . . . . . . . . . . . . . . 43 Printing a List in Reverse Order . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
📄 Page
15
Contents in Detail xiii Finding the Length of a List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 Exercise 3-8: Seeing the World . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 Exercise 3-9: Dinner Guests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 Exercise 3-10: Every Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 Avoiding Index Errors When Working with Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 Exercise 3-11: Intentional Error . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 4 WORKING WITH LISTS 49 Looping Through an Entire List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 A Closer Look at Looping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 Doing More Work Within a for Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 Doing Something After a for Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 Avoiding Indentation Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 Forgetting to Indent . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 Forgetting to Indent Additional Lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 Indenting Unnecessarily . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 Indenting Unnecessarily After the Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 Forgetting the Colon . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 Exercise 4-1: Pizzas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 Exercise 4-2: Animals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 Making Numerical Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 Using the range() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 Using range() to Make a List of Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 Simple Statistics with a List of Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 List Comprehensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 Exercise 4-3: Counting to Twenty . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 Exercise 4-4: One Million . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 Exercise 4-5: Summing a Million . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 Exercise 4-6: Odd Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 Exercise 4-7: Threes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 Exercise 4-8: Cubes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 Exercise 4-9: Cube Comprehension . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 Working with Part of a List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 Slicing a List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 Looping Through a Slice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 Copying a List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 Exercise 4-10: Slices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 Exercise 4-11: My Pizzas, Your Pizzas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .65 Exercise 4-12: More Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 Tuples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 Defining a Tuple . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 Looping Through All Values in a Tuple . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 Writing Over a Tuple . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 Exercise 4-13: Buffet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 Styling Your Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 The Style Guide . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 Indentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 Line Length . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 Blank Lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
📄 Page
16
xiv Contents in Detail Other Style Guidelines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 Exercise 4-14: PEP 8 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 Exercise 4-15: Code Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 5 IF STATEMENTS 71 A Simple Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 Conditional Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 Checking for Equality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 Ignoring Case When Checking for Equality . . . . . . . . . . . . . . . . . . . . . . . . . 73 Checking for Inequality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 Numerical Comparisons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 Checking Multiple Conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 Checking Whether a Value Is in a List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 Checking Whether a Value Is Not in a List . . . . . . . . . . . . . . . . . . . . . . . . . . 76 Boolean Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 Exercise 5-1: Conditional Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 Exercise 5-2: More Conditional Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 if Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 Simple if Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 if-else Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 The if-elif-else Chain . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 Using Multiple elif Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 Omitting the else Block . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 Testing Multiple Conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 Exercise 5-3: Alien Colors #1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 Exercise 5-4: Alien Colors #2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 Exercise 5-5: Alien Colors #3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 Exercise 5-6: Stages of Life . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 Exercise 5-7: Favorite Fruit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 Using if Statements with Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 Checking for Special Items . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 Checking That a List Is Not Empty . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 Using Multiple Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 Exercise 5-8: Hello Admin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 Exercise 5-9: No Users . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 Exercise 5-10: Checking Usernames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 Exercise 5-11: Ordinal Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 Styling Your if Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 Exercise 5-12: Styling if Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 Exercise 5-13: Your Ideas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 6 DICTIONARIES 91 A Simple Dictionary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 Working with Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 Accessing Values in a Dictionary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 Adding New Key-Value Pairs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 Starting with an Empty Dictionary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
📄 Page
17
Contents in Detail xv Modifying Values in a Dictionary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 Removing Key-Value Pairs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 A Dictionary of Similar Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 Using get() to Access Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 Exercise 6-1: Person . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 Exercise 6-2: Favorite Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 Exercise 6-3: Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 Looping Through a Dictionary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 Looping Through All Key-Value Pairs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 Looping Through All the Keys in a Dictionary . . . . . . . . . . . . . . . . . . . . . . . 101 Looping Through a Dictionary’s Keys in a Particular Order . . . . . . . . . . . . . . 102 Looping Through All Values in a Dictionary . . . . . . . . . . . . . . . . . . . . . . . . 103 Exercise 6-4: Glossary 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104 Exercise 6-5: Rivers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 Exercise 6-6: Polling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 Nesting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 A List of Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 A List in a Dictionary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 A Dictionary in a Dictionary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110 Exercise 6-7: People . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 Exercise 6-8: Pets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 Exercise 6-9: Favorite Places . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 Exercise 6-10: Favorite Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 Exercise 6-11: Cities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 Exercise 6-12: Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 7 USER INPUT AND WHILE LOOPS 113 How the input() Function Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114 Writing Clear Prompts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114 Using int() to Accept Numerical Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 The Modulo Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116 Exercise 7-1: Rental Car . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 Exercise 7-2: Restaurant Seating . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 Exercise 7-3: Multiples of Ten . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 Introducing while Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 The while Loop in Action . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 Letting the User Choose When to Quit . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118 Using a Flag . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120 Using break to Exit a Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 Using continue in a Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 Avoiding Infinite Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 Exercise 7-4: Pizza Toppings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .123 Exercise 7-5: Movie Tickets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 Exercise 7-6: Three Exits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 Exercise 7-7: Infinity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 Using a while Loop with Lists and Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124 Moving Items from One List to Another . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124 Removing All Instances of Specific Values from a List . . . . . . . . . . . . . . . . . . 125 Filling a Dictionary with User Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 Exercise 7-8: Deli . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
📄 Page
18
xvi Contents in Detail Exercise 7-9: No Pastrami . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 Exercise 7-10: Dream Vacation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 8 FUNCTIONS 129 Defining a Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130 Passing Information to a Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130 Arguments and Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131 Exercise 8-1: Message . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131 Exercise 8-2: Favorite Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131 Passing Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131 Positional Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132 Keyword Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133 Default Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134 Equivalent Function Calls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 Avoiding Argument Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136 Exercise 8-3: T-Shirt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136 Exercise 8-4: Large Shirts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137 Exercise 8-5: Cities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137 Return Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137 Returning a Simple Value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137 Making an Argument Optional . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138 Returning a Dictionary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139 Using a Function with a while Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140 Exercise 8-6: City Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141 Exercise 8-7: Album . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142 Exercise 8-8: User Albums . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142 Passing a List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142 Modifying a List in a Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143 Preventing a Function from Modifying a List . . . . . . . . . . . . . . . . . . . . . . . . 145 Exercise 8-9: Messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146 Exercise 8-10: Sending Messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146 Exercise 8-11: Archived Messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146 Passing an Arbitrary Number of Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146 Mixing Positional and Arbitrary Arguments . . . . . . . . . . . . . . . . . . . . . . . . 147 Using Arbitrary Keyword Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148 Exercise 8-12: Sandwiches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149 Exercise 8-13: User Profile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149 Exercise 8-14: Cars . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149 Storing Your Functions in Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149 Importing an Entire Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150 Importing Specific Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151 Using as to Give a Function an Alias . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151 Using as to Give a Module an Alias . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152 Importing All Functions in a Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152 Styling Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153 Exercise 8-15: Printing Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154 Exercise 8-16: Imports . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154 Exercise 8-17: Styling Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
📄 Page
19
Contents in Detail xvii 9 CLASSES 157 Creating and Using a Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158 Creating the Dog Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158 The __init__() Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159 Making an Instance from a Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159 Exercise 9-1: Restaurant . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162 Exercise 9-2: Three Restaurants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162 Exercise 9-3: Users . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162 Working with Classes and Instances . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162 The Car Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162 Setting a Default Value for an Attribute . . . . . . . . . . . . . . . . . . . . . . . . . . . 163 Modifying Attribute Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164 Exercise 9-4: Number Served . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166 Exercise 9-5: Login Attempts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .167 Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167 The __init__() Method for a Child Class . . . . . . . . . . . . . . . . . . . . . . . . . . . 167 Defining Attributes and Methods for the Child Class . . . . . . . . . . . . . . . . . . 169 Overriding Methods from the Parent Class . . . . . . . . . . . . . . . . . . . . . . . . . 170 Instances as Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170 Modeling Real-World Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172 Exercise 9-6: Ice Cream Stand . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173 Exercise 9-7: Admin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173 Exercise 9-8: Privileges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173 Exercise 9-9: Battery Upgrade . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173 Importing Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173 Importing a Single Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174 Storing Multiple Classes in a Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175 Importing Multiple Classes from a Module . . . . . . . . . . . . . . . . . . . . . . . . . 176 Importing an Entire Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176 Importing All Classes from a Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177 Importing a Module into a Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177 Using Aliases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178 Finding Your Own Workflow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179 Exercise 9-10: Imported Restaurant . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179 Exercise 9-11: Imported Admin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179 Exercise 9-12: Multiple Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179 The Python Standard Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179 Exercise 9-13: Dice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180 Exercise 9-14: Lottery . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180 Exercise 9-15: Lottery Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180 Exercise 9-16: Python Module of the Week . . . . . . . . . . . . . . . . . . . . . . . 180 Styling Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181 10 FILES AND EXCEPTIONS 183 Reading from a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184 Reading the Contents of a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184 Relative and Absolute File Paths . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186 Accessing a File’s Lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
📄 Page
20
xviii Contents in Detail Working with a File’s Contents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187 Large Files: One Million Digits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188 Is Your Birthday Contained in Pi? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189 Exercise 10-1: Learning Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189 Exercise 10-2: Learning C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190 Exercise 10-3: Simpler Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190 Writing to a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190 Writing a Single Line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190 Writing Multiple Lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191 Exercise 10-4: Guest . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192 Exercise 10-5: Guest Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .192 Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192 Handling the ZeroDivisionError Exception . . . . . . . . . . . . . . . . . . . . . . . . . 192 Using try-except Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193 Using Exceptions to Prevent Crashes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193 The else Block . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194 Handling the FileNotFoundError Exception . . . . . . . . . . . . . . . . . . . . . . . . . 195 Analyzing Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196 Working with Multiple Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197 Failing Silently . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198 Deciding Which Errors to Report . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199 Exercise 10-6: Addition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200 Exercise 10-7: Addition Calculator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200 Exercise 10-8: Cats and Dogs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200 Exercise 10-9: Silent Cats and Dogs . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200 Exercise 10-10: Common Words . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200 Storing Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201 Using json .dumps() and json .loads() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201 Saving and Reading User-Generated Data . . . . . . . . . . . . . . . . . . . . . . . . . 202 Refactoring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204 Exercise 10-11: Favorite Number . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206 Exercise 10-12: Favorite Number Remembered . . . . . . . . . . . . . . . . . . . . 206 Exercise 10-13: User Dictionary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206 Exercise 10-14: Verify User . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207 11 TESTING YOUR CODE 209 Installing pytest with pip . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210 Updating pip . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210 Installing pytest . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211 Testing a Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211 Unit Tests and Test Cases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212 A Passing Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212 Running a Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213 A Failing Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214 Responding to a Failed Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215 Adding New Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216 Exercise 11-1: City, Country . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217 Exercise 11-2: Population . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .217