Search code examples
floating-pointpython-3.6equalityzero

How to check if a number is 0.0 ie floating point zero in Python3?


I have a list of items of different types, say

arr = [False, 0, 78, 'string', 0.0, True, None, [], ()] Now I need a function to check if 0.0 i.e. float(0) exists in the array, and return its index. How do I do it?

Note that it should ignore others values like False, 0, None, etc.

Edit: I also tried if n is 0.0 and if n is float(0) and failed. So bonus question, why does this not work?


Solution

  • This is a possible solution:

    arr = [False, 0, 78, 'string', 0.0, True, None, [], ()]
    for idx, item in enumerate(arr):
        if isinstance(item, float) and item == 0.0:
            print(idx)