Day 20 Insights: Dive into the Types of Arguments in Python

Day 20 Insights: Dive into the Types of Arguments in Python

Introduction :

Welcome back to my Python journey! Yesterday, I laid the foundation with control structures and functions, arguments in python.

Today, I dove into Types of arguments, Let's explore what I learned!

Types OF Arguments :

Example :

def Fun (no1, no2):

....

...

Fun (10,20)

  • a, b are formal parameter & 10, 20, are actual parameter.

  • there are four types of actual argument are allowed in python.

  • . Positional argument

  • . keyword as argument

  • . default argument

  • . variable length argument

1. positional argument / required positional argument ~~~~~~~~~~

  • . the no of arguments given at the function definition time is known as required positional argument.

  • these are the arguments passed to function in correct positional order.

  • Example :

def Hello (no1, no2, no3) :
    print (no1 + no2 + no3)
Hello (3,4,5)
Hello (1,2,3)

. while working with required positional argument TypeError exception is expected.

def Hello (no1, no2, no3,no4) :
    print (no1 + no2 + no3)
Hello (3,4,5)
Hello (1,2,3)
  • . TypeError exception can be generated in two different scenario .

  • too few argument error .

  • too many aruments error

2. keyword as argument / named argument.

  • . to change the order of passing the argument in python keyword as argument or named argument mechanism is used.

  • . keyword -> reserved words -> if elif else while for etc

  • . in keyword as argument mechanism keyword represents to the arguments name given at the function def time.

  • . positional argument follows keyword argument is a SyntaxError in python.

  • . we can pass argument values by keyword i.e by using parameter name .

  • example :

  •        def Demo (name , age , dept) :
              print ("Hello I am", name , "& my age is",age, "dept is ", dept)
          Demo (name ="rajani", age = 23, dept = "Civil")
          Demo (dept = "Civil", name = "rajani", age = 23)
    
  • in this program order of argument is not important but number of arguments must be matched .

Note :

we can use both positional & keyword arguments simultaneously

. but first we have to take positional arguments and then keyword argument, other wise we will get error .

def Demo (name , age , dept) :
    print ("Hello I am", name , "& my age is",age, "dept is ", dept)
Demo ("rajani", 23, "Civil")
Demo ("rajani", age = 23, dept = "ME")
Demo (name ="rajani",  23, "ME") #Error

. default argument : -

. Some times we can provide default values of our positional arguments.

. to avoid TypeError exception due to too few arguments default argument mechanism is used in python.

. default argument must be given at the time of function def

. if the param is not given explicitly during function call then only default argument will be activated.

. default argument can be any type object.

. non-default argument follows default argument is a SyntaxError in python.

example :

def Demo(name , cource = "programmming "):
    print ("Hello ", name , "we learn ", cource )
Demo ("amit", "python")
Demo ("raja")

VARIABLE LENGTH ARGUMENT ~

  • . to avoid TypeError exception due to too many arguments variable length argument mechanism is used in python.

  • . to declare variable length argument UNARY * operator is used in python.

. binary * x * y task multiplication
. unary ** \
x task variable length argument

. variable length argument is internally implemented with the help of tuple object

def add ( * x ) :
        print ( x ,  end = ' ' )
        sum = 0
        for number in x :
                sum = sum + number
        return sum

print ( add ( 10, 20, 30 ))
print ( add ( 10, 20 ))
print ( add ( 10  ))
print ( add (  ))

print ( add ( 10, 20, 30, 40, 50 ) )
print ( add ( 1 , 2, 3, 4, 5, 6, 7, 8, 9, 0 ) )

Challenges :

  1. Understanding various types of arguments.

  2. Handling errors.

Resources :

  1. Official Python Documentation: types of argument

  2. W3Schools' Python Tutorial: types of argument

  3. Scaler's Python Course: types of arguments

Goals for Tomorrow :

  1. Explore something more on python.

  2. that is builtins module.

Conclusion :

Day 20’s a success!

What are your favorite Python resources? Share in the comments below.

Connect with me :

GitHub: [ github.com/p-archana1 ]

LinkedIn : [ linkedin.com/in/archana-prusty-4aa0b827a ]

Join the conversation :

Share your own learning experiences or ask questions in the comments.

HAPPY LEARNING!!

THANK YOU!!