Python - Conditional expression

0

Hello,

I'm learning Python and in my studies I came across the following situation:

>>> responses = ['Y', 'Yes', 'No', 'no', '', 'Yep']
>>> responses = [x[0].lower() if x else 'n' for x in responses]
>>> responses
['y', 'y', 'n', 'n', 'n', 'y']

I can not understand, in this case what makes x True or False ?

I thank you for your attention.

    
asked by anonymous 08.07.2017 / 19:02

1 answer

1

See another version without the listcomps:

responses = ['Y', 'Yes', 'No', 'no', '', 'Yep']
n=0
for x in responses:
    n+=1
    if x:
        print(x,n)
    else:    
        print ('o unico elemento em que x é false', n)

Y 1
Yes 2
No 3
no 4
o unico elemento em que x é false 5
Yep 6 

That is, x is false only in the element where the string is null.

    
08.07.2017 / 19:28