Python_三元运算

条件三元运算

# 三元条件运算,如果条件为真则返回x,如果条件为假则返回y
x = 3
y = 5
ret = x if x > y else y
print(ret)    # 返回 y值

for循环三元运算

ret = [i for i in range(5)]
print(ret)

带条件的for循环三元运算

ret = [i for i in range(5) if i == 3]
print(ret)

 

posted @ 2019-09-05 19:11  码上测  阅读(170)  评论(0编辑  收藏  举报