Computer Science
Find and write the output of the following python code :
a = 10
def call():
global a
a = 15
b = 20
print(a)
call()
Answer
15
Working
a = 10
— This line assigns the value 10 to the global variablea
.def call()
— This line defines a function namedcall
.a = 15
— Inside the call function, this line assigns the value 15 to the global variablea
. Asglobal
keyword is used earlier, this assignment modifies the value of the global variablea
.b = 20
— Inside the call function, this line assigns the value 20 to a local variableb
.print(a)
— This line prints the value of the global variablea
, which is 15. This is because we've modified the global variablea
inside the call function.
Related Questions
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))
Draw flow of execution for the above program.
In the following code, which variables are in the same scope ?
def func1(): a = 1 b = 2 def func2(): c = 3 d = 4 e = 5
Write a program with a function that takes an integer and prints the number that follows after it. Call the function with these arguments :
4, 6, 8, 2 + 1, 4 - 3 * 2, -3 -2