Global Scope
A name declared in top level segment of a program is said to have a global scope and is usable inside the whole
program and all blocks (functions, other blocks) contained within the program.
Local Scope
A name declared in a function body is said to have a local scope and is usable within that function and the other blocks
contained under it. The formal arguments also have local scope.
Ways to declare a function:
1. Functions with no arguments and no return type
def sum():
a=int(input(“Enter the first number”))
b=int(input(“Enter the second number”))
c=a+b
print (c)
sum()
2. Functions with arguments and no return type
def sum(a,b):
c=a+b
print (c)
a=int(input(“Enter the first number”))
b=int(input(“Enter the second number”))
sum(a,b)
3. Functions with arguments and return type
def sum(a,b):
c=a+b
return c
a=int(input(“Enter the first number”))
b=int(input(“Enter the second number”))
x=sum(a,b)
print(x)
OUTPUT FINDING QUESTIONS
1. Find the output of the following code:
def update(x=10):
x+=15
print(‘x=’,x)
x=20
update()
print(‘x=’,x)
OUTPUT
x=25
x=20
2. Find the output:
def changeval(m,n):
for i in range(n):
if m[i]%5==0:
m[i]//=5
if m[i]%3==0:
m[i]//=3
l=[25,8,75,12]
changeval(l,4)
for i in l:
print(i, end=’#’)
25 8 75 12
0 1 2 3
5 8 5 4
OUTPUT
5#8#5#4#
5854#..................X
5#8#5#4……………X
3. Find the output:
a=10
def call():
global a
a=15
b=20
print(a)
call()
OUTPUT
15
4. Find the output:
def func(message, num=1):
print(message*num)
func(‘Python’)
func(‘Easy’,3)
OUTPUT
Python
EasyEasyEasy
5. Find the output:
def fun(s):
k=len(s) 11
m=” ”
for i in range (0,k): 0, 11 0 - 10
if (s[i].isupper()):
m=m+s[i].lower()
elif s[i].isalpha():
m=m+s[i].upper()
else:
m=m+’bb’
print(m) m
fun(‘school2@com)
OUTPUT
SCHOOLbbbbCOM
6. Find The output:
def Change(P, Q=30):
P=P+Q
Q=P-Q
print(P,”#”,Q)
return(P)
R=150
S=100
R=Change(R,S)
print(R,”#”,S)
S=Change(S)
7. Find The output:
def check(n1=1,n2=2):
n1=n1+n2
n2+=1
print(n1,n2)
check()
check(2,1)
check(3)
working
check() check(2,1) check(3)
n1=1,n2=2 n1=2,n2=1 n1=3 n2 = 2
n1=n1+n2 1+2=3 2+1=3 3+2=5
n2+=1 2+1=3 1+1=2 2 +1=3
print(n1,n2) 3,3 3,2 5,3
OUTPUT
33
32
53
8. Find the output
a=1
def f():
a=10
print(a)
Output
9. Find the Output
def call(P=40, Q=20):
P=P+Q
Q=P-Q
print(P,’@’,Q)
return P
R=200
S=100
R=call(R,S)
print(R,’@’,S)
S=call(S)
print(R,’@’,S)
1. call(R,S)
R=200 S=100
P=200 Q=100
P= P+Q=300 Q=P-Q=300-100=200
300@200
R=300 S=100
300@100
2. call(S)
S=100
P=100 Q=20
P= P+Q=120 Q=P-Q=120-20=100
120@100
R=300 S=120
300@120
OUTPUT
300@200
300@100
120@100
300@120
10. Find the output
def interest(p,t=2,r=0.10):
return(p*r*t)
print(interest(6100,1))
print(interest(5000,r=0.05))
print(interest(5000,3,0.12))
print(interest(t=4,p=5000))
WORKING
print(interest(6100,1)) 6100x1x0.10=610.0
print(interest(5000,r=0.05)) 5000x2x0.05=500.0
print(interest(5000,3,0.12)) 5000x3x0.12=1800.0
print(interest(t=4,p=5000)) 5000x0.10x4=2000.0
OUTPUT
610.0
500.0
1800.0
2000.0
11. Find the error. Correct the code and predict the output:
total=0;
def sum(arg1,arg2):
total=arg1+arg2;
print(“Total :”,total)
return total;
sum(10,20);
print(“Total : “,total)
CORRECT CODE
total=0 //no semi colon should be there
def sum(arg1,arg2): //wrong indentation
total=arg1+arg2 // no semi colon should be there
print(“Total :”,total)
return total //wrong indentation, no semi colon should be there
total=sum(10,20) // no semi colon should be there, variable total should be
there to receive the value from the function
print(“Total : “,total)
OUTPUT
Total :30
Total : 30
12. Find the error. Correct the code and predict the output:
def tot(n)
s=0
for i in range(1,n+1):
s=s+i
RETURN s
print(tot[3])
print(tot[6])
CORRECT CODE
def tot(n): //colon missing
s=0
for i in range(1,n+1):
s=s+i
return s //return should be in small letters
print(tot(3)) //instead of list brackets, it should be method brackets
print(tot(6)) ////instead of list brackets, it should be method brackets
OUTPUT
6
21