Types of Arguments:
Positional Arguments:
The values are assigned to the parameters in the order in which they are passed.
def display(first_name, last_name):
print("Welcome", first_name, last_name)
display("Lakshmi", "Pritha")
Welcome Lakshmi Pritha
def find_total(l):
total = 0
for num in l:
total+=num
print(total)
marks = [90,56, 78]
find_total(marks)
224
Variable length arguments:
variable length arguments also called as arbitrary arguments.
Same method/function with different number of arguments or with different types of arguments.
Sometimes we don’t know how many arguments will be passed to a function. Python provides ways to handle this with *
def find_total(*l):
total = 0
for num in l:
total+=num
print(total)
mark1 = 100
mark2 = 90
mark3 = 87
find_total(mark1, mark2,mark3)
find_total(45,54)
find_total()
277
99
0
keyword arguments:
keyword arguments by specifying the parameter name followed by an equal sign (=) and the value you want to assign to it.
def display(*marks,name):
print('Welcome', name)
print(marks)
display(90,80,name="Lakshmi")
display(80,name="pritha")
Welcome Lakshmi
(90, 80)
Welcome pritha
(80,)
Default Arguments:
Default arguments are parameters that have default values. If the argument is not passed when calling the function, the default value is used.
def login(username, password="admin"):
print(username, password)
login("abcd", "abcd")
login("pqrs")
abcd abcd
pqrs admin
Keyword variable length arguments:
keyword variable-length arguments allow you to pass a variable number of keyword arguments to a function.
def login(**args):
print(args)
login(name='raja')
{'name': 'raja'}
def login(**args):
print(args)
login(name='raja', age=25, city='madurai')
{'name': 'raja', 'age': 25, 'city': 'madurai'}
def modify(l):
l.append(100)
print(f'Inside modify, {l}')
l = [10,20,30]
print(f'Outside modify {l}')
modify(l)
print(f'Outside modify2 {l}')
Outside modify [10, 20, 30]
Inside modify, [10, 20, 30, 100]
Outside modify2 [10, 20, 30, 100]
Keyword only arguments:
Keyword-only arguments must be specified by name when calling the function.
The * symbol is used in the function signature to indicate that subsequent arguments are keyword-only.
def add(*, no1, no2):
return no1+no2
print(add(10,20)) # It returns error
print(add(no1=100,no2=200)) #output:300
Function returning dictionary:
In Python, a function returning a dictionary simply means that the function creates and returns a dictionary object.
The zip() function combines the two lists element-wise, and dict() converts the resulting pairs into a dictionary.
def display(player_names, scores):
return dict(zip(player_names, scores))
player_names = ['virat', 'rohit']
scores = [100,105]
result = display(player_names, scores)
print(result)
{'virat': 100, 'rohit': 105}
Mutable default arguments:
When you use a list or dictionary as a default argument, it’s initialized only once. If the argument is modified during the function call, the changes persist across future calls.
def add(no, l=[]):
l.append(no)
return l
print(add(10))
print(add(20))
[10]
[10, 20]
def add(no, l=None):
l.append(no)
return l
print(add(10))
print(add(20))
AttributeError: 'NoneType' object has no attribute 'append'
def add(no, l=None):
l = []
l.append(no)
return l
print(add(10))
print(add(20))
[10]
[20]
Types of variables:
Global variable:
Defined Outside Functions: Global variables are usually declared outside of all functions, at the top level of the code.
no = 10
def f1():
print(no)
def f2():
print(no)
f1()
f2()
10
10
Local variable:
Local variables are declared inside functions or blocks of code.
no = 10
def f1():
no2 = 100
print(no)
print(no2)
f1()
10
100
If local and global variable can have same value means the precedence given to the local variable.
amount = 10000
def shopping():
amount = 1000
print(amount)
shopping()
1000
The global keyword allows you to modify the global variable amount inside the function.
amount = 10000
def shopping():
global amount
print("Total Amount for Trip is",amount)
amount = 1000
print("Shopping Amount",amount)
shopping()
Total Amount for Trip is 10000
Shopping Amount 1000
Inner functions:
Inner functions are functions defined inside other functions.
They can access variables from their outer function.
def open_fridge():
capacity = 165
print("opening fridge", capacity)
def take_water():
capacity = 1
print("Drink Water", capacity)
take_water()
open_fridge()
opening fridge 165
Drink Water 1
def open_fridge():
capacity = 165
print("opening fridge", capacity)
def take_water():
print("Drink Water", capacity)
take_water()
open_fridge()
opening fridge 165
Drink Water 165
def open_tank():
tank = 2
print(f'Inside open_tank {tank}')
def fill_petrol():
nonlocal tank # Refers to the variable 'tank' from the enclosing scope
tank = tank + 3
print(f'Inside fill_petrol {tank}')
fill_petrol()
open_tank()
Inside open_tank 2
Inside fill_petrol 5
Top comments (0)