Python 101
Python 101
2nd Edition
About the Book
The second edition of Python 101 is completely rewritten from the ground up. In this book, you will learn the Python programming language and lots more.
This book is split up into four sections:
- The Python Language
- Intermediate Topics
- Creating Sample Applications
- Distributing Your Code
Following is how it will break down in more detail:
Part I - The Python Language
- 01 - Installation
- 02 - Python editors
- 03 - Python style guide / comment rules
- 04 - Integers and floats
- 05 - Lists
- 06 - Tuples
- 07 - Dictionaries
- 08 - Sets
- 09 - Strings
- 10 - Boolean
- 11 - Conditional statements
- 12 - Loops
- 13 - Comprehensions
- 14 - Exception Handling
- 15 - Files
- 16 - Imports
- 17 - Functions
- 18 - Classes
In Part 2 - Intermediate Topics, you will then learn about some intermediate topics, such as:
- Introspection
- Pip
- Python virtual environments
- Threading
- Multiprocessing
- Debugging
- Decorators
- Profiling
- Testing
- Version Control (git)
In Part 3 - Creating Sample Applications, you will learn how to write sample applications:
- How to create a command line application (argparse)
- How to parse XML
- How to parse JSON
- How to Scrape a website
- How to Work with CSV files
- How to work with a database (sqlite)
- How to Create an Excel document
- How to Generate a PDF
Finally in Part 4 you will learn how distribute your code as an executable and as source.
Bundles that include this book
Table of Contents
-
About the Technical Reviewers
- Ethan Furman
- Martin Breuss
- Acknowledgments
-
Introduction
- Part I - The Basics
- Part II - Intermediate Materials
- Part III - Tutorials
- Part IV - Python Packaging and Distribution
- Target Audience
- About the Author
- Conventions
- Requirements
- Book Source Code
- Reader Feedback
- Errata
- Part I - The Python Language
-
Chapter 1 - Installing Python
- Installing on Windows
- Installing on Mac
- Installing on Linux
- Android / iOS
- Other Operating Systems
- Other Python Variants
- Wrapping Up
-
Chapter 2 - Python Editors
- What About the REPL?
- Getting Started with IDLE
- Getting Started with PyCharm Community Edition
- Getting Started with Wing Personal
- Getting Started with Visual Studio Code
- Wrapping Up
-
Chapter 3 - Documenting Your Code
- What are Comments?
- Commenting Out
- Multiline Comments
- Learning About docstrings
- Python’s Style Guide: PEP8
- Tools that can help
- Wrapping Up
- Review Questions
-
Chapter 4 - Working with Strings
- Creating Strings
- String Methods
- String Formatting
- Formatting Strings Using %s (printf-style)
- Formatting Strings Using .format()
- Formatting Strings with f-strings
- String Concatenation
- String Slicing
- Wrapping Up
- Review Questions
-
Chapter 5 - Numeric Types
- Integers
- Floats
- Complex Numbers
- Numeric Operations
- Augmented Assignment
- Wrapping Up
- Review Questions
-
Chapter 6 - Learning About Lists
- Creating Lists
- List Methods
- List Slicing
- Copying a List
- Wrapping Up
- Review Questions
-
Chapter 7 - Learning About Tuples
- Creating Tuples
- Working With Tuples
- Concatenating Tuples
- Special Case Tuples
- Wrapping Up
- Review Questions
-
Chapter 8 - Learning About Dictionaries
- Creating Dictionaries
- Accessing Dictionaries
- Dictionary Methods
- Modifying Your Dictionary
- Deleting Items From Your Dictionary
- Wrapping Up
- Review Questions
-
Chapter 9 - Learning About Sets
- Creating a Set
- Accessing Set Members
- Changing Items
- Adding Items
- Removing Items
- Clearing or Deleting a Set
- Set Operations
- Wrapping Up
- Review Questions
-
Chapter 10 - Boolean Operations and None
- The bool() Function
- What About None?
- Wrapping Up
- Review Questions
-
Chapter 11 - Conditional Statements
- Comparison Operators
- Creating a Simple Conditional
- Branching Conditional Statements
- Nesting Conditionals
- Logical Operators
- Special Operators
- Wrapping Up
- Review Questions
-
Chapter 12 - Learning About Loops
-
Creating a
for
Loop - Looping Over a String
- Looping Over a Dictionary
- Extracting Multiple Values in a Tuple While Looping
-
Using
enumerate
with Loops -
Creating a
while
Loop - Breaking Out of a Loop
-
Using
continue
-
Loops and the
else
Statement - Nesting Loops
- Wrapping Up
- Review Questions
-
Creating a
-
Chapter 13 - Python Comprehensions
- List Comprehensions
- Nested List Comprehensions
- Dictionary Comprehensions
- Set Comprehensions
- Wrapping Up
- Review Questions
-
Chapter 14 - Exception Handling
- The Most Common Exceptions
- Handling Exceptions
- Raising Exceptions
- Examining the Exception Object
-
Using the
finally
Statement -
Using the
else
Statement - Wrapping Up
- Review Questions
-
Chapter 15 - Working with Files
- The open() Function
- Reading Files
- Reading Binary Files
- Writing Files
- Seeking Within a File
- Appending to Files
- Catching File Exceptions
- Wrapping Up
- Review Questions
-
Chapter 16 - Importing
-
Using
import
-
Using
from
to Import Specific Bits & Pieces -
Using
as
to assign a new name - Importing Everything
- Wrapping Up
- Review Questions
-
Using
-
Chapter 17 - Functions
- Creating a Function
- Calling a Function
- Passing Arguments
- Type Hinting Your Arguments
- Passing Keyword Arguments
- Required and Default Arguments
-
What are
*args
and**kwargs
? - Positional-only Parameters
- Scope
- Wrapping Up
- Review Questions
-
Chapter 18 - Classes
- Class Creation
-
Figuring Out
self
- Public and Private Methods / Attributes
- Subclass Creation
- Polymorphism
- Making the Class Nicer
- Wrapping Up
- Review Questions
- Part II - Beyond the Basics
-
Chapter 19 - Introspection
-
Using the
type()
Function -
Using the
dir()
Function -
Getting
help()
- Other Built-in Introspection Tools
- Wrapping Up
- Review Questions
-
Using the
-
Chapter 20 - Installing Packages with pip
- Installing a Package
- Exploring Command Line Options
-
Installing with
requirements.txt
- Upgrading a Package
- Checking What’s Installed
- Uninstalling Packages
- Alternatives to pip
- Wrapping Up
- Review Questions
-
Chapter 21 - Python Virtual Environments
-
Python’s
venv
Library -
The
virtualenv
Package - Other Tools
- Wrapping Up
- Review Questions
-
Python’s
-
Chapter 22 - Type Checking in Python
- Pros and Cons of Type Hinting
- Built-in Type Hinting / Variable Annotation
- Collection Type Hinting
-
Hinting Values That Could be
None
- Type Hinting Functions
- What To Do When Things Get Complicated
- Classes
- Decorators
- Aliasing
- Other Type Hints
- Type Comments
- Static Type Checking
- Wrapping Up
- Review Questions
-
Chapter 23 - Creating Multiple Threads
- Pros of Using Threads
- Cons of Using Threads
- Creating Threads
-
Subclassing
Thread
- Writing Multiple Files with Threads
- Wrapping Up
- Review Questions
-
Chapter 24 - Creating Multiple Processes
- Pros of Using Processes
- Cons of Using Processes
-
Creating Processes with
multiprocessing
-
Subclassing
Process
- Creating a Process Pool
- Wrapping Up
- Review Questions
-
Chapter 25 - Launching Subprocesses with Python
-
The
subprocess.run()
Function -
The
subprocess.Popen()
Class -
The
subprocess.Popen.communicate()
Function -
Reading and Writing with
stdin
andstdout
- Wrapping Up
- Review Questions
-
The
-
Chapter 26 - Debugging Your Code with pdb
-
Starting
pdb
in the REPL -
Starting
pdb
on the Command Line - Stepping Through Code
-
Adding Breakpoints in
pdb
-
Creating a Breakpoint with
set_trace()
-
Using the built-in
breakpoint()
Function - Getting Help
- Wrapping Up
- Review Questions
-
Starting
-
Chapter 27 - Learning About Decorators
- Creating a Function
- Creating a Decorator
- Applying a Decorator with @
- Creating a Decorator for Logging
- Stacking Decorators
- Passing Arguments to Decorators
- Using a Class as a Decorator
- Python’s Built-in Decorators
- Python Properties
- Wrapping Up
- Review Questions
-
Chapter 28 - Assignment Expressions
- Using Assignment Expressions
- What You Cannot Do With Assignment Expressions
- Wrapping Up
- Review Questions
-
Chapter 29 - Profiling Your Code
-
Learning How to Profile with
cProfile
-
Profiling a Python Script with
cProfile
-
Working with Profile Data Using
pstats
- Other Profilers
- Wrapping Up
- Review Questions
-
Learning How to Profile with
-
Chapter 30 - An Introduction to Testing
-
Using
doctest
in the Terminal -
Using
doctest
in Your Code -
Using
doctest
From a Separate File -
Using
unittest
For Test Driven Development - Wrapping Up
- Review Questions
-
Using
-
Chapter 31 - Learning About the Jupyter Notebook
- Installing The Jupyter Notebook
- Creating a Notebook
- Adding Content
- Adding an Extension
- Exporting Notebooks to Other Formats
- Wrapping Up
- Review Questions
- Part III - Practical Python
-
Chapter 32 - How to Create a Command-line Application with
argparse
- Parsing Arguments
- Creating Helpful Messages
- Adding Aliases
- Using Mutually Exclusive Arguments
- Creating a Simple Search Utility
- Wrapping Up
- Review Questions
-
Chapter 33 - How to Parse XML
-
Parsing XML with
ElementTree
-
Creating XML with
ElementTree
-
Editing XML with
ElementTree
-
Manipulating XML with
lxml
- Wrapping Up
- Review Questions
-
Parsing XML with
-
Chapter 34 - How to Parse JSON
- Encoding a JSON String
- Saving JSON to Disk
- Decoding a JSON String
- Loading JSON from Disk
-
Validating JSON with
json.tool
- Wrapping Up
- Review Questions
-
Chapter 35 - How to Scrape a Website
- Rules for Web Scraping
- Preparing to Scrape a Website
- Scraping a Website
- Downloading a File
- Wrapping Up
- Review Questions
-
Chapter 36 - How to Work with CSV files
- Reading a CSV File
-
Reading a CSV File with
DictReader
- Writing a CSV File
-
Writing a CSV File with
DictWriter
- Wrapping Up
- Review Questions
-
Chapter 37 - How to Work with a Database Using
sqlite3
- Creating a SQLite Database
- Adding Data to Your Database
- Searching Your Database
- Editing Data in Your Database
- Deleting Data From Your Database
- Wrapping Up
- Review Questions
-
Chapter 38 - Working with an Excel Document in Python
- Python Excel Packages
- Getting Sheets from a Workbook
- Reading Cell Data
- Iterating Over Rows and Columns
- Writing Excel Spreadsheets
- Adding and Removing Sheets
- Adding and Deleting Rows and Columns
- Wrapping Up
- Review Questions
-
Chapter 39 - How to Generate a PDF
- Installing ReportLab
- Creating a Simple PDF with the Canvas
- Creating Drawings and Adding Images Using the Canvas
- Creating Multi-page Documents with PLATYPUS
- Creating a Table
- Wrapping Up
- Review Questions
-
Chapter 40 - How to Create Graphs
- Installing Matplotlib
- Creating a Simple Line Chart with PyPlot
- Creating a Bar Chart
- Creating a Pie Chart
- Adding Labels
- Adding Titles to Plots
- Creating a Legend
- Showing Multiple Figures
- Wrapping Up
- Review Questions
-
Chapter 41 - How to Work with Images in Python
- Installing Pillow
- Opening Images
- Cropping Images
- Using Filters
- Adding Borders
- Resizing Images
- Wrapping Up
- Review Questions
-
Chapter 42 - How to Create a Graphical User Interface
- Installing wxPython
- Learning About Event Loops
- How to Create Widgets
- How to Lay Out Your Application
- How to Add Events
- How to Create an Application
- Wrapping Up
- Review Questions
- Part IV - Distributing Your Code
-
Chapter 43 - How to Create a Python Package
- Creating a Module
- Creating a Package
- Packaging a Project for PyPI
- Creating Project Files
-
Creating
setup.py
- Generating a Python Wheel
- Uploading to PyPI
- Wrapping Up
- Review Questions
-
Chapter 44 - How to Create an Exe for Windows
- Installing PyInstaller
- Creating an Executable for a Command-Line Application
- Creating an Executable for a GUI
- Wrapping Up
- Review Questions
-
Chapter 45 - How to Create an Installer for Windows
- Installing Inno Setup
- Creating an Installer
- Testing Your Installer
- Wrapping Up
- Review Questions
-
Chapter 46 - How to Create an “exe” for Mac
- Installing PyInstaller
- Creating an Executable with PyInstaller
- Wrapping Up
- Review Questions
- Afterword
-
Appendix A - Version Control
- Version Control Systems
- Distributed vs Centralized Versioning
- Common Terminology
- Python IDE Version Control Support
- Wrapping Up
-
Appendix B - Version Control with Git
- Installing Git
- Configuring Git
- Creating a Project
- Ignoring Files
- Initializing a Repository
- Checking the Project Status
- Adding Files to a Repository
- Committing Files
- Viewing the Log
- Changing a File
- Reverting a File
- Checking Out Previous Commits
- Pushing to Github
- Wrapping Up
-
Review Question Answer Key
- Chapter 3 - Documenting Your Code
- Chapter 4 - Working with Strings
- Chapter 5 - Numeric Types
- Chapter 6 - Learning About Lists
- Chapter 7 - Learning About Tuples
- Chapter 8 - Learning About Dictionaries
- Chapter 9 - Learning About Sets
- Chapter 10 - Boolean Operations and None
- Chapter 11 - Conditional Statements
- Chapter 12 - Learning About Loops
- Chapter 13 - Python Comprehensions
- Chapter 14 - Exception Handling
- Chapter 15 - Working with Files
- Chapter 16 - Importing
- Chapter 17 - Functions
- Chapter 18 - Classes
- Chapter 19 - Introspection
- Chapter 20 - Installing Packages with pip
- Chapter 21 - Python Virtual Environments
- Chapter 22 - Type Checking in Python
- Chapter 23 - Creating Multiple Threads
- Chapter 24 - Creating Multiple Processes
- Chapter 25 - Launching Subprocesses with Python
- Chapter 26 - Debugging Your Code
- Chapter 27 - Learning About Decorators
- Chapter 28 - Assignment Expressions
- Chapter 29 - Profiling Your Code
- Chapter 30 - An Introduction to Testing
- Chapter 31 - Learning About the Jupyter Notebook
-
Chapter 32 - How to Create a Command Line Application with
argparse
- Chapter 33 - How to Parse XML
- Chapter 34 - How to Parse JSON
- Chapter 35 - How to Scrape a Website
- Chapter 36 - How to Work with CSV files
-
Chapter 37 - How to Work with a Database Using
sqlite
- Chapter 38 - Working with an Excel Document in Python
- Chapter 39 - How to Generate a PDF
- Chapter 40 - How to Create Graphs
- Chapter 41 - How to Work with Images in Python
- Chapter 42 - How to Create a Graphical User Interface
- Chapter 43 - How to Create a Python Package
- Chapter 44 - How to Create an Exe for Windows
- Chapter 45 - How to Create an Installer for Windows
- Chapter 46 - How to Create an “exe” for Mac
The Leanpub 60 Day 100% Happiness Guarantee
Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.
Now, this is technically risky for us, since you'll have the book or course files either way. But we're so confident in our products and services, and in our authors and readers, that we're happy to offer a full money back guarantee for everything we sell.
You can only find out how good something is by trying it, and because of our 100% money back guarantee there's literally no risk to do so!
So, there's no reason not to click the Add to Cart button, is there?
See full terms...
Earn $8 on a $10 Purchase, and $16 on a $20 Purchase
We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.
(Yes, some authors have already earned much more than that on Leanpub.)
In fact, authors have earnedover $13 millionwriting, publishing and selling on Leanpub.
Learn more about writing on Leanpub
Free Updates. DRM Free.
If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).
Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.
Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.
Learn more about Leanpub's ebook formats and where to read them