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.

BUT

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:

try:
    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.

Answer from Katriel on Stack Overflow
Top answer
1 of 16
1433

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.

BUT

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:

try:
    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.

2 of 16
204

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:

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)

🌐
GitHub
github.com › ggml-org › whisper.cpp
GitHub - ggml-org/whisper.cpp: Port of OpenAI's Whisper model in C/C++ · GitHub
Python 3.11 is recommended.
Starred by 48.7K users
Forked by 5.4K users
Languages   C++ 53.6% | C 22.9% | Cuda 10.4% | Metal 3.1% | GLSL 2.0% | CMake 1.4%
🌐
GeeksforGeeks
geeksforgeeks.org › python › check-if-value-is-int-or-float-in-python
Check If Value Is Int or Float in Python - GeeksforGeeks
July 1, 2025 - In Python, you might want to see if a number is a whole number (integer) or a decimal (float). Python has built-in functions to make this easy. There are simple ones like type() and more advanced ones like isinstance().
🌐
Python documentation
docs.python.org › 3 › library › stdtypes.html
Built-in Types — Python 3.14.4 documentation
Appending 'j' or 'J' to a numeric ... to an integer or float to get a complex number with real and imaginary parts. The constructors int(), float(), and complex() can be used to produce numbers of a specific type. Python fully supports mixed arithmetic: when a binary arithmetic operator has operands of different built-in numeric types, the operand with the “narrower” type is widened to ...
Find elsewhere
🌐
Note.nkmk.me
note.nkmk.me › home › python
Check If a Number Is an Integer in Python | note.nkmk.me
April 23, 2025 - The float type provides an is_integer() method, which returns True if the value is an integer and False otherwise.
🌐
Reddit
reddit.com › r/learnpython › is_integer function trigger
r/learnpython on Reddit: is_integer function trigger
October 20, 2022 -

how to trigger or use is_integer function without using the float inside the parenthesis? or is there more pretty way to use this function.

def add(x: float, y: float):
    result = x + y
    if result.is_integer():
        return int(result)
    else:
        return result
🌐
PYnative
pynative.com › home › python exercises › python basic exercise for beginners: 40 coding problems with solutions
Python Basic Exercise for Beginners: 40 Coding Problems with Solutions
February 8, 2026 - Exercise Purpose: This exercise introduces the idea of “Reversing Logic.” Reversing a string is simple, but reversing an integer takes some math, like using division and modulo, or changing its type. This shows how data types can work differently. ... Use a simple “Pythonic” way str(number) to convert the number to a string and check if str_num == str_num[::-1].
🌐
Reddit
reddit.com › r/learnpython › not sure how to check if a variable is an int
r/learnpython on Reddit: Not sure how to check if a variable is an int
July 27, 2022 -

So I learned about ValueError and using the try - except function, but learned that is something exceptional that shouldn't be the bread and butter to allow the code to keep running through errors.

So this is the code I wrote, I tried other ways but didn't really work, so here it is

def number():
    user_number = int((input("Insert a number ")))
    while user_number is not int:
        user_number = int((input("Insert a correct number ")))
        print(user_number)
    else:print(user_number)


number()

Any tips on how to check wether user_number is an int? I also tried with "while ValueError is True:" and didn't work as well.

🌐
Quora
quora.com › What-is-is_integer-in-Python
What is is_integer() in Python? - Quora
Answer (1 of 6): We can convert one type of number into another. This is also known as coercion. Operations like addition, subtraction coerce integer to float implicitly (automatically), if one of the operands is float. [code]>>> 1 + 2.0 3.0 [/code]We can see above that 1 (integer) is coerced i...
🌐
W3Schools
w3schools.com › python › python_numbers.asp
Python Numbers
Python Examples Python Compiler ... Certificate Python Training ... Int, or integer, is a whole number, positive or negative, without decimals, of unlimited length....
🌐
FastAPI
fastapi.tiangolo.com › tutorial › path-params
Path Parameters - FastAPI
Notice that the value your function received (and returned) is 3, as a Python int, not a string "3". So, with that type declaration, FastAPI gives you automatic request "parsing". But if you go to the browser at http://127.0.0.1:8000/items/foo, you will see a nice HTTP error of: { "detail": [ { "type": "int_parsing", "loc": [ "path", "item_id" ], "msg": "Input should be a valid integer, unable to parse string as an integer", "input": "foo" } ] }
🌐
Mimo
mimo.org › glossary › python › integer
Python Integer: Master Numeric Data Types
Integer, or int, is a data type in Python that represents whole numbers without a decimal point.
🌐
Tutorial Teacher
tutorialsteacher.com › python › python-number-type
Python Numbers: int, float, complex (With Examples)
In Python, integers are zero, positive or negative whole numbers without a fractional part and having unlimited precision, e.g. 0, 100, -10.
🌐
Pandas
pandas.pydata.org › docs › user_guide › 10min.html
10 minutes to pandas — pandas 3.0.2 documentation
Integer slices acts similar to NumPy/Python: In [35]: df.iloc[3:5, 0:2] Out[35]: A B 2013-01-04 0.721555 -0.706771 2013-01-05 -0.424972 0.567020 · Lists of integer position locations: In [36]: df.iloc[[1, 2, 4], [0, 2]] Out[36]: A C 2013-01-02 1.212112 0.119209 2013-01-03 -0.861849 -0.494929 2013-01-05 -0.424972 0.276232 ·
🌐
Quora
quora.com › How-do-you-represent-an-integer-in-Python
How to represent an integer in Python - Quora
Python s/w developer since 2011 - published since 2015 · Author has 13.3K answers and 23.8M answer views · Updated 6y · An ‘Integer Number’ - more properly called an integer object; is an internal object of type ‘int’ - the object contains the value of the number, and references ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-sympy-is_integer-method
Python | sympy.is_integer method - GeeksforGeeks
May 13, 2022 - Syntax : sympy.is_integer Return : Return True if integer else False.
🌐
NumPy
numpy.org › doc › stable › user › absolute_beginners.html
NumPy: the absolute basics for beginners — NumPy v2.4 Manual
You might hear of a 0-D (zero-dimensional) array referred to as a “scalar”, a 1-D (one-dimensional) array as a “vector”, a 2-D (two-dimensional) array as a “matrix”, or an N-D (N-dimensional, where “N” is typically an integer greater than 2) array as a “tensor”. For clarity, it is best to avoid the mathematical terms when referring to an array because the mathematical objects with these names behave differently than arrays (e.g. “matrix” multiplication is fundamentally different from “array” multiplication), and there are other objects in the scientific Python ecosystem that have these names (e.g.