The global keyword lets you change a variable outside a function from inside it. Without it, you can only read the outside variable, not change it.
Global keyword in Python
Start learning this pattern below
Jump into concepts and practice - no test required
global variable_nameUse global inside a function before changing the variable.
If you don't use global, Python treats the variable as new inside the function.
x from 5 to 10 inside the function.x = 5 def change(): global x x = 10 change() print(x)
add_one() runs, it increases the global count by 1.count = 0 def add_one(): global count count += 1 add_one() add_one() print(count)
global, the global value stays 3 because the function only changes a local variable.value = 3 def try_change(): value = 7 # This creates a new local variable try_change() print(value)
This program shows how the global keyword lets the function increase_score change the global score variable.
score = 0 def increase_score(): global score score += 5 print(f'Score before: {score}') increase_score() print(f'Score after: {score}')
Use global only when you really need to change a global variable.
Too much use of global can make your code harder to understand.
For many cases, passing variables as function parameters is clearer.
The global keyword lets you change variables outside functions.
Without global, variables inside functions are local by default.
Use global carefully to keep your code easy to read.
Practice
What does the global keyword do in Python?
Solution
Step 1: Understand variable scopes
Variables defined outside functions are global, inside functions are local by default.Step 2: Role of
Theglobalkeywordglobalkeyword tells Python to use the global variable inside the function, allowing modification.Final Answer:
It allows a function to modify a variable defined outside it. -> Option BQuick Check:
global lets function change outside variable [OK]
- Thinking global creates new local variables
- Confusing global with import statements
- Assuming global deletes variables
Which of the following is the correct way to modify a global variable count inside a function?
count = 0
def increment():
?
count += 1Solution
Step 1: Identify the need to modify global variable
The function wants to increase the global variablecount.Step 2: Use correct keyword to access global variable
Usingglobal countinside the function tells Python to use the globalcount, not create a local one.Final Answer:
global count -> Option CQuick Check:
Use 'global' to modify global variables inside functions [OK]
- Using 'local' which is not a Python keyword
- Using 'nonlocal' which applies to enclosing functions, not globals
- Trying to define variable with 'def'
What is the output of this code?
value = 5
def change():
global value
value = 10
change()
print(value)Solution
Step 1: Analyze the function change()
The function declaresvalueas global and sets it to 10.Step 2: Effect of calling change()
Callingchange()updates the globalvaluefrom 5 to 10.Final Answer:
10 -> Option DQuick Check:
global lets function update outside variable [OK]
- Thinking print shows original value 5
- Expecting an error without global keyword
- Confusing local and global scopes
Find the error in this code and choose the fix:
counter = 0
def add():
counter += 1
add()
print(counter)Solution
Step 1: Identify the error cause
Trying to incrementcounterinsideadd()without declaring it global causes UnboundLocalError.Step 2: Fix by declaring global variable
Addingglobal counterinsideadd()tells Python to use the globalcountervariable.Final Answer:
Addglobal counterinsideadd()before incrementing. -> Option AQuick Check:
Declare global to modify outside variable inside function [OK]
- Ignoring the need for global declaration
- Trying to create local variable with same name
- Removing increment instead of fixing scope
You want to count how many times a function is called using a global variable calls. Which code correctly updates calls each time track() runs?
calls = 0
def track():
?
calls += 1
track()
track()
print(calls)Solution
Step 1: Understand the goal
We want to update the global variablecallsinside the functiontrack().Step 2: Use the correct keyword
Usingglobal callsinsidetrack()allows incrementing the globalcallsvariable.Step 3: Confirm output
Callingtrack()twice incrementscallsfrom 0 to 2, soprint(calls)outputs 2.Final Answer:
global calls -> Option AQuick Check:
Use global to update global counter inside function [OK]
- Using 'nonlocal' which is for nested functions
- Trying to assign local variable without global
- Resetting calls inside function
