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 - type() function returns the type of the object you pass to it. It’s a quick and easy way to inspect whether a value is an int or a float. ... Explanation: type() function returns the data type of the variable.
🌐
Reddit
reddit.com › r/learnpython › how do i check for a float?
r/learnpython on Reddit: How do I check for a float?
December 17, 2022 -

while True:
while True:
print("Enter a number")
num1 = input("")
if (num1.isfloat()):
break
else:
continue
while True:
print("Enter a second number")
num2 = input("")
if (num2.isfloat()):
break
else:
continue
For context. I'm writing a super simple, two number calculator. And i wanted to write a section where it tests for a float. So when you type in a number, for number 1 (the variable is called num1), python is suppose to check if it is a float or not. And if it it, it will continue the script if it is. And ask for the number again if its not. However, I'm struggling to find the right syntax to check for a float. isdigit and numeric work, but for some reason isfloat() does not work for me. What am I typing wrong, I'm quite new to programming.

🌐
Python documentation
docs.python.org › 3 › library › functions.html
Built-in Functions — Python 3.14.4 documentation
February 27, 2026 - If the argument is a number, the constructor serves as a numeric conversion like int and float. For a general Python object x, complex(x) delegates to x.__complex__(). If __complex__() is not defined then it falls back to __float__().
🌐
Python documentation
docs.python.org › 3 › library › stdtypes.html
Built-in Types — Python 3.14.4 documentation
Also referred to as integer division. For operands of type int, the result has type int. For operands of type float, the result has type float. In general, the result is a whole integer, though the result’s type is not necessarily int.
🌐
W3Schools
w3schools.com › python › ref_func_float.asp
Python float() Function
Python Examples Python Compiler ... Python Certificate Python Training ... The float() function converts the specified value into a floating point number....
Find elsewhere
🌐
Mimo
mimo.org › glossary › python › float
Python Floats: Coding Essentials | Learn Now
The return value of such operations is always a float, ensuring precision even when the inputs are whole numbers. Floats in Python can also be converted to their hex representation using the float.hex() method, which provides a hexadecimal floating-point approximation.
🌐
Python documentation
docs.python.org › 3 › tutorial › floatingpoint.html
15. Floating-Point Arithmetic: Issues and Limitations — Python 3.14.4 documentation
On most machines today, floats are approximated using a binary fraction with the numerator using the first 53 bits starting with the most significant bit and with the denominator as a power of two. In the case of 1/10, the binary fraction is 3602879701896397 / 2 ** 55 which is close to but not exactly equal to the true value of 1/10. Many users are not aware of the approximation because of the way values are displayed. Python only prints a decimal approximation to the true decimal value of the binary approximation stored by the machine.
🌐
Simplilearn
simplilearn.com › home › resources › software development › python float() function: key concepts [with examples]
Python float() Function: Key Concepts [With Examples]
November 27, 2024 - Learn how the float() function in Python converts a specified value into a floating point number. Syntax: float(value). A key function for numeric conversions.
Address   5851 Legacy Circle, 6th Floor, Plano, TX 75024 United States
🌐
Emeritus
emeritus.org › home › blog › coding › what is a float in python and how does it benefit programmers
What is a Float in Python and How Does it Benefit Programmers
August 12, 2025 - Float is a function or reusable code in the Python programming language that converts values into floating point numbers. Floating point numbers are decimal values or fractional numbers like 133.5, 2897.11, and 3571.213, whereas real numbers ...
🌐
Python.org
discuss.python.org › typing
Clarifying the float/int/complex special case - Typing - Discussions on Python.org
May 23, 2024 - The type system has a special case where int is considered to be compatible with float, and in turn float is compatible with complex. This was added for pragmatic reasons in PEP 484. This behavior is currently specified in the typing spec as follows (Special types in annotations — typing documentation): Python’s numeric types complex, float and int are not subtypes of each other, but to support common use cases, the type system contains a straightforward shortcut: when an argument is annotate...
🌐
Real Python
realpython.com › ref › builtin-types › float
float | Python’s Built-in Data Types – Real Python
You'll explore integer, floating-point numbers, and complex numbers and see how perform calculations using Python's arithmetic operators, math functions, and number methods.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-float-type-and-its-methods
Float type and its methods in python - GeeksforGeeks
July 11, 2025 - The as_integer_ratio() method returns a tuple of two integers, whose ratio equals the float. This method is useful for precise representation of floating-point numbers as fractions, which can help avoid floating-point precision errors ...
🌐
DataCamp
campus.datacamp.com › courses › intro-to-python-for-data-science › chapter-1-python-basics
Other variable types | Python
In the previous exercise, you worked with the integer Python data type: int, or integer: a number without a fractional part. savings, with the value 100, is an example of an integer. Next to numerical data types, there are three other very common data types: float, or floating point: a number that has both an integer and fractional part, separated by a point.
🌐
W3Schools
w3schools.com › python › python_numbers.asp
Python Numbers
... x = 1 y = 35656222554887711 z = -3255522 print(type(x)) print(type(y)) print(type(z)) Try it Yourself » · Float, or "floating point number" is a number, positive or negative, containing one or more decimals.
🌐
PythonHow
pythonhow.com › how › check-if-a-string-is-a-float
Here is how to check if a string is a float in Python
def is_float(string): if string.replace(".", "").isnumeric(): return True else: return False print(is_float('1.23')) # True print(is_float('123')) # True print(is_float('1.23a')) # False ... Solve Python exercises and get instant AI feedback on your solutions.