What is the output of the following code when greet('Anna') is called?
def greet(name): message = f"Hello, {name}!" return message print(greet('Anna'))
Look at how the function uses the input name inside the message.
The function greet takes the input name and inserts it into the message string using an f-string. So calling greet('Anna') returns "Hello, Anna!" which is printed.
Which statement best describes the difference between a function parameter and an argument?
Think about the function definition versus the function call.
Parameters are placeholders in the function definition that receive values. Arguments are the actual values given to the function when it is called.
Which function definition correctly calculates the square of a number and returns it?
Option A: def square(x): return x * x Option B: def square(x): print(x * x) Option C: def square(x): return x + x Option D: def square(x): return x ** 3
Remember that the square of a number is the number multiplied by itself.
Option C returns the product of x times x, which is the square. Option C prints but does not return, so it cannot be used to get the value. Option C adds x to x, which is double, not square. Option C cubes the number.
What error will occur when running this code?
def add_numbers(a, b): return a + b result = add_numbers(5) print(result)
Check how many arguments the function expects versus how many are given.
The function add_numbers requires two arguments, but only one (5) is provided. This causes a TypeError for missing an argument.
What is the output of this code?
def multiply(x, y): return x * y def add_and_multiply(a, b, c): return multiply(a + b, c) result = add_and_multiply(2, 3, 4) print(result)
First add a and b, then multiply the sum by c.
The function add_and_multiply adds 2 + 3 = 5, then calls multiply(5, 4) which returns 20.