Python Ternary Operator Without else

后端 未结 7 858
执笔经年
执笔经年 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:15

    if : myList.append('myString')
    

    Otherwise, no. Why the need to put it on one line?

    Note that the "ternary operator" is an operator. Like any operator, it must return something, so how can you have a ternary operator without the else clause? What is it supposed to return if the condition isn't true-like?

提交回复
热议问题