if语句
1.简单示例
cars = ['audi','bwm','subaru','toyota'] for car in cars: if car == 'bwm': print(car.upper()) else: print(car.lower())
2.条件测试
①检查特定的值是否再列表中
baned_users = ['andrew','carolina','david'] user = 'marie' if user not in baned_users: print(user+" is not in baned_users.")
3.if-else语句
示例:
age = 19 if age >= 18: print("You are old!") else: print("You are young!")
4.if-elif-else语句
示例:
age =12 if age<12: print("Young") elif age==12: print("12 year old") else: print("you are old")