一.三元表达式
1.三元表达式适用于
- 条件成立返回一个值
- 条件不成立返回一个值
2.判断两个数的大小
🍔使用函数来写代码
def max2(x,y):
if x > y:
return x
else:
return y
res = max2(3,4)
print(res) # 4
🍔使用三元表达式
x = 3
y = 4
res = x if x > y else y
print(res(3,4)) # 4
🍔函数加三元表达式
def max2(x,y):
return "ok" if x > y else "no"
print(max2(3,4)) # no
二.生成式
1.列表生成式
🍔使用 "for" 循环实现
l = []
for i in range(6):
l.append(i)
print(l)