zata 发表于 2017-9-26 14:04:14

python 三元操作符的分解是什么样子

small = x if (x < y and x < z) else (y if y < z else z)

zata 发表于 2017-9-26 14:07:52

small = x
if x < y and x < z:
else:
    y
    if y < z:
      else:
            z
是这样么,但是第一个if成立的话,没有结果额

$DIM 发表于 2017-9-26 14:57:56

python 三元操表达式结构如下:
为真时的结果 if 判定条件 else 为假时的结果
small = x if (x < y and x < z) else (y if y < z else z)
操作结果如下:
>>> x=1
>>> y=2
>>> z=3
>>> small = x if (x < y and x < z) else (y if y < z else z)
>>> small
1
>>> x=3
>>> y=2
>>> z=4
>>> small = x if (x < y and x < z) else (y if y < z else z)
>>> small
2

zata 发表于 2017-9-26 15:27:27

$DIM 发表于 2017-9-26 14:57
python 三元操表达式结构如下:
为真时的结果 if 判定条件 else 为假时的结果
small = x if (x < y and...

是不是 x<y and x<z 为真 small =x
         x<y and x<z 为假 且 y<z 为真 small = y
         x<y and x<z 为假 且 y<z 为假 small = z
      
刚刚python运行了,懂了      

$DIM 发表于 2017-9-26 15:46:53

zata 发表于 2017-9-26 15:27
是不是 x

{:10_256:}
页: [1]
查看完整版本: python 三元操作符的分解是什么样子