python 中if-else的多种简洁的写法
第一种:普通写法a, b, c = 1, 2, 3
if a>b:
c = a
else:
c = b
第二种:表达式c = a if a>b else b
第三种:二维列表,
利用大小判断,0和1作为索引 true 为 1 false 为0c= [b, a][a > b]
# 解读
c = [2,1][1]
第四种:利用逻辑运算符进行操作c = (a>b and a or b)
利用and 的特点,若and前位置为假则直接判断为假。
利用 or的特点,若or前位置为真则判断为真。# 从前往后找,and找假,or找真
# 前真返后,
print(1 and 2) # 2
# 前假返前
print(0 and 3) # 0
# 若x真【x】, x假,y真【y】,xy假【y】,只有前真返回前
print(1 or 2) # 1
print(0 or 2) # 2
print('' or 0) # 0