Python is Integer

To check if a value is an integer in Python 3 you can use the isinstance() method:

>>> isinstance(None, int)
False
>>> isinstance(3, int)
True
>>> isinstance(3.2, int)
False
>>> isinstance("a", int)
False

For Python 2 you had to do something like this:

isinstance(<var>, (int, long))

If you know you are working with a float and what to test if it’s an integer or not, you can use the is_integer() method:

>>> (3.0).is_integer()
True
>>> (3.2).is_integer()
False

Leave a Reply