0
0
Pythonprogramming~10 mins

Global keyword in Python - Step-by-Step Execution

Choose your learning style9 modes available
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.