Python Crash Course, 2nd Edition PDF

Title Python Crash Course, 2nd Edition
Author Boris V
Pages 548
File Size 7.4 MB
File Type PDF
Total Downloads 151
Total Views 911

Summary

2ND EDITION WORLDWIDE PY THON BEST SELLER LEARN PYTHON— FAST! OVER 500,000 2ND EDITION COPIES SOLD CR ASH COURSE PY THON CR A SH COURSE Python Crash Course is the world’s best-selling guide • Create and customize web apps and deploy them to the Python programming language. This fast-paced, safely on...


Description

2ND EDITION WORLDWIDE BEST SELLER OVER 500,000 COPIES SOLD

In the first half of the book, you’ll learn basic programming concepts, such as variables, lists, classes, and loops, and practice writing clean code with exercises for each topic. You’ll also learn how to make your programs interactive and test your code safely before adding it to a project. In the second half, you’ll put your new knowledge into practice with three substantial projects: a Space Invaders–inspired arcade game, a set of data visualizations with Python’s handy libraries, and a simple web app you can deploy online. As you work through the book, you’ll learn how to: • Use powerful Python libraries and tools, including Pygame, Matplotlib, Plotly, and Django • Make 2D games that respond to keypresses and mouse clicks, and that increase in difficulty • Use data to generate interactive visualizations

• Create and customize web apps and deploy them safely online • Deal with mistakes and errors so you can solve your own programming problems This updated second edition has been thoroughly revised to reflect the latest in Python code and practices. The first half of the book includes improved coverage of topics like f-strings, constants, and managing data. In the second half, the code for the projects has been updated with better structure, cleaner syntax, and more popular and up-to-date libraries and tools, like Plotly and the latest version of Django. (For a full list of updates, see the Preface.) If you’ve been thinking about digging into programming, Python Crash Course will get you writing real programs fast. Why wait any longer? Start your engines and code! ABOUT THE AUTHOR

Eric Matthes is a high school science, math, and programming teacher living in Alaska. He has been writing programs since he was five years old and is the author of the Python Flash Cards, also from No Starch Press.

COVERS PYTHON 3.X

“ I L I E F L AT .” This book uses a durable binding that won’t snap shut

SHELVE IN: PROGRAMMING LANGUAGES/ PYTHON

FSC FPO

$39.95 ($53.95 CDN)

CR ASH COURSE 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

ERIC MAT THES

MATTHES

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

PY THON

2ND EDITION

PY THON CR A SH COURSE

Python Crash Course is the world’s best-selling guide to the Python programming language. This fast-paced, thorough introduction to programming with Python will have you writing programs, solving problems, and making things that work in no time.

LEARN PYTHON— FAST!

www.allitebooks.com

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 L aden, 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 M agazine “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 R eviews “Learning Python with Python Crash Course was an extremely positive experience! A great choice if you’re new to Python.” —Mikke G oes 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.” —R ealP ython.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

www.allitebooks.com

www.allitebooks.com

Python Crash Course 2nd Edition A Hands-On, Project-Based Introduction to Programming

b y E r ic M a t t h e s

San Francisco

www.allitebooks.com

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, 1972Python 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 software 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

Brief Contents

Preface to the Second 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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223 Project 1: Alien Invasion Chapter 12: A Ship that Fires Bullets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227 Chapter 13: Aliens! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255 Chapter 14: Scoring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279

Project 2: Data Visualization Chapter 15: Generating Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305 Chapter 16: Downloading Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333 Chapter 17: Working with APIs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359

Project 3: Web Applications Chapter 18: Getting Started with Django . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379 Chapter 19: User Accounts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409 Chapter 20: Styling and Deploying an App . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 437 Afterword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 465 Appendix A: Installation and Troubleshooting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 467 Appendix B: Text Editors and IDEs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473 Appendix C: Getting Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 479 Appendix D: Using Git for Version Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 485 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 495

x   Brief Contents

Conte nt s in De ta il Preface to the Second Edition

xxvii

Acknowledgments

xxxi

Introduction Who Is This Book For? . . . . . . . . What Can You Expect to Learn? . Online Resources . . . . . . . . . . . Why Python? . . . . . . . . . . . . . .

xxxiii . . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

xxxiv xxxiv . xxxv xxxvi

Part I: Basics

1

1 Getting Started

3

Setting Up Your Programming Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Python Versions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Running Snippets of Python Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . About the Sublime Text Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Python on Different Operating Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Python on Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Python on macOS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Python on Linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Running a Hello World Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Configuring Sublime Text to Use the Correct Python Version . . . . . . . . . . . . . . Running hello_world.py . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Troubleshooting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Running Python Programs from a Terminal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . On Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . On macOS and Linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Exercise 1-1: python.org . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Exercise 1-2: Hello World Typos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Exercise 1-3: Infinite Skills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. 3 . 4 . 4 . 4 . 5 . 5 . 7 . 8 . 9 . 9 10 11 12 12 12 13 13 13 13

2 Variables and Simple Data Types

15

What Really Happens When You Run hello_world.py . . . . . . . . . . . . . . . . . . . . . . . . . Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Naming and Using Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Avoiding Name Errors When Using Variables . . . . . . . . . . . . . . . . . . . . . . . . Variables Are Labels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Exercise 2-1: Simple Message . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Exercise 2-2: Simple Messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

15 16 17 17 18 19 19

Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Changing Case in a String with Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . Using Variables in Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Adding Whitespace to Strings with Tabs or Newlines . . . . . . . . . . . . . . . . . . . Stripping Whitespace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Avoiding Syntax Errors with Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Exercise 2-3: Personal Message . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Exercise 2-4: Name Cases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Exercise 2-5: Famous Quote . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Exercise 2-6: Famous Quote 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Exercise 2-7: Stripping Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Integers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Floats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Integers and Floats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Underscores in Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Multiple Assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Exercise 2-8: Number Eight . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Exercise 2-9: Favorite Number . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . How Do You Write Comments? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . What Kind of Comments Should You Write? . . . . . . . . . . . . . . . . . . . . . . . . . Exercise 2-10: Adding Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The Zen of Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Exercise 2-11: Zen of Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

3 Introducing Lists What Is a List? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Accessing Elements in a List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Index Positions Start at 0, Not 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using Individual Values from a List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Exercise 3-1: Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Exercise 3-2: Greetings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Exercise 3-3: Your Own List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Changing, Adding, and Removing Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Modifying Elements in a List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Adding Elements to a List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Removing Elements from a List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Exercise 3-4: Guest List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Exercise 3-5: Changing Guest List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Exercise 3-6: More Guests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Exercise 3-7: Shrinking Guest List...


Similar Free PDFs