How does Python determine if a value is true?

1

I have the following question:

>>> a = [200,100]
>>> a[True]

Output >>> 100

>>> a = [200,100]
>>> a[False]

Output >>> 200

Why does this happen? Is the first value false and the second true?

    
asked by anonymous 21.01.2018 / 19:05

1 answer

7

This is because false equals 0 and true equals 1.

Then it would be the same thing as:

a = [200,100]
a[1]
  

Output > > > 100

a = [200,100]
a[0]
  

Output > > > 200


In Python any value other than 0 automatically is true when you are going to do some logical verification, for example:

(3 == true) //true
(0 == true) //false
    
21.01.2018 / 19:14