Python
From Zero to Real Programs
Thirteen chapters covering variables, control flow, functions, files, objects, error handling
and modern Python — each with worked examples and a practice lab, finishing with two
complete projects.
,Python: From Zero to Real Programs
Contents
01 Meet Python
02 Variables, Types & Operators
03 Working with Text
04 Collections
05 Making Decisions
06 Loops
07 Functions
08 Files
09 Objects & Classes
10 Inheritance & Special Methods
11 Errors & Modern Syntax
12 Power Tools
13 Capstone Projects
— Quick Reference
How to use this course
■ Read a section, then type the examples yourself. Copy-pasting skips the learning.
■ Change values, break things on purpose, and predict the result before you run the code.
■ Do the Practice Lab at the end of every chapter before moving on.
■ Code samples were written for Python 3.10 or newer (chapter 11 uses match/case).
2
,Python: From Zero to Real Programs
CHAPTER 01
Meet Python
Set up your tools and run your first program.
Why learn a programming language?
A computer follows instructions exactly, and only exactly. A programming language is the vocabulary
and grammar we use to write those instructions down. Once you can express a task in code, the
computer can repeat it a million times without getting tired or bored.
Why Python?
Python was designed to be readable. Its code often looks like plain English, so you spend your energy
on the problem instead of on punctuation. It is also enormously versatile:
■ Web back-ends with frameworks such as Django and Flask.
■ Data analysis and science with pandas, NumPy and Jupyter.
■ Machine learning with PyTorch, scikit-learn and friends.
■ Automation of boring computer chores: renaming files, sending reports, scraping pages.
■ Scripting and tooling for almost every operating system.
NOTE Good to know
Python is free, open source, and runs on Windows, macOS and Linux. Code you write on one
system almost always runs unchanged on another.
Installing Python
■ Visit the official download page at python.org and grab the newest stable release for your
system.
■ On Windows, tick Add python.exe to PATH on the first installer screen. Forgetting this is the
most common beginner snag.
■ Open a terminal (Command Prompt, PowerShell, or Terminal) and check the install worked:
python --version
If that prints something like Python 3.12.4, you are ready. On some macOS and Linux machines the
command is python3 instead of python. Use whichever works.
TIP Pick an editor
Any text editor works, but Visual Studio Code with its Python extension, or PyCharm Community,
will highlight mistakes and autocomplete for you.
3
, Python: From Zero to Real Programs
Your first program
Create a new file named hello.py, type the line below, and save it.
print("Hello, world!") # print() shows text on the screen
Run it from the terminal, in the folder where you saved the file:
python hello.py
The words Hello, world! appear. Congratulations: you have written and run a Python program.
The interactive shell (REPL)
Type python on its own and you enter the REPL, short for Read-Eval-Print Loop. It reads a line,
evaluates it, prints the result, and waits for the next one. It is perfect for quick experiments and for
using Python as a powerful calculator.
>>> 7 * 6
42
>>> 2 ** 10
1024
>>> "ab" * 3
'abba'
Leave the REPL with exit() or by pressing Ctrl+Z then Enter on Windows (Ctrl+D on macOS and
Linux).
Comments: notes for humans
Python ignores anything after a # on a line. Use comments to explain why the code does something,
not to repeat what it obviously does.
# Convert the price to cents to avoid rounding surprises
price_cents = 1999
total = price_cents * 3 # three items
For a longer explanation you can use a triple-quoted string. Strictly speaking it is a string that nobody
uses, but it works as a block note and, when placed first in a function or file, becomes its
documentation.
"""
Weekly report generator.
Reads sales.csv and prints a summary.
"""
Modules and pip
A module is simply a Python file whose code you can borrow with import. Python ships with a large
collection called the standard library, and thousands more are available from the community.
4