In [1]: a=5
In [2]: print(["is odd", "is even"][a % 2 == 0])
is odd
In [3]: [a%2 == 0]
Out[3]: [False]
What I understood is a % 2 == 0
evaluates to True
or False
.
So if it is True then that is equivalent to 1 and using list indices it's going to print 'is even'
.
I've read this and came to know bool
is an instance of int
.
So when used as in index bool
evaluates to it's equivalent number i.e 0
or 1
.
My question
Based on intuition we can know whether it's going to be an int
or bool
But how does Python know? Does it have any criteria when to use as bool
and when to use as int
? Anything from Python3 documentation would be appreiated.