try-except & Boolean | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

try-except & Boolean

a = [4, 5, 6, 7] try: print(a[True]) except: print(a[0]) here is my question: [List a] has no elements associated with True. So, it won't trigger the condition of try. Only the condition of except will be triggered. All i know is [List a]'s first element is 4, so a[0] = 4 Why this code's output is 5 ?

15th Nov 2019, 9:31 AM
Friday
Friday - avatar
2 Answers
+ 3
Good question Friday , ~ swim ~ has answered the question perfectly
15th Nov 2019, 10:21 AM
Rik Wittkopp
Rik Wittkopp - avatar
0
For this reason: 0 is False 1 is True So "a[True]" is "a[1]"
15th Nov 2019, 2:05 PM
Mehran sanea
Mehran sanea - avatar