Python 9.5 : Variable number of parameters
We already know that Python functions can take a fixed number of parameters, or none at all. A simple function such as def ab(): print("Hello") takes no argument; another one such as print(name, height, age, married) mixes a string, a float, an integer and a boolean. But what if we want to write a function that sums up the marks of a student, without knowing in advance how many subjects they have?
Accepting any number of arguments
Hard-coding parameters like a, b, c, d, e, f quickly becomes impractical and crashes as soon as the caller passes a different number of values. The clean solution in Python is to declare a special parameter prefixed with a star, conventionally written *args (here we use *numbers). Inside the function, numbers becomes a tuple containing every value passed at the call site, and we iterate over it with a normal for loop to compute the sum.
def sum_marks(title, *numbers):
print(title)
note = 0
for i in numbers:
note += i
return note
print(sum_marks("Sum of the notes:", 12, 13, 15, 8, 11))
Calling the function with two, three or ten numbers now works without changing its signature. If no numbers are passed at all, the loop simply does not execute and the function returns the initial 0. We can also add a regular positional parameter such as title before *numbers to print a header.
There is also a double-star variant **kwargs that works like a dictionary of keyword arguments, for example physics=12, math=16, history=15, french=11. To sum the values we have to iterate over kwargs.values(), otherwise Python complains because the iteration variable would be the key, not a number. In our case the simple single-star version is enough, so we stick with it. In the next section we will move on to collections.
Summary
This lesson covers variable number of parameters in Python functions, specifically the use of *args (for multiple positional arguments) and **kwargs (for keyword arguments). The instructor demonstrates how to create flexible functions that can accept an arbitrary number of arguments, using practical examples like summing student grades without knowing in advance how many subjects or parameters will be passed.
Key points
- Functions in Python can be designed to accept a variable number of arguments using *args syntax
- The *args parameter collects multiple positional arguments into a tuple, allowing them to be iterated over
- **kwargs (double asterisks) allows passing keyword arguments as a dictionary with named keys
- Variable-length parameters enable more flexible and reusable functions without fixed argument counts
- Practical use cases include functions that sum values, calculate grades, or handle dynamic numbers of inputs
FAQ
What is the difference between *args and **kwargs in Python?
*args collects positional arguments into a tuple accessed by index, while **kwargs collects keyword arguments into a dictionary where values are accessed by their key names.
Can I use *args to sum any number of values?
Yes, you can define a function with *args to accept any number of numeric arguments and sum them using a loop, making the function flexible regardless of how many inputs are provided.
When should I use *args instead of passing a list or collection?
Use *args when you want a cleaner function signature and when callers will pass individual arguments naturally rather than pre-packaging them into a collection first.