['f', 't'][bool('spam')]
this gives result ,
't'
But I cannot understand how this sample of code works?
['f', 't'][bool('spam')]
this gives result ,
't'
But I cannot understand how this sample of code works?
Any non-empty string is evaluated to True
in a boolean context, so we have
['f', 't'][True]
The bool
type is a subtype of int
, with True
and False
basically being 1
and 0
respectively, so we end up with
['f', 't'][1]
Which should make it clear why the output is 't'
.
Similarly,
['f', 't'][bool('spam')]
will output 'f'
.