0

I need to find if a number is a float or an integer in an if condition and I'm having trouble doing so in Python. Will I need to use the is_integer() method and if so how?

kavigun
  • 2,219
  • 2
  • 14
  • 33
Cdr
  • 1

2 Answers2

2

You can write your code like this:

value = 1.5

if type(value) == float:
    print("it is float")
0

isinstance is a built in method for this purpose.

my_var = 5.5    # float

if isinstance(my_var, float):
   print(f'{my_var} is a float')
elif isinstance(my_var, int):
   print(f'{my_var} is an integer')
alvrm
  • 311
  • 2
  • 9