The Wayback Machine - https://web.archive.org/web/20241218091255/https://www.geeksforgeeks.org/how-to-check-if-a-python-variable-exists/
Open In App

How to check if a Python variable exists?

Last Updated : 02 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Variables in Python can be defined locally or globally. There are two types of variables first one is a local variable that is defined inside the function and the second one are global variable that is defined outside the function. 

Method 1: Checking the existence of a local variable

To check the existence of variables locally we are going to use the locals() function to get the dictionary of the current local symbol table. 

Python
def func():

  # defining local variable
    a_variable = 0

  # for checking existence in locals() function
    if 'a_variable' in locals():
        return True

# driver code
print(func())

Output:

True

Method 2: Checking the existence of a global variable

To check the existence of variables globally we are going to use the globals() function to get the dictionary of the current global symbol table. 

Python
# defining local variable
a_variable = 0

def func():  

  # for checking existence in globals() function
    if 'a_variable' in globals():
        return True

# driver code
print(func())

Output
True

Method 3: Testing if a Variable Is Defined or not using try and except

A NameError exception is raised when attempting to access a variable that hasn’t yet been defined, you can manage this with a try/except statement

Python
# a_variable = 0

try:
    a_variable
    print("Yes")
except NameError:
    print("Error: No value detected")

Output:

Error: No value detected

Using the ‘in’ keyword:

Approach:

You can use the ‘in’ keyword to check if a variable is defined or not.

Python
if 'my_variable' in locals():
    print('Variable exists')
else:
    print('Variable does not exist')

Output
Variable does not exist

Time complexity: O(1)
Space complexity: O(1)

How to check if a Python variable exists? – FAQs

How to check if a variable has any value in Python?

To check if a variable has any value (i.e., it is not None and not an empty value like 0, "", [], etc.), you can use an if statement. Here’s an example:

var = "Hello"

if var:
print("The variable has a value.")
else:
print("The variable is empty or None.")

How to check if a variable has been defined in Python?

To check if a variable has been defined, you can use a try and except block:

try:
var
except NameError:
print("The variable is not defined.")
else:
print("The variable is defined.")

How to check if a variable is None in Python?

To check if a variable is None, you can use the is operator:

var = None

if var is None:
print("The variable is None.")
else:
print("The variable is not None.")

How to use if variable in Python?

Using if variable in Python allows you to check if a variable evaluates to True (i.e., it has a truthy value). For example:

var = [1, 2, 3]

if var:
print("The variable has a truthy value.")
else:
print("The variable is empty or falsy.")

How to check variable type in Python?

To check the type of a variable, you can use the type() function:

var = 42

print(type(var)) # Output: <class 'int'>

if isinstance(var, int):
print("The variable is an integer.")
else:
print("The variable is not an integer.")

Next Article

Similar Reads

Practice Tags :
three90RightbarBannerImg