+ 1

Please guide steps

print(['abc','def'][bool('spam')])

7th May 2019, 6:30 AM
Navneet
Navneet - avatar
3 Respuestas
+ 3
"spam" is a non-empty string, non-empty strings evaluate to True. bool('spam') is True (or 1 in integers). ['abc','def'][bool('spam')] is the same as ['abc','def'][1], hence it will return the second element (index 1) of the list ['abc','def'].
7th May 2019, 6:50 AM
Anna
Anna - avatar
+ 3
I made a simpler looking code that does the same: a = ['abc','def'] b = bool('spam') # b = True b = int(b) # b = 1 print (a[b])
7th May 2019, 7:07 AM
Paul
Paul - avatar