Python - Conditional Expression

Asked

Viewed 40 times

1

Hello,

I am 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’t understand in this case what makes the x was True or False?

Thanks for your attention.

1 answer

1


See another version without 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.

  • Putz, simple! I didn’t pay attention to True, I kept trying to understand the n of the others. Thank you Sidon.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.