user_information(name="Satish", contact_number="5678")
Please note that “name” and “contact_number” are parameters to the function user_information(). And, we are specifying the values of the function parameters by using “parameter = value” format.
So, the above function will print the following output:
Name: Satish Age: Not Given Contact Number: 5678
Please note that even though we do not specify the second parameter of the function only, the function gets the values of all parameters correctly.
How to pass an arbitrary number of arguments to a Python function?
In Python, we can pass an arbitrary number of arguments to a function. In the function, the parameters are stored as a tuple. The function can get the parameters from the tuple and operate on them.
For example, let’s say there is a function that takes an arbitrary number of integers as arguments. The function then iterates through the parameters and prints them one by one.
def print_numbers(*numbers): print("There are ", len(numbers), " numbers.") for number in numbers: print(number) print_numbers(1, 2, 3, 4)
Here, we are passing 4 numbers as arguments. The passed numbers are stored as a tuple. The function print_numbers() then iterates through the parameter *numbers and prints the numbers. Also, we are using len(numbers) to print the number of passed arguments. The above program will print the following output:
There are 4 numbers. 1 2 3 4
Similarly, we can pass other data types also as arbitrary arguments. For example,






0 Comments