python if else 操作(笔记)
1、基础写法其实python的锁进就类似于java括号里面的内容
print("if else 操作")
temperature = 15
if temperature > 30:
print("It's warm")
print("drink water")
elif temperature > 20:
print("It's nice")
else:
print("It's cold")
print("done")
2、三元表达式
if else的简略写法 --> 演变python三元表达式
age = 22
if age >= 18:
print("Eligible")
else:
print("No eligible")
age = 22
if age >= 18:
mesage = "Eligible"
else:
mesage = "No eligible"
print(mesage)
age = 22
# python 三元表达式 为真的结果 if 表达式 else 为假的结果
mesage = "Eligible" if age >= 18 else "No eligible"
print(mesage)
3、多条件表达式写法
包含或 且 非
hight_income = True
good_credit = True
student = False
if hight_income and good_credit:
print("Eligible")
else:
print("No Eligible")
if not student:
print("Eligible")
else:
print("No Eligible")
if (hight_income or good_credit) and not student:
print("Eligible")
else:
print("No Eligible")
不错,代码记得放在代码块里~
https://t4.wodetu.cn/2023/06/15/b35ef5153df38d93acdfdacb6471bb12.png
页:
[1]