zsh_18
zsh_18

Reputation: 1202

How to make the code accept both int and float type values as double type? - Python

I have dataset which have mixed values, in which few values are float(e.g. 3.4) and few are int (e.g. 3.0). I want to check if all the values are double. I want that if the values are either int or float, they are accepted as double datatype.

I tried doing:


data = [3.0, 3.4, 3.2, 3.0, 3.1]

for valuedata in data:
  is_double = isinstance(valuedata, float)
  print(is_double)

The result is coming out to be FALSE, where as i want that int and float both should be accepted as double.

Thanks

Upvotes: 2

Views: 480

Answers (2)

user12842282
user12842282

Reputation:

You can see if it's an instance of numbers.Number.

>>> import numbers
>>> isinstance(1.1, numbers.Number)
True
>>> isinstance(1, numbers.Number)  
True
>>> 

Upvotes: 2

yut23
yut23

Reputation: 3064

You can check for multiple allowed types by passing a tuple to isinstance():

is_double = isinstance(valuedata, (int, float))

Also note that Python's int has unlimited precision, which could be too large to fit into a C/C++ double.

Upvotes: 0

Related Questions