3_Functions
3_Functions
syllab
us
2025-
26
Chapter
2
Functio
ns
mymodule.greeting(“India")
executed
print("hello after calling a function")
Save the above source code in python file and
execute itVaibhav Mishra , PGT Computer
Variable’s Scope in
function
There are three types of variables with the view of scope.
1. Local variable – accessible only inside the functional block where it
is declared.
2. Global variable – variable which is accessible among whole
program using global keyword.
3. Non local variable – accessible in nesting of functions,using
Local variable program: Global variable program:
nonlocal keyword.
def fun(): def fun():
s = "I love India!" #local global s #accessing/making global variable
variable print(s) for fun() print(s)
s = "I love India!“ #changing global
s = "I love World!" variable’s value
fun() print(s)
print(s) s = "I love
Outpu world!" fun()
t: print(s)
I love Output:
India! I I love
love world! I
World! love India!
Vaibhav Mishra ,I PGT Computer
love
Variable’s Scope in
#Find the function
output of below program
def fun(x, y): # argument /parameter x and y
global a
a = 10
x,y =
y,x b =
20
b = 30
c = 30
print(a,
b,x,y)
a, b, x, y
= 1, 2, 3,4
fun(50, 100) #passing value 50 and 100 in parameter x and y of
function fun()
Print(a,b,x,y)
a, b, x, y = 1, 2, 3,4
fun(50, 100) #passing value 50 and
100 in parameter x and y of function
fun()
print(a, b, x, y)
OUTPUT
:- 10 30 Vaibhav Mishra , PGT Computer
Variable’s
Scope in
function
Global variables in nested function
def fun1():
x = 100
def fun2():
global x
x = 200
print("Before
calling
fun2: " + OUTPUT:
str(x))
Before calling
print("Calling
fun1()
fun2 fun2: 100 Calling
now:")
print("x in main: " + fun2 now: After
fun2()
str(x)) calling fun2: 100 x
print("After
in main: 200
calling
fun2: " +
str(x))
E.g.
x = lambda a, b : a * b
print(x(5, 6))
OUTPUT:
30
Vaibhav Mishra , PGT Computer
Mutable/immutable
properties
of data objects w/r
function
OUTPUT
: 2.0 Vaibhav Mishra , PGT Computer
Functions using
libraries
Functions available in Python Math
Module
s="i love
programming"
r=s.capitalize()
print(r)
OUTPUT:
I love programming
Vaibhav Mishra , PGT Computer
Functions using
libraries
String
Method Description functions:
capitalize() Converts the first character to upper case
casefold() Converts string into lower case
center() Returns a centered string
startswith() Returns true if the string starts with the specified value
swapcase() Swaps cases, lower case becomes upper case and vice
versa
title() Converts the first character of each word to upper case