Python Glossary Русский

Python 3.12+Numbers & mathfunction

int.is_integer

as a fraction

Always returns True — an integer has no fractional part. Added in 3.12 for parity with float.is_integer().

Syntax

n.is_integer()

The point is uniformity: code that accepts both int and float can call is_integer() without first checking the type. On Python 3.11 and earlier, though, int has no such method and the call raises AttributeError, so for code that must run on older versions prefer isinstance(x, int) or comparing the value with its truncated form.

Examples

print((5).is_integer())   # → True
print((-3).is_integer())  # → True
print((0).is_integer())   # → True
print((5.0).is_integer())  # → True
print((2.5).is_integer())  # → False
print([v.is_integer() for v in (7, 7.0, 7.5)])  # → [True, True, False]

See also

Open in the full glossarydocs.python.orgReport an inaccuracy