
Practical Guide to Python
Learning Paths:
Topics:
Table of Contents
Introduction
Introduction
Nina Zakharenko introduces the Practical Python course and walks through the editor setup, and the prerequisites.Course Setup
Nina creates a directory and virtual environment for the Python project. The virtual environment allows a specific version of Python to be used in a project without interfering with other versions required by the operating system.REPL & Running Python
Nina demonstrates how to use the Python REPL to run programs line-by-line. Using the REPL can be faster than using a Python file since it eliminates the hassle of running a full program. The output is instantaneous inside the REPL as each line is run.
Why Python?
Why Python & Python Philosophy
Nina describes the history of Python, and why it appeals to a wide range of developers, from beginners to large-scale development teams at Instagram or Dropbox. Since Python is open source, there is a large community of contributors and third-party packages.Anatomy of a Python Program
Nina walks through a completed Python program that uses the GitHub API to load data from Python, JavaScript, and Ruby repositories with over 60000 stars. The code contains examples of variables, functions, lists, dictionaries, and loops.Running Python Q&A
Nina answers questions about different string formatters, and how to install third-party packages like the "request" package.
Variables & Data Types
Variables
Nina demonstrates how to create variables in Python. Variables are dynamically typed and the recommended naming convention is all lowercase letters with underscores between words.Helpful REPL Methods
Nina shares three helpful methods that can be used in the REPL. The type() method will return the type of a variable. The dir() method returns all the built-in methods for a class. The help() method returns the documentation for any class type or class method.Numbers
Nina demonstrates the int, float, and complex number types. The Boolean values True and False cast to the int values 1 and 0.Strings
Nina demonstrates how to work with strings. Preceding a string with the letter "f" creates an f-string which allows variables and expressions to be added inside curly brackets. String methods like replace() return the modified string value without mutating the original string.Lists
Nina demonstrates the list data type. Lists can be sorted or mutated with methods like append(), insert(), and reverse().Data Types, Strings & Numbers Practice
Nina walks the students through the data types, strings and numbers practice.
Functions, Loops, & Logic
Tuples
Nina explains that tuples are light-weight collections for keeping track of related, but different items. Unlike lists, tuples are immutable.Sets
Nina demonstrates that sets are mutable data types that store immutable items in an unsorted way. Items can be added and removed from sets. Unlike a list or tuple, a set can only contain one instance of a unique item.Dictionaries
Nina explains that dictionaries store data in key-value pairs. Dictionaries allow for fast item look up and fast membership testing because their keys are immutable.Running py Files
Nina shares some naming convention guidelines for Python files. File names should be short, all lowercase and use underscores to separate words. Python .pyc files are compiled intermediary files that can be ignored and should not be checked into source control. The pprint module is useful for printing large data structures like long lists or big dictionaries.Functions, Arguments & Scope
Nina explains that functions are defined with the "def" keyword. Arguments are included between the parentheses of a function. The body of a function is indented after the function definition.Boolean Logic & Control Statements
Nina demonstrates boolean operators and controls statements like if, elif, and else. The "is" keyword best for checking identity between two objects, not equality. If/Else statements use a colon and indentation similar to function blocks.Sets, Tuples, & Dictionaries Practice
Nina walks the students through the Sets, Tuples, & Dictionaries practice.Functions & Logic Practice
Nina walks the students through the Functions and Logic practice.Loops
Nina demonstrates "for" loops. The range() and enumerate() functions simplify the syntax when looping over large amounts of data. Tuple unpacking is useful when looping over tuples and dictionaries.Loops Practice
Nina walks the students through the Loops practice.
Practical Applications
List Comprehensions
Nina uses a list comprehension to perform an operation on every item in a list with only one line of code. A condition can be added to the list comprehension to determine if that value should be included in the returned list. Generator expressions are also covered in this segment.Slicing
Nina demonstrates how to use slicing to return a subset from a larger list. Slicing can be used on any data type that maintains an order, such as a string, list, or tuple.Working with Files
Nina explains how Python works with files on the operating system and the different modes available when opening a file. A context manager is a safer way to work with files since it will automatically close the file even if an exception occurs.Working with Files Practice
Nina walks the students through the working with Files practice.Practical Applications Practice
Nina walks the students through the Practical Applications practice.
Object Oriented Python
Classes vs Instances
Nina introduces Python classes, and explains the difference between a classes and the instance of a class. All Python data types inherit from a superclass. Within a class definition, the "self" keyword is used to reference the individual instance of that class.Methods & Magic Methods
Nina uses the @classmethod annotation to define a static method on a class that's shared across all instances. Python classes also have magic methods that are defined with two underscores before and after the method name. Some examples of magic methods are __init__(), __str__(), and __repr__().Inheritance
Nina shares a brief inheritance example. If a subclass is defined in the same file as its superclass, the class definition should be below the superclass's definition.Tracebacks & Exceptions
Nina implements exception handling with the "try" and "except" keywords. If an exception is thrown inside the "try" block, the error can be handled inside the "except" block. Multiple "except" blocks can be added to handle specific exception types.
Libraries & Modules
Main Method
Nina recommends adding a main method to Python programs to make them more modular. The main method will only execute when the program is running as a stand-alone application. If it has been imported in another module, the main method will not run.Virtual Environments
Nina shares best practices around using virtual environments. A requirements.txt file is used to annotate dependencies for a virtual environment. This file can be created manually or generated automatically by Python's package manager.Libraries, Modules & Imports
Nina explains how the import keyword is used to import either an entire module or just a function from a module. Python's standard library contains a number of useful modules for handling dates, system-specific functions, and json data.External Libraries & requests Library
Nina recommends copying the "pip install" command from the Python Package Index website to ensure the correct external package is installed. The requests library loads data from an external API. The status_code property in the response object can be used to handle successful or erroneous requests.Object Oriented Python Practice
Nina walks the students through the Object Oriented Python practice.Libraries & Modules Practice
Nina walks the students through the Libraries and Modules practice.
Web Frameworks
Python Web Frameworks
Nina introduces the Django and Flask frameworks for Python. Django includes a lot of features out of the box like ORM, user authentication, and content management. Flask allows users to create basic backend APIs easily, but requires third-party packages to extend beyond this basic functionality.Setup Django Blog Project
Nina installs the Django framework and uses it to create a basic blog application. The migrate command uses the manage.py file to initialize the database. The runserver command starts the local development server.Databases
Nina explores the Django project and the code that communicates with the SQLite database. Whenever a field is added or removed from the models in Django, the makemigrations and migrate commands should be run to update the database. The Django "shell" command allows models to be worked with in the command line.Views, Routes, & Templates
Nina explains how Django manages views and routes within the application. HTML templates are returned based on the pattern of the URL. Class-based views are also discussed in this segment.Django Admin
Nina uses the Django admin interface to manage the content of the blog. The admin can also manage user authentication and permissions.Django Tests & Final Project Practice
Nina explains why unit tests in Django should inherit from the TestCase class and not the built-in unit test package in Python. This segment also covers the final project practice which involves sorting blog posts and adding an is_draft field.