Home  >  Article  >  Backend Development  >  Python中实现三目运算的方法

Python中实现三目运算的方法

WBOY
WBOYOriginal
2016-06-06 11:18:241396browse

C语言中三目运算符

代码如下:


   expression ?expr1:expr2;  //expression 为真则取表达式expr1的值,否则取expr2的值


python三目实现方法:

(1) expr=判断表达式 and expr1 or expr2

判断表达式为真,此时如果expr1为真则expr=expr1,为假则变成False or expr2,expr=expr2.

判断表达式为假,则演变成False or expr2,此时expr=expr2.

只要表达式expr1,expr2都为真,上述等式就等同于C中的

expr=判断表达式 ?expr1:expr2;

(2)min(x,y)=(x (3)min(x,y=x if x

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn