python ? (conditional/ternary) operator for assignments

This question already has an answer here:

  • Does Python have a ternary conditional operator? 22 answers

  • Python has such an operator:

    variable = something if condition else something_else
    

    Alternatively, although not recommended (see @karadoc's comment):

    variable = (condition and something) or something_else
    

    In older Python code, you may see the trick:

    condition and something or something_else
    

    however, this has been superseded by the vastly superior ... if ... else ... construct:

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

    上一篇: 如何写python中的if和else

    下一篇: python? (条件/三元)操作员