Python supports 2 types of variables.
- Global Variables
- 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.
Eg:
1) a=10 # global variable 2) def f1(): 3) print(a) 4) 5) def f2(): 6) print(a) 7) 8) f1() 9) f2() 10) 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.
Eg:
1) def f1(): 2) a=10 3) print(a) # valid 4) 5) def f2(): 6) print(a) #invalid 7) 8) f1() 9) f2() 10) 11) NameError: name 'a' is not defined
global keyword:
We can use the global keywords for the following 2 purposes:
- To declare a global variable inside a function
- 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) 5) 6) def f2(): 7) print(a) 8) 9) f1() 10) f2() 11) 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"