Как проверить целое число в Python
x = 1/3 # insert your number here
print(x - int(x) == 0) # True if x is a whole number, False if it has decimals.
Talented Toucan
x = 1/3 # insert your number here
print(x - int(x) == 0) # True if x is a whole number, False if it has decimals.
a = 2.3
if a//1 == a/1:
# it does not have decimal values
else:
# otherwise it does
i = 100
f = 1.23
print(type(i))
print(type(f))
# <class 'int'>
# <class 'float'>
Check if object is int or float: isinstance()
Check if float is integer: is_integer()
Check if numeric string is integer:
def is_integer(n):
try:
float(n)
except ValueError:
return False
else:
return float(n).is_integer()