Test your understanding of local and global variables in Python with these multiple choice questions. Practice concepts like variable scope, usage, and common mistakes with beginner-friendly MCQs and detailed answers. Ideal for students and Python learners.
Topic: local-global
global
The global keyword makes the variable refer to the global scope.
A variable defined inside a function
Local variables are declared inside functions and only exist while the function runs.
PI
Variables written in uppercase are treated as constants by convention in Python.
Both inside and outside functions
Global variables are accessible throughout the program.
At the top level of a module
A variable declared outside all functions is global.
x = 10
def my_function():
x = 5
return x
print(my_function(), x)
(5, 10)
Inside the function, a local variable x shadows the global variable. So my_function() returns 5 and the global x remains 10.
x = 15
def foo():
global x
x = x + 5
foo()
print(x)
20
The global keyword allows the function to modify the global variable x. x becomes 20.
Y_CONSTANT = 50
def change():
y = 10
return y
y = change()
print(y)
10
The constant Y_CONSTANT remains 50 and is not used in change(). y gets the local value 10 returned from change().
PI = 3.14
def area(radius):
return PI * radius * radius
PI = 3
print(area(2))
12.56
area() uses the value 3 for PI when executing, even though PI is conventionally a constant.
count = 5
def show():
count = 3
print(count)
show()
print(count)
3 5
The function defines a new local variable count=3; the global count remains 5.
A = 7
def test():
return A * 2
print(test())
14
A is global and can be used inside the function for computing the result.
x = 1
def func():
x = x + 1
return x
print(func())
UnboundLocalError
Python treats x as a local variable inside the function and raises an UnboundLocalError.
MAX_SIZE = 100
def reset():
global MAX_SIZE
MAX_SIZE = 50
reset()
print(MAX_SIZE)
50
Using global inside the function modifies the global constant MAX_SIZE.
Only a naming convention
Python does not enforce constants; they are written in uppercase by convention.
def example():
a = 6
def inner():
nonlocal a
a += 2
return a
return inner()
print(example())
8
nonlocal is used to modify the variable from the parent function scope.
Refers to a variable in an outer function
The nonlocal keyword refers to variables in the nearest enclosing scope that is not global.
RATE = 5
def calc(val):
return val + RATE
print(calc(10))
15
RATE is treated as a constant and is available inside the function.
x = 2
def add():
return x + 3
x = 7
print(add())
10
The value of x used is the updated global value 7.
z = 4
def test():
z = 6
return z
test()
print(z)
4
z inside the function is local and does not affect the global z.
count = 5
def update():
# fill the blank
count = count + 1
update()
print(count)
global count
Using the global keyword is required to update the global variable inside the function.
Tutorials, Roadmaps, Bootcamps & Visualization Projects