Global, Local And Nonlocal Variables In Python Programming

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

When the code runs we get the following output:

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.

Recommended:  Keywords And Identifiers In Python Programming Language

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.

When the code runs we get the following output:

Why it shows both strings in the output? The reason behind it that the variable xhas been defined as a string prior we call the function dock(). In this case, there is no local variable xand 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

When the code executes we get the following output:

It looks really simple to understand this.

Recommended:  Python Function Arguments - Types of Arguments

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.

Code Output:

In the above function of Python programming, we declare a glvariable with global scope and lcwith 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.

Code Output:

Recommended:  Basic Operators In Python Programming Language

In the above program, we declare a variable zoofor 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

Following output produces

For running this we use a nonlocalkeyword. 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.

Jack Sparrow

Hay I Am Admin Of This Website. I Am Very Passionate About Computers And Technology. This Website Is The Result Of My Passion. I Love To Eliminate The Barriers That Come In Seeking Education. This Website Is The Free Source Of Learning About New Technology And Up To Date Knowledge.

Leave a Reply

Close Menu