0% found this document useful (0 votes)
9 views11 pages

Python Execution Atlas

The Python Execution Atlas details how Python executes various common programming patterns, including loops, list mutation, function calls, and default arguments. It provides execution timelines for operations such as appending to lists and demonstrates the differences between using append() and the + operator. The document also covers concepts like rebinding, default argument mutation, and nested loops, offering a mental model for understanding iteration and state changes.

Uploaded by

ksanket153
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views11 pages

Python Execution Atlas

The Python Execution Atlas details how Python executes various common programming patterns, including loops, list mutation, function calls, and default arguments. It provides execution timelines for operations such as appending to lists and demonstrates the differences between using append() and the + operator. The document also covers concepts like rebinding, default argument mutation, and nested loops, offering a mental model for understanding iteration and state changes.

Uploaded by

ksanket153
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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

You might also like