Question 1

Are these primitive expressions or call expressions?

• 3
• add(2, 3)
• print(5)
• 3 + 4

Toggle Solution

• 3 is a number (primitive expression)
• add(2, 3) is calling the function add (call expression)
• print(5) is calling the builtin function print (call expression)
• 3 + 4 is calling the builtin function __add__ (call expression)

The last one’s tricky! Try typing these two into a Python interpreter to convince yourself:

>>> 3 + 4
7
7


Question 2

When do we make a new frame in an environment diagram?

Toggle Solution

We make a new frame whenever we call a user-defined function. This means we don’t create frames for builtin function calls like abs(x) and 3 + 4. We also don’t create frames for imported functions!

Question 3

Imagine we run the following lines through a Python interpreter. Draw the environment diagram after executing the program.

def swap(x, y):
x, y = y, x
return print('Swapped!', x, y)

x, y = 60, 1
a = swap(x, y)
swap(a, y)


Toggle Solution