每条if语句的核心都是一个值为True或False的表达式,这种表达式被称为条件测试。
要判断两个值是否不等,可结合使用惊叹号和等号(!=),其中惊叹号表示不。
条件语句中可包含各种数学比较,如小于、大于等于、大于、小于等于
用and检查多个条件,使用and时,必须所有条件都为真,则表达式为真,否则,则为假
使用or检查多个条件,关键字or,只要至少一个条件满足,则为真,全部不满足则为假。
要判断特定值是否已经包含在列表中,可使用关键字in
要判断特定值是否已经包含在列表中,可使用关键字 not in
布尔表达式是条件测试的别称
布尔表达式通常用于记录条件
在跟踪程序状态或程序中重要的条件方面,布尔值提供了一种高效的方式
If 条件:
缩进
缩进的作用与for循环中相同
else可以让你能够指定条件测试未通过时执行操作
经常需要检查超过两个的情形,可使用 if-elif-else 结构
可以使用多个elif代码块
通过结合使用if语句,可以对列表中特定的值做特殊处理;高效地管理不断变化的情形;证明代码在各种情况下都将按预期那样运行。
可以用来检查特殊元素
用来确定列表不是空的
5-1条件测试
car = 'bengchi' print("Is car == 'bengchi'? I predict True.") print(car == 'bengchi') # true print("\nIs car == 'baoma'? I predict False.") print(car == 'baoma') # true print(car.lower() == 'bengchi') # true print(car.title() == 'bengchi') # false,因为跟car.title的字母大小写有关 age1 = 18 age2 = 20 print(age1 >= 18 and age2 >= 20) # true print(age1 >= 21 and age2 >= 20) # false print(age1 >= 21 or age2 >= 20) # true print(age1 >= 21 or age2 >= 22) # false5-2更多的条件测试
car = 'Dabeng' car2 = 'baoma' print(car2 == car) # false print(car.title() == 'Dabeng') # true print(car.lower() == 'Dabeng') # false age1 = 18 age2 = 20 print(age1 == age2) # false print(age1 != age2) # true print(age1 >= 18 and age2 >= 20) # true print(age1 >= 21 and age2 >= 20) # false print(age1 >= 21 or age2 >= 20) # true print(age1 >= 21 or age2 >= 22) # false cars = ['dabeng', 'baoma', 'aodi'] print('dabeng' in cars) # true print('aodi' in cars) # true print('baoma' not in cars) # false5-3外星人颜色#1
alien_colors = ['green', 'red', 'yellow'] alien_color = 'green' if alien_color == 'green': print("恭喜你获得五个点") alien_color = 'red' if alien_color == 'green': print("恭喜你获得五个点")
5-4外星人颜色#2
alien_color = 'green' if alien_color == 'green': print("恭喜你获得五个点") if alien_color != 'green': print("恭喜你获得十个点") alien_color = 'red' if alien_color == 'green': print("恭喜你获得五个点") else: print("恭喜你获得十个点")
5-5外星人颜色#3
alien_color = 'green' if alien_color == 'green': print("恭喜你获得五个点") elif alien_color == 'yellow': print("恭喜你获得十个点") else: print("恭喜你获得十五个点") alien_color = 'yellow' if alien_color == 'green': print("恭喜你获得五个点") elif alien_color == 'yellow': print("恭喜你获得十个点") else: print("恭喜你获得十五个点") alien_color = 'red' if alien_color == 'green': print("恭喜你获得五个点") elif alien_color == 'yellow': print("恭喜你获得十个点") else: print("恭喜你获得十五个点")5-6人生的不同阶段
def func01(): # 5-6人生的不同阶段 age = input("请输入年龄:") age = int(age) if age < 2: print("他还是个婴儿。") elif age < 4: print("他正蹒跚学步") elif age < 13: print("他是儿童") elif age < 20: print("他是青少年") elif age < 65: print("他是成年人") else: print("他是老年人")5-7喜欢的水果
def func02(): # 5-7喜欢的水果 favorite_fruits = ['西瓜', '苹果', '香蕉'] if '西瓜' in favorite_fruits: print("你真的很喜欢西瓜") if '苹果' in favorite_fruits: print("你真的很喜欢苹果") if '香蕉' in favorite_fruits: print("你真的很喜欢香蕉") if '菠萝' in favorite_fruits: print("你真的很喜欢菠萝") if '荔枝' in favorite_fruits: print("你真的很喜欢荔枝")5-8以特殊方式跟管理员打招呼
def func03(): # 5-8以特殊方式跟管理员打招呼 user_names = ['admin', 'hyk', 'yjx', 'zxq', 'hx'] for user_name in user_names: if user_name == 'admin': print("Hello %s,would you like to see a status report?" % user_name) else: print("Hello %s,thank you for logging in again." % user_name)5-8处理没有用户的情形
# 5-9处理没有用户的情形 def func04(): user_names = [] if user_names: for user_name in user_names: if user_name == 'admin': print("Hello %s,would you like to see a status report?" % user_name) else: print("Hello %s,thank you for logging in again." % user_name) else: print("We need to find some users!")5-10检查用户名
# 5-10检查用户名 def func05(): current_users = ['admin', 'hyk', 'yjx', 'zxq', 'hx'] new_users = ['admin', 'hyk', 'sxh', 'cxc', 'hw'] current_users_upper = [user.upper() for user in current_users] for new_user in new_users: print(new_user) if new_user.upper() in current_users_upper: print("用户名%s已被使用,请输入其他用户名。" % new_user) else: print("用户名%s未被使用。" % new_user)
5-11序数
# 5-11序数 def func06(): numbers = range(1, 10) numbers = list(numbers) for number in numbers: if number == 1: print("%dst" % number) elif number == 2: print("%dnd" % number) else: print("%dth" % number)