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)

๐ŸŒ
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.
๐ŸŒ
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().
๐ŸŒ
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....
๐ŸŒ
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.
Find elsewhere
๐ŸŒ
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
Python (programming langu... Functions (computer scien... ... is_integer() is a method on Python float objects (float.is_integer()) that returns True if the float represents an exact integer value, and False otherwise.
๐ŸŒ
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.
๐ŸŒ
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
๐ŸŒ
NBShare
nbshare.io โ€บ notebook โ€บ 331114525 โ€บ Python-Is-Integer
Python Is Integer
Well, try except can be used to catch anything in Python. ... However above code will fail for following example. ... Let us fix the above code using try except. ... def is_integer(x): try: float(x) except ValueError: print("%s is not Integer"%x) else: if float(x).is_integer(): print("%d is Integer"%x) else: print("%f is not Integer"%x)
๐ŸŒ
LabEx
labex.io โ€บ tutorials โ€บ python-how-to-check-if-a-variable-is-an-integer-in-python-559605
How to Check If a Variable Is an Integer in Python | LabEx
Then, you'll explore the type() function to identify the data type of a variable and the isinstance() function to confirm if a variable is an integer. This will equip you with the tools to effectively work with integers and ensure you're performing ...
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ methods โ€บ built-in โ€บ int
Python int() (With Examples)
Become a certified Python programmer. Try Programiz PRO! ... The int() function converts a number or a string to its equivalent integer. # converting a floating-point number to its equivalent integer result = int(9.9) print('int(9.9):', result) # int(9.9): 9 ... # int() with an integer value print("int(123) is:", int(123)) # int() with a floating point value print("int(123.23) is:", int(123.23)) # int() with a numeric-string value print("int('123') is:", int("123"))
๐ŸŒ
Flexiple
flexiple.com โ€บ python โ€บ check-if-int-python
How to Check if a String is an Integer in Python? - Flexiple
March 21, 2024 - Use the isdigit() method in Python to check if a string is a number. This method is straightforward and returns True when all characters in the string are digits, and the string is not empty. It's particularly useful for validating positive integers.
๐ŸŒ
W3docs
w3docs.com โ€บ python
Checking whether a variable is an integer or not
In Python, you can check if a variable is an integer using the isinstance() function.
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ ref_func_int.asp
Python int() Function
Python Examples Python Compiler ... Q&A Python Bootcamp Python Certificate Python Training ... The int() function converts the specified value into an integer number....
๐ŸŒ
Quora
quora.com โ€บ How-do-you-represent-an-integer-in-Python
How to represent an integer in Python - Quora
The above statement returns True if the variable is integer else it gives false. ... Python provides a function type() which returns what is the type of the variable provided in parameter.