isnan(n)
import math
print(math.isnan(2)) # False
print(math.isnan(-2)) # False
print(math.isnan(0.0)) # False
print(math.isnan(0/1)) # False
print(math.isnan(0.0/1)) # False
Note : 0.0 is considered finite.
import math
print(math.isnan(float('nan'))) # True
print(math.isinf(float('inf'))) # True
print(math.isinf(float('-inf'))) # True
import math
result = math.isnan(float('inf') / float('inf'))
print(result) # Output: True
Checking with Lists:
import math
numbers = [float('nan'), 1.0, -3.5, float('inf')]
for num in numbers:
print(math.isnan(num))
Output
True
False
False
False
Practical Differences:import math
numbers = [float('nan'), float('inf'), -float('inf'), 3.5]
for num in numbers:
if math.isnan(num):
print(f"{num} is NaN")
elif math.isinf(num):
print(f"{num} is Infinity")
else:
print(f"{num} is a valid number")
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.