简单的if语句
age=19
if age>=10:
print("You are Old!")
简单的If-else语句
cars=['audi','bmw','subaru','toyota']
for car in cars:
if car=='bmw':
print(car.upper())
else:
print(car.title())
#结果:
Audi
BMW
Subaru
Toyota
if-elif-else结构
age=10
if age<4:
print("Your admission cost is $0.")
elif age<18:
print("Your admission cost is $5.")
else:
print("Your admission cost is $10.")
检查是否相等
car='bmw'
car=='bmw' #True
检查是否相等时区分大小写
car='Audi'
car=='audi' # False
将变量的值转为小写再比较
car='Audi'
car.lower()=='audi' #True
是否不相等
car='bwm'
if car!='audio':
print("不相等")
使用and和or检查多个条件
>>> age_1=20
>>> age_2=24
>>> age_1>=22 and age_2>=22
False
>>> age_1>=21 or age_2>=22
True
检查特定值是否不包含再列表中
cars=['audi','bmw','subaru','toyota']
car='jeep'
if car not in cars:
print("Not in cars!")
# Not in cars!
布尔表达式
can_eat=False
can_move=True