Python Global and Local variables

In Python, there are basically two types of variables are available. The first one is the global variable and another one is the local variable. The global variable basically declared outside of the function, so that it can be accessible entire the program. The local variable always declared inside the function body. It can be used only inside the function body. 

Local Variable:

A local variable is declared inside the function and it can be used only inside the function. It cannot be used outside of the function. If a variable is declared inside the function, it is considered as local to the function by default. The scope of the local variable is local. If any operation happens with the local variable, it does not affect the variables that are declared outside of the function.

Sample Code for local variable:

def display():

    x = "Local Variable"

print(x) # It will print the value of variable x

display()

Global Variable:

A global variable is declared outside of functions and it can be used anywhere in the entire program. The scope of the global variable is global. The Global keyword is used to define a variable inside the function so that it can be modified according to requirements. If a variable is not declared with a global keyword inside the function, it works like a local variable by default. If a variable is declared outside of the function, it works like a global variable by default. It is not required to declare a variable outside of a function using a global keyword, because a variable outside of a function is always a global variable by default.

Sample code for global variable:

x = "Global Variable"

def display():

      print(x) # output is Global Variable

display()

print(x) # output is Global Variable

Scope of global and local variable:

The scope of variables plays an important role in the case of declaring the local and global variables. Depending on the declaration of the variable, the scope of variables is decided. The global variables can be used throughout the program. In the case of the local variable, it is only accessible inside a function.  So, the scope of the global is variable is the whole program, but the scope of the local variable is only inside the function.  A global variable is always declared outside of a function. This is an advantage of using global variables because global variables can be used inside and outside of a function.

Global and Local Variable:

It is needed sometimes when global variables and local variables have to use in the same Python application. Some sample codes are given in which global and local variables are used together in the same code.

Sample Code for using a local and global variable in the same code:

a = "Global Variable" # global variable declaration

def display():

      global a

      b = "Local Variable" # local variable declaration

      print(a) # It will print Global Variable

print(b) # It will print Local Variable

display()

Sample Code for using a global and local variable with the same name:

a = 10 # global variable

def  local():

      a = 20 # local variable

      print(a) # output is 20 because this variable prints the value of local variable a

local()

print(a) # output will be 10 because this variable prints the value of global variable a