In this Python tutorial, you will learn about the Global, Local and Nonlocal Variables In Python Programming and how to use them properly.
In this tutorial, you will learn the following important things.
Global Variables In Python Programming
Global variables in Python programming are variables that have a universal scope. By universal we mean it can be used both inside and outside of a function body. You can read variables at Python variable
If global variables are declared outside at any place in the whole program it can be called out inside of a function because of it’s global nature.
Now let’s have a look at how we can create a global variable in Python
# This is how a global variable declare a = "global Variable" def pythonglobal(): print("a inside variable :", a) pythonglobal() print("a outside variable:", a)
When the code runs we get the following output:
a inside variable : global Variable a outside variable: global Variable
In the above-mentioned exampled we just created a variable a
with global nature and a function as pythonglobal()
to print the global variable. And then we call the function.
In case what if the variable with the same name defined in the function then what happens let’s see and then I make you understand why this happens.
# This function has a variable with the same name def dock(): x = "Bookmark to WalkIntoPC" print (x) # Global scope x = "Humpty Dumpty" dock() print (x)
When the code runs we get the following output:
Bookmark to WalkIntoPC Humpty Dumpty
Why it shows both strings in the output? The reason behind it that the variable x
has been defined as a string prior we call the function dock()
. In this case, there is no local variable x
and the value comes from the global variable x
.
Local Variables In Python Programming
A variable in Python Programming that has a restricted scope just to the inside of a function’s body or has a local scope is called a local variable.
You cannot call or use these variables outside the function’s body if you do so you will get an error message.
Example of using a local variable
def duck(): a = "I am a local Variable" print(a) duck()
When the code executes we get the following output:
I am a local Variable
It looks really simple to understand this.
Global And Local Variable In the Same Python
Now, what happens if we want to use the global and local variables in the same function of Python programming.
gl = "I am a Global Variable * " def duck(): global gl lc = "I am a Local Variable" gl = gl * 2 print(gl) print(lc) duck()
Code Output:
I am a Global Variable * I am a Global Variable * I am a Local Variable
In the above function of Python programming, we declare a gl
variable with global scope and lc
with local scope and define a function as duck()
.
Now the interesting part is that I use multiplication operators * to distinguish global and local variable from each other. When we call the duck()
function global variable gl
to get multiplied with 2 and thus print two times and local print one time.
Global And Local Variable In Python Programming With The Same Name
In this example, we will see how Python handles the situation if the name of global and local variables are the same.
zoo = 10 def duck(): zoo = 15 print("Local variable zoo:", zoo) duck() print("Global variable zoo:", zoo)
Code Output:
Local variable zoo: 15 Global variable zoo: 10
In the above program, we declare a variable zoo
for both global and local variables. The thing I want to mention here is that Python didn’t throw an error here because variables declared in both scopes.
Variable zoo
has a global scope outside the duck()
and local scope inside the duck()
function. That is why we get both outputs on the screen.
Nonlocal Variable And How To Use Them?
A variable that has neither the global nor the local scope is called the Nonlocal variable in Python programming. These variables generally used in nested function which has not any local scope either.
Example Of Nonlocal Variable
def outerFunction(): z = "Local Variable" def innerFunction(): nonlocal z z = "Nonlocal Variable" print("inner:", z) innerFunction() print("outer:", z) outerFunction()
Following output produces
inner: Nonlocal Variable outer: Nonlocal Variable
For running this we use a nonlocal
keyword. We use a innerFunction()
that is a nested function. This function is defined inside the scope of another function outerFunction()
.
Remember! if we change nonlocal
variable value, changes take effect at all places in the local variable.