Args and Kwargs

Welcome to part 25 of the intermediate Python programming tutorial series. In this tutorial, we're going to cover *args and **kwargs.

The idea behind *args and **kwargs is that there may be times when you have a function and you want to be able to handle an unknown number of arguments. The *args will handle for any number of parameters, and **kwargs will handle for any number of keyword arguments (hence kwargs). Let's see some examples. Let's say you've got a blog with some posts saved to variables. Something like:

Now, there will be many times where you want args that are still assigned to some sort of name, which is where keyword arguments, **kwargs come in! Where *args are like a list, **kwargs are like a dictionary.

Interestingly, you can also use *args when passing arguments to a function, even when that function wasn't built to accept an unlimited number of arguments. For example, let's say you've got a function that graphs, but for now we can just print: