parameter “contact_number”. Hence, user_information(“John Smith”, 27) will print the following output:
Name: John Smith Age: 27 Contact Number: Not Given
Please note that the position of the arguments is important here. If we provide two arguments instead of three, then the third parameter will get the default value. Similarly, if the second and the third parameters have default values, then we can pass one argument to the function also. In that case, the second and the third parameters will get the default values.
def user_information(name, age = "Not Given", contact_number = "Not Given"):
print("Name: ", name)
print("Age: ", age)
print("Contact Number: ", contact_number)
user_information("John Smith", "27")
user_information("Amit Sen")
The program will print the following output:
Name: John Smith Age: 27 Contact Number: Not Given Name: Amit Sen Age: Not Given Contact Number: Not Given
What are the keyword arguments of a Python function?
Now, let’s look into the following piece of code again:
def user_information(name, age = "Not Given", contact_number = "Not Given"):
print("Name: ", name)
print("Age: ", age)
print("Contact Number: ", contact_number)
Let’s say we want to specify a user whose name and contact number are given, but age is not given. Please note that age is the second parameter and contact_number is the third parameter of the function. So, if we pass two arguments to the function user_information(), the function will assume the default value for the third parameter only. But, in our case, the user has provided his contact number, but not age. So, without using keyword arguments we cannot print the user information correctly.
Using keyword arguments, we can call the function in the following way:








































0 Comments