I Have NEVER Used Floats Like THIS Before In Python

Amir Ali Hashemi
1 min readJul 18, 2023

Today I learned something new about floats in Python

Did you know that Python has its very own special syntax for defining not a number?

To do this you would use float followed by parentheses and type in “nan” and this will define not a number

value = float("nan")

You can now use this and verify whether it’s a number or not by importing math and using the isnan() method

import math
value = float("nan")
print(math.isnan(value))

It will return a

True

This is because it’s not a number and something very interesting is that it is not equal to anything, not even itself

As you can see the following snippet will return false

import math
value = float("nan")
print(value = value)
False

And the other two values that you can pass into float are infinite and negative infinite

inf = float("inf")
negative_ing = float("-inf")

--

--

Amir Ali Hashemi

I'm an AI student who attempts to find simple explanations for questions and share them with others