I Have NEVER Used Floats Like THIS Before In Python
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")