Default arguments in Python
Python allows function arguments to have default values. If the function is called without the argument, the argument gets its default value.
Default Arguments:
Python has a different way of representing syntax and default values for function arguments. Default values indicate that the function argument will take that value if no argument value is passed during function call. The default value is assigned by using assignment(=) operator of the form keywordname=value.
Let’s understand this through a function student. The function student contains 3-arguments out of which 2 arguments are assigned with default values. So, function student accept one required argument (firstname) and rest two arguments are optional.
def student(firstname, lastname ='Mark', standard ='Fifth'): print(firstname, lastname, 'studies in', standard, 'Standard') |
We need to keep the following points in mind while calling functions:
- In case of passing keyword argument, order of arguments is not important.
- There should be only one value for one parameter.
- The passed keyword name should match with the actual keyword name.
- In case of calling function containing non-keyword arguments, order is important.
Example #1: Calling functions without keyword arguments
def student(firstname, lastname ='Mark', standard ='Fifth'): print(firstname, lastname, 'studies in', standard, 'Standard') # 1 positional argument student('John') # 3 positional arguments student('John', 'Gates', 'Seventh') # 2 positional arguments student('John', 'Gates') student('John', 'Seventh') |
Output:
John Mark studies in Fifth Standard John Gates studies in Seventh Standard John Gates studies in Fifth Standard John Seventh studies in Fifth Standard
In the first call, there is only one required argument and the rest arguments use the default values. In the second call, lastname and standard arguments value is replaced from default value to new passing value. We can see order of arguments is important from 2nd, 3rd, and 4th call of function.
Example #2: Calling functions with keyword arguments
def student(firstname, lastname ='Mark', standard ='Fifth'): print(firstname, lastname, 'studies in', standard, 'Standard') # 1 keyword argument student(firstname ='John') # 2 keyword arguments student(firstname ='John', standard ='Seventh') # 2 keyword arguments student(lastname ='Gates', firstname ='John') |
Output:
John Mark studies in Fifth Standard John Mark studies in Seventh Standard John Gates studies in Fifth Standard
In the first call, there is only one required keyword argument. In the second call, one is required argument and one is optional(standard), whose value get replaced from default to new passing value. In the third call, we can see that order in keyword argument is not important.
Example #3: Some Invalid function calls
def student(firstname, lastname ='Mark', standard ='Fifth'): print(firstname, lastname, 'studies in', standard, 'Standard') # required argument missing student() # non keyword argument after a keyword argument student(firstname ='John', 'Seventh') # unknown keyword argument student(subject ='Maths') |
Above code will throw error because –
- In the first call, value is not passed for parameter firstname which is the required parameter.
- In the second call, there is non-keyword argument after a keyword argument.
- In the third call, the passing keyword argument is not matched with the actual keyword name arguments.
Recommended Posts:
- Packing and Unpacking Arguments in Python
- Python | Set 6 (Command Line and Variable Arguments)
- NLP | Part of Speech - Default Tagging
- Important differences between Python 2.x and Python 3.x with examples
- Reading Python File-Like Objects from C | Python
- Python | Add Logging to a Python Script
- Python | Sort Python Dictionaries by Key or Value
- Python | Add Logging to Python Libraries
- Python | Set 4 (Dictionary, Keywords in Python)
- JavaScript vs Python : Can Python Overtop JavaScript by 2020?
- Python Set | pop()
- set add() in python
- SQL using Python | Set 1
- zip() in Python
- bin() in Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



