+ 1

Query on python challenge question

I came across this question during a challenge. What does line 3 mean? a,b = 5,0 test = a**b > 0 ans = (0,1)[test] print(ans)

19th Nov 2019, 11:45 AM
Rini
Rini - avatar
3 Réponses
+ 5
test is a boolean, if a**b > 0, then test = True, if not, test = False In this test case, a**b = 1, and 1>0, so test = True Now to line 3, (0, 1)[test] is like saying (0, 1)[True], which is similar to saying (0, 1)[1], and this returns 1 So answer = 1
19th Nov 2019, 11:59 AM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 1
Oh..so it's referring to index [1], I didn't think of that. Thanks
19th Nov 2019, 12:19 PM
Rini
Rini - avatar
0
No problem 👌
19th Nov 2019, 12:24 PM
Aymane Boukrouh
Aymane Boukrouh - avatar