Python tutorial
Learn Python from the first line of code, with every example runnable in the page.
Course contents
Getting Started
- 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.
- 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.
- 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
- 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.
- 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.
- Loops — Repeating work with for and while, counting with range(), break, continue and pass, nested loops, avoiding infinite loops, and the loop else.
- Functions — Writing and calling functions, every kind of argument, variable scope and the LEGB rule, packing and unpacking, annotations and docstrings.
Modules and Packages
- 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
- Arrays — The array module: type codes, indexing from both ends, inserting and removing, searching, every array method, enumerate() and the numeric built-ins.
- 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.
- 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
- Classes — Classes and objects, attributes and methods, self, the constructor, access modifiers and name mangling, class and static methods, and Enum.
- Inheritance — Parent and child classes, the five types of inheritance, inheriting and overriding, super(), and isinstance() and issubclass().
- Polymorphism — One name and many behaviours: method overriding, why Python has no overloading, dynamic binding, and operator overloading with magic methods.
- Abstraction — Hiding how something works behind what it does, abstract classes with the abc module, and abstract methods every child must implement.
- Encapsulation — Keeping data inside the class: data hiding, getters and setters, validation, and why Python uses @property instead.
- Advanced OOP — Inner classes, data classes with @dataclass, and the magic methods that make your objects work with print(), len(), in and for.
Collections
- Lists — Creating, indexing and slicing lists, updating and removing items, sorting with sort() and sorted(), shallow against deep copying, and every list method.
- Tuples — Immutable ordered records: the trailing-comma trap, slicing, rebuilding instead of changing, packing and unpacking, and the two tuple methods.
- Sets — Unique unordered values: creating sets, add and discard, union, intersection and difference, the whole method list, and frozen sets.
- 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
- Handling Exception — Syntax errors against exceptions, the ten you meet first, try-except-else-finally, raising, custom exception types and exception chaining.
File Operations
- 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
- Advanced Functions — Lambdas, closures and nonlocal, iterators and StopIteration, generators and yield, and decorators with functools.wraps.
- 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.
- Multithreading — Threads, processes, the main thread and child threads, the threading module, locks and events, and why the browser cannot start one.
References
- Python References — A lookup page for the whole course: one table per topic, from data types and operators to files, class relationships and threads.