(Python Tutorial – 019)
Python Global, Local and Nonlocal variables
In this tutorial, you’ll learn about Python Global variables, Local variables, Nonlocal variables and where to use them.
Global Variables
In Python, a variable declared outside of the function or in global scope is known as a global variable. This means that a global variable can be accessed inside or outside of the function.
Let’s see an example of how a global variable is created in Python.
Example 1: Create a Global Variable
x = "global"
def foo():
print("x inside:", x)
foo()
print("x outside:", x)
Output
x inside: global x outside: global
In the above code, we created x as a global variable and defined a foo()
to print the global variable x. Finally, we call the foo()
which will print the value of x.
What if you want to change the value of x inside a function?
x = "global"
def foo():
x = x * 2
print(x)
foo()
Output
UnboundLocalError: local variable 'x' referenced before assignment
The output shows an error because Python treats x as a local variable and x is also not defined inside foo()
.
To make this work, we use the global
keyword. Visit Python Global Keyword to learn more.
Local Variables
A variable declared inside the function’s body or in the local scope is known as a local variable.
Example 2: Accessing local variable outside the scope
def foo():
y = "local"
foo()
print(y)
Output
NameError: name 'y' is not defined
The output shows an error because we are trying to access a local variable y in a global scope whereas the local variable only works inside foo()
or local scope.
Let’s see an example on how a local variable is created in Python.
Example 3: Create a Local Variable
Normally, we declare a variable inside the function to create a local variable.
def foo():
y = "local"
print(y)
foo()
Output
local
Let’s take a look at the earlier problem where x was a global variable and we wanted to modify x inside foo()
.
Global and local variables
Here, we will show how to use global variables and local variables in the same code.
Example 4: Using Global and Local variables in the same code
x = "global "
def foo():
global x
y = "local"
x = x * 2
print(x)
print(y)
foo()
Output
global global local
In the above code, we declare x as a global and y as a local variable in the foo()
. Then, we use multiplication operator *
to modify the global variable x and we print both x and y.
After calling the foo()
, the value of x becomes global global
because we used the x * 2
to print two times global
. After that, we print the value of local variable y i.e local
.
Example 5: Global variable and Local variable with same name
x = 5
def foo():
x = 10
print("local x:", x)
foo()
print("global x:", x)
Output
local x: 10 global x: 5
In the above code, we used the same name x for both global variable and local variable. We get a different result when we print the same variable because the variable is declared in both scopes, i.e. the local scope inside foo()
and global scope outside foo()
.
When we print the variable inside foo()
it outputs local x: 10
. This is called the local scope of the variable.
Similarly, when we print the variable outside the foo()
, it outputs global x: 5. This is called the global scope of the variable.
Nonlocal Variables
Nonlocal variables are used in nested functions whose local scope is not defined. This means that the variable can be neither in the local nor the global scope.
Let’s see an example of how a global variable is created in Python.
We use nonlocal
keywords to create nonlocal variables.
Example 6: Create a nonlocal variable
def outer():
x = "local"
def inner():
nonlocal x
x = "nonlocal"
print("inner:", x)
inner()
print("outer:", x)
outer()
Output
inner: nonlocal outer: nonlocal
In the above code, there is a nested inner()
function. We use nonlocal
keywords to create a nonlocal variable. The inner()
function is defined in the scope of another function outer()
.
Note : If we change the value of a nonlocal variable, the changes appear in the local variable.
Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.
Learn by Coding: v-Tutorials on Applied Machine Learning and Data Science for Beginners
Latest end-to-end Learn by Coding Projects (Jupyter Notebooks) in Python and R:
All Notebooks in One Bundle: Data Science Recipes and Examples in Python & R.
End-to-End Python Machine Learning Recipes & Examples.
End-to-End R Machine Learning Recipes & Examples.
Applied Statistics with R for Beginners and Business Professionals
Data Science and Machine Learning Projects in Python: Tabular Data Analytics
Data Science and Machine Learning Projects in R: Tabular Data Analytics
Python Machine Learning & Data Science Recipes: Learn by Coding
R Machine Learning & Data Science Recipes: Learn by Coding
Comparing Different Machine Learning Algorithms in Python for Classification (FREE)
There are 2000+ End-to-End Python & R Notebooks are available to build Professional Portfolio as a Data Scientist and/or Machine Learning Specialist. All Notebooks are only $29.95. We would like to request you to have a look at the website for FREE the end-to-end notebooks, and then decide whether you would like to purchase or not.
Python tutorials for Business Analyst – Python Global Keyword