What will be the output of the following Python code?m = 3 def ct1(): global m m = 1def ct2(p,q): global m return p+q+mct1()sum = ct2(2,4)print(sum)OptionsError9107
Question
What will be the output of the following Python code?m = 3 def ct1(): global m m = 1def ct2(p,q): global m return p+q+mct1()sum = ct2(2,4)print(sum)OptionsError9107
Solution
The output of the given Python code will be 7. Here's the step-by-step explanation:
-
The global variable
mis initially set to 3. -
The function
ct1()is defined to change the global variablemto 1. -
The function
ct2(p,q)is defined to return the sum of its parameterspandqand the global variablem. -
The function
ct1()is called, which changes the global variablemto 1. -
The function
ct2(2,4)is called, which returns the sum of its parameters and the global variablem(which is now 1). So, 2 + 4 + 1 = 7. -
The result is assigned to the variable
sum. -
The value of
sum(which is 7) is printed.
Similar Questions
What will be the output of the following Python code?y=7def ct(m,n=y): print(m,n)y=9ct(5)OptionsError5 75 97 5
What will be the output of the following Python code?def ct(y): global y y+=2 print(y)ct(5)print("hi")Optionserror77hihi
What will be the output of the following Python code?def ct(): y=76 print(x)y=+2ct()OptionsError767874
What will be the output of the following Python code?def foo(): total += 1 return totaltotal = 0print(foo())
What will be the output of the following Python code?def ct(x, y, z): global a x = 5 y = 6 z = 7 a = 8 print(x,y,z,a)x,y,z,a = 15,16,17,18ct(1,3,9)Options15 16 17 181 5 9 185 6 7 81 5 9 8
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.