Python uses indentation for blocks. Types are optional at runtime but type hints help teams and static checkers (mypy, pyright).
Core concepts
- Basic types:
str,int,float,bool,None. — W3Schools: Python data types - Collections:
list,tuple,dict,set— know mutability (tupleis immutable). — Python docs: Data structures - Comprehensions:
[x * 2 for x in nums if x > 0]— idiomatic transforms. — Python docs: List comprehensions - Control flow:
if/elif/else,for,while,match(3.10+) for structural pattern matching. — Python docs: Control flow - f-strings: Preferred formatting —
f"{user} logged in". — Python docs: f-strings
from typing import Iterable
def total(scores: Iterable[int]) -> int:
return sum(scores)
print(total([10, 20, 5]))Concept videos
YouTube lessons for these topics. On a large screen they open in the reading panel next to the roadmap, same as the article links on each core concept.
- Types & variables — Python Variables (Programming with Mosh)
- Collections — Python Modules & imports (Corey Schafer playlist)
Explanation
Python reads like pseudocode that actually runs. Indentation replaces braces — the whitespace is syntax, not decoration. That forces visual consistency but surprises newcomers from C-style languages.
Basic types
Dynamic typing means names bind to values of any type. type(x) at runtime tells you what you hold. None means “no value here” — like an empty slot in a form. Type hints (name: str) are optional labels for humans and tools.
Collections
Lists are mutable ordered sequences; tuples are frozen lists for fixed records. Dicts map keys to values like a phone book; sets store unique items for membership tests. Choosing the right collection is choosing the right organizer drawer for your data.
Comprehensions
Comprehensions are one-line loops that build lists, sets, or dicts. They replace verbose for + append patterns. Read them aloud: “double x for each x in nums if x positive.” If a comprehension gets unreadable, drop back to a regular loop — clarity wins.
Control flow
match (3.10+) is like a smart switch that destructures shapes — great for HTTP handlers and AST nodes. for iterates any iterable, not just indexes. Python prefers iteration protocols over manual counting when possible.
f-strings
f-strings evaluate expressions inside {...} at runtime. They are faster and clearer than % formatting or .format(). Use them for logs, errors, and user-visible messages — but never for HTML without escaping (templates handle that).
What Is It?
Python uses significant whitespace for blocks instead of braces.
Built-in collections include list, dict, set, and tuple — each with different mutability and use cases.
Control flow covers if/elif/else, for/while loops, comprehensions, and pattern matching (3.10+).
Why Should I Care?
Readable syntax is Python's superpower in teams — until inconsistent indentation breaks merges.
Type hints catch bugs before runtime in larger codebases.
Real World Analogy
Indentation is Python's curly braces — the whitespace is not decoration, it is the syntax that keeps everyone's code looking like one author wrote it.
Visual Mental Model
if / elif / else
indent block (4 spaces typical)
for x in iterable:Beginner Explanation
Blocks are defined by indentation. Types are dynamic at runtime; type hints are optional documentation checked by tools like mypy.
Smallest Possible Example
scores = [10, 20, 30]
total = sum(scores)
if total >= 50:
print("pass")Common Mistakes
❌ Mixing tabs and spaces.
TabError and silent mis-indentation in editors. Stick to spaces (PEP 8).
❌ Using mutable default arguments (def f(x=[]):).
Defaults are evaluated once. Shared mutable defaults leak state between calls.
❌ Comparing floats with == without tolerance.
Use math.isclose for numeric work.
Real Project Example
pandas and NumPy build on lists and dict-like structures — same semantics, bigger scale.
mypy runs in CI at Dropbox, Stripe, and many FastAPI shops.
Build This Mini Project
Rewrite a script that uses indexes into a list to use a dict or dataclass instead. Add type hints and run mypy or pyright once.
Interview Question
Are Python types enforced at runtime?
By default, no — types are hints for developers and static checkers. Runtime remains dynamic unless you use validation libraries (Pydantic) or explicit checks.
Key Takeaways
- Indentation is syntax, not style.
- Know list vs dict vs set tradeoffs.
- Avoid mutable default args.
- Add hints as code grows.
Resources
The 60-second story
A school notebook where the indent is the law. If a line sits under a heading, it belongs to that heading. Slide it left and it is a new thought. Python does not use braces as fences. The whitespace is the fence. That feels like pseudocode until you paste from a blog and the fence moves.
Names are luggage tags, not engraved boxes. count can point at 3 now and "3" later if you are careless. The value has the type. str, int, float, bool, and None are the everyday shapes. None is not zero and not an empty string. It is a blank line that means “no answer,” like a form field left empty on purpose.
Collections are drawers. A list is a drawer you can rearrange. A tuple is a drawer that was glued shut: a date, a coordinate, a pair that should not grow a third sock. A dict is a labeled drawer: name to phone number. A set is a bowl of unique marbles. Ask “is this marble in the bowl?” and you do not scan a line of duplicates.
A comprehension is reading the drawer aloud in one breath: “two times x for each x in nums if x is positive.” If the breath runs out, write a normal loop. Clever one-liners that need a legend are how teams start hating each other.
if, for, and while are the hallways. match in modern Python is a sorting table that looks at the shape of the package, not just one sticker. f-strings are a rubber stamp: f"{user} logged in" prints the name inside the sentence while you watch.
Map the notebook.
- Indentation is the fence.
- The luggage tag is the variable.
- The object tied to the tag is the value and its type.
- List, tuple, dict, set are drawers with different rules.
- A comprehension is one spoken breath.
matchis sorting by shape.- An f-string is a stamp, not a pile of plus signs.
user = "Ava"
print(f"{user} logged in")Type hints, user: str, are pencil notes in the margin for humans and for checkers like mypy. The notebook still runs if you lie in the margin. The hint is a promise to teammates, not a lock.
Python was designed so the code would read like the plan. Indentation-as-syntax forces the plan to be visible. f-strings won because % formatting and .format() made simple sentences look like tax forms.
Mental model: a notebook whose indents are fences, and drawers that remember whether you are allowed to rearrange them.
Tell a friend: lists bend, tuples do not, dicts label, sets refuse duplicates, and f-strings are how you speak. If the comprehension needs a comment to be read, it is a loop.
