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!!

22nd Feb 2019, 12:33 AM
alexsandro deitos
alexsandro deitos - avatar
5 Respostas
+ 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!
22nd Feb 2019, 12:40 AM
Schindlabua
Schindlabua - avatar
+ 6
True & False are subset of integer set. True==1, False==0 so a[True] is a[1]
18th Mar 2019, 8:48 PM
Janusz Bujak đŸ‡”đŸ‡± đŸ‡ș🇩
Janusz Bujak đŸ‡”đŸ‡± đŸ‡ș🇩 - avatar
+ 5
Maybe your keyboard did this or something but take note that the try and except must be in lowercase.
22nd Feb 2019, 12:59 AM
👑 Prometheus 🇾🇬
👑 Prometheus 🇾🇬 - avatar
+ 2
thanks a lot brother! it’s really cleary now!
22nd Feb 2019, 12:46 AM
alexsandro deitos
alexsandro deitos - avatar
+ 2
This code prints 5 because a[True] is a[1] The exception is not raised.
23rd Feb 2019, 4:24 AM
Vladimir {Tomsk}
Vladimir  {Tomsk} - avatar