def yell(text="hello"):
print(text.upper() + "!")Review: Functions
Module 1: Python Fundamentals
Review of Session 1.4: Functions
Without running the code, what is going to be the output of this cell?
::: {.callout-tip collapse=“true”} ## Solution Some variants:
:::
# Same as above, with type hints
def yell(text: str="hello"):
print(text.upper() + "!")# What if we use return?
def yell(text="hello"):
return text.upper() + "!"# When using return, the function "yell()" outputs a string
# We can concatenate that string to another
print(yell() + " CLASS!")Without running the code, what is going to be the output of this cell?
def powers_of_number(n, limit=4):
for x in range(1, limit):
print(f"{n}^{x} = {n**x}")
powers_of_number(2)# Same as above, with type hints
def powers_of_number(n: int, limit: int=4):
for x in range(1, limit):
print(f"{n}^{x} = {n**x}")
powers_of_number(2)Look at the following piece of code and explain why it fails.
# Ask the user for a password until it gets it right
password = "coconout"
while user_input != password:
user_input = input("Enter a password: ")
print("Access granted!")::: {.callout-tip collapse=“true”} ## Solution Answer: We need to define the variable user_input before performing the comparison user_input != password.
Fixed:
:::
# Ask the user for a password until it gets it right
password = "coconout"
user_input = input("Enter a password: ")
while user_input != password:
user_input = input("Enter a password: ")
print("Access granted!")Look at the following piece of code and explain why it fails.
# This code computes the sum of the first N natural numbers
n = input("Enter a positive integer: ")
print(f"The sum of the first {n} natural numbers is...")
while n > 0:
result += n # result = result + n
n -= 1 # n = n - 1
print(result)Answer: Same as before, we need to initialize the variable result before we start the while loop.
Look at the following piece of code and explain why it fails.
# Exercise 4.6
# Define a function rectangle_area(length, width) that calculates the area of
# a rectangle based on its length and width.
def rectangle_area(length, width):
return len * width
print(rectangle_area(10, 20))# Exercise 4.6
# Define a function rectangle_area(length, width) that calculates the area of
# a rectangle based on its length and width.
def rectangle_area(length, width):
return length * width
length = 20
width = 40
rectangle_area = rectangle_area(length, width)
print(rectangle_area)
length_2 = 10
width_2 = 20
rectangle_area_2 = rectangle_area(length_2, width_2)
print(rectangle_area_2)print = "Hello world"
print(print)print(8)::: {.callout-tip collapse=“true”} ## Solution Answer: The name of the variable we are using is length, not len! In fact, len is reserved to be the name of a Python built-in function.
:::
# Same as above, with type hints
def rectangle_area(length: float, width: float) -> float:
return len * width
print(rectangle_area(10, 20))Look at the following piece of code and explain why it fails.
# Exercise 4.19
# Define a function circle_area(radius, is_diameter=False) that calculates
# the area of a circle based on its radius.
# If the keyword argument is_diameter is input as True,
# then the function considers that radius is instead the diameter
def circle_area(radius, is_diameter = False):
# Compute for radius
return 3.1415 * radius**2
# Correction for diameter
if is_diameter:
radius = radius / 2
return 3.1415 * radius**2
print(circle_area(10)) # Expected output: 314.15
print(circle_area(10, is_diameter=True)) # Expected output: 78.54# Exercise 4.19
# Define a function circle_area(radius, is_diameter=False) that calculates
# the area of a circle based on its radius.
# If the keyword argument is_diameter is input as True,
# then the function considers that radius is instead the diameter
def circle_area(radius, is_diameter = False):
# Correction for diameter
if is_diameter:
radius = radius / 2
return 3.1415 * radius**2
else:
# Compute for radius
return 3.1415 * radius**2
print(circle_area(10)) # Expected output: 314.15
print(circle_area(10, is_diameter=True)) # Expected output: 78.54::: {.callout-tip collapse=“true”} ## Solution Answer: The return statement will finish the function right at the start. The if clause will never happen because of that return. We need to include an if clause when is_diameter is False.
:::
# Same as above, with type hints
def circle_area(radius: float, is_diameter: bool = False) -> float:
# Compute for radius
return 3.1415 * radius**2
# Correction for diameter
if is_diameter:
radius = radius / 2
return 3.1415 * radius**2
print(circle_area(10)) # Expected output: 314.15
print(circle_area(10, is_diameter=True)) # Expected output: 78.54