Jed Rembold
October 1, 2025
What would be the printed value of z at
the end of the code to the right?
def f(x,y=0):
z = (x + 3) ** 2
return y + z
w = 1
z = w + f(y=w,x=2)
print(z)
def Vegas(x):
y = 2
for i in range(5):
x += y
return x
x = 3
z = Vegas(x)
print('z =', z)
print('x =', x)
Consider the code to the left. When the final value of
x is printed, what will its value be?
NoneVegas stays in
Vegas…We’ll annotate the stack frames by hand as the earlier code runs:
def Vegas(x):
y = 2
for i in range(5):
x += y
return x
x = 3
z = Vegas(x)
print('z =', z)
print('x =', x)
return, compute the return value and
substitute that value in place of the function callabs,
str, print,
etc.def func1(x,y):
return z + func2(x,y)
def func2(x,y):
def func3(x):
return (y + x) ** 2
z = x - func3(y)
return z - y
z = 1
print(func1(2,z))
In Python, assigning any value to a variable means that the variable is assumed to be local
Can lead to issues though:
def increment():
x = x + 1
x = 0
increment()There are a few ways to address this, but we’ll focus on one in particular when it comes to PGL
(). Including the parentheses is how you
call the function!import math
def evaluate_numbers(func):
print(func)
print(func(0))
print(func(2))
print(func(10))
evaluate_numbers(math.sqrt)
evaluate_numbers(math.exp)
Consider the code to the right.
f2 must also keep track
of any local variables!b = 1
def f1(a):
print(a)
print(b)
def f2():
c = a + b
return c * 3
return f2
f2 = f1(10)
c = f2()