Kalebu Jordan

Become a Pro Python Developer

Normally during building our Python function we may set to receive specific arguments in order to make it re usable on different input values.

For instance a function to find average of two numbers

>>> def average(a, b):
...     return (a+b)/2
>>> average(5, 9)
>>> average(10, 44)

But imagine if we don’t know specific number of arguments which we will pass to the function but still making it able to compute the average of those numbers

Thus where *args and **kwargs  comes into play since they allow you to pass multiple arguments or keyword arguments to a function.

 how do we use args ?

Well it’s pretty straight forward , when you use * on your argument , it will receive any number of arguments and store it into one iterable (usually a tuple).

Example of Usage (1):

For instance , Let’s make a general purpose function to calculate the average of any given number of arguments using *

>>> def average(*numbers):
...     return sum(numbers)/len(numbers)
>>> average(1, 5, 66)
>>> average(1, 5, 66, 78, 12)
>>> average(1, 5)

Example of Usage (2):

Now Let’s try making a simple function which receives any number of fruits as arguments and print their capitalized name to the screen using *args

>>> def PrintFruits(*fruits):
...     for fruit in fruits:
...             Capitalized_fruit = fruit.capitalize()
...             print(Capitalized_fruit)
>>> PrintFruits('orange', 'mango')
>>> PrintFruits('orange', 'mango', 'passion')

Thus how you can use *args to build a function to to receive any number of Non keyword arguments . now let’s go read *kwargs

How do we use kwargs ?

kwargs is just an addition of kw to args, while kw stands for keyword , therefore kwargs when want to receive any number of keyword arguments .

The data in kwargs variable are being stored in dictionary instead of tuple , where by key is the keyword of the argument. Let’s see the example below

Example of Usage (3) :

For instance lets build a function to receive any number keyword arguments of customer information.

>>> def CustomerDetails(**customerinfo):
...     print('Customer Information')
...     for key, value in customerinfo.items():
...             print('{} : {}'.format(key, value))
>>> CustomerDetails(name='John', age=26)
Customer Information
age : 26
name : John
>>> CustomerDetails(name='Nebula', age=26,status ='single', job ="Frelancer")
Customer Information
age : 26
job : Frelancer
status : single
name : Nebula

Things to Note :

  • args and kwargs are used to receive arguments of a variable length
  • args is used for none keyword arguments and its data are stored in tuple
  • kwargs is used for keyword argument and its data are stored in Dictionary

Hope you find this post interesting , don’t forget to subscribe to get more tutorials like this.

In case of any suggestion or comment , drop it on the comment box and I will reply to you immediately.

One thought on “Learn the basics of args and kwargs in python

Leave a Reply


Enjoy this blog? Please spread the word :)