Transcription of PY THON - ia801701.us.archive.org
1 A HANDS-ON, PROJECT-BASEDINTRODUCTION TO PROGRAMMINGERIC MAT THESPYTHONCRASH COURSEPYTHONCRASH COURSESHELVE IN:PROGRAMMING LANGUAGES/ python $ ($ CDN)FAST!LEARN python FAST!LEARN python py thon CR ASH COURSEPY THON CR ASH COURSEMATTHESCOVERS python 2 AND 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.
2 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 problemsIf 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!ABOUT THE AUTHOREric 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. w w H E F INES T I N G EEK E N T E RTAI N M E N T I LIE FLAT. This book uses RepKover a durable binding that won t snap shutPython Crash CoursePython Crash Coursea hands-on, Project-Based Introduction to Programmingby Eric MatthesSan FranciscoPython Crash Course.
3 Copyright 2016 by Eric 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 printing19 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 PollockProduction Editor: Riley HoffmanCover Illustration: Josh EllingsonInterior Design: Octopod StudiosDevelopmental Editors: William Pollock, Liz Chadwick, and Leslie ShenTechnical Reviewer: Kenneth LoveCopyeditor: Anne Marie WalkerCompositor: Riley HoffmanProofreader: James FraleighFor information on distribution, translations, or bulk sales, please contact No Starch Press, Inc. directly:No Starch Press, 8th Street, San Francisco, CA 94103phone: ; of Congress Cataloging-in-Publication DataMatthes, Eric, 1972- python crash course : a hands-on, project-based introduction to programming / by Eric Matthes.
4 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. 2015 '3--dc23 2015018135No 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 information in this book is distributed on an As Is basis, without warranty.
5 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 the authorEric 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 the technical reviewerKenneth 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.
6 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 my father, who always made time to answer my questions about programming, and for Ever, who is just beginning to ask me his questionsBrIef ContentsAcknowledgments ..xxviiIntroduction ..xxixPart I: 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 InvasionChapter 12: A Ship That Fires Bullets ..235 Chapter 13: Aliens! ..265 Chapter 14: Scoring ..291x Brief ContentsProject 2: Data VisualizationChapter 15: Generating Data.
7 321 Chapter 16: Downloading Data ..349 Chapter 17: Working with APIs ..377 Project 3: Web ApplicationsChapter 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 Contents In DetaIlaCknowledgments xxviiIntroduCtIon xxixWho Is This Book For? ..xxxWhat Can You Expect to Learn? ..xxxWhy python ? ..xxxiPart I: BasICs 11 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 ..16On Linux and OS X ..16On Windows ..16 Exercise 1-1.
8 17 Exercise 1-2: Hello World 1-3: Infinite Skills ..17 Summary ..172 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 ..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 2-5: Famous Contents in DetailExercise 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?.
9 33 What Kind of Comments Should You Write? ..33 Exercise 2-10: Adding Zen of python ..34 Exercise 2-11: Zen of python ..36 Summary ..363 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: 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 3-6: More 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 3-9: Dinner Guests ..50 Exercise 3-10: Every Index Errors When Working with Lists.
10 50 Exercise 3-11: Intentional ..524 workIng with lIsts 53 Looping Through an Entire List ..53A Closer Look at Looping ..54 Doing More Work Within a for Loop ..55 Doing Something After a for Loop ..56 Contents in Detail xiiiAvoiding 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 4-5: Summing a 4-6: Odd 4-7: Threes ..64 Exercise 4-8: 4-9: Cube with Part of a List ..65 Slicing a List ..65 Looping Through a Slice ..66 Copying a List ..67 Exercise 4-10: 4-11: My Pizzas, Your 4-12: More.