Welcome To
Arguments


Arguments in python

The argument is a value or data that we used in the function calling. and the argument provide the input to the function . example in the below

Example

def greet(name):
    return name
result=greet("Darshan")# here passing the argument to the name function
print(result)

Passing Argument to function in Python

The pass argument in a python means the value of an variable assigned during the function definition time . the function can have two types of arguments Actual Arguments and Formal Arguments the function can using this arguments to initial the function variable and that value as a argument

def greet(name):
    return name
result=greet("Darshan")
print(result)

Actual Arguments

The actual argument is variable of a function in the parameters. the declare during the definition time. the parameter as actual argument

Example

def greet(name):#here the parameter as a actual argument
    return name
result=greet("Darshan")
print(result)

Formal Arguments

The Formal argument is value passed during the function caller time. the argument of value assigned during the function definition time. that variable value is called formal argument. example in the below

Example

def greet(name):
    return name
result=greet("Darshan")# darshan is a Formal argument
print(result)

Types Of Arguments In Python

  1. Positional Arguments
  2. keyword Arguments
  3. default argument / default parameters
  4. Arbitrary arguments/ variable length Arguments

Positional Arguments

The positional arguments are assigned to the function definition in the sequential order. the first argument is passed from the function call is assigned to the first parameter in the function and the second argument is passed from the function call is assigned to the second parameter in the function.
Positional argument is a order list the order function argument is assigned to the correspond of function parameters. the python allows it to declare the value. Examples in the below

Example

def greet(a,b):
    return a+b
result=greet(20,30)
print(result)

Keyword Arguments

The Keyword argument are arguments that are passed to a function using the name of the argument followed by the equal sign and the value of the argument .the do no pass the argument as specific order . because the function is used name of the argument an directly assigned the value. Example in the below and the name="darshan" is the way of assigned the value of a function .

Example

def greet(a,b,c):
   return a+b+c
result=greet(a=10,b=20,c=30)# here passing Keyword arguments

Default Argument and Default Parameters

The Default argument in the function the default value assigned to the argument and parameter during the function or method define time. The argument name and there value assigned in the single line that is function creation line the more information about this please under stand the example

Example

def greet(a=10 ,b=120):
    sum=a+b
    print(" sum is",sum)
greet(20,30)
greet(20,20)
greet(10,50)

Arbitrary Argument or variable Length Argument

The arbitrary argument ia also called variable -Length arguments. we don't know the number of argument needed for the function in advance, we can use arbitrary arguments.

Types of arbitrary arguments

  1. variable length positional Arguments/li>
  2. variable length keyword Arguments

Variable Length positional Arguments

The variable length positional argument . when we want to declare the variable length argument with the asterisk(*) symbol .place an asterisk (*) before the parameter of a function definition to define arbitrary positional argument . we can pass multiple arguments to the function . internally all these values are represented in the form of tuple . This is simply ean when we declare the single parameter and we can call that function in multiple times by using the multiple arguments . as shown in the below Example

Example

def sum(*number):
   result=0
   for i in number:
     result=result+i
   print("sum:",result)
sum(10,20)
sum(10,20,30)
sum(10,20,30,40)
sum(10,20,30,40,50)

variable length keyword argument

python passes variable length non keyword argument to function *args but we cannot use this to pass keyword argument. the **kwargs allows to pass multiple keyword arguments to function. we can use the **kwargs if we want to handle keyword arguments in a function . the unpacking operator(**) is used to define variable length keyword arguments. keyword arguments passed to a kwargs are accessed key-value pair( same as accessing a dictionary in python)

example

def display(**x):
   for key, value in x.items():
     print(f'key={key} and value={value}),
display(a=10,b=20)
display(name="Darshan", age=36,salary=21000)