0

in: my_tuple=('a',9,1,True,'a', [2,5])

in: my_tuple.index(1), my_tuple.index(True)

out: (2, 2)

can anyone explain what exactly is going on? Why index of the entry True and 1 are displayed the same?

Harish
  • 103
  • 4
  • 1
    It's because [1 == True](https://stackoverflow.com/q/2764017/674039) in Python. – wim Jan 12 '21 at 02:45

1 Answers1

1

Since True == 1:

>>> 1 == True
True
>>> (1, True,).index(True)
0

bool subclasses from int in Python, and in Python 3.x, you will always have False = 0 and True = 1.

>>> isinstance(True, int)
True
>>> int(True)
1
Jarvis
  • 8,494
  • 3
  • 27
  • 58