I know I can get nan in Python by
>>>float("nan")
nan
But is there any other way of getting it without "nan"? Like in Javascript, I can do
>"something"-1
NaN
Can I do something simillar in Python? Thanks.
I know I can get nan in Python by
>>>float("nan")
nan
But is there any other way of getting it without "nan"? Like in Javascript, I can do
>"something"-1
NaN
Can I do something simillar in Python? Thanks.
If you are using Python 3.5 or later, NaN is available as the constant math.nan:
import math
print(math.nan) # nan
In earlier versions, you can get NaN only by casting it from string:
print(float('NaN')) # nan
Or by trying to do weird operations on infinity:
print(float('Inf') - float('Inf')) # nan
You can also use the numpy library. However, numpy is a big library and you shouldn't use it just for this.
import numpy as np
print(np.nan) # nan