Part1 PDF
Part1 PDF
FAST!
PY THON
CR ASH COURSE
PY THON CR A SH COURSE
Python Crash Course is a fast-paced, thorough intro- • Make 2D games that respond to keypresses and A H A N D S - O N , P R O J E C T - B A S E D
duction to programming with Python that will have you mouse clicks, and that grow more difficult as the
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
writing programs, solving problems, and making things game progresses
that work in no time.
• Work with data to generate interactive visualizations
In the first half of the book, you’ll learn about basic
• Create and customize simple web apps and deploy ERIC MAT THES
programming concepts, such as lists, dictionaries,
them safely online
classes, and loops, and practice writing clean and
readable code with exercises for each topic. You’ll • Deal with mistakes and errors so you can solve your
also learn how to make your programs interactive own programming problems
and how to test your code safely before adding it to
If you’ve been thinking seriously about digging into
a project. In the second half of the book, you’ll put
programming, Python Crash Course will get you up to
your new knowledge into practice with three substantial
speed and have you writing real programs fast. Why
projects: a Space Invaders–inspired arcade game, data
wait any longer? Start your engines and code!
visualizations with Python’s super-handy libraries, and a
simple web app you can deploy online. ABOUT THE AUTHOR
As you work through Python Crash Course, you’ll learn Eric Matthes is a high school science and math teacher
how to: living in Alaska, where he teaches an introductory
Python course. He has been writing programs since he
• Use powerful Python libraries and tools, including
was five years old.
matplotlib, NumPy, and Pygal
T H E F I N E ST I N G E E K E N T E RTA I N M E N T ™
w w w.nostarch.com MATTHES
b y E r ic M a t t h e s
San Francisco
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
For information on distribution, translations, or bulk sales, please contact No Starch Press, Inc. directly:
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.
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvii
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxix
Part I: Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Chapter 1: Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Chapter 5: if Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
Chapter 6: Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95