Bird
Raised Fist0
Pythonprogramming~10 mins

Global keyword in Python - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - Global keyword
Start
Define global variable
Define function
Inside function: use 'global' keyword
Modify global variable
Function ends
Global variable changed outside function
End
This flow shows how a global variable is defined, then modified inside a function using the 'global' keyword, affecting the variable outside the function.
Execution Sample
Python
x = 5

def change():
    global x
    x = 10

change()
print(x)
This code changes a global variable x inside a function using the global keyword and prints the updated value.
Execution Table
StepActionVariable 'x' ValueOutput
1Initialize x = 55
2Define function change()5
3Call change()5
4Inside change(): declare global x5
5Inside change(): assign x = 1010
6change() ends10
7print(x)1010
💡 Program ends after printing updated global variable x = 10
Variable Tracker
VariableStartAfter change() callFinal
x51010
Key Moments - 2 Insights
Why do we need the 'global' keyword inside the function?
Without 'global', assigning to x inside the function creates a new local variable x, not changing the global one. See step 4 and 5 in execution_table where 'global x' allows modifying the global x.
What happens if we remove the 'global' keyword and assign x = 10 inside the function?
The global x remains 5 because the assignment creates a local x inside the function. The print outside still shows 5. This is explained by the difference in step 5 if 'global' was missing.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of x right after step 5?
A5
B10
CUndefined
D0
💡 Hint
Check the 'Variable x Value' column at step 5 in the execution_table.
At which step does the global variable x actually change its value?
AStep 2
BStep 4
CStep 5
DStep 7
💡 Hint
Look for the assignment to x inside the function in the execution_table.
If we remove the 'global' keyword, what will be printed at step 7?
A5
B10
CError
DNone
💡 Hint
Refer to the key_moments section explaining the effect of removing 'global'.
Concept Snapshot
Global keyword in Python:
- Use 'global var' inside a function to modify a global variable.
- Without 'global', assignment creates a local variable.
- Changes to global variables affect code outside the function.
- Syntax:
  global x
  x = new_value
Full Transcript
This visual execution shows how the global keyword works in Python. First, a global variable x is set to 5. Then a function change() is defined, which uses 'global x' to tell Python that x inside the function is the global one. Inside change(), x is set to 10. When change() is called, it updates the global x to 10. Finally, printing x shows 10, confirming the global variable was changed. Without the global keyword, the assignment inside the function would create a local x, leaving the global x unchanged at 5. This is a common beginner confusion. The execution table tracks each step and variable value, helping learners see exactly when and how the global variable changes.

Practice

(1/5)
1.

What does the global keyword do in Python?

easy
A. It creates a new local variable inside a function.
B. It allows a function to modify a variable defined outside it.
C. It deletes a variable from the global scope.
D. It imports a module globally.

Solution

  1. Step 1: Understand variable scopes

    Variables defined outside functions are global, inside functions are local by default.
  2. Step 2: Role of global keyword

    The global keyword tells Python to use the global variable inside the function, allowing modification.
  3. Final Answer:

    It allows a function to modify a variable defined outside it. -> Option B
  4. Quick Check:

    global lets function change outside variable [OK]
Hint: Global lets functions change outside variables directly [OK]
Common Mistakes:
  • Thinking global creates new local variables
  • Confusing global with import statements
  • Assuming global deletes variables
2.

Which of the following is the correct way to modify a global variable count inside a function?

count = 0
def increment():
    ?
    count += 1
easy
A. def count
B. local count
C. global count
D. nonlocal count

Solution

  1. Step 1: Identify the need to modify global variable

    The function wants to increase the global variable count.
  2. Step 2: Use correct keyword to access global variable

    Using global count inside the function tells Python to use the global count, not create a local one.
  3. Final Answer:

    global count -> Option C
  4. Quick Check:

    Use 'global' to modify global variables inside functions [OK]
Hint: Use 'global' before variable to modify it inside function [OK]
Common Mistakes:
  • Using 'local' which is not a Python keyword
  • Using 'nonlocal' which applies to enclosing functions, not globals
  • Trying to define variable with 'def'
3.

What is the output of this code?

value = 5
def change():
    global value
    value = 10
change()
print(value)
medium
A. 5
B. Error
C. None
D. 10

Solution

  1. Step 1: Analyze the function change()

    The function declares value as global and sets it to 10.
  2. Step 2: Effect of calling change()

    Calling change() updates the global value from 5 to 10.
  3. Final Answer:

    10 -> Option D
  4. Quick Check:

    global lets function update outside variable [OK]
Hint: global changes outside variable, so print shows updated value [OK]
Common Mistakes:
  • Thinking print shows original value 5
  • Expecting an error without global keyword
  • Confusing local and global scopes
4.

Find the error in this code and choose the fix:

counter = 0
def add():
    counter += 1
add()
print(counter)
medium
A. Add global counter inside add() before incrementing.
B. Change counter to a local variable inside add().
C. Remove the increment line counter += 1.
D. Define counter inside add() without global.

Solution

  1. Step 1: Identify the error cause

    Trying to increment counter inside add() without declaring it global causes UnboundLocalError.
  2. Step 2: Fix by declaring global variable

    Adding global counter inside add() tells Python to use the global counter variable.
  3. Final Answer:

    Add global counter inside add() before incrementing. -> Option A
  4. Quick Check:

    Declare global to modify outside variable inside function [OK]
Hint: Declare global before modifying global variable inside function [OK]
Common Mistakes:
  • Ignoring the need for global declaration
  • Trying to create local variable with same name
  • Removing increment instead of fixing scope
5.

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)
hard
A. global calls
B. nonlocal calls
C. local calls
D. calls = 0

Solution

  1. Step 1: Understand the goal

    We want to update the global variable calls inside the function track().
  2. Step 2: Use the correct keyword

    Using global calls inside track() allows incrementing the global calls variable.
  3. Step 3: Confirm output

    Calling track() twice increments calls from 0 to 2, so print(calls) outputs 2.
  4. Final Answer:

    global calls -> Option A
  5. Quick Check:

    Use global to update global counter inside function [OK]
Hint: Use 'global' to update global counter inside function [OK]
Common Mistakes:
  • Using 'nonlocal' which is for nested functions
  • Trying to assign local variable without global
  • Resetting calls inside function