In our journey through Part 4, we’ve learned how to pass data into functions as arguments and get data out of them using return
. Now we need to ask a fundamental question: what happens to the variables we create inside a function? Where do they live, and who can see them?
This brings us to the crucial programming concept of variable scope. In this post, we’ll introduce what scope is and why it’s the foundation for keeping our programs organized and bug-free.
What is Scope?
In programming, scope refers to the region of your code where a variable can be accessed or “seen.” It’s the set of rules that determines the visibility and lifetime of your variables.
Think of it like this: people in different houses can each have a dog named “Rover.” The “Rover” in your house is separate from the “Rover” in your neighbor’s house. You can call your dog without your neighbor’s dog responding. Each house is its own scope.
Scope in programming prevents naming conflicts. It allows a function to have a variable named result
without worrying that another function somewhere else also has a variable named result
. Each function has its own private workspace.
The Function Boundary
The most fundamental rule of scope in Python is:
A variable created inside a function can only be used inside that function.
Let’s prove this with an experiment. We’ll create a variable inside a function and then try to access it from outside.
def my_function():
# This variable is created INSIDE the function
function_variable = "I live inside the function"
print(f"Inside the function, I can see: '{function_variable}'")
# First, call the function to show it works
my_function()
# Now, try to access the variable from OUTSIDE the function
print(function_variable) # This will cause a NameError!
When you run this code, you’ll see the first print
statement from within the function works fine, but then the program will crash with an error:
Inside the function, I can see: 'I live inside the function'
Traceback (most recent call last):
...
NameError: name 'function_variable' is not defined
This NameError
is Python’s way of telling you, “From where you’re standing (outside the function), I have no idea what function_variable
is.” The variable is effectively invisible from the outside; it only existed for the brief moment the function was running.
Introducing Local and Global Scope
This experiment reveals the two most basic types of scope:
- Local Scope: The “inside” of a function is called the local scope. Any variable you create there, like our
function_variable
, is a local variable. It is created when the function is called and is destroyed when the function finishes and returns. - Global Scope: The main body of your script, at the top level of indentation, is called the global scope. Any variable you create here is a global variable.
Unlike local variables, global variables can be read from anywhere in your code, including from inside a function.
global_variable = "I am visible everywhere"
def another_function():
# This function can READ the global variable
print(f"Inside the function, I can see: '{global_variable}'")
another_function()
print(f"Outside the function, I can also see: '{global_variable}'")
This code runs without any errors because the global_variable
exists in the global scope, which is accessible to another_function
.
What’s Next?
You’ve now been introduced to variable scope—the rules that govern where your variables live. The key takeaway is that variables created inside a function are local to that function and cannot be accessed from the outside. This “scoping” is what keeps our functions self-contained and our programs organized.
We’ve just scratched the surface of this topic. In Post #95, we will take a much closer look at the “inside of the box” and formally explore the rules of local scope.
Author

Experienced Cloud & DevOps Engineer with hands-on experience in AWS, GCP, Terraform, Ansible, ELK, Docker, Git, GitLab, Python, PowerShell, Shell, and theoretical knowledge on Azure, Kubernetes & Jenkins. In my free time, I write blogs on ckdbtech.com