close

optional arguments in python

Hello guys. How are you all? I hope you all fine. In this tutorial we will learn about optional arguments in python. so without wasting time lets learn about of this.

optional arguments in python

  1. optional arguments in python

    To make optional arguments in python just Use *args.By using *args you can make optional arguments in python. So without wasting time lets learn about of this by given below example: def myfunction(first_name, last_name, *args): print(first_name) print(last_name) for argument in args: print(argument) myfunction('aarti','patel',8,30,45) Output : aarti patel 8 30 45

  2. optional arguments in python

    To make optional arguments in python just Use **kargs.By using **kargs you can make optional arguments in python. So without wasting time lets learn about of this by given below example: def myFunction(**kwargs): for key, value in kwargs.items(): print ("%s = %s" %(key, value)) myFunction(std='12', name='arti patel', subject='physics', marks='83') Output : std = 12 name = arti patel subject = physics marks = 83

  3. python optional arguments

    To make optional arguments in python just Use *args.By using *args you can make optional arguments in python. So without wasting time lets learn about of this by given below example: def myfunction(first_name, last_name, *args): print(first_name) print(last_name) for argument in args: print(argument) myfunction('aarti','patel',8,30,45) Output : aarti patel 8 30 45

Method 1: Use *args

By using *args you can make arguments optional. So without wasting time lets learn about of this by given below example:

def myfunction(first_name, last_name, *args):
    print(first_name)
    print(last_name)
    for argument in args:
        print(argument)
myfunction('aarti','patel',8,30,45)

Output :

aarti
patel
8
30
45

Method 2: Use **kargs

By using **kargs you can make optional arguments. So without wasting time lets learn about of this by given below example:

def myFunction(**kwargs):
    for key, value in kwargs.items(): 
        print ("%s = %s" %(key, value)) 
myFunction(std='12', name='arti patel', subject='physics', marks='83')

Output :

std = 12
name = arti patel
subject = physics
marks = 83

Conclusion

It’s all About this Tutorial. Hope all methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which method worked for you?

Also, Read

Leave a Comment