Python 三元表达式

# 针对以下需求
# def func(x,y):
#     if x > y:
#         return x
#     else:
#         return y
#
# res=func(1,2)
# print(res)

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

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


res=111111 if 'egon' == 'egon' else 2222222222
print(res)



# 应用举例
def func():
    # if 1 > 3:
    #     x=1
    # else:
    #     x=3

    x = 1 if 1 > 3 else 3
原文地址:https://www.cnblogs.com/LYPZX/p/13429824.html