Python Function | Python Keyword Arguments

Python Keyword Arguments

Normally When we call a function with some values, these values get assigned to the arguments according to their position.

Example:1

def greet(name,msg):
        print(“Hello”,name,” “,msg)

For example, in the above function greet(),

when we called it as

greet(“sumit”,”How do you do?”),

the value

“sumit” gets assigned to the argument name and
“How do you do?” to msg.

Python allows functions to be called using keyword arguments. When we call functions in this way, the order (position) of the arguments can be changed.

Following calls to the above function are all valid and produce the same result.

#function definition 
def greet(name="amit",msg="how are you"):
            print("Hello ",name," ",msg)

#function calling
greet(name = "sumit",msg = "How do you do?")

greet(msg = "How do you do?",name = "sumit")

greet(name="sumit",msg = "Hope all is fine")

greet("sumit",msg = "How do you do?")