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?
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?
You can write your code like this:
value = 1.5
if type(value) == float:
print("it is float")
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')