Types of arguments in python

1. Default arguments
2. Keyword arguments
3. Positional arguments
4. Arbitrary positional arguments
5. Arbitrary keyword arguments

Default arguments :

Default arguments are values that are
provided while defining functions.The
assignment operator = is used to assign a default value to the argument.

Ex:

def add(a,b=5,c=10):
 return (a+b+c)
x=add(14)
print(x)

Keyword Arguments :

Functions can also be called using keyword arguments of the form
kwarg=value.During a function call, values passed through arguments don’t need to be in the order of parameters in the function definitions.

Ex :

def add(a,b,c):
return (a+b+c)
m=add(b=10,c=15,a=20)
print(m)

Positional Arguments :

arguments are passed in order one after the other, those arguments are called the "Positional arguments."

Eg :

def add(a,b,c):
 return (a+b+c)
print (add(10,20,30))

Arbitrary Positional Argument :

For arbitrary positional argument, an asterisk (*) is placed before a parameter in function definition which can hold non-
keyword variable-length arguments. These arguments will be wrapped up in a tuple

Ex :

def add(*b):
 result=0
for i in b:
 result=result+i
 return result
t1=add(1,2,3,4,5)
print(t1)
t2=add(10,20)
print(t2)

Arbitrary Keyword Arguments :

For arbitrary keyword argument, a double asterisk (**) is placed before a parameter in a function which can hold keyword variable-length arguments.

Ex :

def fn(**a):
 for i in a.items():
 print (i)
fn(numbers=5,colors="blue",fruits="apple")

Popular posts from this blog

Update

Select