Different Argument Types in Python Function

Different Argument Types in Python Function

Hello everyone, In this article, we will learn about the Different Argument Types in Python.

There are several types of arguments that can be passed at the time of the function call, Argument type of function depends on the way of passing input values(arguments) to the function.

  1. Default Arguments
  2. Required Arguments
  3. Keyword Arguments
  4. Variable Arguments

1. Default Arguments

  • Defining a function by assigning default values to arguments.
  • Passing values to the default argument is optional while calling in the function.

Let's understand the following program.

def default(x,y=30):     
    print(x,y)
default(10)            # The variable 'y' is not passed into the function however the default value of 'y=30' is considered in the function 
default(50,100)     #The value of 'y' is overwritten here, 100 will be printed as 'y' 
default(10,"python")
Output:
10 30       
50 100
10 python
  • A Non-default argument can not follow default arguments.

Let's understand the following program, where x=20 is the default argument and y is a non-default argument.

def default(x=20,y):   #Error: non-default argument follows default argument
    print(x,y)
default(10)
default(50,100)

2. Required Arguments

  • Defining a function without default argument.
  • We need to pass the values to all positional arguments.

Let's understand the following program.

def required(x,y):
    print(x,y)
required(50,100)        #Output:  50 100
required(10)            #Error: required() missing 1 required positional argument: 'y'

3. Keyword Arguments

  • Passing values to the arguments along with the name of the argument as a key.
  • If we specify the names of the arguments, no need to follow the order in the function call.

Let's understand the following program.

def keyword(name,age):
    print("Name =", name)
    print("Age =", age)
keyword('Annie', 21)      
keyword( age = 25 , name = 'Anu')
Output:
Name = Annie
Age = 21
Name = Anu
Age = 25

Let's understand another program.

def keyword(name,age, loc = 'lucknow'):
    print("Name =", name)
    print("Age =", age)
    print("location = ",loc)
keyword('Annie', 21 )
Output:
Name = Annie
Age = 21
location =  lucknow

4. Variable Arguments

  • Passing arguments to functions of different lengths.
  • We collect all these arguments using pointer-type variables.
  • Pointer variable stores the address of the memory block which contains a set of elements.

Let's understand the following program.

def variable(*a):
    print(a)
variable(1,2,3,4,5)
variable(1,2.5,'a',"Python")
variable()
Output:
(1, 2, 3, 4, 5)
(1, 2.5, 'a', 'Python')
()
  • We can process the elements using for loop easily.

Let's understand the following program.

def variable(*arr):
    print("Length = ", len(arr))
    print("List is ")
    for element in arr:
        print(element)
variable(1,2,3,4,5)
Output:
Length =  5
List is 
1
2
3
4
5

Thank you for reading this article. I hope it was a great read for you. If you have any feedback please share it in the comment below. Also, if you find it helpful, please like and hit the follow button on the right top corner.

For a quick response, You can reach me on Twitter - twitter.com/Mahima13_dev

Did you find this article valuable?

Support Mahima gupta by becoming a sponsor. Any amount is appreciated!