How to use this topic

The pages below form a learning path: each one builds on the ones before it, so reading them in order works well if you are new to Python. They also stand on their own as reference pages — every page starts with a short TL;DR.

All examples use Python 3.12 or later and are complete: copy one into a file, run it, and you get the output shown below it. Pages marked PCEP cover material from the PCEP™ – Certified Entry-Level Python Programmer exam. Testing Python code with pytest is covered in Unit Testing with pytest in Testing & QA.

Learning path

  1. 01What Is Python?What Python is, how the interpreter runs your code, and where the language is used.BeginnerPCEP
  2. 02Variables and Data TypesHow names refer to objects, and the built-in types for numbers, text, truth values and nothing.BeginnerPCEP
  3. 03Operators and ExpressionsArithmetic, comparison, logical and bitwise operators, and the order in which Python evaluates them.BeginnerPCEP
  4. 04StringsCreating, indexing, slicing and formatting text, and the string methods you will use every day.BeginnerPCEP
  5. 05Input and OutputReading from the keyboard with input(), writing with print(), and turning text into numbers.BeginnerPCEP
  6. 06Control FlowMaking decisions with if, elif and else, conditional expressions, and structural pattern matching with match.BeginnerPCEP
  7. 07LoopsRepeating work with for and while, counting with range(), and steering a loop with break, continue and else.BeginnerPCEP
  8. 08Lists and TuplesOrdered sequences: the mutable list and the immutable tuple, with indexing, slicing, methods and unpacking.BeginnerPCEP
  9. 09Dictionaries and SetsLooking up values by key with dictionaries, and keeping unique items with sets.BeginnerPCEP
  10. 10FunctionsDefining reusable blocks of code: parameters and arguments, return values, defaults, keywords and scope.BeginnerPCEP
  11. 11ComprehensionsBuilding lists, dictionaries and sets in one readable expression, and when a plain loop is clearer.IntermediatePCEP
  12. 12Error HandlingWhat exceptions are, how to catch them with try, except, else and finally, and how to raise your own.IntermediatePCEP
  13. 13Files and PathsReading and writing text files safely with open() and with, working with paths in pathlib, and handling CSV and JSON.Intermediate
  14. 14Modules, Packages, pip and Virtual EnvironmentsSplitting code into modules and packages, importing it, and installing third-party packages in an isolated environment.Intermediate
  15. 15Object-Oriented Programming BasicsClasses and objects, attributes and methods, __init__, and reusing behavior through inheritance.Intermediate
  16. 16Useful Standard LibraryA tour of modules that ship with Python: os, sys, datetime, random, collections and json.Intermediate
  17. 17Code Style and Good HabitsWriting code others can read: PEP 8, naming, comments and docstrings, and the basics of type hints.Beginner

Python glossary

Short definitions of the terms used in this topic. The full glossary covers every topic.