Checking whether a variable is an integer or not [duplicate] – Dev

The best answers to the question “Checking whether a variable is an integer or not [duplicate]” in the category Dev.


How do I check whether a variable is an integer?


All proposed answers so far seem to miss the fact that a double (floats in python are actually doubles) can also be an integer (if it has nothing after the decimal point). I use the built-in is_integer() method on doubles to check this.

Example (to do something every xth time in a for loop):

for index in range(y): 
    # do something
    if (index/x.).is_integer():
        # do something special


You can always convert to a float before calling this method. The three possibilities:

>>> float(5).is_integer()
>>> float(5.1).is_integer()
>>> float(5.0).is_integer()

Otherwise, you could check if it is an int first like Agostino said:

def is_int(val):
    if type(val) == int:
        return True
        if val.is_integer():
            return True
            return False


If you need to do this, do

isinstance(<var>, int)

unless you are in Python 2.x in which case you want

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

Do not use type. It is almost never the right answer in Python, since it blocks all the flexibility of polymorphism. For instance, if you subclass int, your new class should register as an int, which type will not do:

class Spam(int): pass
x = Spam(0)
type(x) == int # False
isinstance(x, int) # True

This adheres to Python’s strong polymorphism: you should allow any object that behaves like an int, instead of mandating that it be one.


The classical Python mentality, though, is that it’s easier to ask forgiveness than permission. In other words, don’t check whether x is an integer; assume that it is and catch the exception results if it isn’t:

    x += 1
except TypeError:

This mentality is slowly being overtaken by the use of abstract base classes, which let you register exactly what properties your object should have (adding? multiplying? doubling?) by making it inherit from a specially-constructed class. That would be the best solution, since it will permit exactly those objects with the necessary and sufficient attributes, but you will have to read the docs on how to use it.


If you really need to check then it’s better to use abstract base classes rather than concrete classes. For an integer that would mean:

>>> import numbers
>>> isinstance(3, numbers.Integral)

This doesn’t restrict the check to just int, or just int and long, but also allows other user-defined types that behave as integers to work.


Here’s a summary of the different methods mentioned here:

  • int(x) == x
  • try x = operator.index(x)
  • isinstance(x, int)
  • isinstance(x, numbers.Integral)

and here’s how they apply to a variety of numerical types that have integer value:

Table of methods for checking whether Python numerical types are integers

You can see they aren’t 100% consistent. Fraction and Rational are conceptually the same, but one supplies a .index() method and the other doesn’t. Complex types don’t like to convert to int even if the real part is integral and imaginary part is 0.

(np.int8|16|32|64(5) means that np.int8(5), np.int32(5), etc. all behave identically)