1. A variable's scope determines where it is visible within a program. Local variables are only visible within their defined function, while global variables are visible program-wide.
2. Mutable objects like lists allow their values to change when passed to functions, while immutable objects like integers do not.
3. Functions can return single values or multiple values as tuples. If no return statement is specified, a function implicitly returns None.
Measures of Dispersion and Variability: Range, QD, AD and SD
functions.docx
1. Scope of Variables inPython
A variable isn’t visible everywhere and alive every time.
1. Scope
A variable’s scope tells us where in the program it is visible. A variable may have local or global
scope.
Local Scope- A variable that’s declared inside a function has a local scope. In other words, it
is local to that function.
def fun():
s = "I love India!" #local
variable
print(s)
fun()
print(s)
output is :-
I love India
Name error s not defined
def fun():
s = "I love India!"
#local variable
print(s)
s = "I love World!"
fun()
print(s)
output is :-
I love India
I love World
def fun(x):
a=10 # local
x=x+10
print(a,x)
a,x= 1,2
fun(x)
print(a,x)
output is :-
(10, 12)
(1, 2)
Global Scope- When you declare a variable outside python function, or anything else, it has
global scope. It means that it is visible everywhere within the program.
However, you can’t change its value from inside a local scope(here, inside a function). To do so,
you must declare it global inside the function, using the ‘global’ keyword.
a=10 #global
def fun(x):
x=x+10
print(a,x)
x= 1
fun(x)
print(a,x)
output is :-
(10, 11)
(10, 1)
def fun(x):
global a
a=10
x=x+10
print(a,x)
a,x= 1,2
fun(x)
print(a,x)
output is :-
(10, 12)
(10, 2)
2. As we know in python, every thing is as object. If the value of the object is changed, it is
called mutable, while if the value of object cannot change, it is called immutable.Example of
immutable objects are int, float, tuple, bool and str. Example of immutable are list, dict and set.
Passing int variable to function Passing string to function
def change(a):
a=a+10
q=10
change(q)
print (q)
output is :-10
def count(str):
count = 0
for ch in str:
if ch in "aeiouAEIOU":
count +=1
return count
str="computer"
print (count(str))
output is :- 3
Passing tuple as
arguement
Passing list/ array as
arguement
Passing dictionary as argument
def change(marks):
for i in
range(len(marks)):
print (marks[i]),
li=(10,20,30,40)
change(li)
output is :- 10 20 30 40
def change(marks):
for i in range(len(marks)):
marks[i]+=10
li=[10,20,30,40]
change(li)
print (li)
output is :- [20, 30, 40, 50]
def change(d1):
d1['roll']=1
d1['sec']='a'
d1={ "name": "amit", "class":12}
change(d1)
print (d1)
output is :-
{'roll': 1, 'sec': 'a', 'name': 'amit', 'cla
ss': 12}
NOTE: The changes reflect back after calling function as for mutable data types by default (call
by refernce method). The changes does not reflect back after calling function as for immutable
data types by default (call by value method)
VALUE RETURNING FUNCTION
Single Value returning function Multiple Values returning function
def disp(A, B):// formal parameters
return (A+B)
X,Y=10,20
C=disp(X,Y)
def cal(a, b):
c=a+b
d=a-b
e=a*b
f=a/b
3. print ( C)
output is:- 30
g=a%b
return c,d,e,f,g
q=cal(10,20)
print (q)
output is:- (30, -10, 200, 0, 10)
In Python, comma-separated values are
considered tuples without parentheses
Note :- If functiondoes not have return statement in it then it return None
Passing Parameters to function
Required/Positional
arguments (have to pass in
the same order as used in
function definition)
Keyword Arguments (we
are not required to
remember the position of
argument)
Default
argument(always
should be last
argument)
Variable
length (any
no. of
arguments
can be
passed)
def cal(a, b, ch):
if ch=='+':
print (a+b)
elif ch=='-':
print (a-b)
elif ch=='*':
print (a*b)
elif ch=='/':
print (a/b)
elif ch=='%':
print (a%b)
cal(4,5,’+’)
x,y=20,30
c='*'
cal(x,y,c)
cal(c,x,y)#wrong (no
output)
output is :-
9
600
def cal(a, b, ch):
if ch=='+':
print (a+b)
elif ch=='-':
print (a-b)
elif ch=='*':
print (a*b)
elif ch=='/':
print (a/b)
elif ch=='%':
print (a%b)
cal(b=10,ch='%', a=30)
#keyword arguments
output is:-
0
def cal(a, b, ch='+'):
if ch=='+':
print (a+b)
elif ch=='-':
print (a-b)
elif ch=='*':
print (a*b)
elif ch=='/':
print (a/b)
elif ch=='%':
print (a%b)
cal(4,5)
cal(4,5,’*’)
output is:-
9
20
def sum(*
var):
s=0
for i in var:
s=s+i
print (s)
sum(10)
sum(10,20)
output is:-
10
30