Python declare variable type : Global and local variables in python programming language. The python variables are the reversed memory locations used to store the values. The variable in the python program that will give the data for processing. The value in python language will give the data for computer processing. The data types have number, list, strings, etc and declare by name and the alphabets.
Example below will explain the declaration of variable,
a=100
print a
Example:-
f=0
print f
f=’guru99’
print f
Variables are the block available in that block only and not accessible outside the block.
The variables of the same property are called the local variables.
The formal argument identifier will behave like the local variables.
They are secure and cannot be accessed by the function of the same program.
It will exist till the block of function is in the execution process and gets destroyed after execution is exited.
These variables lost the content and left the blocks in which they are declared.
The variables are stored on the stack and storage is specified.
Example will explain the local variables as follows,
If we want to print the local variable outside the scope the result is in NameError.
def SayHello ():
user=’hello’
print(“user=”, user)
return
The local variable for sayHello () and not accessible outside the block.
The local variables only reach with their scope like the funct().
Example:-
def sum(x, y):
sum=x+y
return sum
print(5, 10)
Output:-
15
The variable which is present outside any function block is called as the global variable.
The value is accessible inside any function so-called the global variable.
To use the global variable function python will provide the keyword as “global”. It will allow you to specify the scope of the variable.
The global variable id declared outside the function and access function present in the program.
The global variables are reliable and changed by the function in the program.
The variables retain their values and the program is in the execution.
Then we declare the variable in function with the global keyword and called as the “global variable”.
Example:-
user=’john’
def SayHello ():
print(“user=”, user)
return
The access is possible because the global variable is defined outside the function.
If we assign the value to globally declare the variable a new variable is created in the namespace,
The assignment will not alter the value of the global variable so to verify the behavior run the code.
The global variable is used anywhere in the program and has scope in the entire program.
Example:-
z=20
def funct ():
global z
print(z)
z=10
func()
print(z)
Output:-
20
10
Basic comparison |
Local variable |
Global variable |
scope |
Within a function inside they are declared |
Throughout the program |
Declaration |
The variables are declared inside a function |
It is declared outside any function |
Access |
It is accessible only by the statements inside the function which they are declared |
They are accessed by the statement in the entire program |
value |
The uninitialized local variable will result in the storage of the garbage value |
It is store zero by default. |
life |
It is created when the function block is entered and then destroyed upon the exit. |
It will remain in existence for the entire time of the program. |
storage |
The local variables are stored on stack unless it is specified |
It is stored on the fixed location decided by a complier. |
Parameter passing |
It is required |
It is not required for the global variables. |
Disadvantages of the Local variables:-
Disadvantages of the Global variables:-