Python: Types of Variables

Python supports 2 types of variables.

  1. Global Variables
  2. Local Variables

1. Global Variables

The variables which are declared outside of a function are called global variables.

These variables can be accessed in all functions of that module.


1) a=10 # global variable 
2) def f1(): 
3) print(a) 
5) def f2(): 
6) print(a) 
8) f1() 
9) f2() 
11) Output 
12) 10 
13) 10

2. Local Variables:

The variables which are declared inside a function are called local variables.

Local variables are available only for the function in which we declared it. i.e from outside 

of a function, we cannot access.


1) def f1(): 
2) a=10 
3) print(a) # valid 
5) def f2(): 
6) print(a) #invalid 
8) f1() 
9) f2() 
11) NameError: name 'a' is not defined 

global keyword:

We can use the global keywords for the following 2 purposes:

  1. To declare a global variable inside a function
  2. To make a global variable available to the function so that we can perform required modifications

Eg 1:

1) a=10 
2) def f1(): 
3) a=777 
4) print(a) 
6) def f2(): 
7) print(a) 
9) f1() 
10) f2() 
12) Output
13) 777 
14) 10 

"Python Types Of Variables"

"How Many Types Of Variables In Python"

"Types Of Variables In Python Oops"

"Types Of Variables In Python With Example"

"Data Types Of Variables In Python"

"Python Compare Types Of Two Variables"

"Types Of Variables In Python"

"Python Different Types Of Variables"

"Python Program To Read And Print Various Types Of Variables"

"Python 3 Types Of Variables"

"Python Check Types Of Variables"