What is the output of this Python code?
def greet(name, greeting="Hello"): return f"{greeting}, {name}!" print(greet("Alice")) print(greet("Bob", greeting="Hi"))
Check how default values work for keyword arguments.
The function greet has a default value for the greeting argument. When calling greet("Alice"), it uses the default "Hello". When calling greet("Bob", greeting="Hi"), it uses the provided keyword argument "Hi".
What will this code print?
def show_args(*args, **kwargs): print(f"Positional: {args}") print(f"Keyword: {kwargs}") show_args(1, 2, a=3, b=4)
Remember that *args collects positional arguments as a tuple, and **kwargs collects keyword arguments as a dictionary.
The *args parameter collects all positional arguments into a tuple. The **kwargs parameter collects all keyword arguments into a dictionary.
What is the output of this code?
def add_item(item, items=[]): items.append(item) return items print(add_item('apple')) print(add_item('banana'))
Think about how default mutable arguments behave in Python functions.
The default list is created once when the function is defined. Each call without an explicit items argument uses the same list, so items accumulate.
What will this code print?
def multiply(x, y, z): return x * y * z values = (2, 3, 4) print(multiply(*values))
Look at how the tuple is unpacked into function arguments.
The * operator unpacks the tuple values into separate arguments for the function multiply. So multiply(2, 3, 4) returns 24.
What is the output of this code?
def func(a, b, /, c, d, *, e, f): return a + b + c + d + e + f print(func(1, 2, 3, d=4, e=5, f=6))
Understand how positional-only (/) and keyword-only (*) arguments work in Python 3.8+.
The parameters a and b are positional-only, so they must be passed positionally. c and d can be positional or keyword. e and f are keyword-only and must be passed by name. The call matches these rules, so the sum is 1+2+3+4+5+6=21.