Use isinstance.

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

So:

Use with AI>>> 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:

Use with AI>>> 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.
Discussions

How do I check if a number is a float number or whole number?
As in if number== And then there is something on the end that can define what the number is? More on reddit.com
🌐 r/learnpython
7
2
September 6, 2018
python - check if an input is a float - Stack Overflow
I am starting learning Python and tried to build a simple calculator. I want to make my program check if number_1 and number_2 are really floats, and if they aren't, start the calculation again. Does More on stackoverflow.com
🌐 stackoverflow.com
python - function that checks if a number is a float - Stack Overflow
I currently have this how can I get it so it checks for a float and uses a while loop def get_float(): number = input('Input a decimal number ') while number != float: print('bad in... More on stackoverflow.com
🌐 stackoverflow.com
November 21, 2022
Float and int in if else statment.
I suppose you could compare value to its rounded value int(value): if value == int(value): ... By using /, value will always be a float so isinstance(value, int) won't be too useful. If you want value to stay a float, use integer division // instead. More on reddit.com
🌐 r/learnpython
10
3
September 6, 2022
🌐
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.

🌐
Finxter
blog.finxter.com › home › learn python blog › 5 best ways to check if a number is float in python
5 Best Ways to Check if a Number is Float in Python - Be on the Right Side of Change
February 16, 2024 - The expected output is a Boolean value: True if the number is a float, and False otherwise. An easy and direct way to check if a value is a float in Python is by using the built-in isinstance() function.
🌐
CodeSpeedy
codespeedy.com › home › how to check if a number is float or not in python
How to check if a number is float or not in Python - CodeSpeedy
November 25, 2019 - In python, it is very easy to check whether the number is float or not. Here are the few methods. Let’s start with type() in Python. ... In the above example, isinstance() is used to check whether the first argument is an object or ‘instance’ of the second argument or not. ... num = 34.22 if(num == float): print(‘This number is float’) else: print(‘This number is not float’)
🌐
Bobby Hadz
bobbyhadz.com › blog › python-check-if-number-is-int-or-float
Check if a number is an Integer or Float in Python | bobbyhadz
Use the `isinstance()` function to check if a number is an int or float, e.g. `if isinstance(my_num, int):`.
🌐
Programiz
programiz.com › python-programming › examples › check-string-number
Python Program to Check If a String Is a Number (Float) | Programiz
March 14, 2024 - In the function isfloat(), float() tries to convert num to float. If it is successful, then the function returns True. Else, ValueError is raised and returns False. For example, 's12' is alphanumeric, so it cannot be converted to float and False ...
🌐
Learn By Example
learnbyexample.org › how-to-check-if-a-number-is-an-integer-or-a-float-in-python
How to Check if a Number is an Integer or a Float in Python - Learn By Example
April 19, 2024 - You pass the variable you want to check to the type() function, and it returns its type. To determine if a number is an integer or float, you compare the result with int or float.
Find elsewhere
🌐
LabEx
labex.io › tutorials › python-how-to-check-if-a-variable-is-a-float-in-python-559600
How to Check If a Variable Is a Float in Python | LabEx
The type() function is a useful tool for understanding the data types you're working with in Python. In this step, you will learn how to use the isinstance() function in Python to check if a variable is of a specific type.
🌐
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.
🌐
Quora
quora.com › How-do-you-check-if-a-number-is-a-float-in-Python
How to check if a number is a float in Python - Quora
Answer (1 of 18): [code]if isinstance(maybeFloat, float): print("It is float, amazing!") else: print("Nope, it is not a float.") [/code]Although if you do look up this question online, the most common answer is “Don’t check types”. Python programmers really like hoping that their unit tes...
🌐
Reddit
reddit.com › r/learnpython › float and int in if else statment.
r/learnpython on Reddit: Float and int in if else statment.
September 6, 2022 -

Hi!

Im wondering how to check if a value is a float or int using an if else statement.

E.g

test = int(input("Add a random number!"))
value = test / 4
if ..............?
print("Hello")
else:
print("Hello2") 

I have no idea if this is even close to being the right way to do it since i just started to learn Python and have no prior experience in programming.

Can someone please show me the way to do it since I can't find the answer on the internet!!

🌐
Tutorial Reference
tutorialreference.com › home › python › examples › faq › how to check if a variable is an integer or float in python
How to Check If a Variable Is an Integer or Float in Python | Tutorial Reference
Learn how to distinguish between Python integers (int) and floats (float) using isinstance(), type(), and the is_integer() method for precise type validation.
🌐
Codecademy
codecademy.com › forum_questions › 5187c9af569b6ae7ab004fae
Did you write "if type(a) == int or float" and it's not working? See here | Codecademy
Many people have been writing the following code to check if the number entered is an integer or a float · def distance_from_zero(a): if type(a) == int or float: .... But, if they enter something that isn’t a number they get an error. The reason is that this if statement will always be True. This is because of a concept called TRUTHVALUES. When Python has an if statement it checks the TRUTHVALUES of the statements.
🌐
Medium
medium.com › @davidmill1 › python-check-if-string-is-an-integer-or-float-aa122521c99f
Python — Check If String is an Integer or Float | by David Mill | Medium
December 24, 2022 - Let us see in the below example code the use of the isdigit() method in Python. Note that this method only verifies if the input is Integer and it throws an error message when Float Value is Input.
Top answer
1 of 16
456

I would just use..

try:
    float(element)
except ValueError:
    print("Not a float")

..it's simple, and it works. Note that it will still throw OverflowError if element is e.g. 1<<1024.

Another option would be a regular expression:

import re
if re.match(r'^-?\d+(?:\.\d+)$', element) is None:
    print("Not float")
2 of 16
325

Python3 method to check for float:

def is_float(element: any) -> bool:
    #If you expect None to be passed:
    if element is None: 
        return False
    try:
        float(element)
        return True
    except ValueError:
        return False

Python2 version of the above: How do I parse a string to a float or int?

Always do unit testing. What is and is not a float may surprise you:

Command to parse                        Is it a float?  Comment
--------------------------------------  --------------- ------------
print(isfloat(""))                      False
print(isfloat("1234567"))               True 
print(isfloat("1_2_3.4"))               True        123.4, underscores ignored
print(isfloat("NaN"))                   True        nan is also float
print(isfloat("123.456"))               True
print(isfloat("123.E4"))                True
print(isfloat(".1"))                    True
print(isfloat("6.523537535629999e-07")) True
print(isfloat("6e777777"))              True        This is same as Inf
print(isfloat("-iNF"))                  True
print(isfloat("1.797693e+308"))         True
print(isfloat("infinity"))              True
print(isfloat("1,234"))                 False
print(isfloat("NULL"))                  False       case insensitive
print(isfloat("NaNananana BATMAN"))     False
print(isfloat(",1"))                    False           
print(isfloat("123.EE4"))               False           
print(isfloat("infinity and BEYOND"))   False
print(isfloat("12.34.56"))              False       Two dots not allowed.
print(isfloat("#56"))                   False
print(isfloat("56%"))                   False
print(isfloat("0E0"))                   True
print(isfloat("x86E0"))                 False
print(isfloat("86-5"))                  False
print(isfloat("True"))                  False       Boolean is not a float.   
print(isfloat(True))                    True        Boolean is a float
print(isfloat("+1e1^5"))                False
print(isfloat("+1e1"))                  True
print(isfloat("+1e1.3"))                False
print(isfloat("+1.3P1"))                False
print(isfloat("-+1"))                   False
print(isfloat("(1)"))                   False       brackets not interpreted

Sinking exceptions like this is bad, because killing canaries is bad because the float method can fail for reasons other than user input. Do not be using code like this on life critical software. Also python has been changing its contract on what unicode strings can be promoted to float so expect this behavior of this code to change on major version updates.