e correct answerWhat will be the output of the following Python code?m=10n=20def update(): global n m=45 n=56update()print(m)print(n)
Question
What will be the output of the following Python code?
m = 10
n = 20
def update():
global n
m = 45
n = 56
update()
print(m)
print(n)
Solution
The output of the Python code will be:
10 56
Here's the step by step explanation:
- The variables m and n are initially set to 10 and 20 respectively.
- The function update() is defined. Inside this function, the global keyword is used to indicate that the variable n referred to here is the global variable n. The variables m and n are then set to 45 and 56 respectively. However, the change to m only applies within the function, because m is not declared as a global variable.
- The function update() is called. This changes the global variable n to 56, but the global variable m remains 10 because the change to m in the function does not affect the global variable m.
- print(m) prints the global variable m, which is 10.
- print(n) prints the global variable n, which is 56.
Similar Questions
e correct answerWhat will be the output of the following Python code?m=10n=20def update(): global n m=45 n=56update()print(m)print(n)
What will be the output of the following program?while i < 20: print(i) i += 1
What will be the output of the following statement?print(17 // 10)Options: Pick one correct answer from below1.712None of the Above
______________ is the output of the following statements in python.x = "TIME"for i in range(len(x)): print(i)
correct answerWhat will be the output of the following Python program?z=set('abc')z.add('san')z.update(set(['p', 'q']))z
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.