PYTHON VARIABLES
- Name given to the memory location (Identifier)
- It is also called as identifier and hold the value
- Variable must be declared before it is used in the code
- No need to specify the data type of variable in python. Because python is a type infer language
- Python supports two types of variables like local variable and global variable.
NOTE
- Variable name should not be a keyword
Syntax and Example
SCOPE OF THE VARIABLE
- The scope of the variable can be determined by the location of the variable definition.
- Each variable cannot be accessed in each part of a program
- Python supports two types of the variables based on the scope. They are
- Local variable
- Global variable
1. LOCAL VARIABLE
- If a variable is defined inside of a function or function within a class, that is called as local variable
- It can be accessed (shared) only by the same function.
Calling:
- Local variable is called by using its name.
I. EXAMPLE OF LOCAL VARIABLE
(ganesh.py)
1. SOURCE CODE
# defining function
def disp():
# defining a local variable
k=10
print(“k= “,k)
# calling function
print(“——————————“)
print(“\tLocal Variable”)
print(“——————————“)
disp()
2. OUTPUT
DRAWBACKS
- It is not possible to access (share) a local variable outside of the function.
2. GLOBAL VARIABLE
- If a variable is defined outside of a function or inside of a class, that is called as global variable
- Unlike local variables, it can be accessed (shared) by every function in the program.
Calling:
- Global variable is called by using its name.
II. EXAMPLE OF GLOBAL VARIABLE
(ganesh.py)
1. SOURCE CODE
# defining a global variable
i=50
def disp():
print(“Inside of a function”)
print(“i= “,i)
print(“——————————“)
print(“\tGlobal Variable”)
print(“——————————“)
# calling function
disp()
print(“Outside of a function”)
print(“i= “,i)
2. OUTPUT
COMBINATION OF LOCAL AND GLOBAL VARIABLE
- If both local and global variables are defined with the same name in the python code, then the python interpreter will provide the first priority to the local variable.
III. COMBINATION OF LOCAL AND GLOBAL VARIABLES
(welcome.py)
1. SOURCE CODE
# defining a global variable
a=95
print(“—————————————-“)
print(“ombination of Local & Global Variable”)
print(“—————————————-“)
# define a function
def disp():
# defining a local variable
a=25
print(“Inside the function”)
print(“—————————————-“)
print(“Local Variable a= “,a)
print(“Global Variable a= “,a)
print(“—————————————-“)
# calling function
disp()
print(“Outside of function”)
print(“Global Variable a= “,a)
print(“—————————————-“)
2. OUTPUT
USAGE OF GLOBAL KEYWORD
Existing Problem
- A Python interpreter will always provide the first priority to a local variable, if the local variable is defined with the same name as the global variable inside a function.
- So, any changes of global variable(s) inside a function will be hidden.
Solution
- In order to access or modify the global variable inside a function, we can use the global keyword.
- The global keyword is to refer the global variable inside a function
- It allows the modification of global variables inside a function.
IV. USAGE OF GLOBAL KEYWORD
(welcome.py)
1. SOURCE CODE
# define a global variable
sum=0
print(“—————————————-“)
print(“Usage of Global Keyword”)
print(“—————————————-“)
def findsum(n):
# the keyword global refers global variable (not local variable)
global sum
while(n!=0):
r=n%10
sum=sum+r
n=int(n/10)
return sum
# calling function
res=findsum(191)
print(“Input Number \t: “,191)
print(“Sum of Digits \t: “,res)
2. OUTPUT