Python Ternary Operator Without else

后端 未结 7 747
执笔经年
执笔经年 2020-11-29 00:36

Is it possible to do this on one line in Python?

if :
    myList.append(\'myString\')

I have tried the ternary operator:

相关标签:
7条回答
  • 2020-11-29 01:21

    You can do something like this:

    myList.append('myString') if <condition> else False
    

    or

    myList.append('myString') if <condition> else 0
    
    0 讨论(0)
提交回复
热议问题