Python: **Kwargs and *Args

Outside of beings incredibly fun to say, Kwargs and Args can be pretty useful.

Here is how they work. Let us say you wanted to make a function that adds 4 numbers:

Notice what happens when we only provide 3 arguments, we error out. The same thing happens if we try to give the function 5 arguments.


Well, if the world perfectly predictable, this wouldn’t be an issue. But in the real world, making a function that adds 4 and only 4 numbers isn’t very useful. A function that can add any given set of numbers however is very useful.


*args to the rescue

Using the keyword *args inside the function parenthesis gives you the flexibility of having as many, or as few arguments as you want.


Try your code in our online Python console:  


Kwargs are basically args with keywords. Think dictionaries.


You can combine regular arguments with *args and **kwargs

Try your code in our online Python console:  

If you enjoyed this lesson, click LIKE below, or even better, leave me a COMMENT. 

Last lesson: Regular expressions

Back to Python Course: Course


One thought on “Python: **Kwargs and *Args

  1. Pingback: Python: Regular Expressions – Analytics4All

Leave a Reply