Showing posts with label function arguments in python. Show all posts
Showing posts with label function arguments in python. Show all posts

Thursday, June 14, 2018

Difference of Single Aterisk * to Double Asterisk ** in python argument


The single asterisk in argument is given to read multiple arguments at once . In the below code if we call multiply(3,4,5,6) then the code will read all the inputs and then multiply. Otherwise we can not read multiple arguments.

def multiply(*args):
    total=1
    for arg in args:
        total*=arg
    return total

>>> multiply(3,4,5)
....  60
====================== 
If we want to read a dictionary as argument or arbitrary keyword arguments then ** is needed.e.g.

def accept(**kwargs):
    for keyword, value in kwargs.items():
        print("%s -> %r" % (keyword, value))

>>> accept(foo=2,faa=3,gaa="gaa")
('foo', 2)
('faa', 3)
('gaa', 'gaa')
>>> 

====================== 

Feautured Post

Python Trivia Post: enumerate()

Python has a very useful inbuilt function, which is called enumerate(). This is needed whenever you need the numbering of a certain element...