The Python foundations cheat sheet.
Everything the foundations track teaches, on one page. Bookmark it, print it, keep it beside you — no sign-up, no catch.
Variables & f-strings
client = "Okafor & Sons"
hours = 12
print(f"{client} logged {hours} hours — {hours + 2} with the review.")
Text goes in quotes (a string); numbers don’t. An f before the quotes lets you drop values — even calculations — into a sentence with {curly braces}.
Lists
matters = ["Contract review", "Due diligence", "IP filing"]
matters.append("Board minutes") # add to the end
print(matters[0]) # first item — counting starts at 0
Loops
for matter in matters:
print(f"Status check: {matter}")
The indented line runs once per item. The loop variable (matter) is just a label for “whichever item we’re on”.
Decisions
if overdue_days > 30:
print("Send a reminder.")
elif overdue_days > 0:
print("Keep an eye on it.")
else:
print("All up to date.")
Indentation isn’t decoration — it’s how Python knows which lines belong to which branch.
Functions
def estimate_fee(hours, rate=250):
return hours * rate
estimate_fee(8) # uses the default rate
estimate_fee(8, 350) # overrides it
Write the logic once, name it, reuse it everywhere. return hands the result back.
Files
with open("clients.txt") as f:
for line in f:
print(line.strip())
with open("summary.txt", "w") as f: # "w" overwrites, "a" appends
f.write("Reviewed 3 files today.\n")
with guarantees the file closes safely, whatever happens.
Errors, decoded
Read the last line first, then find the line number it points to.
| Error | What it means | First thing to check |
|---|---|---|
NameError | You used a name Python doesn't know. | Usually a typo — check the spelling matches where you defined it. |
TypeError | You mixed types, e.g. text + number. | Convert first: int("12") for a number, str(12) for text. |
IndentationError | A line isn't indented where Python expected it. | Check the line the error points to lines up with the block above it. |
Want the full explanations? Every section here has a matching lesson in the free tutorials.