try-except & Boolean | Sololearn: Learn to code for FREE!

+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 ?

11/15/2019 9:31:21 AM

Friday

3 Answers

New Answer

+7

True is convertible to 1 hence you get 5 (a[1])

+3

Good question Friday , ~ swim ~ has answered the question perfectly

0

For this reason: 0 is False 1 is True So "a[True]" is "a[1]"