In python when you try to access True/False in list as index it will consider True=1 and False=0.
As a result when you wrote a[True]
it actually means a[1]
and a[False]
means a[0]
. To clarify this try a[-True]
it will interpret it as a[-1]
and print 9
a = [5,6,7,8,9]
print(a[True]) #prints 6
print(a[False]) #prints 5
print(a[-True]) #prints 9
1
solved How this code works in python?