Leanpub Header

Skip to main content

Python 101

2nd Edition

Learn how to write Python and beyond. You will not only learn the syntax of Python, but you will also create prototype applications and binaries that you can share with your family and friends.

The author is letting you choose the price you pay for this book!

Pick Your Price...
PDF
EPUB
WEB
14,265
Readers
548
Pages
110,062Words
About

About

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:

  1. The Python Language
  2. Intermediate Topics
  3. Creating Sample Applications
  4. 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.

Share this book

Price

Pick Your Price...

Minimum price

$19.99

$24.99

You pay

$24.99

Author earns

$19.99
$

All prices are in US $. You can pay in US $ or in your local currency when you check out.

EU customers: prices exclude VAT, which is added during checkout.

...Or Buy With Credits!

Number of credits (Minimum 2)

2
The author will earn $24.00 from your purchase!
You can get credits monthly with a Reader Membership

Author

About the Author

Michael Driscoll

Michael has been programming with Python since 2006. He is the author of the popular Python blog, The Mouse Vs. the Python. Michael has been a technical reviewer for Packt Publishing and No Starch Press.  He is also a contributor on Real Python.

He also co-authored The Essential Core Python Cheat Sheet for DZone.

Michael released his first book, Python 101, June 3rd, 2014. He wrote the follow up, Python 201: Intermediate Python and published it in the summer of 2016. He is also the author of wxPython Cookbook from Apress as well as Python Interviews from Packt Publishing.

Michael is currently working on some new books as well.

Leanpub Podcast

Episode 47

An Interview with Michael Driscoll

Contents

Table of Contents

About the Technical Reviewers

  1. Ethan Furman
  2. Martin Breuss

Acknowledgments

Introduction

  1. Part I - The Basics
  2. Part II - Intermediate Materials
  3. Part III - Tutorials
  4. Part IV - Python Packaging and Distribution
  5. Target Audience
  6. About the Author
  7. Conventions
  8. Requirements
  9. Book Source Code
  10. Reader Feedback
  11. Errata

Part I - The Python Language

Chapter 1 - Installing Python

  1. Installing on Windows
  2. Installing on Mac
  3. Installing on Linux
  4. Android / iOS
  5. Other Operating Systems
  6. Other Python Variants
  7. Wrapping Up

Chapter 2 - Python Editors

  1. What About the REPL?
  2. Getting Started with IDLE
  3. Getting Started with PyCharm Community Edition
  4. Getting Started with Wing Personal
  5. Getting Started with Visual Studio Code
  6. Wrapping Up

Chapter 3 - Documenting Your Code

  1. What are Comments?
  2. Commenting Out
  3. Multiline Comments
  4. Learning About docstrings
  5. Python’s Style Guide: PEP8
  6. Tools that can help
  7. Wrapping Up
  8. Review Questions

Chapter 4 - Working with Strings

  1. Creating Strings
  2. String Methods
  3. String Formatting
  4. Formatting Strings Using %s (printf-style)
  5. Formatting Strings Using .format()
  6. Formatting Strings with f-strings
  7. String Concatenation
  8. String Slicing
  9. Wrapping Up
  10. Review Questions

Chapter 5 - Numeric Types

  1. Integers
  2. Floats
  3. Complex Numbers
  4. Numeric Operations
  5. Augmented Assignment
  6. Wrapping Up
  7. Review Questions

Chapter 6 - Learning About Lists

  1. Creating Lists
  2. List Methods
  3. List Slicing
  4. Copying a List
  5. Wrapping Up
  6. Review Questions

Chapter 7 - Learning About Tuples

  1. Creating Tuples
  2. Working With Tuples
  3. Concatenating Tuples
  4. Special Case Tuples
  5. Wrapping Up
  6. Review Questions

Chapter 8 - Learning About Dictionaries

  1. Creating Dictionaries
  2. Accessing Dictionaries
  3. Dictionary Methods
  4. Modifying Your Dictionary
  5. Deleting Items From Your Dictionary
  6. Wrapping Up
  7. Review Questions

Chapter 9 - Learning About Sets

  1. Creating a Set
  2. Accessing Set Members
  3. Changing Items
  4. Adding Items
  5. Removing Items
  6. Clearing or Deleting a Set
  7. Set Operations
  8. Wrapping Up
  9. Review Questions

Chapter 10 - Boolean Operations and None

  1. The bool() Function
  2. What About None?
  3. Wrapping Up
  4. Review Questions

Chapter 11 - Conditional Statements

  1. Comparison Operators
  2. Creating a Simple Conditional

Indentation Matters in Python

  1. Branching Conditional Statements
  2. Nesting Conditionals
  3. Logical Operators
  4. Special Operators
  5. Wrapping Up
  6. Review Questions

Chapter 12 - Learning About Loops

  1. Creating a for Loop
  2. Looping Over a String
  3. Looping Over a Dictionary
  4. Extracting Multiple Values in a Tuple While Looping
  5. Using enumerate with Loops
  6. Creating a while Loop
  7. Breaking Out of a Loop
  8. Using continue
  9. Loops and the else Statement
  10. Nesting Loops
  11. Wrapping Up
  12. Review Questions

Chapter 13 - Python Comprehensions

  1. List Comprehensions
  2. Nested List Comprehensions
  3. Dictionary Comprehensions
  4. Set Comprehensions
  5. Wrapping Up
  6. Review Questions

Chapter 14 - Exception Handling

  1. The Most Common Exceptions
  2. Handling Exceptions
  3. Raising Exceptions
  4. Examining the Exception Object
  5. Using the finally Statement
  6. Using the else Statement
  7. Wrapping Up
  8. Review Questions

Chapter 15 - Working with Files

  1. The open() Function
  2. Reading Files
  3. Reading Binary Files
  4. Writing Files
  5. Seeking Within a File
  6. Appending to Files
  7. Catching File Exceptions
  8. Wrapping Up
  9. Review Questions

Chapter 16 - Importing

  1. Using import
  2. Using from to Import Specific Bits & Pieces
  3. Using as to assign a new name
  4. Importing Everything
  5. Wrapping Up
  6. Review Questions

Chapter 17 - Functions

  1. Creating a Function
  2. Calling a Function
  3. Passing Arguments
  4. Type Hinting Your Arguments
  5. Passing Keyword Arguments
  6. Required and Default Arguments
  7. What are *args and **kwargs?
  8. Positional-only Parameters
  9. Scope
  10. Wrapping Up
  11. Review Questions

Chapter 18 - Classes

  1. Class Creation
  2. Figuring Out self
  3. Public and Private Methods / Attributes
  4. Subclass Creation
  5. Polymorphism
  6. Making the Class Nicer
  7. Wrapping Up
  8. Review Questions

Part II - Beyond the Basics

Chapter 19 - Introspection

  1. Using the type() Function
  2. Using the dir() Function
  3. Getting help()
  4. Other Built-in Introspection Tools
  5. Wrapping Up
  6. Review Questions

Chapter 20 - Installing Packages with pip

  1. Installing a Package

What is the “Path”?

  1. Exploring Command Line Options
  2. Installing with requirements.txt
  3. Upgrading a Package
  4. Checking What’s Installed

Packages Not Installed with pip

  1. Uninstalling Packages
  2. Alternatives to pip
  3. Wrapping Up
  4. Review Questions

Chapter 21 - Python Virtual Environments

  1. Python’s venv Library
  2. The virtualenv Package
  3. Other Tools
  4. Wrapping Up
  5. Review Questions

Chapter 22 - Type Checking in Python

  1. Pros and Cons of Type Hinting
  2. Built-in Type Hinting / Variable Annotation
  3. Collection Type Hinting
  4. Hinting Values That Could be None
  5. Type Hinting Functions
  6. What To Do When Things Get Complicated
  7. Classes
  8. Decorators
  9. Aliasing
  10. Other Type Hints
  11. Type Comments
  12. Static Type Checking
  13. Wrapping Up
  14. Review Questions

Chapter 23 - Creating Multiple Threads

  1. Pros of Using Threads
  2. Cons of Using Threads
  3. Creating Threads
  4. Subclassing Thread
  5. Writing Multiple Files with Threads
  6. Wrapping Up
  7. Review Questions

Chapter 24 - Creating Multiple Processes

  1. Pros of Using Processes
  2. Cons of Using Processes
  3. Creating Processes with multiprocessing
  4. Subclassing Process
  5. Creating a Process Pool
  6. Wrapping Up
  7. Review Questions

Chapter 25 - Launching Subprocesses with Python

  1. The subprocess.run() Function
  2. The subprocess.Popen() Class

What is SIGKILL / SIGTERM?

  1. The subprocess.Popen.communicate() Function
  2. Reading and Writing with stdin and stdout
  3. Wrapping Up
  4. Review Questions

Chapter 26 - Debugging Your Code with pdb

  1. Starting pdb in the REPL
  2. Starting pdb on the Command Line
  3. Stepping Through Code
  4. Adding Breakpoints in pdb
  5. Creating a Breakpoint with set_trace()
  6. Using the built-in breakpoint() Function
  7. Getting Help
  8. Wrapping Up
  9. Review Questions

Chapter 27 - Learning About Decorators

  1. Creating a Function
  2. Creating a Decorator
  3. Applying a Decorator with @
  4. Creating a Decorator for Logging
  5. Stacking Decorators
  6. Passing Arguments to Decorators
  7. Using a Class as a Decorator
  8. Python’s Built-in Decorators
  9. Python Properties
  10. Wrapping Up
  11. Review Questions

Chapter 28 - Assignment Expressions

  1. Using Assignment Expressions
  2. What You Cannot Do With Assignment Expressions
  3. Wrapping Up
  4. Review Questions

Chapter 29 - Profiling Your Code

  1. Learning How to Profile with cProfile
  2. Profiling a Python Script with cProfile
  3. Working with Profile Data Using pstats
  4. Other Profilers
  5. Wrapping Up
  6. Review Questions

Chapter 30 - An Introduction to Testing

  1. Using doctest in the Terminal
  2. Using doctest in Your Code
  3. Using doctest From a Separate File
  4. Using unittest For Test Driven Development
  5. Wrapping Up
  6. Review Questions

Chapter 31 - Learning About the Jupyter Notebook

  1. Installing The Jupyter Notebook
  2. Creating a Notebook
  3. Adding Content
  4. Adding an Extension
  5. Exporting Notebooks to Other Formats
  6. Wrapping Up
  7. Review Questions

Part III - Practical Python

Chapter 32 - How to Create a Command-line Application with argparse

  1. Parsing Arguments
  2. Creating Helpful Messages
  3. Adding Aliases
  4. Using Mutually Exclusive Arguments
  5. Creating a Simple Search Utility
  6. Wrapping Up
  7. Review Questions

Chapter 33 - How to Parse XML

  1. Parsing XML with ElementTree
  2. Creating XML with ElementTree
  3. Editing XML with ElementTree
  4. Manipulating XML with lxml
  5. Wrapping Up
  6. Review Questions

Chapter 34 - How to Parse JSON

  1. Encoding a JSON String
  2. Saving JSON to Disk
  3. Decoding a JSON String
  4. Loading JSON from Disk
  5. Validating JSON with json.tool
  6. Wrapping Up
  7. Review Questions

Chapter 35 - How to Scrape a Website

  1. Rules for Web Scraping
  2. Preparing to Scrape a Website
  3. Scraping a Website
  4. Downloading a File
  5. Wrapping Up
  6. Review Questions

Chapter 36 - How to Work with CSV files

  1. Reading a CSV File
  2. Reading a CSV File with DictReader
  3. Writing a CSV File
  4. Writing a CSV File with DictWriter
  5. Wrapping Up
  6. Review Questions

Chapter 37 - How to Work with a Database Using sqlite3

  1. Creating a SQLite Database
  2. Adding Data to Your Database
  3. Searching Your Database
  4. Editing Data in Your Database
  5. Deleting Data From Your Database
  6. Wrapping Up
  7. Review Questions

Chapter 38 - Working with an Excel Document in Python

  1. Python Excel Packages
  2. Getting Sheets from a Workbook
  3. Reading Cell Data
  4. Iterating Over Rows and Columns
  5. Writing Excel Spreadsheets
  6. Adding and Removing Sheets
  7. Adding and Deleting Rows and Columns
  8. Wrapping Up
  9. Review Questions

Chapter 39 - How to Generate a PDF

  1. Installing ReportLab
  2. Creating a Simple PDF with the Canvas
  3. Creating Drawings and Adding Images Using the Canvas
  4. Creating Multi-page Documents with PLATYPUS
  5. Creating a Table
  6. Wrapping Up
  7. Review Questions

Chapter 40 - How to Create Graphs

  1. Installing Matplotlib
  2. Creating a Simple Line Chart with PyPlot
  3. Creating a Bar Chart
  4. Creating a Pie Chart
  5. Adding Labels
  6. Adding Titles to Plots
  7. Creating a Legend
  8. Showing Multiple Figures
  9. Wrapping Up
  10. Review Questions

Chapter 41 - How to Work with Images in Python

  1. Installing Pillow
  2. Opening Images
  3. Cropping Images
  4. Using Filters
  5. Adding Borders
  6. Resizing Images
  7. Wrapping Up
  8. Review Questions

Chapter 42 - How to Create a Graphical User Interface

  1. Installing wxPython
  2. Learning About Event Loops
  3. How to Create Widgets
  4. How to Lay Out Your Application
  5. How to Add Events
  6. How to Create an Application
  7. Wrapping Up
  8. Review Questions

Part IV - Distributing Your Code

Chapter 43 - How to Create a Python Package

  1. Creating a Module
  2. Creating a Package
  3. Packaging a Project for PyPI
  4. Creating Project Files
  5. Creating setup.py
  6. Generating a Python Wheel
  7. Uploading to PyPI
  8. Wrapping Up
  9. Review Questions

Chapter 44 - How to Create an Exe for Windows

  1. Installing PyInstaller
  2. Creating an Executable for a Command-Line Application
  3. Creating an Executable for a GUI
  4. Wrapping Up
  5. Review Questions

Chapter 45 - How to Create an Installer for Windows

  1. Installing Inno Setup
  2. Creating an Installer
  3. Testing Your Installer
  4. Wrapping Up
  5. Review Questions

Chapter 46 - How to Create an “exe” for Mac

  1. Installing PyInstaller
  2. Creating an Executable with PyInstaller
  3. Wrapping Up
  4. Review Questions

Afterword

Appendix A - Version Control

  1. Version Control Systems
  2. Distributed vs Centralized Versioning
  3. Common Terminology
  4. Python IDE Version Control Support
  5. Wrapping Up

Appendix B - Version Control with Git

  1. Installing Git
  2. Configuring Git
  3. Creating a Project
  4. Ignoring Files
  5. Initializing a Repository
  6. Checking the Project Status
  7. Adding Files to a Repository
  8. Committing Files
  9. Viewing the Log
  10. Changing a File
  11. Reverting a File
  12. Checking Out Previous Commits
  13. Pushing to Github
  14. Wrapping Up

Review Question Answer Key

  1. Chapter 3 - Documenting Your Code
  2. Chapter 4 - Working with Strings
  3. Chapter 5 - Numeric Types
  4. Chapter 6 - Learning About Lists
  5. Chapter 7 - Learning About Tuples
  6. Chapter 8 - Learning About Dictionaries
  7. Chapter 9 - Learning About Sets
  8. Chapter 10 - Boolean Operations and None
  9. Chapter 11 - Conditional Statements
  10. Chapter 12 - Learning About Loops
  11. Chapter 13 - Python Comprehensions
  12. Chapter 14 - Exception Handling
  13. Chapter 15 - Working with Files
  14. Chapter 16 - Importing
  15. Chapter 17 - Functions
  16. Chapter 18 - Classes
  17. Chapter 19 - Introspection
  18. Chapter 20 - Installing Packages with pip
  19. Chapter 21 - Python Virtual Environments
  20. Chapter 22 - Type Checking in Python
  21. Chapter 23 - Creating Multiple Threads
  22. Chapter 24 - Creating Multiple Processes
  23. Chapter 25 - Launching Subprocesses with Python
  24. Chapter 26 - Debugging Your Code
  25. Chapter 27 - Learning About Decorators
  26. Chapter 28 - Assignment Expressions
  27. Chapter 29 - Profiling Your Code
  28. Chapter 30 - An Introduction to Testing
  29. Chapter 31 - Learning About the Jupyter Notebook
  30. Chapter 32 - How to Create a Command Line Application with argparse
  31. Chapter 33 - How to Parse XML
  32. Chapter 34 - How to Parse JSON
  33. Chapter 35 - How to Scrape a Website
  34. Chapter 36 - How to Work with CSV files
  35. Chapter 37 - How to Work with a Database Using sqlite
  36. Chapter 38 - Working with an Excel Document in Python
  37. Chapter 39 - How to Generate a PDF
  38. Chapter 40 - How to Create Graphs
  39. Chapter 41 - How to Work with Images in Python
  40. Chapter 42 - How to Create a Graphical User Interface
  41. Chapter 43 - How to Create a Python Package
  42. Chapter 44 - How to Create an Exe for Windows
  43. Chapter 45 - How to Create an Installer for Windows
  44. Chapter 46 - How to Create an “exe” for Mac

Get the free sample chapters

Click the buttons to get the free sample in PDF or EPUB, or read the sample online here

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 earned over $14 million writing, 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

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub