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.



filter_none

edit
close

play_arrow

link
brightness_4
code

def student(firstname, lastname ='Mark', standard ='Fifth'):
  
     print(firstname, lastname, 'studies in', standard, 'Standard')

chevron_right


 
We need to keep the following points in mind while calling functions:

  1. In case of passing keyword argument, order of arguments is not important.
  2. There should be only one value for one parameter.
  3. The passed keyword name should match with the actual keyword name.
  4. In case of calling function containing non-keyword arguments, order is important.

Example #1: Calling functions without keyword arguments

filter_none

edit
close

play_arrow

link
brightness_4
code

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')

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

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')     

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

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')              

chevron_right


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.


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.




Article Tags :

2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.