Challenge - 5 Problems
Variable-length Arguments Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ Predict Output
intermediate2:00remaining
Output of function using *args with sum
What is the output of this Python code?
Python
def add_all(*args): return sum(args) result = add_all(1, 2, 3, 4) print(result)
Attempts:
2 left
๐ก Hint
Remember that *args collects all arguments into a tuple and sum adds them up.
โ Incorrect
The function add_all takes any number of arguments and sums them. So 1+2+3+4 equals 10.
โ Predict Output
intermediate2:00remaining
Output when *args is empty
What will this code print?
Python
def greet(*args): if args: print('Hello, ' + args[0]) else: print('Hello, stranger') greet()
Attempts:
2 left
๐ก Hint
Check what happens when no arguments are passed and how the if condition works.
โ Incorrect
Since no arguments are passed, args is empty, so the else branch runs printing 'Hello, stranger'.
โ Predict Output
advanced2:00remaining
Output of function with *args and unpacking
What is the output of this code?
Python
def multiply(*args): result = 1 for num in args: result *= num return result numbers = [2, 3, 4] print(multiply(*numbers))
Attempts:
2 left
๐ก Hint
The * operator unpacks the list into separate arguments.
โ Incorrect
The list [2,3,4] is unpacked into arguments 2,3,4. Multiplying them gives 2*3*4=24.
โ Predict Output
advanced2:00remaining
Output when mixing *args with normal parameters
What does this code print?
Python
def describe(name, *args): print(f'Name: {name}') print(f'Other info: {args}') describe('Alice', 25, 'Engineer')
Attempts:
2 left
๐ก Hint
Check how *args collects extra arguments as a tuple.
โ Incorrect
The first argument goes to name, the rest are collected in args as a tuple (25, 'Engineer').
๐ง Conceptual
expert2:00remaining
Why use *args in function definitions?
Which of these best explains why *args is used in Python functions?
Attempts:
2 left
๐ก Hint
Think about how *args collects arguments.
โ Incorrect
*args collects all extra positional arguments into a tuple, allowing flexible argument counts.