Python Crash Course A Hands-On, Project-Based Introduction to Programming (Eric Matthes) (Z-Library)

Author: Eric Matthes

科学

Python Crash Course is a fast-paced, thorough introduction to programming with Python that will have you writing programs, solving problems, and making things that work in no time. In the first half of the book, you'll learn about basic programming concepts, such as lists, dictionaries, classes, and loops, and practice writing clean and readable code with exercises for each topic. You'll also learn how to make your programs interactive and how to test your code safely before adding it to a project. In the second half of the book, you'll put your new knowledge into practice with three substantial projects: a Space Invaders-inspired arcade game, data visualizations with Python's super-handy libraries, and a simple web app you can deploy online. As you work through Python Crash Course, you'll learn how to: Use powerful Python libraries and tools, including matplotlib, NumPy, and Pygal Make 2D games that respond to keypresses and mouse clicks, and that grow more difficult as the game progresses Work with data to generate interactive visualizations Create and customize simple web apps and deploy them safely online Deal with mistakes and errors so you can solve your own programming problems If you've been thinking seriously about digging into programming, Python Crash Course will get you up to speed and have you writing real programs fast. Why wait any longer? Start your engines and code!

📄 File Format: PDF
💾 File Size: 5.4 MB
18
Views
0
Downloads
0.00
Total Donations

📄 Text Preview (First 20 pages)

ℹ️

Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

📄 Page 1
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 E R I C M A T T H E S P Y T H O N C R A S H C O U R S E SHELVE IN: PROGRAM M ING LANGUAGES/ PYTHON $39.95 ($45.95 CDN) F A S T ! L E A R N P Y T H O N — P Y T H O N C R A S H C O U R S E P Y T H O N C R A S H C O U R S E M A T T H E S C O V E R S P Y T H O N 2 A N D 3 Python Crash Course is a fast-paced, thorough intro- duction to programming with Python that will have you writing programs, solving problems, and making things that work in no time. In the first half of the book, you’ll learn about basic programming concepts, such as lists, dictionaries, classes, and loops, and practice writing clean and readable code with exercises for each topic. You’ll also learn how to make your programs interactive and how to test your code safely before adding it to a project. In the second half of the book, you’ll put your new knowledge into practice with three substantial projects: a Space Invaders–inspired arcade game, data visualizations with Python’s super-handy libraries, and a simple web app you can deploy online. As you work through Python Crash Course, you’ll learn how to: • Use powerful Python libraries and tools, including matplotlib, NumPy, and Pygal • Make 2D games that respond to keypresses and mouse clicks, and that grow more difficult as the game progresses • Work with data to generate interactive visualizations • Create and customize simple web apps and deploy them safely online • Deal with mistakes and errors so you can solve your own programming problems If you’ve been thinking seriously about digging into programming, Python Crash Course will get you up to speed and have you writing real programs fast. Why wait any longer? Start your engines and code! A B O U T T H E A U T H O R Eric Matthes is a high school science and math teacher living in Alaska, where he teaches an introductory Python course. He has been writing programs since he was five years old. www.nostarch.com TH E F I N EST I N G E E K E NTE RTA I N M E NT™ “ I L I E F LAT .” Th is book uses RepKover — a durab le b ind ing that won’t snap shut
📄 Page 2
Python Crash Course
📄 Page 3
(This page has no text content)
📄 Page 4
P y t h o n C r a s h C o u r s e 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 San Francisco
📄 Page 5
Python Crash Course. Copyright © 2016 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 19 18 17 16 15 1 2 3 4 5 6 7 8 9 ISBN-10: 1-59327-603-6 ISBN-13: 978-1-59327-603-4 Publisher: William Pollock Production Editor: Riley Hoffman Cover Illustration: Josh Ellingson Interior Design: Octopod Studios Developmental Editors: William Pollock, Liz Chadwick, and Leslie Shen Technical Reviewer: Kenneth Love Copyeditor: Anne Marie Walker Compositor: Riley Hoffman Proofreader: James Fraleigh For information on distribution, translations, or bulk sales, please contact No Starch Press, Inc. directly: No Starch Press, Inc. 245 8th Street, San Francisco, CA 94103 phone: 415.863.9900; info@nostarch.com www.nostarch.com Library of Congress Cataloging-in-Publication Data 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 6
about the author Eric Matthes is a high school science and math teacher living in Alaska, where he teaches an introductory Python course. He has been writing programs since he was five years old. Eric currently focuses on writing soft- ware that addresses inefficiencies in education and brings the benefits of open source software to the field of education. In his spare time he enjoys climbing mountains and spending time with his family. about the technical reviewer Kenneth Love has been a Python programmer and teacher for many years. He has given talks and tutorials at conferences, done professional train- ings, been a Python and Django freelancer, and now teaches for an online education company. Kenneth is also the co-creator of the django-braces package, which provides several handy mixins for Django’s class-based views. You can keep up with him on Twitter at @kennethlove.
📄 Page 7
(This page has no text content)
📄 Page 8
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 9
(This page has no text content)
📄 Page 10
B r I e f C o n t e n t s Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvii Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxix Part I: BasICs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Chapter 1: Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Chapter 2: Variables and Simple Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Chapter 3: Introducing Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 Chapter 4: Working with Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 Chapter 5: if Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 Chapter 6: Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 Chapter 7: User Input and while Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 Chapter 8: Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133 Chapter 9: Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161 Chapter 10: Files and Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189 Chapter 11: Testing Your Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215 Part II: ProjeCts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231 Project 1: Alien Invasion Chapter 12: A Ship That Fires Bullets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235 Chapter 13: Aliens! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265 Chapter 14: Scoring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291
📄 Page 11
x Brief Contents Project 2: Data Visualization Chapter 15: Generating Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321 Chapter 16: Downloading Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349 Chapter 17: Working with APIs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 377 Project 3: Web Applications Chapter 18: Getting Started with Django . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397 Chapter 19: User Accounts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427 Chapter 20: Styling and Deploying an App . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 455 Afterword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 483 Appendix A: Installing Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 485 Appendix B: Text Editors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 491 Appendix C: Getting Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 499 Appendix D: Using Git for Version Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 505 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 515
📄 Page 12
C o n t e n t s I n D e t a I l aCknowledgments xxvii IntroduCtIon xxix Who Is This Book For? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxx What Can You Expect to Learn? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxx Why Python? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxxi Part I: BasICs 1 1 gettIng started 3 Setting Up Your Programming Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Python 2 and Python 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Running Snippets of Python Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Hello World! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Python on Different Operating Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Python on Linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Python on OS X . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Python on Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Troubleshooting Installation Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 Running Python Programs from a Terminal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 On Linux and OS X . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 On Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 Exercise 1-1: python.org . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Exercise 1-2: Hello World Typos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Exercise 1-3: Infinite Skills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 2 VarIaBles and sImPle data tyPes 19 What Really Happens When You Run hello_world .py . . . . . . . . . . . . . . . . . . . . . . . . . 19 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 Naming and Using Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 Avoiding Name Errors When Using Variables . . . . . . . . . . . . . . . . . . . . . . . 21 Exercise 2-1: Simple Message . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 Exercise 2-2: Simple Messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 Changing Case in a String with Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 Combining or Concatenating Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 Adding Whitespace to Strings with Tabs or Newlines . . . . . . . . . . . . . . . . . . 26 Stripping Whitespace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 Avoiding Syntax Errors with Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 Printing in Python 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 Exercise 2-3: Personal Message . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 Exercise 2-4: Name Cases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 Exercise 2-5: Famous Quote . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
📄 Page 13
xii Contents in Detail Exercise 2-6: Famous Quote 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 Exercise 2-7: Stripping Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 Integers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 Floats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 Avoiding Type Errors with the str() Function . . . . . . . . . . . . . . . . . . . . . . . . . 31 Integers in Python 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 Exercise 2-8: Number Eight . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 Exercise 2-9: Favorite Number . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 How Do You Write Comments? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 What Kind of Comments Should You Write? . . . . . . . . . . . . . . . . . . . . . . . . 33 Exercise 2-10: Adding Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 The Zen of Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 Exercise 2-11: Zen of Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 3 IntroduCIng lIsts 37 What Is a List? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 Accessing Elements in a List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 Index Positions Start at 0, Not 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 Using Individual Values from a List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 Exercise 3-1: Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 Exercise 3-2: Greetings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 Exercise 3-3: Your Own List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 Changing, Adding, and Removing Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 Modifying Elements in a List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 Adding Elements to a List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 Removing Elements from a List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 Exercise 3-4: Guest List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 Exercise 3-5: Changing Guest List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 Exercise 3-6: More Guests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 Exercise 3-7: Shrinking Guest List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 Organizing a List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 Sorting a List Permanently with the sort() Method . . . . . . . . . . . . . . . . . . . . . . 47 Sorting a List Temporarily with the sorted() Function . . . . . . . . . . . . . . . . . . . . 48 Printing a List in Reverse Order . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 Finding the Length of a List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 Exercise 3-8: Seeing the World. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 Exercise 3-9: Dinner Guests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 Exercise 3-10: Every Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 Avoiding Index Errors When Working with Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 Exercise 3-11: Intentional Error . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 4 workIng wIth lIsts 53 Looping Through an Entire List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 A Closer Look at Looping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 Doing More Work Within a for Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 Doing Something After a for Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
📄 Page 14
Contents in Detail xiii Avoiding Indentation Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 Forgetting to Indent . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 Forgetting to Indent Additional Lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 Indenting Unnecessarily . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 Indenting Unnecessarily After the Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 Forgetting the Colon . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 Exercise 4-1: Pizzas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 Exercise 4-2: Animals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 Making Numerical Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 Using the range() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 Using range() to Make a List of Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 Simple Statistics with a List of Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 List Comprehensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 Exercise 4-3: Counting to Twenty . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 Exercise 4-4: One Million. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 Exercise 4-5: Summing a Million . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 Exercise 4-6: Odd Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 Exercise 4-7: Threes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 Exercise 4-8: Cubes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 Exercise 4-9: Cube Comprehension. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 Working with Part of a List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 Slicing a List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 Looping Through a Slice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 Copying a List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 Exercise 4-10: Slices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 Exercise 4-11: My Pizzas, Your Pizzas. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 Exercise 4-12: More Loops. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 Tuples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 Defining a Tuple . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 Looping Through All Values in a Tuple . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 Writing over a Tuple . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 Exercise 4-13: Buffet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 Styling Your Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 The Style Guide . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 Indentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 Line Length . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 Blank Lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 Other Style Guidelines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 Exercise 4-14: PEP 8 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 Exercise 4-15: Code Review. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 5 If statements 75 A Simple Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 Conditional Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 Checking for Equality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 Ignoring Case When Checking for Equality . . . . . . . . . . . . . . . . . . . . . . . . . 77 Checking for Inequality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 Numerical Comparisons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 Checking Multiple Conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 Checking Whether a Value Is in a List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
📄 Page 15
xiv Contents in Detail Checking Whether a Value Is Not in a List . . . . . . . . . . . . . . . . . . . . . . . . . . 81 Boolean Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 Exercise 5-1: Conditional Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 Exercise 5-2: More Conditional Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 if Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 Simple if Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 if-else Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 The if-elif-else Chain . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 Using Multiple elif Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 Omitting the else Block . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 Testing Multiple Conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 Exercise 5-3: Alien Colors #1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 Exercise 5-4: Alien Colors #2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 Exercise 5-5: Alien Colors #3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 Exercise 5-6: Stages of Life. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 Exercise 5-7: Favorite Fruit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 Using if Statements with Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 Checking for Special Items . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 Checking That a List Is Not Empty . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 Using Multiple Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 Exercise 5-8: Hello Admin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 Exercise 5-9: No Users . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 Exercise 5-10: Checking Usernames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 Exercise 5-11: Ordinal Numbers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 Styling Your if Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 Exercise 5-12: Styling if statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 Exercise 5-13: Your Ideas. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 6 dICtIonarIes 95 A Simple Dictionary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 Working with Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 Accessing Values in a Dictionary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 Adding New Key-Value Pairs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 Starting with an Empty Dictionary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 Modifying Values in a Dictionary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 Removing Key-Value Pairs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100 A Dictionary of Similar Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100 Exercise 6-1: Person . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 Exercise 6-2: Favorite Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 Exercise 6-3: Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 Looping Through a Dictionary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 Looping Through All Key-Value Pairs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 Looping Through All the Keys in a Dictionary . . . . . . . . . . . . . . . . . . . . . . . 104 Looping Through a Dictionary’s Keys in Order . . . . . . . . . . . . . . . . . . . . . . 106 Looping Through All Values in a Dictionary . . . . . . . . . . . . . . . . . . . . . . . . 107 Exercise 6-4: Glossary 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 Exercise 6-5: Rivers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 Exercise 6-6: Polling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 Nesting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 A List of Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 A List in a Dictionary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
📄 Page 16
Contents in Detail xv A Dictionary in a Dictionary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 Exercise 6-7: People . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .114 Exercise 6-8: Pets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .115 Exercise 6-9: Favorite Places . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .115 Exercise 6-10: Favorite Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .115 Exercise 6-11: Cities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .115 Exercise 6-12: Extensions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .115 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 7 user InPut and whIle looPs 117 How the input() Function Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118 Writing Clear Prompts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118 Using int() to Accept Numerical Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 The Modulo Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120 Accepting Input in Python 2 .7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 Exercise 7-1: Rental Car. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 Exercise 7-2: Restaurant Seating . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 Exercise 7-3: Multiples of Ten . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 Introducing while Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 The while Loop in Action . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 Letting the User Choose When to Quit . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 Using a Flag . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124 Using break to Exit a Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 Using continue in a Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126 Avoiding Infinite Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126 Exercise 7-4: Pizza Toppings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 Exercise 7-5: Movie Tickets. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 Exercise 7-6: Three Exits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128 Exercise 7-7: Infinity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128 Using a while Loop with Lists and Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128 Moving Items from One List to Another . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128 Removing All Instances of Specific Values from a List . . . . . . . . . . . . . . . . . . 129 Filling a Dictionary with User Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130 Exercise 7-8: Deli . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131 Exercise 7-9: No Pastrami . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131 Exercise 7-10: Dream Vacation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131 8 funCtIons 133 Defining a Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134 Passing Information to a Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134 Arguments and Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 Exercise 8-1: Message. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 Exercise 8-2: Favorite Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 Passing Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 Positional Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136 Keyword Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137 Default Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
📄 Page 17
xvi Contents in Detail Equivalent Function Calls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139 Avoiding Argument Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140 Exercise 8-3: T-Shirt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141 Exercise 8-4: Large Shirts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141 Exercise 8-5: Cities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141 Return Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141 Returning a Simple Value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142 Making an Argument Optional . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142 Returning a Dictionary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144 Using a Function with a while Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145 Exercise 8-6: City Names. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146 Exercise 8-7: Album. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146 Exercise 8-8: User Albums . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146 Passing a List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147 Modifying a List in a Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147 Preventing a Function from Modifying a List . . . . . . . . . . . . . . . . . . . . . . . . 149 Exercise 8-9: Magicians. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150 Exercise 8-10: Great Magicians . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150 Exercise 8-11: Unchanged Magicians . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150 Passing an Arbitrary Number of Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151 Mixing Positional and Arbitrary Arguments . . . . . . . . . . . . . . . . . . . . . . . . 152 Using Arbitrary Keyword Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152 Exercise 8-12: Sandwiches. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154 Exercise 8-13: User Profile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154 Exercise 8-14: Cars. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154 Storing Your Functions in Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154 Importing an Entire Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154 Importing Specific Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156 Using as to Give a Function an Alias . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156 Using as to Give a Module an Alias . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157 Importing All Functions in a Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157 Styling Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158 Exercise 8-15: Printing Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159 Exercise 8-16: Imports . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159 Exercise 8-17: Styling Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159 9 Classes 161 Creating and Using a Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162 Creating the Dog Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162 Making an Instance from a Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164 Exercise 9-1: Restaurant. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166 Exercise 9-2: Three Restaurants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166 Exercise 9-3: Users . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166 Working with Classes and Instances . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167 The Car Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167 Setting a Default Value for an Attribute . . . . . . . . . . . . . . . . . . . . . . . . . . . 168 Modifying Attribute Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168 Exercise 9-4: Number Served . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171 Exercise 9-5: Login Attempts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
📄 Page 18
Contents in Detail xvii Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172 The __init__() Method for a Child Class . . . . . . . . . . . . . . . . . . . . . . . . . . . 172 Inheritance in Python 2 .7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173 Defining Attributes and Methods for the Child Class . . . . . . . . . . . . . . . . . . 174 Overriding Methods from the Parent Class . . . . . . . . . . . . . . . . . . . . . . . . . 175 Instances as Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175 Modeling Real-World Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177 Exercise 9-6: Ice Cream Stand . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178 Exercise 9-7: Admin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178 Exercise 9-8: Privileges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178 Exercise 9-9: Battery Upgrade . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178 Importing Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179 Importing a Single Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179 Storing Multiple Classes in a Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180 Importing Multiple Classes from a Module . . . . . . . . . . . . . . . . . . . . . . . . . 181 Importing an Entire Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182 Importing All Classes from a Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182 Importing a Module into a Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183 Finding Your Own Workflow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184 Exercise 9-10: Imported Restaurant . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184 Exercise 9-11: Imported Admin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184 Exercise 9-12: Multiple Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184 The Python Standard Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184 Exercise 9-13: OrderedDict Rewrite. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186 Exercise 9-14: Dice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186 Exercise 9-15: Python Module of the Week . . . . . . . . . . . . . . . . . . . . . . . . 186 Styling Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187 10 fIles and exCePtIons 189 Reading from a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190 Reading an Entire File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190 File Paths . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191 Reading Line by Line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193 Making a List of Lines from a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194 Working with a File’s Contents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194 Large Files: One Million Digits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195 Is Your Birthday Contained in Pi? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196 Exercise 10-1: Learning Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197 Exercise 10-2: Learning C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197 Writing to a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197 Writing to an Empty File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197 Writing Multiple Lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198 Appending to a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199 Exercise 10-3: Guest . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199 Exercise 10-4: Guest Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199 Exercise 10-5: Programming Poll. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199 Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200 Handling the ZeroDivisionError Exception . . . . . . . . . . . . . . . . . . . . . . . . . 200 Using try-except Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200 Using Exceptions to Prevent Crashes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
📄 Page 19
xviii Contents in Detail The else Block . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202 Handling the FileNotFoundError Exception . . . . . . . . . . . . . . . . . . . . . . . . . 203 Analyzing Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204 Working with Multiple Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205 Failing Silently . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206 Deciding Which Errors to Report . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207 Exercise 10-6: Addition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207 Exercise 10-7: Addition Calculator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208 Exercise 10-8: Cats and Dogs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208 Exercise 10-9: Silent Cats and Dogs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208 Exercise 10-10: Common Words . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208 Storing Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208 Using json .dump() and json .load() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209 Saving and Reading User-Generated Data . . . . . . . . . . . . . . . . . . . . . . . . . 210 Refactoring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212 Exercise 10-11: Favorite Number . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214 Exercise 10-12: Favorite Number Remembered . . . . . . . . . . . . . . . . . . . . . 214 Exercise 10-13: Verify User . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214 11 testIng your Code 215 Testing a Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216 Unit Tests and Test Cases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217 A Passing Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217 A Failing Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218 Responding to a Failed Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219 Adding New Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221 Exercise 11-1: City, Country . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222 Exercise 11-2: Population. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222 Testing a Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222 A Variety of Assert Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222 A Class to Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223 Testing the AnonymousSurvey Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225 The setUp() Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227 Exercise 11-3: Employee . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228 Part II: ProjeCts 231 ProjeCt 1: alIen InVasIon 12 a shIP that fIres Bullets 235 Planning Your Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236 Installing Pygame . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236 Installing Python Packages with pip . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237 Installing Pygame on Linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238
📄 Page 20
Contents in Detail xix Installing Pygame on OS X . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239 Installing Pygame on Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240 Starting the Game Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240 Creating a Pygame Window and Responding to User Input . . . . . . . . . . . . . 241 Setting the Background Color . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242 Creating a Settings Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243 Adding the Ship Image . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244 Creating the Ship Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245 Drawing the Ship to the Screen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246 Refactoring: the game_functions Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247 The check_events() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247 The update_screen() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248 Exercise 12-1: Blue Sky . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249 Exercise 12-2: Game Character . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249 Piloting the Ship . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249 Responding to a Keypress . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249 Allowing Continuous Movement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250 Moving Both Left and Right . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252 Adjusting the Ship’s Speed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253 Limiting the Ship’s Range . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255 Refactoring check_events() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255 A Quick Recap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256 alien_invasion .py . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256 settings .py . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256 game_functions .py . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256 ship .py . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257 Exercise 12-3: Rocket . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257 Exercise 12-4: Keys. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257 Shooting Bullets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257 Adding the Bullet Settings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257 Creating the Bullet Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258 Storing Bullets in a Group . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259 Firing Bullets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260 Deleting Old Bullets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261 Limiting the Number of Bullets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262 Creating the update_bullets() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263 Creating the fire_bullet() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264 Exercise 12-5: Sideways Shooter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264 13 alIens! 265 Reviewing Your Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266 Creating the First Alien . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266 Creating the Alien Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267 Creating an Instance of the Alien . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268 Making the Alien Appear Onscreen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268 Building the Alien Fleet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269 Determining How Many Aliens Fit in a Row . . . . . . . . . . . . . . . . . . . . . . . . 269 Creating Rows of Aliens . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270 Creating the Fleet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271
The above is a preview of the first 20 pages. Register to read the complete e-book.

💝 Support Author

0.00
Total Amount (¥)
0
Donation Count

Login to support the author

Login Now
Back to List