Test Index
Functions in Python
© examsnet.com
Question : 23 of 23
Marks:
+1,
-0
(a) Write the output of the Python code given below :
g=0def fun1(x,y): global g g=x+y return gdef fun2(m,n): global g g=m-n return gk=fun1(2,3)fun2(k,7)print(g)OR(b) Write the output of the Python code given below :
a=15def update(x): global a a+=2 if x%2==0: a*=x else: a//=xa=a+5print(a,end="$")update(5)print(a)
[CBSE 2023 Compartment]
Solution:
Concept:The
global statement makes the function use the global variable g (or a), so a change made inside the function changes the global variable.(a)k=fun1(2,3): global g = 2 + 3 = 5, returns 5, so k = 5.fun2(k,7): global g = 5 - 7 = -2 (return value not used).print(g) prints the global g.Output:-2OR (b)
a=15, then a=a+5 gives a = 20.print(a,end="$") prints 20$ and stays on the same line.update(5): a += 2 gives 22; 5 is odd, so a//=5 gives 22 // 5 = 4 (global a changed).print(a) prints 4 right after the $.Output:20$4
© examsnet.com
Go to Question: