Exercises

Exercises#

These exercises are taken from old exams. Check your understanding by working them individually before peeking forward to answers or discussing them with classmates.

  1. What does this program print?

def f(x: int) -> int:
    x = x + 1
    return x

x = 5
y = 7
z = f(y)
print(f"x={x}, y={y}, z={z}")
  1. What will go wrong with this program? How should you fix it?

def sign(n: int):
    """Return -1, 0, or 1 depending on n being negative, zero, or 
    positive
    """
    if n > 0: 
        result = 1
    elif n < 0:
        result = -1
    else: 
        result = 0

sign(-12)
print(result)
  1. What is wrong with this function?

def double(x: int) -> int: 
    """Returns double the original value of x."""
    z = x + x
    print(z)

Answers to exercises#

  1. What does this program print?

def f(x: int) -> int:
    x = x + 1
    return x

x = 5
y = 7
z = f(y)
print(f"x={x}, y={y}, z={z}")

Answer: x=5, y=7, z=8

This is a question about variable scope and argument passing. The x in an activation of function f is distinct from the globabl x. The actual argument y is copied to the formal argument x when f is called, so incrementing x in f affects neither the global nx nor the global y.

Don’t be confused by f-strings like f"x={x}, y={y}, z={z}". This f is not the same as the function f.
f-strings are used in Python to format output by interpolating values into template strings. For example, within the f-string,   {x} is replaced by the value of variable x.

  1. What will go wrong with this program? How should you fix it?

def sign(n: int):
    """Return -1, 0, or 1 depending on n being negative, zero, or 
    positive
    """
    if n > 0: 
        result = 1
    elif n < 0:
        result = -1
    else: 
        result = 0

sign(-12)
print(result)

Function sign is not returning the result variable. The result variable exists only while the function is executing. When we try to print result from outside the sign function, we will encounter an error:

    print(result)
          ^^^^^^
NameError: name 'result' is not defined

We can fix it this way:

def sign(n: int) -> int:
    """Return -1, 0, or 1 depending on n being negative, zero, or 
    positive
    """
    if n > 0: 
        result = 1
    elif n < 0:
        result = -1
    else: 
        result = 0
    return result

s = sign(-12)
print(s)
-1

Since the function stops running as soon as it returns a value, we could also shorten it up like this:

def sign(n: int) -> int:
    """Return -1, 0, or 1 depending on n being negative, zero, or 
    positive
    """
    if n > 0: 
        return 1
    elif n < 0:
        return -1
    return 0

s = sign(-12)
print(s)
-1
  1. What is wrong with this function?

def double(x: int) -> int: 
    """Returns double the original value of x."""
    z = x + x
    print(z)

The signature of this function (given in its header, def double(x: int) -> int: and its docstring is a kind of contract with code that uses it. The header says it has a result, which is an integer, and the docstring says that result will be twice the original value of x. That’s a lie. It does not have a result. It has an effect, which is printing double the value x. It should return the value instead, like this:

def double(x: int) -> int: 
    """Returns double the original value of x."""
    z = x + x
    return z