Python Execution Atlas
Purpose
This atlas explains how Python actually executes common patterns:
loops, list mutation, rebinding, function calls, default arguments,
and nested loops. Each section shows the execution timeline.
PAGE 1 — append() timeline
logs = []
for s in ["error","warning"]:
[Link](s)
Start
logs = []
Iteration 1
s = "error"
append("error")
logs -> ["error"]
Iteration 2
s = "warning"
append("warning")
logs -> ["error","warning"]
PAGE 2 — logs = logs + [s]
logs = []
for s in ["error","warning"]:
logs = logs + [s]
Start
logs = []
Iteration 1
[] + ["error"] -> ["error"]
Iteration 2
["error"] + ["warning"] -> ["error","warning"]
PAGE 3 — append() vs +
append():
same object
[]
↓
['error']
↓
['error','warning']
logs = logs + [s]
[] id1
↓
['error'] id2
↓
['error','warning'] id3
PAGE 4 — Function reference
def f(lst):
[Link](5)
a=[1]
f(a)
a and lst reference same list
[1] -> [1,5]
PAGE 5 — Rebinding
def f(lst):
lst = lst + [5]
return lst
a=[1]
b=f(a)
a -> [1]
b -> [1,5]
PAGE 6 — Default argument mutation
def add(x, bag=[]):
[Link](x)
return bag
Call1 -> [1]
Call2 -> [1,2]
Call3 -> [1,2,3]
PAGE 7 — Safe pattern
def add(x, bag=None):
if bag is None:
bag=[]
[Link](x)
return bag
PAGE 8 — Nested loops
for i in range(2):
for j in range(3):
print(i,j)
(0,0)
(0,1)
(0,2)
(1,0)
(1,1)
(1,2)
PAGE 9 — Nested append
nums=[1,2]
result=[]
for x in nums:
for y in nums:
[Link](x+y)
result=[2,3,3,4]
PAGE 10 — Mental model
iteration
→ operation
→ new state