Lecture_Notes_On_Functions
Lecture_Notes_On_Functions
Sum is: 30
a = 20
b = "TestString"
c = (2,3,'abc')
d=True
print("\nBefore change call:a={} b={} c={} d={}".format(a,b,c,d))
change(a,b,c,d)
print("\nAfter change call:a={} b={} c={} d={}".format(a,b,c,d))
Changing references to other objects with in a function will not have any effect on the actual parameters
passed to the function. Formal parameter references no longer exist once the function returns to its
caller. This is true for all objects (immutable and mutable objects).
list1 = ['a','b','c']
d={'k1':'OldValue'}
s = {'apple','banana'}
print("\nBefore the function call:list1=",list1,"s={} d={}".format(s,d))
changeCompleteReference(list1,s,d)
print("\n\nAfter the function call:list1={} s={} d={}".format(list1,s,d))
Before the function call:list1= ['a', 'b', 'c'] s={'banana', 'apple'} d={'k1': 'Ol
dValue'}
After the function call:list1=['a', 'b', 'c'] s={'banana', 'apple'} d={'k1': 'OldV
alue'}
Formal parameters refering to mutable objects and using the formal parameter, methods are invoked to
change the state of the mutable objects.
Changes made to the membership of a list, set, and dictionary are still visible after returning from the
functions.
list1 = ['a','b','c']
d={'k1':'OldValue'}
s = {'apple','banana'}
print("Before change2 call:list1=",list1,"s={} d={}".format(s,d))
change2(list1,s,d)
print("After change2 call:list1={} s={} d={}".format(list1,s,d))
Before change2 call:list1= ['a', 'b', 'c'] s={'banana', 'apple'} d={'k1': 'OldValu
e'}
After change2 call:list1=['a', 'changed', 'c'] s={'banana', 'Mango', 'apple'} d=
{'k1': 'NewValue2'}
frozenset() can be used to convert a mutable to immutable This function takes input as any iterable
object(list, set, dictionary, etc.) and converts them into immutable object. The order of element is not
guaranteed to be preserved.
In [6]: def change2(l,s,d):
l[1]='changed' # changing a list element
s.add("Mango") # adding a element to set
d["k1"]="NewValue2" # changing value of a key in a dictionary.
list1 = ['a','b','c']
d={'k1':'OldValue'}
s = {'apple','banana'}
print("Before change2 call:list1=",list1,"s={} c={} d={}".format(list1,s,d))
change2(frozenset(list1),s,d)
print("After change2 call:list1={} s={} c={} d={}".format(list1,s,d))
Before change2 call:list1= ['a', 'b', 'c'] s=['a', 'b', 'c'] c={'banana', 'apple'}
d={'k1': 'OldValue'}
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Input In [6], in <cell line: 12>()
9 s = {'apple','banana'}
11 print("Before change2 call:list1=",list1,"s={} c={} d={}".format(list1,s,
d))
---> 12 change2(frozenset(list1),s,d)
13 print("After change2 call:list1={} s={} c={} d={}".format(list1,s,d))
Input In [9]
def add3Parameters(x,y=3,z=4,q):
^
SyntaxError: non-default argument follows default argument
1
abc
city Vizag
name Vikas
age 25
define a function that demos scope: global, local
Scope resolution: Local(L), Enclosed(E), Global(G), Built-in(B)
keywords: N/A, nonlocal, global, None(imported from a module)
In a function, a varible is only referred (but not modified) runtime looks for
its value in its enclosed context (all the way upto global level). The global
keyword is not mandatory.
In a function, a varible is assigned a value (before it ever referred in the
context) it will be treated like a local varible.
If we want to make changes to a global varible,closest enclosed variable
then use global, nonlocal keyword respectively.
inner2()
print(pi2)
inner1()
print(pi)
inner pi variable
global pi variable
print(pi)
pi = 'inner pi variable' # Changes the global variable value.
print(pi)
inner1()
print("After the function Call, The value is:",pi)
---------------------------------------------------------------------------
UnboundLocalError Traceback (most recent call last)
Input In [15], in <cell line: 8>()
5 pi = 'inner pi variable' # Changes the global variable value.
6 print(pi)
----> 8 inner1()
9 print("After the function Call, The value is:",pi)
When global keyword is used, no local variable is created. Any changes made will be visible after the
call (The changes were made to the global variable)
inner1()
print("After the function Call, The value is:",pi)
global pi variable
inner pi variable
After the function Call, The value is: inner pi variable
def outer():
pi = 'outer pi variable' # a local variable in the context of outer function crea
def inner():
pi = 'inner pi variable' # a local variable in the context of inner function
print(pi) #uses only inner variable defined
inner()
print(pi) #uses the local variable variable defined in the outer function
outer()
print(pi)
inner pi variable
outer pi variable
global pi variable
def outer():
pi = 'outer pi variable'
def inner():
print(pi) # Only referred the variable: Uses one of the closest enclosed valu
inner()
outer()
print(pi)
outer pi variable
global pi variable
In [19]: pi = 'global pi variable'
def outer():
pi = 'outer pi variable'
def inner():
print(pi) # referred the variable.
pi="Changed in inner." # attempting to modify the variable.
inner()
outer()
print(pi)
---------------------------------------------------------------------------
UnboundLocalError Traceback (most recent call last)
Input In [19], in <cell line: 11>()
8 pi="Changed in inner." # attempting to modify the variable.
9 inner()
---> 11 outer()
12 print(pi)
def outer():
pi = 'outer pi variable'
def inner():
nonlocal pi # uses one of the closest enclosed value
print("In Inner Before the Change:",pi)
pi="New value changed inside inner."
print("In Inner function:",pi)
inner()
print("In Outer function:",pi)
outer()
print(pi)
def outer():
pi = 'outer pi variable'
def inner():
global pi
global variable can be defined (first time) in a function. If the global variable does not exists then it will
be added to the global scope.
3+5= 8
3*5= 15
print("Value:",ad_mayMultiply(2,3,5))
print("Value:",ad_mayMultiply(2,3))
print("Value:",ad_mayMultiply(2,3,None))
Value: 25
Value: 5
Value: 5
Global variables defined in other modules (other python files) can be imported and used.
In [27]:
#%% Import global variables from other module
import config2
print("x=",config2.x)
print("y=",config2.y)
print("z=",config2.z)
q = config2.z
print("value of z=",q)
x= 1
y= 3.5
z= Hello
value of z= Hello
functions defined in other modules (other python files) can be imported and used.
In [28]: import config2
a=5
b=8
z = config2.utilityAdd(a,b)
print("After calling the uitlityAdd z=",z)
hello
Adding 2 and 3 is: 5
Sleeping 1s
0
Sleeping 1s
1
Sleeping 1s
2
Sleeping 1s
3
Sleeping 1s
4
random() - Return random number between 0.0 and 1.0 randint(i,j) - - Return random number in the
range[i,j]
In [32]: import random
for i in range(10):
x=random.randint(1,6)
y= random.random()
print(x)
print(y)
1
0.1346371733995949
3
0.5897055963394732
4
0.7623801902918336
3
0.06045381312044262
2
0.1842788452038593
3
0.27167156848688145
6
0.32279058941134775
4
0.4002538390637257
5
0.4915254577800502
5
0.5221524057975635
9
3
5
9
8
3
6
4
4
4
In [ ]: