Social Media

7/12/2019

Types of Variables in Python Language (Local and Global)

There are mainly four types of variables that python language provide to us.
These are as follows:
  1. Local variable
  2. Global variable
  3. Instance variable
  4. Static variable
Local Variables:
Local variables are those variables which are defined inside a function. 
Their scope is limited to that function in which they are defined.
They can't be accessible from outside the function.

Global Variables:
Global variables are those variables which are defined outside the function as well as outside the class.
These types of variables can be accessible from anywhere means outside or inside the function or class.
Script: Demonstration of Local and Global Variable

y=7
def fun():
    x=5
    print("x is a local variable")
    print("y is a global variable")
fun()
print("y is a global variable")
    
Output:
x is a local variable
y is a global variable
y is a global variable

From the above script we can conclude that local variables are only accessible inside the function in which they are defined while on other side global variables can be accessible from anywhere in the script.

Be First to Post Comment !
Post a Comment