Python Programming Cheat Sheet
The core ideas of Python Programming distilled into a single, scannable reference — perfect for review or quick lookup.
Quick Reference
Variables and Data Types
Variables are named containers that store data values. Python has several built-in data types: integers (whole numbers like 42), floats (decimal numbers like 3.14), strings (text wrapped in quotes like 'hello'), and booleans (True or False). Python uses dynamic typing, meaning you do not need to declare a variable's type -- Python figures it out from the value you assign.
Control Flow (if/elif/else)
Control flow statements allow your program to make decisions based on conditions. An 'if' statement executes code only when a condition is True. 'elif' (else if) checks additional conditions. 'else' runs when no previous condition was True. Python uses indentation (typically 4 spaces) to define code blocks, unlike many languages that use curly braces.
Loops (for and while)
Loops repeat a block of code multiple times. A 'for' loop iterates over a sequence (list, string, range). A 'while' loop repeats as long as a condition is True. The 'break' statement exits a loop early, and 'continue' skips to the next iteration. Python's for loop is particularly elegant because it works directly with any iterable object.
Functions
Functions are reusable blocks of code defined with the 'def' keyword. They take parameters (inputs), execute code, and optionally return a value. Functions make code modular, readable, and easier to debug. Python also supports default parameter values and keyword arguments for flexibility.
Lists
Lists are ordered, mutable (changeable) collections of items, defined with square brackets. They can hold items of any type and support indexing (accessing by position), slicing (extracting a range), and many built-in methods like append(), remove(), sort(), and len(). Lists are one of the most frequently used data structures in Python.
Dictionaries
Dictionaries are unordered collections of key-value pairs, defined with curly braces. Each key maps to a value, allowing fast lookups by key. Keys must be unique and immutable (strings and numbers are common choices). Dictionaries are ideal for representing structured data like user profiles, configurations, or any mapping relationship.
File I/O
File I/O (Input/Output) is reading from and writing to files on disk. Python's built-in open() function handles file operations. The 'with' statement (context manager) ensures files are properly closed after use, even if an error occurs. Common modes are 'r' (read), 'w' (write/overwrite), and 'a' (append).
Modules and pip
A module is a Python file containing reusable code (functions, classes, variables). Python's standard library includes hundreds of built-in modules (os, math, datetime, json). Third-party packages are installed using pip (Python's package manager) from the Python Package Index (PyPI). This ecosystem of packages is one of Python's greatest strengths.
Debugging Basics
Debugging is the process of finding and fixing errors in your code. Python raises descriptive error messages (tracebacks) that tell you the type of error, the file, and the line number. Common errors include SyntaxError (typo or structural mistake), NameError (using a variable that does not exist), TypeError (wrong data type for an operation), and IndexError (accessing a list index that does not exist). Using print() statements and Python's built-in debugger (pdb) are common debugging strategies.
String Formatting (f-strings)
F-strings (formatted string literals), introduced in Python 3.6, allow you to embed expressions inside string literals using curly braces preceded by the letter 'f'. They are the most readable and fastest way to format strings in Python, replacing older methods like .format() and % formatting.
Key Terms at a Glance
Get study tips in your inbox
We'll send you evidence-based study strategies and new cheat sheets as they're published.
We'll notify you about updates. No spam, unsubscribe anytime.