Python Crash Course A Hands On Project Based Introduction to Programming 2nd Edition Eric Matthes pdf download
Python Crash Course A Hands On Project Based Introduction to Programming 2nd Edition Eric Matthes pdf download
https://ebookmeta.com/product/python-crash-course-a-hands-on-
project-based-introduction-to-programming-2nd-edition-eric-
matthes/
“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 MAGAZINE
“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
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
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
Acknowledgments
Introduction
PART I: BASICS
Chapter 1: Getting Started
Chapter 2: Variables and Simple Data Types
Chapter 3: Introducing Lists
Chapter 4: Working with Lists
Chapter 5: if Statements
Chapter 6: Dictionaries
Chapter 7: User Input and while Loops
Chapter 8: Functions
Chapter 9: Classes
Chapter 10: Files and Exceptions
Chapter 11: Testing Your Code
Afterword
Index
CONTENTS IN DETAIL
PREFACE TO THE SECOND EDITION
ACKNOWLEDGMENTS
INTRODUCTION
Who Is This Book For?
What Can You Expect to Learn?
Online Resources
Why Python?
PART I: BASICS
1
GETTING STARTED
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
2
VARIABLES AND SIMPLE DATA TYPES
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
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
Organizing a List
Sorting a List Permanently with the sort() Method
Sorting a List Temporarily with the sorted() Function
Printing a List in Reverse Order
Finding the Length of a List
Exercise 3-8: Seeing the World
Exercise 3-9: Dinner Guests
Exercise 3-10: Every Function
Avoiding Index Errors When Working with Lists
Exercise 3-11: Intentional Error
Summary
4
WORKING WITH LISTS
Looping Through an Entire List
A Closer Look at Looping
Doing More Work Within a for Loop
Doing Something After a for Loop
Avoiding Indentation Errors
Forgetting to Indent
Forgetting to Indent Additional Lines
Indenting Unnecessarily
Indenting Unnecessarily After the Loop
Forgetting the Colon
Exercise 4-1: Pizzas
Exercise 4-2: Animals
Making Numerical Lists
Using the range() Function
Using range() to Make a List of Numbers
Simple Statistics with a List of Numbers
List Comprehensions
Exercise 4-3: Counting to Twenty
Exercise 4-4: One Million
Exercise 4-5: Summing a Million
Exercise 4-6: Odd Numbers
Exercise 4-7: Threes
Exercise 4-8: Cubes
Exercise 4-9: Cube Comprehension
Working with Part of a List
Slicing a List
Looping Through a Slice
Other documents randomly have
different content
FOOTNOTES.
Updated editions will replace the previous one—the old editions will
be renamed.
1.D. The copyright laws of the place where you are located also
govern what you can do with this work. Copyright laws in most
countries are in a constant state of change. If you are outside the
United States, check the laws of your country in addition to the
terms of this agreement before downloading, copying, displaying,
performing, distributing or creating derivative works based on this
work or any other Project Gutenberg™ work. The Foundation makes
no representations concerning the copyright status of any work in
any country other than the United States.
1.E.6. You may convert to and distribute this work in any binary,
compressed, marked up, nonproprietary or proprietary form,
including any word processing or hypertext form. However, if you
provide access to or distribute copies of a Project Gutenberg™ work
in a format other than “Plain Vanilla ASCII” or other format used in
the official version posted on the official Project Gutenberg™ website
(www.gutenberg.org), you must, at no additional cost, fee or
expense to the user, provide a copy, a means of exporting a copy, or
a means of obtaining a copy upon request, of the work in its original
“Plain Vanilla ASCII” or other form. Any alternate format must
include the full Project Gutenberg™ License as specified in
paragraph 1.E.1.
• You pay a royalty fee of 20% of the gross profits you derive
from the use of Project Gutenberg™ works calculated using the
method you already use to calculate your applicable taxes. The
fee is owed to the owner of the Project Gutenberg™ trademark,
but he has agreed to donate royalties under this paragraph to
the Project Gutenberg Literary Archive Foundation. Royalty
payments must be paid within 60 days following each date on
which you prepare (or are legally required to prepare) your
periodic tax returns. Royalty payments should be clearly marked
as such and sent to the Project Gutenberg Literary Archive
Foundation at the address specified in Section 4, “Information
about donations to the Project Gutenberg Literary Archive
Foundation.”
• You comply with all other terms of this agreement for free
distribution of Project Gutenberg™ works.
1.F.
1.F.4. Except for the limited right of replacement or refund set forth
in paragraph 1.F.3, this work is provided to you ‘AS-IS’, WITH NO
OTHER WARRANTIES OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO WARRANTIES OF
MERCHANTABILITY OR FITNESS FOR ANY PURPOSE.
Please check the Project Gutenberg web pages for current donation
methods and addresses. Donations are accepted in a number of
other ways including checks, online payments and credit card
donations. To donate, please visit: www.gutenberg.org/donate.
Most people start at our website which has the main PG search
facility: www.gutenberg.org.