Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Passing Multiple Arguments to a Function

Download

Sample Code (.zip)

5.8 KB

Download

Course Slides (PDF)

261.9 KB

00:00 Hi, I’m Rich Bibby with realpython.com. Sometimes when you look at a function definition in Python you might see that it takes two strange arguments, *args and **kwargs.

00:10 If you’ve ever wondered what these peculiar variables are or why your IDE defines them in main(), then this tutorial is for you. You’ll learn how to use args and kwargs in Python to add more flexibility to your functions. In this tutorial, you’re going to learn what *args and **kwargs actually are and how they allow you to pass multiple arguments or keyword arguments to a function.

00:33 Then you’ll learn how to use the *args variable in function definitions, and then also how to use the **kwargs variable in function definitions.

00:42 Then, once you’ve learned to write functions that use args and kwargs, you’ll then learn about why the ordering of arguments becomes important.

00:50 Next, you’ll learn how to use single asterisks (*) to unpack iterables and also how to use double asterisks (**) to unpack dictionaries. And finally, we’ll wrap things up in the conclusion.

01:00 This tutorial assumes that you already know how to define Python functions and work with lists and dictionaries. If you don’t or if you need a refresher, then check out some of the other great tutorials here on realpython.com that cover these topics. So, let’s get started.

01:16 *args and **kwargs allow you to pass multiple arguments, in the case of *args, or keyword arguments, in the case of **kwargs, to a function. Consider this example.

01:25 This is a simple function that takes two arguments, a and b, and returns their sum. This function works fine, but it’s limited to only two arguments. What if you need to sum a varying number of arguments, where the specific number of arguments passed is only determined at runtime?

01:43 Wouldn’t it be great to create a function that could sum all the integers passed to it, no matter how many there are? Of course it would. So keep watching, and I’ll show you how you can do just that.

Become a Member to join the conversation.