python? (条件/三元)操作员

这个问题在这里已经有了答案:

  • Python是否有三元条件运算符? 22个答案

  • Python有这样一个运算符:

    variable = something if condition else something_else
    

    或者,尽管不推荐(请参阅@ karadoc的评论):

    variable = (condition and something) or something_else
    

    在较老的Python代码中,您可能会看到这样的技巧:

    condition and something or something_else
    

    然而,这已被大大优越的... if ... else ...构造所取代:

    something if condition else something_else
    
    链接地址: http://www.djcxy.com/p/42709.html

    上一篇: python ? (conditional/ternary) operator for assignments

    下一篇: exit from STDIN from bash script when the user want to close it