
Check if a Python float is an integer
2.4 is a float and not an integer. 3.0 is a float and can be regarded as an integer because 3.0 is the same as 3. A Python float has the method to check if it can be an integer or not.
a = 1.5.is_integer()
b = 2.0.is_integer()
print(a) # False
print(b) # True
A float object has the is_integer()
method that returns a boolean value, true or false. If the value can be an integer, the method returns true. An integer doesn't have this method.
a = 1.is_integer()
# SyntaxError: invalid syntax
In Python, 1
is int and 1.0
is float.
Comments
Powered by Markdown