Module 6c: Local vs Global Variables#
- Differentiate between local and global variables.
When working with functions, it’s important to understand where your variables “live” — in other words, their scope.
A local variable exists only inside the function where it was defined.
A global variable exists outside all functions and can be used throughout your code.
Understanding the difference will help you avoid unexpected behavior when naming variables or modifying data.
Local Variables#
Local variables are created and used inside a function. They disappear once the function finishes running.
stroke_data = setup_code.stroke_data
def count_stroke_cases(df):
stroke_count = df['stroke'].sum()
print("Number of stroke cases:", stroke_count)
The stroke_count
variable is created locally inside the function. If you try to use it outside the function, Python will raise an error:
count_stroke_cases(stroke_data)
print(stroke_count) # ❌ NameError: name 'stroke_count' is not defined
Number of stroke cases: 249
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[4], line 2
1 count_stroke_cases(stroke_data)
----> 2 print(stroke_count) # ❌ NameError: name 'stroke_count' is not defined
NameError: name 'stroke_count' is not defined
Global Variables#
A global variable is defined outside any function and can be accessed inside functions (but not modified unless explicitly declared as global) [@global_var].
stroke_column = 'stroke'
def count_stroke_cases(df):
print("Stroke count:", df[stroke_column].sum())
This works because stroke_column is defined in the global scope, and we’re only reading it.
⚠️ Modifying Global Variables Inside Functions#
If you try to change a global variable inside a function, Python treats it as a new local variable unless you tell it otherwise with the global keyword.
count = 0 # global variable
def update_count():
count = count + 1 # ❌ UnboundLocalError
To modify a global variable safely, you must declare it:
count = 0
def update_count():
global count
count += 1
But in most cases, it’s better to avoid modifying global variables inside functions. Instead, return a value from the function and update the variable outside:
count = 0
def count_high_glucose(df, threshold):
return (df['avg_glucose_level'] > threshold).sum()
count = count_high_glucose(stroke_data, 125)
Note
Use local variables as much as possible. They keep your functions self-contained, predictable, and easier to debug.
Quick Practice#
What will happen in this code?
threshold = 130 # global
def check_threshold(df):
threshold = 110 # local
print("Local threshold:", threshold)
print("Patients above threshold:", (df['avg_glucose_level'] > threshold).sum())
check_threshold(stroke_data)
print("Global threshold is still:", threshold)
Solution
The global `threshold` remains 130. Inside the function, `threshold` is a new local variable set to 110 and doesn’t affect the global one.