In different programming languages, the type assigned to the variable can not be changed from what is declared at the initial stage. Whereas in Python, the case is different. A value of integer type can be declared and the string value can be declared to the same variable. In Python, a variable is known as the “pointer to the object” or the reference to an object. At times, there arises a need to check for the variable existence in Python.
In this article, we will depict different methods to check whether the variable exists or not.
How to Check for the Existence of Variable in Python?
The existence of variables in a code is an important aspect of programming in Python. Since variables play a vital role in the declaration of the object. Therefore, in Python, there are two methods to declare a variable while coding. The first way is to declare it “locally” and the second way is to declare it “globally”. In order to check for the existence of variables in Python there are three different ways listed below:
- Using Local variable
- Using global variable
- Using the hasattr() method of Python
Method 1: Using Local Variable
The variable that is declared within the function in Python is referred to as a “local” variable. The below code checks if the variable is present in locals().
#Declare the function
def Method1():
#Declare the variable
variable1 = "Python Programming"
#The if statement to check for the string
if 'variable1' in locals():
#Print statement
print ('The declared variable is found')
else:
print ('The declared variable is not found')
#Calling the function
Method1()
In the above code:
- The Function is declared as “Method1”.
- The next step involves declaring a variable as “variable1”.
- The if loop in the next step checks for the “variable1” using the locals() function.
- The function is called at the end of the code.
Output
The output below shows that variable 1 is present.

Method 2: Using Global Variable
If the variable is declared outside the main function, it is described as a “global” variable. The globals() function checks for the existence of the variable.
var1 = "Coding"
def Method2():
if 'var1' in globals():
print ("The declared variable is present in global")
else:
print ("The declared variable is not present in global")
Method2()
In the above code block:
- The var1 is declared outside the function.
- The globals() function checks if the variable is either declared inside the function or outside the function.
- The function Method2() is called at the end.
Output
The output below shows that the variable var1 is declared outside the function therefore it is global.

Method 3: Using hasattr() Function
The hasattr() function takes two parameters: the object and the name. The variable is checked for its existence using the hasattr() function of Python.
#Declare the class
class Programminglanguages:
#Declare the variables
Name = 'Python'
Level = 'Intermediate'
pl = Programminglanguages()
#The hasattr function checks for the existence of variables.
if hasattr(pl, 'Name'):
print("The name of Language is :", pl.Name)
if hasattr(pl, 'Level'):
print("Programming Level:", pl.Level)
In the above code:
- The class is declared as “Programminglanguages”.
- The variables “Name” and “Level” are declared.
- The hasattr() function checks for the variable presence in the code.
- The print statement prints the output accordingly.
Output
The output below shows that the Python and Intermediate are printed.

This ends the discussion to check for the existence of a variable in Python
Conclusion
Variables in Python are the reference to the objects and their presence matters while coding. The variable presence in Python can be checked using globals(), locals(), and hasattr() function. In this article, we have effectively demonstrated the implementation of different methods to check for the existence of variables in Python.
Frequently Asked Questions
How can I check the existence of a variable in Python using local variable method?
You can check if a variable is present in locals() by declaring the variable within a function and using 'if' statement to check for its existence.
What is the significance of variables in Python programming?
Variables play a vital role in Python as they are pointers to objects. They allow dynamic typing where the type of a variable can change during execution.
What is the difference between declaring a variable locally and globally in Python?
Local variables are declared within a function and have local scope, while global variables are declared outside functions and have global scope accessible throughout the code.
How can I determine if a variable exists in Python using global variable method?
You can check for the existence of a variable by using 'if' statement with 'in' keyword to check if the variable is present in the globals().
What is the hasattr() method in Python used for when checking variable existence?
The hasattr() method in Python is used to determine if an object has a particular attribute or variable. It can be used to check the existence of a variable.
Why is it important to check for variable existence in Python?
Checking for variable existence in Python is crucial to prevent errors and ensure that the code runs smoothly without encountering issues related to accessing non-existent variables.
Can a variable in Python change its data type after declaration?
Yes, in Python, variables can change their data type after declaration. Python allows dynamic typing where a variable can hold different types of values during runtime.
What does it mean that a variable in Python is a 'pointer to an object'?
In Python, a variable acts as a pointer to an object in memory rather than directly holding the data. This allows flexibility in assigning different types of values to the same variable.