0
Doubt
Hi friends, I really don’t understood why this code prints the 5... a = [4,5,6,7] Try: print(a[True]) Except: print(0) Can somebody explain this program please? Tks!!
5 Réponses
+ 7
You can't really do `a[True]` because `True` is not a number. So python will convert `True` to a number, and `True` will become `1`. `False` becomes `0`.
This is called type coercion!
+ 6
True & False are subset of integer set. True==1, False==0 so a[True] is a[1]
+ 5
Maybe your keyboard did this or something but take note that the try and except must be in lowercase.
+ 2
thanks a lot brother! it’s really cleary now!
+ 2
This code prints 5 because a[True] is a[1]
The exception is not raised.