Foundations · #0 of 11
Welcome + Python Mental Model
How Python executes and why indentation matters
Why it matters
A clean mental model prevents 80% of beginner bugs and makes problem-solving faster.
The idea
Think of Python as a smart calculator plus a notebook. Expressions create values; names point to those values; statements do work.
The interpreter reads your file top-to-bottom. Indentation isn't decoration — it's syntax. Two spaces in, two spaces out: that's a block.
Try it
A name is a label stuck on a value. Re-binding x doesn't change 5, it just points the label elsewhere.
x = 5
print("x is", x)
x = "now I'm a string"
print("x is", x, "of type", type(x).__name__)
Blocks are defined by indentation. Mix tabs and spaces and Python will rightfully refuse to run.
for i in range(3):
if i % 2 == 0:
print(i, "even")
else:
print(i, "odd")
Quick check
- Q: What does
type(3/2)return? A:float - Q: Why does indentation matter? A: It defines code blocks (
if/for/def).
Mini drills
- Print your name and age on separate lines.
- Evaluate
7 // 2and7 % 2. - Try
"hi" * 3.
Do's and don'ts
- Do keep indentation consistent (4 spaces).
- Do use the playground to test small ideas.
- Don't mix tabs and spaces.
Common mistakes
- Mistake: Forgetting a colon after
if/for/def. Fix: Add:. - Mistake: Inconsistent indentation. Fix: Use 4 spaces everywhere.
- Mistake: Expecting
print()to return a value. Fix:print()returnsNone.
Key takeaways
- Python reads code top-to-bottom.
- Indentation creates blocks (be consistent: spaces, not tabs).
- Expressions produce values; statements do work.
- Names bind to objects — the object is what has the type.