Search the courses, subjects, tracks, games and features, or jump straight to a page.
Learn it your way
A short class with Sarah and her students. The other ways are under More.
Sign up free to listen. Transcripts are free to read.
Today, we’re exploring variable-length arguments, starting with *args. Can anyone tell me what they think *args means?
Does it mean we can pass different numbers of arguments to a function?
Exactly! When we define a function with *args, it collects all extra positional arguments into a tuple. For example, look at this function: def total_marks(*marks): return sum(marks). How do you think it works?
It sums up any number of marks we provide, right? So can I pass in five or ten marks?
Exactly! You can pass any number of arguments, and the sum() function will handle it. Remember, we can think of *args as "A Variable Number of Arguments". Now, who can give me a summary of how to use it?
We define *args in the function definition, and then we can call the function with multiple values that will be summed up!
Good summary! And remember, your input is collected as a tuple.
Now let's shift gears to explore keyword arguments with **kwargs. Who can explain what happens when we use **kwargs?
Does it mean we can pass keyword arguments to the function, and it will collect them into a dictionary?
Absolutely! Look at this example: def display_info(**kwargs): for key, value in kwargs.items(): print(f"{key} : {value}"). What does it do?
It prints out each key-value pair that was passed to it, like name and year.
Correct! It allows us to easily pass attributes without needing to define every possible parameter. Remember: **kwargs stands for 'Keyword Arguments'. Now, can anyone summarize how we would use it?
We define the function with **kwargs, call it with named arguments, and it gathers them into a dictionary format.
Precisely! This gives us great flexibility in our functions.
Now that we’ve seen *args and **kwargs, let's talk about why we use these features. Why do you think they might be useful in real-life programming?
It makes functions flexible, allowing them to handle different scenarios!
Exactly! They let you define functions that can catch various input formats which aids in writing cleaner and more modular code. What’s another advantage?
We don't have to change the function definition every time we need to add more arguments.
Yes! This supports code maintenance and evolution. Let’s have a brief quiz: Can anyone tell me a key point about using *args?
It can handle any number of positional inputs!
Correct! And what about **kwargs?
It collects keyword arguments as a dictionary!
Excellent! Great discussion today, everyone!
With *args you can share, numbers without care; just pass as you please, as many as you dare!
Imagine you are at a feast, collecting many dishes. *args allows you to take as many dishes as you like. Meanwhile, **kwargs gives you a menu to select from by name.
Remember: * for *args means 'all about positions'; ** for **kwargs means 'keys with the definition'.
Acronym for remembering: 'Flexible Functions', where F is for *args (Flexible Positional) and K is for **kwargs (Keyword Kind).
Term
What does *args do?
Click to flip
Term
What does **kwargs do?
Click to flip
Variable-length arguments allow functions in Python to accept any number of positional or keyword arguments, providing flexibility in function design.
In this section, we explore variable-length arguments in Python, specifically arbitrary positional arguments using *args and arbitrary keyword arguments using **kwargs. These feature enable more dynamic function interfaces, allowing greater versatility in argument handling.
In Python, functions can often be created to handle more flexible argument passing methods through the use of variable-length arguments. This allows functions to accept a variable number of positional and keyword arguments. We cover two main mechanisms here:
*args)Using *args, a function can accept any number of positional arguments. All arguments passed are captured in a tuple, which can be manipulated within the function. For example:
def total_marks(*marks):
return sum(marks)
# Example Usage
total = total_marks(90, 85, 75) # total will hold the value 250This approach is particularly useful when the number of inputs isn't predetermined.
**kwargs)With **kwargs, a function can receive any number of keyword arguments, which are captured in a dictionary, allowing named parameters to be passed. For instance:
def display_info(**kwargs):
for key, value in kwargs.items():
print(f"{key} : {value}")
# Example Usage
display_info(name="AI", year=2025)This feature enhances the function's flexibility and can simplify handling of optional parameters. Overall, understanding and utilizing *args and **kwargs promotes cleaner and more manageable code.