Home > Software design >  Why does python print([False, True][True]) output True
Why does python print([False, True][True]) output True

Time:03-04

Is this some kind of ternary operation?

print([False, True][True])

Why is the output True?

CodePudding user response:

Because the True is interpreted as the index location 1.

Try [1, 2, 3][True] and you will get 2.

CodePudding user response:

In this case Python translates True to 1 (and False to 0). Index 1 means that you are accessing the second element in the array which in this case is true.

So if you try print([True, False][True]) it will output False, print([True, False][False]) will output True, ...

CodePudding user response:

The numeric representation of True is 1 (0 for False)

You can try print(int(True)), it will output 1

print([False, True][True]) will then be quivalent to print([False, True][1]), and then, display True

  • Related