Python Functions Tutorial | Types of function arguments in Python

You must first complete Python Functions Tutorial | Using Functions in Python 3 before viewing this Lesson

Types of function arguments in Python

In Python, we can take four different types of arguments in a user defined function. They are as follows:

1. Default arguments:

In default arguments, we can assign some value to the formal arguments of a function but the assignment of values is possible from last argument of the function towards left.

The main use of default argument is that we may or may not provide value for default argument while calling the function because function will automatically take the default value of formal argument in the function.

Example: def show(a=5,b=10)

In the above example, a is a default argument having value 5 and b is also a default argument having value 10.

Example-1 : Program of function without default arguments.
def show(a=5,b=10):

show() #a takes 5, b takes 10 
show(25) #a takes 25, b takes 10 
show(25,40) #a takes 25, b takes 40
5 10
25 10
25 40


Example-2 Program of function without default arguments.
def display(fname,lname="Kumar"):

display("amit") #fname gets 'amit', lname gets 'Kumar' 
display("amit","singh") #fname gets 'amit', lname gets 'singh'
amit Kumar
amit singh

2. Required arguments:

In case of Required arguments, number and order of actual arguments and formal arguments must be same. The values of actual arguments are assigned to formal arguments on one to one basis i.e. First actual argument in assigned to first formal argument, second actual argument in assigned to second formal argument and so on.

Program of function with Required arguments.
def show(name,salary):

Amit 50000

3. Keyword arguments:

The keyword arguments are used to specify the names of formal arguments while calling the function. The main advantage of keywords arguments is that we can write actual arguments in any order we want.

Values of actual arguments will be passed to formal arguments on the basis of their names rather than their order.

Program of function with Keyword arguments.
def show(name,age,salary):


”’value of age will be 30 in formal argument age,
name will receive ‘Amit’ and
salary will recieve 50000.”’

name= Amit
age= 30
salary= 50000

4. Variable number of arguments:

This technique of passing arguments to a function is very useful when we do not know the exact number of arguments that will be passed to a function. Syntax of function with variable number of arguments is:

def func_name(var1, *vartuple)

Here var1 takes very first argument value. All argument values passed after first argument will be stored in *vartuple in the form of a tuple. We can perform operations on these values just as we work with tuples.

Program of function with variable number of arguments.
def show(var1,*var2):

”’Amit’ will be stored in argument var1. Values 1,3,4,5,6 will be stored as a tuple in var2.”’

(1, 3, 4, 5, 6)

Lesson tags: argument passing in python, default arguments in python, keywords arguments in python, passing arguments of a function in python, required arguments in python, types of arguments in python, variable number of arguments in python
Back to: Python Programming Tutorial