美文网首页
Python 三元表达式

Python 三元表达式

作者: Ten_Minutes | 来源:发表于2018-04-27 16:43 被阅读62次

Python 2.5中,定义了三元表达式,简化了条件表达式:

 语法格式:

    X if C else Y

 有了三元表达式,你只需一行就能完成条件判断和赋值操作:

  x, y = 3, 4

  if x

    smaller= x

  else 

    smaller =y

在以前 Python程序员大多这样做

  smaller= (x

  smaller

  3

现在 只需一句

  smaller = x if x

  smaller

  3

相关文章

网友评论

      本文标题:Python 三元表达式

      本文链接:https://www.haomeiwen.com/subject/hucilftx.html