+ 1
Please guide steps
print(['abc','def'][bool('spam')])
2 Antworten
+ 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'].
+ 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])