How do you check if a number is int or float in python?

Here's a piece of code that checks whether a number is an integer or not, it works for both Python 2 and Python 3.

import sys

if sys.version < '3':
    integer_types = (int, long,)
else:
    integer_types = (int,)

isinstance(yourNumber, integer_types)  # returns True if it's an integer
isinstance(yourNumber, float)  # returns True if it's a float

Notice that Python 2 has both types int and long, while Python 3 has only type int. Source.

If you want to check whether your number is a float that represents an int, do this

(isinstance(yourNumber, float) and (yourNumber).is_integer())  # True for 3.0

If you don't need to distinguish between int and float, and are ok with either, then ninjagecko's answer is the way to go

import numbers

isinstance(yourNumber, numbers.Real)

This article describes how to check if a number is an integer or a decimal in Python.

  • Check if object is int or float: isinstance()
  • Check if float is integer: is_integer()
  • Check if numeric string is integer

See the following article for how to get the fractional and integer parts.

  • Get the fractional and integer parts with math.modf() in Python

See the following article for checking if a string is a number.

  • Check if a string is numeric, alphabetic, alphanumeric, or ASCII

Check if object is int or float: isinstance()

You can get the type of an object with the built-in function type().

i = 100
f = 1.23

print(type(i))
print(type(f))
# 
# 

You can also check if an object is of a particular type with the built-in function isinstance(object, type).

  • Get and check the type of an object in Python: type(), isinstance()

print(isinstance(i, int))
# True

print(isinstance(i, float))
# False

print(isinstance(f, int))
# False

print(isinstance(f, float))
# True

In this case, since only the type is checked, you cannot check if the value of float is an integer (the fractional part is 0).

f_i = 100.0

print(type(f_i))
# 

print(isinstance(f_i, int))
# False

print(isinstance(f_i, float))
# True

Check if float is integer: is_integer()

float has is_integer() method that returns True if the value is an integer, and False otherwise.

  • Built-in Types - float.is_integer — Python 3.10.5 documentation

f = 1.23

print(f.is_integer())
# False

f_i = 100.0

print(f_i.is_integer())
# True

For example, a function that returns True for an integer number (int or integer float) can be defined as follows. This function returns False for str.

def is_integer_num(n):
    if isinstance(n, int):
        return True
    if isinstance(n, float):
        return n.is_integer()
    return False

print(is_integer_num(100))
# True

print(is_integer_num(1.23))
# False

print(is_integer_num(100.0))
# True

print(is_integer_num('100'))
# False

Check if numeric string is integer

If you want to determine that the string of integer numbers is also an integer, the following function can be used.

If possible, the value is converted to float with float(), then is_integer() method is called, and the result is returned.

def is_integer(n):
    try:
        float(n)
    except ValueError:
        return False
    else:
        return float(n).is_integer()

print(is_integer(100))
# True

print(is_integer(100.0))
# True

print(is_integer(1.23))
# False

print(is_integer('100'))
# True

print(is_integer('100.0'))
# True

print(is_integer('1.23'))
# False

print(is_integer('string'))
# False

See the following articles for details on converting strings to numbers and handling exceptions with try ... except ....

  • Convert a string to a number (int, float) in Python
  • "try ... except ... else ... finally ..." in Python

How do you check if an input is a float in Python?

To check if the input string is an integer number, convert the user input to the integer type using the int() constructor. To check if the input is a float number, convert the user input to the float type using the float() constructor.

How do you check if a number is integer or float?

Follow the steps below to solve the problem:.
Initialize a variable, say X, to store the integer value of N..
Convert the value float value of N to integer and store it in X..
Finally, check if (N – X) > 0 or not. If found to be true, then print “NO”..
Otherwise, print “YES”..

How do you check if a number is an integer in Python?

To check if the variable is an integer in Python, we will use isinstance() which will return a boolean value whether a variable is of type integer or not. After writing the above code (python check if the variable is an integer), Ones you will print ” isinstance() “ then the output will appear as a “ True ”.

How do you check if a value is float or not?

Check if the value has a type of number and is not an integer. Check if the value is not NaN . If a value is a number, is not NaN and is not an integer, then it's a float.