Python Global, Local and Non-local Variables

Here, we have learnt about the Global, Local and Non-local variables. Python Global variables are those variables which are declared outside of the...

Variables are the reserved memory location for storing value. 

Python Global, Local and Non-local Variables

Here, in this article we will discuss about the python global variable, python local variable and python non-local variable with examples.

Global Variable

In python, global variables are those variables which are declared outside of the function or are declared in the global scope. Global variable can be used everywhere in a program by the programmers and the scope of these variables are available through the entire program and inside every function.

Python Global Variable Example;

x = "Python Global variables"

def pre():

    print("Welcome to:", x)

pre()

print("let's learn:", x)

Output

Welcome to: Python Global variables
let's learn: Python Global variables
Above, is declared as the global variable and to print the global variable , we defined a . And when we call , the value of will be printed.

if we try to change the value of a "x" global variable inside the function;
x = "Python global variables"

def pre():

    x = x * 4

    print(x)

pre()

Output

UnboundLocalError: local variable 'x' referenced before assignment

Here, we get error as a result because is treated as local variable in python and inside the , not defined. To make the above condition work, we have to use the python global keyword.

Local Variable

In python, local variables are those variables which are declared inside of the function or are declared in the local scope. Local variables only belongs to that particular function in which it was declared. And we can not access local variables from outside of the function. 

Python Local Variable Example;

def pre():

    x = "Python Local Variables"

    print(x)

pre()

Output

Python Local Variables
To create a local variable, we have to declare the variable inside the function.

If we will try to use this local variable outside the scope;
def pre():

    x = "Python Local Variables"

    print("Inside Function:", x)

pre()

print(x)

Output

NameError: name 'x' is not defined
Here in the output, it shows error because is local variable and it only works inside the local scope . We can't access a local variable in global scope.

Non-local Variable

In Python, Non-local variables are those variables which are declared inside the nested function and they don't have any defined local scope. Simply we can say that Non-local variables are neither declared in local scope nor the global scope.

Python Non-local Variable Example;

def func():

    x = "Answersjet"

    def fn():

        nonlocal x

        x = "Python Non-local Variables"

        print("Welcome to:", x)

    fn()

    print("let's learn:", x)

func()

Output

Welcome to: Python Non-local Variables

let's learn: Python Non-local Variables
Above, to create a Non-local variable we have used the keyword. And in the above code we have a nested function . And in the scope of function, the function is defined.

Conclusion

From the above information we have learnt about the Global, Local and Non-local variables. Python Global variables are those variables which are declared outside of the function. Python Local variables are those variables which are declared inside of the function. Python Non-local variables are those variables which are declared inside the nested function.