Bird
Raised Fist0
Pythonprogramming~20 mins

Variable-length arguments (*args) in Python - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
๐ŸŽ–๏ธ
Variable-length Arguments Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2: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)
A10
B1234
CTypeError
DNone
Attempts:
2 left
๐Ÿ’ก Hint
Remember that *args collects all arguments into a tuple and sum adds them up.
โ“ Predict Output
intermediate
2: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()
ATypeError
BHello,
CIndexError
DHello, stranger
Attempts:
2 left
๐Ÿ’ก Hint
Check what happens when no arguments are passed and how the if condition works.
โ“ Predict Output
advanced
2: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))
A24
B[2, 3, 4]
C9
DTypeError
Attempts:
2 left
๐Ÿ’ก Hint
The * operator unpacks the list into separate arguments.
โ“ Predict Output
advanced
2: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')
A
Name: Alice
Other info: 25, Engineer
B
Name: Alice
Other info: (25, 'Engineer')
C
Name: Alice
Other info: [25, 'Engineer']
DTypeError
Attempts:
2 left
๐Ÿ’ก Hint
Check how *args collects extra arguments as a tuple.
๐Ÿง  Conceptual
expert
2:00remaining
Why use *args in function definitions?
Which of these best explains why *args is used in Python functions?
ATo make the function return multiple values.
BTo force the function to accept exactly one argument.
CTo allow the function to accept any number of positional arguments as a tuple.
DTo collect keyword arguments into a dictionary.
Attempts:
2 left
๐Ÿ’ก Hint
Think about how *args collects arguments.

Practice

(1/5)
1. What does *args do in a Python function?
easy
A. Allows the function to accept any number of extra positional arguments
B. Allows the function to accept only keyword arguments
C. Restricts the function to accept exactly one argument
D. Makes the function return multiple values

Solution

  1. Step 1: Understand the role of *args

    *args collects extra positional arguments passed to a function into a tuple.
  2. Step 2: Compare options with this behavior

    Only Allows the function to accept any number of extra positional arguments correctly describes this behavior; others describe different concepts.
  3. Final Answer:

    Allows the function to accept any number of extra positional arguments -> Option A
  4. Quick Check:

    *args = flexible positional inputs [OK]
Hint: Remember *args packs extra positions into a tuple [OK]
Common Mistakes:
  • Confusing *args with **kwargs
  • Thinking *args limits arguments
  • Assuming *args returns multiple values
2. Which of the following is the correct syntax to define a function that accepts variable-length positional arguments?
easy
A. def func(args*):
B. def func(*args):
C. def func(**args):
D. def func(args**):

Solution

  1. Step 1: Recall the syntax for variable-length positional arguments

    The correct syntax uses an asterisk before the parameter name: *args.
  2. Step 2: Match options with correct syntax

    def func(*args): matches the correct syntax; others are invalid or for different purposes.
  3. Final Answer:

    def func(*args): -> Option B
  4. Quick Check:

    Star before name = variable positional args [OK]
Hint: Use * before parameter name for variable positional args [OK]
Common Mistakes:
  • Using **args instead of *args
  • Placing * after the parameter name
  • Confusing syntax with keyword arguments
3. What is the output of the following code?
def add_numbers(*args):
    return sum(args)

print(add_numbers(1, 2, 3, 4))
medium
A. 10
B. 1234
C. TypeError
D. None

Solution

  1. Step 1: Understand how *args works in the function

    The function collects all arguments into a tuple and sums them using sum().
  2. Step 2: Calculate the sum of the arguments

    1 + 2 + 3 + 4 = 10, so the function returns 10.
  3. Final Answer:

    10 -> Option A
  4. Quick Check:

    Sum of (1,2,3,4) = 10 [OK]
Hint: Sum all *args values to get total [OK]
Common Mistakes:
  • Concatenating numbers as strings
  • Expecting a TypeError for multiple args
  • Forgetting that *args is a tuple
4. Identify the error in this function definition:
def greet(*names):
    for name in names
        print(f"Hello, {name}!")
medium
A. print statement syntax error
B. Incorrect use of *args syntax
C. Missing colon after for loop
D. Function cannot have *args

Solution

  1. Step 1: Check the for loop syntax

    The for loop line is missing a colon at the end, which is required in Python.
  2. Step 2: Verify other parts of the function

    The *names syntax and print statement are correct; the function can have *args.
  3. Final Answer:

    Missing colon after for loop -> Option C
  4. Quick Check:

    For loops need a colon [:] [OK]
Hint: Check colons after for, if, while statements [OK]
Common Mistakes:
  • Forgetting colon after for loop
  • Confusing *args with **kwargs
  • Incorrect indentation
5. How can you write a function that accepts any number of positional arguments and returns a list of their squares?
hard
A. def squares(args): return [x**2 for x in args]
B. def squares(*args): return x**2 for x in args
C. def squares(**args): return [x**2 for x in args]
D. def squares(*args): return [x**2 for x in args]

Solution

  1. Step 1: Use *args to accept variable positional arguments

    The function parameter must be *args to accept any number of positional arguments.
  2. Step 2: Return a list of squares using list comprehension

    Use [x**2 for x in args] to square each argument and collect results in a list.
  3. Final Answer:

    def squares(*args): return [x**2 for x in args] -> Option D
  4. Quick Check:

    Use *args and list comprehension for squares [OK]
Hint: Use *args and list comprehension for flexible squares [OK]
Common Mistakes:
  • Using **args instead of *args
  • Forgetting to return a list
  • Incorrect comprehension syntax