Python 三元表达式

 Python 2.5中,定义了三元表达式,简化了条件表达式:  语法格式:     X if C else Y  有了三元表达式,你只需一行就能完成条件判断和赋值操作:   x, y = 3, 4   if x<y :     smaller= x   else      smaller =y

在以前 Python程序员大多这样做   smaller= (x<y and [x] or [y])[0]   smaller   3 现在 只需一句   smaller = x if x<y else y   smaller   3

    原文作者:jiangnanandi
    原文地址: https://blog.csdn.net/jiangnanandi/article/details/3322192
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞