Python tutorial

Learn Python from the first line of code, with every example runnable in the page.

Start the course

Course contents

Getting Started

  1. Introduction to Python — What Python is, why people learn it, where it is used, how to run your first program, and the three tools every program needs: print(), input() and comments.
  2. Variables and Data Types — How to store a value in a name, the kinds of value Python works with, how to check and convert between them, and why indentation changes what a program does.
  3. Operators — The symbols that do the work in Python: arithmetic, comparison, assignment shortcuts, and, or, is, in, bitwise, the walrus operator, and precedence.

Control Flow

  1. Conditional Statements — How to run code only when a test is true: if, else, elif chains and why their order matters, nesting, and the one-line conditional expression.
  2. Pattern Matching — The match statement: cases, the | pattern for grouping values, case _ as the default, the bare-name trap, and when match beats an if chain.
  3. Loops — Repeating work with for and while, counting with range(), break, continue and pass, nested loops, avoiding infinite loops, and the loop else.
  4. Functions — Writing and calling functions, every kind of argument, variable scope and the LEGB rule, packing and unpacking, annotations and docstrings.

Modules and Packages

  1. Code Organization — Modules and packages: creating and importing them, __name__ and the __main__ guard, the module search path, the standard library, math, and __init__.py.

Reference Types

  1. Arrays — The array module: type codes, indexing from both ends, inserting and removing, searching, every array method, enumerate() and the numeric built-ins.
  2. String Operations — Creating and slicing strings, escape sequences, three ways to format, and the string methods grouped by job: case, search, validation, split and join.
  3. Date and Time — The datetime module: date, time and datetime objects, strftime and strptime, timedelta arithmetic, timestamps, and naive against aware moments.

Object Oriented Programming

  1. Classes — Classes and objects, attributes and methods, self, the constructor, access modifiers and name mangling, class and static methods, and Enum.
  2. Inheritance — Parent and child classes, the five types of inheritance, inheriting and overriding, super(), and isinstance() and issubclass().
  3. Polymorphism — One name and many behaviours: method overriding, why Python has no overloading, dynamic binding, and operator overloading with magic methods.
  4. Abstraction — Hiding how something works behind what it does, abstract classes with the abc module, and abstract methods every child must implement.
  5. Encapsulation — Keeping data inside the class: data hiding, getters and setters, validation, and why Python uses @property instead.
  6. Advanced OOP — Inner classes, data classes with @dataclass, and the magic methods that make your objects work with print(), len(), in and for.

Collections

  1. Lists — Creating, indexing and slicing lists, updating and removing items, sorting with sort() and sorted(), shallow against deep copying, and every list method.
  2. Tuples — Immutable ordered records: the trailing-comma trap, slicing, rebuilding instead of changing, packing and unpacking, and the two tuple methods.
  3. Sets — Unique unordered values: creating sets, add and discard, union, intersection and difference, the whole method list, and frozen sets.
  4. Dictionary — Key-value pairs: key rules, get() against square brackets, adding and removing, looping over keys, values and items, and every dictionary method.

Exception Handling

  1. Handling Exception — Syntax errors against exceptions, the ten you meet first, try-except-else-finally, raising, custom exception types and exception chaining.

File Operations

  1. File Handling — Opening, the nine modes, three ways to read, why closing matters, the with statement, file errors, and text, CSV and JSON files.

Advanced Python

  1. Advanced Functions — Lambdas, closures and nonlocal, iterators and StopIteration, generators and yield, and decorators with functools.wraps.
  2. Class Relationships — Has-a instead of is-a: aggregation where the part is passed in, composition where the whole builds it, and how to tell them apart.
  3. Multithreading — Threads, processes, the main thread and child threads, the threading module, locks and events, and why the browser cannot start one.

References

  1. Python References — A lookup page for the whole course: one table per topic, from data types and operators to files, class relationships and threads.