Hello everyone⚡, In this article, we will learn about Python Variables.
Introduction
Variables are important to understand to work with any programming language. It is used to store information.
Variables or Identifiers
- The memory location name is called variable and variable is also known as an identifier. it is used to store and process the information.
- Variable names can be a group of both the letters and digits, but they have to begin with a letter or an underscore.
syntax : name = value
example : a = 10, b = 4.5
In Python, The variable is described in two categories
- Local variable
- Global variable.
Local variable:
- local variable means defining a variable inside the function.
- We can access the local variables directly, local variables available only inside the same function to access.
Let's understand the following example.
def fun(): a=12 #local print("A = ",a) fun() #calling
Output: A = 12
- Arguments of a function work like local variables, hence we can access arguments only from the same functions.
Let's understand the following example.
def fun(x,y):
print("X value =",x)
print("Y value =",y)
fun(12,20)
Output:
X value = 12
Y value = 20
Global Variable:
- Global variable means defining a variable outside of all the functions.
- We can access global variables directly.
- Global variable available throughout the source file .
Let's understand the following example.
a=10 #global
def fun1():
print("A =",a)
def fun2():
print("B =",a)
fun1() #calling function
fun2()
print("C =",a)
Output:
A = 10
B = 10
C = 10
Special case:
- Local and global variables can have the same name.
- Where we access the variables directly inside the function. It gives the first priority to local variable.
Let's understand the following example.
a=10 #global
def fun1():
a=20
print("A =",a)
def fun2():
print("B =",a)
fun1() #calling function
fun2()
print("outside =",a)
Output:
A = 20
B = 10
outside = 10
Can not access variables/identifiers before declaration
- Python is interpreted programming language, Hence the source code executes line by line.
- Because of interpretation we can not access the members before definitions.
Let's understand the following Program.
test() #Error: name 'test' is not defined
def fun():
print("test")
test()
- We can access variables after memory allocation.
Let's understand the following Program.
def fun():
print("test = ", a)
fun() #Error: name 'a' is not defined
a=10 #global
fun() # output: test = 10
Note
We can access global variable inside the function but we can not modify
Let's understand the following Program.
a=10
def test():
print("a =",a)
a=a+10 #Error: local variable 'a' referenced before assignment
print("a =",a)
test()
How to use Global keyword
- It is the keyword.
- It is used to access global variables inside the function. Let's understand the following Program.
a=10
def fun():
global a
print("a=",a)
a=a+10
print("a=",a)
a=a+25
fun()
print("a=",a)
Output:
a= 10
a= 20
a= 45
Note
Inside the function, we use either local variable or global variable with the same name.
- We need to specify the global statement inside the function before the use of variable.
- Once we use local variable inside the function, we can not access global variable. Let's understand the following Program.
a=10 #global
def fun():
a=20 #local
print("a=",a)
global a #Error: name 'a' is used prior to global declaration
print("a=",a)
fun()
Program - If variables are different, we can access them as follows.
a=10 #global
def fun():
b=20 #local
print("b=",b)
global a
print("a=",a)
fun()
Output:
b= 20
a= 10
- We can create global variables inside the function. program -
def fun():
global a
a=10
print("a=",a)
a=a+20
fun()
print("a=",a)
Output:
a= 10
a= 30
To delete a variable, Use the keyword "del"
A pre-defined keyword "del", which is used to release the memory allocated to a variable.
Let's understand the following Program.
def Create():
global a
a=10
print("Initial value = ",a)
def modify():
global a
a=a+10
print("modified value=",a)
def delete():
global a
del a
print("a value=",a)
Create()
modify()
delete()
Output
Initial value = 10
modified value= 20
Traceback (most recent call last):
File "<string>", line 15, in <module>
File "<string>", line 12, in delete
NameError: name 'a' is not defined
Thank you for reading this article. I hope it was a great read for you. If you have any feedback please share it in the comment below. Also, if you find it helpful, please like and hit the follow button on the right top corner.
For a quick response, You can reach me on Twitter - twitter.com/Mahima13_dev