Python - What is the output: reference to global variable?

Question

What is the output of the following code?

X = 'Test' 
def func(): 
   global X 
   X = 'test' 

func() 
print(X) 


Click to view the answer

test

Note

The global declaration forces the variable assigned inside the function to refer to the variable in the enclosing global scope.

Related Quiz