2ND EDITION WWWWOOOORRRRLLLLDDDDWWWWIIIIDDDDEEEE BBBBEEEESSSSTTTT SSSSEEEELLLLLLLLEEEERRRR LEARN PYTHON— PP YY TT HH OO NN FAST! OOOOVVVVEEEERRRR 555500000000,,,,000000000000 2ND EDITION CCCCOOOOPPPPIIIIEEEESSSS SSSSOOOOLLLLDDDD PP CCRR AASSHH CCOOUURRSSEE YY Python Crash Course is the world’s best-selling guide • Create and customize web apps and deploy them TT to the Python programming language. This fast-paced, safely online HH A H A N D S - O N , P R O J E C T - B A S E D thorough introduction to programming with Python will • Deal with mistakes and errors so you can solve your 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 have you writing programs, solving problems, and OO own programming problems making things that work in no time. This updated second edition has been thoroughly revised NN In the first half of the book, you’ll learn basic program- to reflect the latest in Python code and practices. The ming concepts, such as variables, lists, classes, and E R I C M A T T H E S first half of the book includes improved coverage of CC loops, and practice writing clean code with exercises topics like f-strings, constants, and managing data. In the for each topic. You’ll also learn how to make your second half, the code for the projects has been updated RR programs interactive and test your code safely before with better structure, cleaner syntax, and more popular adding it to a project. In the second half, you’ll put and up-to-date libraries and tools, like Plotly and the AA your new knowledge into practice with three substantial latest version of Django. (For a full list of updates, see projects: a Space Invaders–inspired arcade game, a SS the Preface.) set of data visualizations with Python’s handy libraries, HH and a simple web app you can deploy online. If you’ve been thinking about digging into programming, Python Crash Course will get you writing real programs As you work through the book, you’ll learn how to: fast. Why wait any longer? Start your engines and code! CC • Use powerful Python libraries and tools, including ABOUT THE AUTHOR OO Pygame, Matplotlib, Plotly, and Django • Make 2D games that respond to keypresses and Eric Matthes is a high school science, math, and program- UU ming teacher living in Alaska. He has been writing mouse clicks, and that increase in difficulty programs since he was five years old and is the author RR • Use data to generate interactive visualizations of the Python Flash Cards, also from No Starch Press. SS COVERS PYTHON 3.X EE THE FINEST IN GEEK ENTERTAINMENT™ M A www.nostarch.com T $39.95 ($53.95 CDN) T H PYTHONPROGRAMMSHELVE IN: ES IN FSC FPO “ThIi sL bIoEo kF LuAseTs. ”a durable binding that won’t snap shut G LANGUAG ES/ 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 con- sistent, 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 experience! 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 experi- ence. 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 P y t h o n C r a s h C o u r s e 2 n d ed i t i o n a hands-on, Project-Based introduction to Programming by Eric Matthes San Francisco Python Crash Course, 2nd edition. Copyright © 2019 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. ISBN-10: 1-59327-928-0 ISBN-13: 978-1-59327-928-8 Publisher: William Pollock Production Editor: Riley Hoffman Cover Illustration: Josh Ellingson Cover and Interior Design: Octopod Studios Developmental Editor: Liz Chadwick Technical Reviewer: Kenneth Love Copyeditor: Anne Marie Walker Compositors: Riley Hoffman and Happenstance Type-O-Rama 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: 1.415.863.9900; [email protected] 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. 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, teacher, and conference organizer for many years. He has spoken and taught at many conferences, been a Python and Django freelancer, and is currently a software engineer for O’Reilly Media. Kenneth is 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. 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