Syntax
Python
def f(a, b):
****
****
****
f(30, 60)
- a, b are formal arguments where as 30, 60 are actual arguments.
- There are 4 types of actual arguments in python.
- positional arguments
- keyword arguments
- default arguments
- Variable length arguments
positional arguments
- These are the arguments passed to function in correct positional order.
Example
Python
def sub(a,b):
print(a-b)
sub(100,200)
sub(200,100)
Output
PowerShell
-100
100
- The number of arguments and position of arguments must be matched. If we change the order then result may be changed.
- If we change the number of arguments then we will get error.
keyword arguments
- We can pass argument values by keyword i.e. by parameter name.
Example
Python
def wish(name, msg):
print("Hello",name,msg)
wish(name="Ajay",msg="Good Morning")
wish(msg="Good Morning",name="Ajay")
Output
PowerShell
Hello Ajay Good Morning
Hello Ajay Good Morning
- Here the order of arguments is not important but number of arguments must be matched.
Note
- We can use both positional and keyword arguments simultaneously. But first we have to take positional arguments and then keyword arguments, otherwise we will get syntax error.
Example
Python
def wish(name,msg):
print("Hello",name,msg)
wish("Ajay","Good Morning")
wish("Ajay", msg="Good Morning")
wish(name="Ajay","Good Morning")
Output
PowerShell
Hello Ajay Good Morning
Hello Ajay Good Morning
SyntaxError: positional argument follows keyword argument
Default Arguments
- Sometimes we can provide default values for our positional arguments.
Example
Python
def wish(name="Guest"):
print("Hello",name,"Good Morning")
wish("Ajay")
wish()
Output
PowerShell
Hello Ajay Good Morning
Hello Guest Good Morning
- If we are not passing any name then only default value will be considered.
Note
- After default arguments we should not take non default arguments.
Python
def hello(name="Ajay", msg):
print(name, msg)
hello(name="Amit", msg="Good Morning")
Output
Python
SyntaxError: non-default argument follows default argument
Variable length arguments
- 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.
Example
Python
def sum(*n):
total=0
for n1 in n:
total=total+n1
print("The Sum=",total)
sum()
sum(10)
sum(10,20)
sum(10,20,30,40)
Output
PowerShell
The Sum= 0
The Sum= 10
The Sum= 30
The Sum= 100
Note
- We can mix variable length arguments with positional arguments.
Example
Python
def f1(n1,*s):
print(n1)
for s1 in s:
print(s1)
f1(10)
f1(10,20,30,40)
f1(10,"A",30,"B")
Output
PowerShell
10
10
20
30
40
10
A
30
B
Note
- After variable length argument, if we are taking any other arguments then we should provide values as keyword arguments.
Example
Python
def f1(*s, n1):
for s1 in s:
print(s1)
print(n1)
f1("A","B",n1=10)
#f1("A","B",10)
Output
PowerShell
A
B
10
#TypeError: f1() missing 1 required keyword-only argument: 'n1'
Note
- We can declare key word variable length arguments also.
Syntax
PowerShell
def fun(**kwargs):
****
****
- We can call this function by passing any number of keyword arguments. Internally these keyword arguments will be stored inside a dictionary.
Example
PowerShell
def display(**kwargs):
for k,v in kwargs.items():
print(k,"=",v)
display(n1=10,n2=20,n3=30)
display(rno=100, name="Ajay", marks=70, subject="Python")
Output
PowerShell
n1 = 10
n2 = 20
n3 = 30
rno = 100
name = Ajay
marks = 70
subject = Python
Ungraded Questions
Get ready for an exhilarating evaluation of your understanding! Brace yourself as we dive into the upcoming assessment. Your active participation is key, so make sure to attend and demonstrate your knowledge. Let’s embark on this exciting learning journey together!