Python Function Arguments – Types of Arguments
In this article, you will learn what is Python function arguments their different types and how to use them. You will also learn how you can define a function that takes different types of arguments.
Types Of Python Function Arguments
Python Default Arguments
Python Keyword Arguments
Python Arbitrary Arguments
Types Of Python Function Arguments
In Python programming, there are a number of ways that help a programmer to define a function that can take a different number of arguments. This will help the programmer to define their function according to the requirement of the Python program.
There are three ways to define Python function arguments that are very helpful for the programmer.
Python Default Arguments
In Python programming, the function arguments can have the default values but we can add default values to an argument by using an assignment ( = ) operator.
Don’t confuse this assignment operator ( = ) with the equal operator ( == ) these are two different things.
The assignment operator (=) is used to assign the values of the left-hand side to the right-hand side and the equality operator (==) is used to make the values of the right-hand side equal to the left-hand side.
Example of Python Default Arguments
def greetings(name, message = "Good morning!"): """ This function greets to the person or string with the provided message. And in case there is no message, it defaults to the string as "Good morning!" """ print("Hello",name + ', ' + message) greetings("WalkIntoPC") greetings("Please","Share, And Bookmark Me!") print("Happy Codding!")
Output:
Hello WalkIntoPC, Good morning! Hello Please, Share, And Bookmark Me! Happy Codding!
In the above example, we have set a default argument as ‘Good morning!’ it means whenever we print some string as we do as WalkintoPC the string or message ‘Good morning’ will automatically call because it is a default argument.
But in case if we overwrite it like we do in the second print statement with string ‘Share, And Bookmark Me!’ the default argument gets replaced with our own text.
There is no restriction, you have any number of arguments in a function as a default value. But you must remember one thing is that once we have defined a default argument then all the arguments to its right side must also have default values.
If we do so means if non-default argument follows default arguments we have an error. See the example below.
def greetings(message = "Good WalkIntoPC!", name):
Output:
SyntaxError: non-default argument follows default argument
Python Keyword Arguments
The values we give to the arguments these values are assigned to the arguments according to their position.
For instance, in the above function greetings() when the function called as greetings(“Please”, “Share, And Bookmark Me!”) the first value “Please” immediately gets assigned to the argument we declared as name and “Share, And Bookmark Me!” to the message respectively.
In Python programming, it is allowed to called a function using keyword arguments. In doing this the order of the argument can be changed but remember don’t write a non-default argument in front of default arguments
Example of Python Keyword Arguments
def student(firstname, lastname ='Sparrow', standard ='Twelve'): print(firstname, lastname, 'studies in', standard, 'Standard') # 1 keyword argument student(firstname ='Jack') # 2 keyword arguments student(firstname ='John', standard ='Tenth') # 2 keyword arguments student(lastname ='Gates', firstname ='Mike')
Output:
Jack Sparrow studies in Twelve Standard John Sparrow studies in Tenth Standard Mike Gates studies in Twelve Standard
In this example, we have a function named as a student() with some keyword arguments. I demonstrate 3 ways of defining keyword arguments in this example.
In the first function call, there is only one keyword argument and in the second and third function call, we have two required arguments which will be replaced as we enter new values in it.
But the thing needs to be noted is the order of the keyword argument is not so much important in Python keyword arguments.
But remember it is not allowed to have a positional argument followed by keyword argument this will result in an error function call. See the example below
greetings(name="Jack", "How do you do Jack?")
Output:
SyntaxError: non-keyword arg after keyword arg
Python Arbitrary Arguments
The third type of Python Function Arguments is Python Arbitrary Arguments. The arbitrary means doing by your own self or not have exact knowledge about something.
In the above types of defining Python function arguments, we know how much arguments we are going to use in a function but what if we don’t know in advance the number of arguments we are going to use in the function, this is where Python arbitrary argument type uses.
Fortunately, Python programming gives has a feature that gives us the ability to handle this type of situation with function calls with an arbitrary number of arguments.
For doing this, the method is simple, in the function definition we use an asterisk(*) before the parameter name that is a sign that this kind of argument is an arbitrary argument in Python function arguments.
Example of Python Arbitrary Arguments
def greetings(*personNames): """This function with arbitrary arguments greets all the person in the names tuple.""" # personNames is a tuple with arguments for names for name in personNames: print("Greetings!",name) greetings("Jack","Ali","Rose Wilson","Emma")
Output:
Greetings! Jack Greetings! Ali Greetings! Rose Wilson Greetings! Emma
Here in the example above, we only use one arbitrary argument and call the function greetings() with multiple values. This kind of argument is parsed and compiled into one tuple before passed into the function.
And for retrieval of all the values, we use for loop in Python arbitrary argument that helps to get all the values with just one argument passed in the function definition.
Perfectly explained. good work
Thank you.
you have done a really good work
Thank you.
I am a new learner of Python. You have done good work. I have learned this topic from your website.
I am happy that you like my post. Thank You.