Practical Guide to Python
Table of Contents
IntroductionNina Zakharenko introduces the Practical Python course and walks through the editor setup, and the prerequisites.
Course SetupNina 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 PythonNina 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 & Python PhilosophyNina 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.
Running Python Q&ANina answers questions about different string formatters, and how to install third-party packages like the "request" package.
Variables & Data Types
VariablesNina 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 MethodsNina 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.
NumbersNina demonstrates the int, float, and complex number types. The Boolean values True and False cast to the int values 1 and 0.
StringsNina 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.
ListsNina demonstrates the list data type. Lists can be sorted or mutated with methods like append(), insert(), and reverse().
Data Types, Strings & Numbers PracticeNina walks the students through the data types, strings and numbers practice.
Functions, Loops, & Logic
TuplesNina explains that tuples are light-weight collections for keeping track of related, but different items. Unlike lists, tuples are immutable.
SetsNina 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.
DictionariesNina 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 FilesNina 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 & ScopeNina 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 StatementsNina 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 PracticeNina walks the students through the Sets, Tuples, & Dictionaries practice.
Functions & Logic PracticeNina walks the students through the Functions and Logic practice.
LoopsNina 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 PracticeNina walks the students through the Loops practice.
List ComprehensionsNina 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.
SlicingNina 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 FilesNina 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 PracticeNina walks the students through the working with Files practice.
Practical Applications PracticeNina walks the students through the Practical Applications practice.
Object Oriented Python
Classes vs InstancesNina 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 MethodsNina 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__().
InheritanceNina 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 & ExceptionsNina 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.
Object Oriented Python
Main MethodNina 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 EnvironmentsNina 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 & ImportsNina 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 LibraryNina 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 PracticeNina walks the students through the Object Oriented Python practice.
Libraries & Modules PracticeNina walks the students through the Libraries and Modules practice.
Python Web FrameworksNina 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 ProjectNina 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.
DatabasesNina 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, & TemplatesNina 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 AdminNina 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 PracticeNina 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.