Python 101

This document was uploaded by one of our users. The uploader already confirmed that they had the permission to publish it. If you are author/publisher or own the copyright of this documents, please report to us by using this DMCA report form.

Simply click on the Download Book button.

Yes, Book downloads on Ebookily are 100% Free.

Sometimes the book is free on Amazon As well, so go ahead and hit "Search on Amazon"

Author(s): Michael Driscoll
Edition: 2
Publisher: Michael Driscoll
Year: 2020

Language: English
Pages: 531
Tags: python, Python, python 101

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
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
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
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
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 and stdout
Wrapping Up
Review Questions
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
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
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
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
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