Python is readable, but nobody memorizes every built-in method. The useful approach is to learn the patterns that repeat across almost every program, then keep a reference for the details. The Printable Python Beginner Cheatsheet is organized around exactly those patterns across four pages. This guide walks through the same topics with examples you can run.
Variables, numbers and strings
Python infers types, so you can assign and reuse names freely. f-strings are the fastest way to combine text and values:
name = "Ana"
score = 92.5
print(f"{name} scored {score:.1f}") # Ana scored 92.5
print(round(score)) # 92
print(" hello ".strip().upper()) # HELLO
Operators to know early: // for integer division, % for remainders, and ** for powers.
Lists and dictionaries
Lists hold ordered items, and dictionaries map keys to values. Use .get() on dictionaries to avoid errors when a key might be missing:
fruits = ["apple", "pear"]
fruits.append("plum")
print(fruits[0], fruits[-1]) # apple plum
print(fruits[1:3]) # ['pear', 'plum']
scores = {"ana": 90, "ben": 75}
print(scores.get("cara", 0)) # 0, no KeyError
Tuples are fixed after creation, which makes them good for values that should not change. Sets remove duplicates and support .union() and .intersection().
Loops, conditions and functions
for i, fruit in enumerate(fruits):
print(i, fruit)
def greet(name, greeting="Hello"):
return f"{greeting}, {name}!"
print(greet("Ben")) # Hello, Ben!
Use enumerate() whenever you need both the index and the item. Default arguments, as in greeting="Hello", let callers skip optional values.
Reading and writing files
The with statement closes the file automatically, even if an error occurs:
with open("notes.txt", "w") as f:
f.write("First line\n")
with open("notes.txt", "r") as f:
text = f.read()
Use "a" mode to append instead of overwrite.
Handling errors
try:
value = int("abc")
except ValueError:
print("That is not a number.")
else:
print("Converted:", value)
finally:
print("Done.")
Catch specific exceptions, such as ValueError, rather than every error. A broad catch can hide bugs.
Modules and basic classes
Built-in modules such as math, random, os and sys are imported with a single line. For object-oriented work, start with __init__ and self:
import math
class Circle:
def __init__(self, radius):
self.radius = radius
def area(self):
return math.pi * self.radius ** 2
print(round(Circle(2).area(), 2)) # 12.57
A study routine that works
- Type each example yourself instead of copying it.
- Change one value and predict the output before running it.
- Keep the cheatsheet beside your keyboard and look up methods as you need them, not all at once.
If you want a printable version of these patterns, the Printable Python Beginner Cheatsheet packages them into four study pages. Related: how to choose and use a printable cheatsheet.
