Python Tutorial: CS-Python Functions Exercises3 Scope Variables

Monday, 7 July 2025

CS-Python Functions Exercises3 Scope Variables

1) Find and write the output of the following python code :

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)

2) Consider the following code and write the flow of execution for this. Line numbers have been given for your reference.

1. def power(b, p):
2.  y = b ** p
3.  return y
4. 
5. def calcSquare(x):
6.  a = power(x, 2)
7.  return a
8.
9.  n = 5
10. result = calcSquare(n)
11. print(result)

3) What will be the output of following program?

num = 1
def myfunc():
    return num
print(num)
print(myfunc())
print(num)
 
num = 1
def myfunc():
    num = 10
    return num
print(num)
print(myfunc())
print(num)
 
 

4) What will be the output of following program?

def display():
    print("Hello", end='')
display()
print("there!")
 
num = 1
def myfunc():
    global num
    num = 10
    return num
print(num)
print(myfunc())
print(num)
 
 

5) Consider the code below and answer the questions that follow :

def multiply(number1, number2):
    answer = number1 * number2
    print(number1, 'times', number2, '=', answer)
    return(answer)
output = multiply(5, 5)

 

(i) When the code above is executed, what prints out ?

(ii) What is variable output equal to after the code is executed ?

6) Draw the entire environment, including all user-defined variables at the time line 10 is being executed.

1. def sum(a, b, c, d):
2.      result = 0
3.      result = result + a + b + c + d 
4.      return result
5. 
6. def length():
7.      return 4
8. 
9. def mean(a, b, c, d):
10.     return float(sum(a, b, c, d))/length()
11. 
12. print(sum(a, b, c, d), length(), mean(a, b, c, d))

7) Find and write the output of the following python code :

a = 10
def call(): 
    global a
    a = 15
    b = 20
    print(a) 
call()
 
V = 25
def Fun(Ch):
    V = 50
    print(V, end = Ch) 
    V *= 2
    print(V, end = Ch)
print(V, end = "*")
Fun("!")
print(V) 
8) Predict the output of the Python code given below:

9) What will be the output of the following code?

c = 10

def add():

global c

c = c + 2

print(c,end='#')

add()

c=15

print(c,end='%') 

10) Consider the code given below and find correct output:

a=5

def add(b=2):

global a

a=a+b

print(a,'#',b)

return a

b=add(a)

print(a,'#',b)

b=add(b)

print(a,'#',b)

 

x=5

def function1():

global x

y=x+x*2

print(y,end=”,”)

x=7

function1()

print(x)

 

x = 5

def func2():

        x = 3

       global x

       x = x + 1

       print(x)

   print(x)

x = 50

def func():

      global x

      print('x is', x)

      x = 2

       print('Changed global x to', x)

func()

print('Value of x is', x)

11) Predict the output of the Python code given below:

value = 50

def display(N):

global value

value = 25

if N%7==0:

value=value+N

else:

value=value-N

print(value, end="#")

display(20)

print(value)

 

L = [5,10,15,1]

G = 4

def Change(X):

global G

N=len(X)

for i in range(N):

X[i] += G

Change(L)

for i in L:

print(i,end='$')

 

12) Predict the output of the Python code given below:

x = 25

def modify(s, c=2):

global x

for a in s:

if a in 'QWEiop':

x //= 5

print(a.upper(),'@',c*x)

else:

x += 5

print(a.lower(),'#',c+x)

string = 'We'

modify(string,10)

print(x, '$', string)

v = 50

def Change(n):

       global v

       v, n = n, v

       print(v, n, sep = “#”, end = “@”)

Change(20)

print(v)

13) What will be the output of the following code?

x = 4

def reset():

global x

x = 2

print(x, end='&')

def update():

x += 3

print(x, end='@')

update()

x = 6

reset()

print(x, end='$')

sal = 5000

def inc_sal(per):

global sal

inc = sal * (per / 100)

sal += inc

inc_sal(10)

print(sal,end='%')

sal=6000

print(sal,end='$')

 

def fun():

       a=10

       print(a)

a=5

fun()

print(a)

 

def fun():

      global a

      a=10

      print(a)

a=5

fun()

print(a)

14) Write the output of the code given below:


p=5

def sum(q,r=2):

global p p=r+q**2

print(p, end= '#')

a=10

b=5

sum(a,b)

sum(r=5,q=1)

total=0

def add(a,b):

global total

total=a+b

print(total)

add(6,6)

print(total)

total=20

price=50

def add(a, b):

global price

total=a+b

price=(a+b)/2

print(total, price)

add(6,6)

print(total, price)

a=10

b=20

def change( ):

      global b

      a=45

      b=56

change( )

print(a)

print(b)

p=5

def sum(q,r=2):

global p

p=r+q**2

print(p, end= ‘#’)

a=10

b=5

print(p, end = ‘#’)

sum(a,b)

sum(r=5,q=1)

print(p, end = ‘#’)

z= 100

def f( ):

global z

print('z is:', z)

z=50

print('New value of global z is:', z)

f( )

print('Value of z is:', z)

def fun():

a=20

print(a)

def fun2():

global a

a=30

print(a)

a=10

fun()

print(a)

fun2()

print(a)

def fun():

global a

a=10

y=a

print(“inside”,a)

print(“y=”,y)

a=5

fun()

print(“outside”,a)

 

def fun(a):

a=10

print(a)

a=5

fun(a)

print(a)

 

def fun():

global a

a=10

y=a

print(“inside”,a)

print(“y=”,y)

a=5

fun()

print(“outside”,a)

a=10

def call():

global a

a=15

b=20

print(a)

call()

def Update(X=10):

X += 15

print( ‘X = ‘, X)

X=20

Update()

print( ‘X = ‘, X)

def fun():

a=10

y=globals()[‘a’]

print(“inside”,a)

print(“y=”,y)

a=5

fun()

print(“outside”,a)

 

def func(x,y=2):

g=5

x=x-y;

g=g*10;

print(x,y,g)

g=7

h=10

func(g,h)

print(g,h)

func(g)

print(g,h)

g=10

def func(x,y=10):

x=x-y

y=x*10

z=x+y+g

return z

g=func(g,15)

print(g)

g=func(g)

print(g)

a=3

def demo(x,y):

global a

a=x+y

z=a+y

y+=x

print(x,y,z)

b=5

demo(a,b)

print(a,b)

demo(a,b)

def pa(a,b,c):

x=4;

c+=x;

c*=globals() [‘x’]

b+=c

return a,b,c

y=1

x=2

z=5

x,y,z=pa(y,x,z)

print(x,y,z)

y,z,z=(z,y,x)

print(x,y,z)

 

 

 

 

 


No comments:

Post a Comment