Python-三元表达式

#针对以下需求

def func(x,y):
if x > y:
return x
else:
return y
res=func(1,2)

print(res)
#2

三元表达式
#语法格式: 条件成立时要返回的值 if (条件) else 条件不成立时要返回的值
x=1
y=2
res=x if x > y else y
print(res)
#2

res=1111 if x > y else 2222
print(res)
#2222

三元表达式运用到函数里
def func1():
res1 = 1111 if x > y else 2222
return res1
r=func1()

print(r)
#2222


posted @ 2020-11-18 14:36  梁博客  阅读(132)  评论(0)    收藏  举报