1. Variable
length arguments
we define a function to make a reusable
code that performs specific operation. To execute that operation, we call a
function with the specific value, this value is called a function argument in
Python.
def add(a,b):
c=a+b
print(c)
#function call
add(2,3)# valid
add(2)#invalid
add(2,4,5)#invalid
Introduction to *args and **kwargs in Python
In Python,
we can pass a variable number of arguments to a function using special symbols.
There are two special symbols:
We use *args
and **kwargs as an argument when we are unsure about the number of arguments to
pass in the functions.
1.
*args (Non Keyword Arguments)
2.
**kwargs (Keyword Arguments)
def add(*args):
sum=0
for i in args:
sum=sum+i
print(sum)
#function call
add(2,3)# valid
add(2)#valid
add(2,4,5)#valid
#
*args for variable number of arguments
def f1(*argv):
for
i in argv:
print
(i)
f1('Hello', 'Ram')
"""
output:
Hello
Ram
1.
Can we give def
f1(*n): ?
Ans: we
will get error
>>> """
#Variable length arguments
def add(*n):
sum=0
for i in n:
sum=sum+i
print("total=",sum)
add()
add(10)
add(10,6)
add(2,3,4)
"""
output:
total= 0
total= 10
total= 16
total= 9
>>>
"""
Sometimes we can pass variable number of
arguments to our function, such type of arguments are called variable length
arguments. We can declare a variable length argument with * symbol
We can call this function by passing any
number of arguments including zero number. Internally all these values
represented in the form of tuple.
A tuple is a collection of
elements/items of different data types is called tuple.
A tuple is similar to list. Elements are
separated by comma operator and enclosed within parentheses ().
Difference between list and tuple is:
List elements can be modified as it is mutable. Whereas tuple elements can not
be modified as it is immutable. So tuple is treated as a read-only list.
Example:
t=(10,-20,10,2.4,”raj”,”$”,True)
Ex2:
We can mix variable length arguments with positional arguments
#Variable length arguments
def add(a,*n):
print(a)
sum=0
for i in n:
sum=sum+i
print("total=",sum)
#add()Error
add(10)
add(10,6)
add(2,3,4)
"""
output:
10
total= 0
10
total= 6
2
total= 7
>>>
"""
Note: After variable length argument, if
we are taking any other arguments then we should provide values as keyword
argument.
#Example program on variable length
arguments
def f1(*n,a):
for i in n:
print(i)
print("a=",a)
f1('sita','ram',5)
"""output:
Note1: f1('sita','ram',a=5)
sita
ram
a= 5
(2)f1('sita','ram',5) #Error bcoz after
variable length argument, if we are taking any
other arguments then we should provide
values as keyword arguments
>>>
"""
def f1(a, b, c):
print("a:",
a)
print("b:",
b)
print("c:",
c)
# Now we can use *args or **kwargs to
# pass arguments to this function :
args = ("Hyd",
"Vij", "Kadapa")
f1(*args)
#kwargs = {"a" :
"Hyd", "b" : "Vij", "c" :
"Kadapa"}
#f1(**kwargs)
"""output:
a: Hyd
b: Vij
c: Kadapa
>>>
"""Related Video: https://www.youtube.com/playlist?list=PL2mD2CO8TKlJdFbbtuhTXPQk2tNQx0pb1
Previous: Formal and actual parameters
Next: kwargs(keyword arguments)
No comments:
Post a Comment