Welcome to the TGB Python tutorials. In this tutorial, we will discuss the variable used in Python and how a variable is categorized into global, local, and non-local categories. Also, we will discuss the global keyword. So let us get started!
Python Global Local Nonlocal Variables
Python has three types of variables:
- Global variables,
- Local variables, and
- Nonlocal variables
1. Python Global Variables
All those variables in Python that are declared outside the function in a global scope are known as global variables. In simple words, any statement of the program can access a global variable.
Let’s understand it with an example:
#Output
Behind the code
In the above example, we first declared a variable with the name global_variable and assigned value 20 to it, and then we defined a function func (), which is printing the output value of global_variable. When we are calling the function func() , it starts execution, and it reaches the print statement and at the print() statement, first the function looks for the globlal _ variable inside itself, but when it could not find it inside func() , then it starts looking for it outside the function and then prints the value.
2. Python Local Variables
All those variables that we define inside the function are known as local variables. Also, you cannot access them outside the function.
Let’s understand it with an example:
#Output
Behind the code
In the above example, g is a global variable, and l is a local variable. You can see that we can access g inside the function local , and we can access it outside the function, but if you try to access the variable l outside the function, it will throw an error.
Let’s call a local variable outside the function:
#Output
Behind the code
As evident from the output, we cannot access the local variable outside the function. If we try to access it, we will get an error, such as "variable not defined."
3. Python Nonlocal Variables
The nonlocal is a keyword that acts as a global keyword for the nested function (function inside another function). We use the nonlocal keyword when a variable is in local scope but acts as a global scope for another function.
Let’s understand it with an example:
#Output
Behind the function
Though variable x of the local() function is a local variable, it act as global variable for the function in_local(). When we use the keyword nonlocal to grab the variable x and use it inside the nested function, i.e., in_local() , every change we make inside the in_local function on variable x reflects back on the original value of x .
Global Keyword in Python
global is a keyword in Python which helps to access the actual global variable from outside the function. Until now, we know that we can access the global variable from outside the function, but if you alter the global variable or assign a new object or value to it inside the function, it will become a local variable, and this has no impact on the actual global variable that is present outside the function. If you want to grab a variable that is outside the function and want to make changes there, we need the global keyword. It can grab the global variable, and we can alter them inside the function.
Let’s understand it with an example.
#Output
Behind the code
In the above example, we have used the
global
keyword to access the
global_variable_1
inside the
local()
function. Inside the
local()
function, we have two variables, namely
global_variable_1
and
global_variable_2
.
global_variable_1
is the same variable that is defined outside the
local()
function, and the
global_variable_2
is the newly created local variable for the
local()
function. When we changed the values of both the variables inside the function only the value of
global_variable_1
showed the changes, because it was accessed and changed by the local() function.
Let’s take another example, but here we do not use the global keyword:
#Output:
Same example with global keyword
#Output
Conclusion
That sums up this Python tutorial on Python global local nonlocal variables. As you can see, the distinction between whether a Python variable is global, local, or nonlocal depends on its scope, i.e., where it can be used throughout the program.