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
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