This post will discuss how to check whether a variable is an integer or not in Python.

1. Using isinstance() function

The standard solution to check if a given variable is an integer or not is using the isinstance() function. It returns True if the first argument is an instance of the second argument.

Download  Run Code

 
You can also use numeric abstract base classes instead of concrete classes. To check for an integer value, you can use the numbers.Integral Python class:

Download  Run Code

2. Using float.is_integer() function

If you need to consider floats with all zeros after the decimal point, consider using the float.is_integer() function. It returns True if the float instance is finite with integral value and False otherwise.

Download  Run Code

3. Using int() function

Finally, you can use the int constructor to check for integral values. The function int(x) converts the argument x to an integer. If x is already an integer or a float with integral value, then the expression int(x) == x will hold true.

Download  Run Code

That’s all about determining whether a variable is an integer or not in Python.