If you are a Python programmer, you might encounter these requirements for checking whether a float variable is empty. In this tutorial, I will explain various methods to check if a float variable is empty in Python with examples.
To check if a float variable is empty by determining if it is None in Python, you can use a simple if statement. For example, if float_var is the variable, you can write if float_var is None: to check if it is None. If the condition is true, the variable is considered empty. This method is straightforward and effective for identifying uninitialized or explicitly set None float variables.
What is “Empty” in Python
Let us first understand what is empty in Python.
- None: The variable is explicitly set to
None. - NaN: The variable is set to Not a Number, which is a special floating-point value.
- Zero: The variable has a value of
0.0.
Method 1: Check for None
A variable that is None is often considered empty. You can check if a float variable is None using a simple if statement in Python. Here is an example.
# Example of checking for None
float_var = None
if float_var is None:
print("The variable is None.")
else:
print("The variable is not None.")If float_var is None, the output will be “The variable is None.” Otherwise, it will print “The variable is not None.”
I executed the above Python code, and you can see in the screenshot below it is giving me the exact output.

Read How to use Pandas to convert float to int in Python
Method 2: Check for NaN
NaN stands for “Not a Number” and is used to represent undefined or unrepresentable floating-point values. You can check for NaN using the math.isnan() function or the numpy.isnan() function if you are using NumPy.
Using math.isnan()
Here is an example of how to use math.isnan() to check if a float variable is empty.
import math
float_var = float('nan')
if math.isnan(float_var):
print("The variable is NaN.")
else:
print("The variable is not NaN.")If float_var is NaN, the output will be “The variable is NaN.” Otherwise, it will print “The variable is not NaN.”
Using numpy.isnan()
Here is an example of how to use numpy.isnan() to check if a float variable is empty in Python.
import numpy as np
float_var = np.nan
if np.isnan(float_var):
print("The variable is NaN.")
else:
print("The variable is not NaN.")Similar to the math.isnan() method, if float_var is NaN, the output will be “The variable is NaN.” Otherwise, it will print “The variable is not NaN.”
You can see the exact output in the screenshot below after I executed the above Python code using an online Python editor.

Read How to convert float to int Python
Method 3: Check for Zero
A float variable with a value of 0.0 can also be considered empty in some contexts. You can check for this using a simple comparison. Here is a simple example of how to check if a float variable is empty using comparison.
float_var = 0.0
if float_var == 0.0:
print("The variable is zero.")
else:
print("The variable is not zero.")If float_var is 0.0, the output will be “The variable is zero.” Otherwise, it will print “The variable is not zero.”
You can see the output in the screenshot below after I executed the above Python code.

Method 4: Check Multiple Empty Conditions
In some cases, you may want to check for multiple “empty” conditions at once. Here’s how you can combine checks for None, NaN, and 0.0 in Python for a float.
import math
def is_empty_float(value):
if value is None:
return True
if math.isnan(value):
return True
if value == 0.0:
return True
return False
# Test cases
test_values = [None, float('nan'), 0.0, 1.0, -1.0]
for val in test_values:
if is_empty_float(val):
print(f"The variable {val} is considered empty.")
else:
print(f"The variable {val} is not considered empty.")This method checks for None, NaN, and 0.0. For None, NaN, and 0.0, the output will be “The variable <value> is considered empty.” For other values like 1.0 and -1.0, the output will be “The variable <value> is not considered empty.”
Conclusion
In this tutorial, I have explained how to check if a float variable is empty in Python using different methods, with examples. I hope this will help you.
You may also like:

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.