Use isinstance.

>>> x = 12
>>> isinstance(x, int)
True
>>> y = 12.0
>>> isinstance(y, float)
True

So:

>>> if isinstance(x, int):
        print('x is a int!')

x is a int!

In case of long integers, the above won't work. So you need to do:

>>> x = 12L
>>> import numbers
>>> isinstance(x, numbers.Integral)
True
>>> isinstance(x, int)
False
Answer from user225312 on Stack Overflow
๐ŸŒ
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 - Explanation: Here, we check each variable a and b against both int and float. a is correctly identified as an integer and b as a float.
Discussions

Why do we need to differentiate between float numbers and integers?
There are many reasons: It is faster for the computer to do math with integers than with floats. Floats accumulate errors, so if you don't need them, integers are more robust. For example 0.1+0.2 != 0.3. There are situations in which floats don't make sense but integers do. For example when accessing an item in a list, my_list[3] makes sense but my_list[3.57] does not. Every programming language (except JavaScript) also separates them. Many also have more than 1 type of int and float (for different sizes). More on reddit.com
๐ŸŒ r/learnpython
9
5
August 13, 2022
python - How to check if a float value is a whole number - Stack Overflow
I could convert it to a string ... cumbersome though. Is there a simpler way? ... Btw do you mean largest integer cube? That can be computed for small values by rounding down with int(12000**(1/3))**3 ... The method was added to the float type in Python 2.6.... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Float and int function
float() and int() are mainly use to convert strings to numbers. They actually create a number from the strings. int() is also often use to create an integer number from a float number. A classic example is when you ask the user for a number. The input function always returns a string of character. To perform calculation you need a number (int or float). So you must create a number from that string. As in: string_number = input("Enter temperature in ยฐF (integer): ") temp_far = int(string_number) temp_cel = (temp_far-32) * (5/9) # the result here is a float print("Temperature in celsius (float)", temp_cel) temp_cel_int = int(temp_cel) # creates an integer number from the float print("Temperature in Celsius (integer)", temp_cel_int) Using float on strings from math import radians angle_string = input("Enter a precise angle in degrees with decimal: ") angle_float = float(angle_string) radians_float = radians(angle_float) print("That angle in degrees is",radians_float,"in radians") More on reddit.com
๐ŸŒ r/learnpython
10
5
March 25, 2023
python - ().is_integer() not working - Stack Overflow
A quick fix would be to change ... and give you a float instance on which you can call the is_integer() method like you are trying to. Do this: ... Sign up to request clarification or add additional context in comments. ... You are using Python 2.7.... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ why do we need to differentiate between float numbers and integers?
r/learnpython on Reddit: Why do we need to differentiate between float numbers and integers?
August 13, 2022 -

Hi all. Im a complete beginner learning the basics. Im curious as to why Python has two different types of numbers (3 including complex) : Floats and integers. Why cant we just use any number and if we do wanna use a decimal point, we just use a decimal point without having to indicate it as a float? What is the significance of differentiating the two? Thanks!

Top answer
1 of 7
9
There are many reasons: It is faster for the computer to do math with integers than with floats. Floats accumulate errors, so if you don't need them, integers are more robust. For example 0.1+0.2 != 0.3. There are situations in which floats don't make sense but integers do. For example when accessing an item in a list, my_list[3] makes sense but my_list[3.57] does not. Every programming language (except JavaScript) also separates them. Many also have more than 1 type of int and float (for different sizes).
2 of 7
4
we just use a decimal point without having to indicate it as a float We can? print(type(1.5)) # There are at least 2 very good reasons to have separate types, though. The first is logical. Floating point math is inexact. For example: >>> (1 - 0.1) - 0.9 0.0 >>> (1 - 0.05 - 0.05) - 0.9 -1.1102230246251565e-16 As you can see, the more calculations with make with floating point arithmetic, the more likely it is we'll accumulate inaccuracies. This does not happen with integers, and we would certainly like to have the option of avoiding this problem whenever possible. It's not possible to eliminate this problem in rational numbers without giving a fractional number unlimited storage space, which is obviously undesirable in a variety of use cases. The second is practical. The algorithms for integer arithmetic are dramatically different. Integer arithmetic uses 2's complement, and floating point arithmetic uses the IEEE 754 standard for representing decimal numbers and performing computations on them. These different representations are what allows us to preserve precision in integers. Also, integer arithmetic is much faster than floating point arithmetic. When only integer arithmetic is needed over a large amount of data, computation will be significantly faster.
๐ŸŒ
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.
Top answer
1 of 15
590

To check if a float value is a whole number, use the float.is_integer() method:

>>> (1.0).is_integer()
True
>>> (1.555).is_integer()
False

The method was added to the float type in Python 2.6.

Take into account that in Python 2, 1/3 is 0 (floor division for integer operands!), and that floating point arithmetic can be imprecise (a float is an approximation using binary fractions, not a precise real number). But adjusting your loop a little this gives:

>>> for n in range(12000, -1, -1):
...     if (n ** (1.0/3)).is_integer():
...         print n
... 
27
8
1
0

which means that anything over 3 cubed, (including 10648) was missed out due to the aforementioned imprecision:

>>> (4**3) ** (1.0/3)
3.9999999999999996
>>> 10648 ** (1.0/3)
21.999999999999996

You'd have to check for numbers close to the whole number instead, or not use float() to find your number. Like rounding down the cube root of 12000:

>>> int(12000 ** (1.0/3))
22
>>> 22 ** 3
10648

If you are using Python 3.5 or newer, you can use the math.isclose() function to see if a floating point value is within a configurable margin:

>>> from math import isclose
>>> isclose((4**3) ** (1.0/3), 4)
True
>>> isclose(10648 ** (1.0/3), 22)
True

For older versions, the naive implementation of that function (skipping error checking and ignoring infinity and NaN) as mentioned in PEP485:

def isclose(a, b, rel_tol=1e-9, abs_tol=0.0):
    return abs(a - b) <= max(rel_tol * max(abs(a), abs(b)), abs_tol)
2 of 15
73

We can use the modulo (%) operator. This tells us how many remainders we have when we divide x by y - expresses as x % y. Every whole number must divide by 1, so if there is a remainder, it must not be a whole number.

This function will return a boolean, True or False, depending on whether n is a whole number.

def is_whole(n):
    return n % 1 == 0
๐ŸŒ
Pythonhumanities
pythonhumanities.com โ€บ home โ€บ lesson 04: python integers and floats
Lesson 04: Python Integers and Floats - Python for Digital Humanities
January 4, 2021 - The way in which you create a number ... has a decimal, Python will automatically consider it a float. If it does not, it will automatically consider it an integer....
Find elsewhere
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ float and int function
r/learnpython on Reddit: Float and int function
March 25, 2023 -

Hello, please can someone explain to me when to use the float and int functions?

I mean should I use float when I am strictly dealing with decimal numbers, or when I want to convert whole numbers to decimal numbers?

pythonlearner

Top answer
1 of 4
5
float() and int() are mainly use to convert strings to numbers. They actually create a number from the strings. int() is also often use to create an integer number from a float number. A classic example is when you ask the user for a number. The input function always returns a string of character. To perform calculation you need a number (int or float). So you must create a number from that string. As in: string_number = input("Enter temperature in ยฐF (integer): ") temp_far = int(string_number) temp_cel = (temp_far-32) * (5/9) # the result here is a float print("Temperature in celsius (float)", temp_cel) temp_cel_int = int(temp_cel) # creates an integer number from the float print("Temperature in Celsius (integer)", temp_cel_int) Using float on strings from math import radians angle_string = input("Enter a precise angle in degrees with decimal: ") angle_float = float(angle_string) radians_float = radians(angle_float) print("That angle in degrees is",radians_float,"in radians")
2 of 4
1
For string conversion, use the one appropriate to the string you're trying to convert. If you care about exact equality, don't use floats. Use integers, or maybe the Decimal class. If you want to know if floats are equal ish, keep in mind that the ish is always there. Use something like math.isclose to make such checks. There is rarely (almost never?) any need to explicitly convert integers to floats (unless you're doing something with numpy or similar). Integers will be automatically treated as floats during division (NOTE: different in some other languages), multiplication, etc. If you want to convert a float to integer, you can do that via int(2.3), but keep in mind how it rounds. There are other rounding functions that may or may not be more helpful (math.ceil, math.floor, round).
๐ŸŒ
CodingNomads
codingnomads.com โ€บ python-float-python-integer
Python Float and Python Integer
That means that any number that contains a decimal point is of the type float in Python. ... In Python, ints and floats behave quite similarly in many ways, even though they are of different data types.
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_numbers.asp
Python Numbers
x = 1 # int y = 2.8 # float z = 1j # complex ยท To verify the type of any object in Python, use the type() function: print(type(x)) print(type(y)) print(type(z)) Try it Yourself ยป ยท Int, or integer, is a whole number, positive or negative, without decimals, of unlimited length.
๐ŸŒ
Quora
quora.com โ€บ What-is-is_integer-in-Python
What is is_integer() in Python? - Quora
In Python, is_integer() is a method for float objects that checks if the number has no decimal part.
๐ŸŒ
Snakify
snakify.org โ€บ integer and float numbers
Integer and float numbers - Learn Python 3 - Snakify
When we read an integer value, ... to integer using int(). When we read a floating-point number, we need to cast the string to float using float(): ... Floats with very big or very small absolute value can be written using a scientific notation.
๐ŸŒ
Codeguage
codeguage.com โ€บ v1 โ€บ courses โ€บ python โ€บ numbers-basics
Python Number Basics - int and float
You'll make mistakes, learn from them, reiterate and fix them, and this is exactly how you develop the skills to build projects.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-float-type-and-its-methods
Float type and its methods in python - GeeksforGeeks
July 11, 2025 - The p+1 means multiplying by 2^1. This format is useful for exact floating-point storage and computation. The is_integer() method checks if a float has no decimal part and returns True if it is equivalent to an integer.
๐ŸŒ
Manifoldapp
cuny.manifoldapp.org โ€บ read โ€บ how-to-code-in-python-3 โ€บ section โ€บ 517db09e-e5a2-4b02-bd3d-d5406a3b40a8
How To Convert Data Types | How To Code in Python 3 | Manifold @CUNY
In Python 3, relevant quotients are converted from integers to floats when doing division though they are not in Python 2. That is, when you divide 5 by 2, in Python 3 you will get a float for an answer (2.5):
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ numbers
Python Numbers, Type Conversion and Mathematics (With Examples)
They are defined as int, float, and complex classes in Python. int - holds signed integers of non-limited length. float - holds floating decimal points and it's accurate up to 15 decimal places. ... Integers and floating points are separated by the presence or absence of a decimal point.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ difference-between-integer-and-float-in-python
Difference Between Integer and Float in Python - GeeksforGeeks
July 23, 2025 - Integers are used to represent whole numbers without any decimal points, floats, or floating-point numbers, accommodate values with decimal places. Understanding the differences between these data types is important for effective programming ...
๐ŸŒ
Codedamn
codedamn.com โ€บ news โ€บ python
Converting Float to Int in Python: A Step-by-Step Guide
July 3, 2023 - For instance, if you're writing a program that calculates the number of people in a room, a float number like 3.5 would not make sense. Python provides a built-in function int() that can convert a float to an integer.
๐ŸŒ
Codecademy
codecademy.com โ€บ forum_questions โ€บ 507df833d018ac020000001b
Why doesn't this code work? Issue with floats vs integers? | Codecademy
This is because of something called the coercion rules that are mentioned in the Python documentation. If our answer is a floating point number, it wonโ€™t be truncated like an integer and will retain the โ€œ.5.โ€