top of page
learn_data_science.jpg

Data Scientist Program

 

Free Online Data Science Training for Complete Beginners.
 


No prior coding knowledge required!

Python Concepts for Data Science: *args and *kwargs

Writer's picture: ben othmen rabebben othmen rabeb


Introduction to *args and **kwargs in Python

In Python, we can pass a variable number of arguments to a function using special symbols. There are two special symbols:

  1. *args (Non Keyword Arguments)

  2. **kwargs (Keyword Arguments)

In this post i will explain the python concept *args and **kwargs with the calculation of area of rectangle.

We use *args and **kwargs as an argument when we are unsure about the number of arguments to pass in the functions.

Python *args

Python *args allow us to pass the variable number of non keyword arguments to function.


In the function, we should use an asterisk * before the parameter name to pass variable length arguments.The arguments are passed as a tuple and these passed arguments make tuple inside the function with same name as the parameter excluding asterisk *.

Python **kwargs

Python passes variable length non keyword argument to function using *args but we cannot use this to pass keyword argument. For this problem Python has got a solution called **kwargs, it allows us to pass the variable length of keyword arguments to the function.


In the function, we use the double asterisk ** before the parameter name to denote this type of argument. The arguments are passed as a dictionary and these arguments make a dictionary inside function with name same as the parameter excluding double asterisk.



Let's explain *arg and **kwargs with this example below


def area_rectangle(*args):  
    if len(args) == 2:
        return args[0]*args[1]
    else:
        print('Please state two parameters')

#Python *kwargs

def area_rectangle2(**kwargs):  
    if len(kwargs) == 2:
        result = 1
        for key, value in kwargs.items():
            result *=value
        return result
    else:
        print('Please state two parameters')

if __name__ == '__main__':
    print (area_rectangle(3,8))
    print (area_rectangle2(cote1=4, cote2=8))

Here is the result of this code




Thank you for reading!


You can find the complete source code here Github



0 comments

Recent Posts

See All

Comentarios


COURSES, PROGRAMS & CERTIFICATIONS

 

Advanced Business Analytics Specialization

Applied Data Science with Python (University of Michigan)

Data Analyst Professional Certificate (IBM)

Data Science Professional Certificate (IBM)

Data Science Specialization (John Hopkins University)

Data Science with Python Certification Training 

Data Scientist Career Path

Data Scientist Nano Degree Program

Data Scientist Program

Deep Learning Specialization

Machine Learning Course (Andrew Ng @ Stanford)

Machine Learning, Data Science and Deep Learning

Machine Learning Specialization (University of Washington)

Master Python for Data Science

Mathematics for Machine Learning (Imperial College London)

Programming with Python

Python for Everybody Specialization (University of Michigan)

Python Machine Learning Certification Training

Reinforcement Learning Specialization (University of Alberta)

Join our mailing list

Data Insight participates in affiliate programs and may sometimes get a commission through purchases made through our links without any additional cost to our visitors.

bottom of page