目录
5.1 示例
>>> cars=['audi','bmw','subaru','toyota']
>>> for car in cars:
... if car=='bwm':
... print(car.upper())
... else:
... print(car.title())
...
Audi
Bmw
Subaru
Toyota
5.2 条件测试
1.检查条件是是否相等
>>> car='bmw'
>>> car=='bmw'
True
>>> car=='toyota'
False
2.检查时区分大小写
>>> car='bmw'
>>> car=='Bmw'
False
3.检查是否不相等
>>> car='bmw'
>>> car!='toyota'
True
4.比较数字
>>> age=18
>>> age==18
True
>>> answer=17
>>> answer==42
False
5.多条件检查
and
>>> age_0=22
>>> age_1=18
>>> age_0>=21 and age_1>=21
False
or
>>> age_0>=21 or age_1>=21
True
6.检查特定值
使用关键字in
>>> foods=["mushrooms","onions","pineapple"]
>>> 'mushrooms' in foods
True
>>> 'tomato' in foods
False
7.检查不在特定值是否在列表中
使用not in
>>> dislike_food='tomato'
>>> if dislike_food not in foods:
... print("we don't have "+ dislike_food)
...
we don't have tomato
8.布尔表达式
布尔表达式要么为True要么为False
5.3 if语句
1.简单语句
>>> if age>=18:
... print("you are an adult")
...
you are an adult
2.if-else语句
>>> if age>=18:
... print("you are an adult")
... else:
... print("you are not an adult")
...
you are an adult
3.if-elif-else语句
>>> age=17
>>> if age>=18:
... print("you need cost 10yuan")
... elif age<4:
... print("you are free")
... else:
... print("5 yuan")
...
5 yuan
4.使用多个elif代码
可使用多个elif代码设定不同条件
5.省略else
if-elif结构后可没有else
6.测试多个条件
在可能有多个条件为True的情况下,不适合使用elif或else
name=['curry','lebron','kobe']
if 'curry' in name:
print('curry')
if 'lebron' in name:
print('lebron')
if 'jordon' in name:
print('jordon')
curry
lebron
5.4 列表处理
1.检查特殊元素
names=['curry','lebron','kobe']
for name in names:
if name=='curry':
print("Golden State")
else:
print("other")
Golden State
other
other
2.检查空列表
Python在列表至少包含一个元素时返回True,在列表为空时返回False。
names=['1']
if names:
print("ok")
ok
3.使用多列表
names=['curry','jordon','bosh','lebron','kobe']
teams=['kobe','wade','jordon']
for team in teams:
if team in names:
print("same player")
else:
print("only one")
same player
only one
same player
作业
1.检查两个字符串相等和不等。
>>> a='my name is liming'
>>> b='My name is liming'
>>> a==b
False
使用函数lower() 的测试。
>>> a='my name is liming'
>>> b='My name is liming'
>>> a==b.lower()
True
检查两个数字相等、不等、大于、小于、大于等于和小于等于。
>>> 3==5
False
>>> 3!=5
True
>>> 3>5
False
>>> 3<5
True
>>> 3>=3
True
>>> 3<=5
True
使用关键字and 和or 的测试.
>>> (3<=5)and(3!=5)
True
>>> (3>=5)or(3!=5)
True
测试特定的值是否包含在列表中并测试特定的值是否未包含在列表中。
>>> a=range(1,11)
>>> b=5
>>> if b in a:
... print("ok")
...
ok
>>> c=11
>>> if c not in a:
... print("yes")
...
yes
2.假设在游戏中刚射杀了一个外星人,请创建一个名为alien_color 的变量,并将其设置为'green' 、'yellow' 或'red' 。
编写一条if 语句,检查外星人是否是绿色的;如果是,就打印一条消息,指出玩家获得了5个点。
alien_color='green'
if alien_color=='green':
print("you get 5 points")
you get 5 points
3.像练习5-3那样设置外星人的颜色,并编写一个if-else 结构。
如果外星人是绿色的,就打印一条消息,指出玩家因射杀该外星人获得了5个点。
如果外星人不是绿色的,就打印一条消息,指出玩家获得了10个点。
alien_color='green'
if alien_color=='green':
print("you get 5 points")
else:
print("you get 10 points")
4.将练习5-4中的if-else 结构改为if-elif-else 结构。
如果外星人是绿色的,就打印一条消息,指出玩家获得了5个点。
如果外星人是黄色的,就打印一条消息,指出玩家获得了10个点。
如果外星人是红色的,就打印一条消息,指出玩家获得了15个点。
alien_color='red'
if alien_color=='green':
print("you get 5 points")
elif alien_color=='yellow':
print("you get 10 points")
else:
print("you get 15 points")
you get 15 points
5.设置变量age 的值,再编写一个if-elif-else 结构,根据age 的值判断处于人生的哪个阶段。
如果一个人的年龄小于2岁,就打印一条消息,指出他是婴儿。
如果一个人的年龄为2(含)~4岁,就打印一条消息,指出他正蹒跚学步。
如果一个人的年龄为4(含)~13岁,就打印一条消息,指出他是儿童。
如果一个人的年龄为13(含)~20岁,就打印一条消息,指出他是青少年。
如果一个人的年龄为20(含)~65岁,就打印一条消息,指出他是成年人。
如果一个人的年龄超过65(含)岁,就打印一条消息,指出他是老年人。
age=23
if age<2:
print("you are baby")
elif age<4:
print("you are walking")
elif age<13:
print("you are child")
elif age<20:
print("you are teenger")
elif age<65:
print("you are adult")
else:
print("you are older")
you are adult
6.创建一个列表,其中包含你喜欢的水果,再编写一系列独立的if 语句,检查列表中是否包含特定的水果。
将该列表命名为favorite_fruits ,并在其中包含三种水果。
编写5条if 语句,每条都检查某种水果是否包含在列表中,如果包含在列表中,就打印一条消息
favorite_fruits=['apple','melo','pee']
if 'apple' in favorite_fruits:
print("you like this")
if 'melo' in favorite_fruits:
print("you like this too")
you like this
you like this too
7.创建一个至少包含5个用户名的列表,且其中一个用户名为'admin' 。想象你要编写代码,在每个用户登录网站后都打印一条问候消息。遍历用户名列表,并向每位用户打印一条问候消息。 如果用户名为'admin' ,就打印一条特殊的问候消息。 否则,打印一条普通的问候消息。
names=['admin','lorry','brand','kite','caca']
for name in names:
if name=='admin':
print("hello admin")
else:
print("welcome")
hello admin
welcome
welcome
welcome
welcome
8.在为完成练习5-8编写的程序中,添加一条if 语句,检查用户名列表是否为空。 如果为空,就打印消息“We need to find some users!”。 删除列表中的所有用户名,确定将打印正确的消息。
names=[]
if names:
for name in names:
print('welcome')
else:
print('we need users')
we need users
9.按下面的说明编写一个程序,模拟网站确保每位用户的用户名都独一无二的方式。 创建一个至少包含5个用户名的列表,并将其命名为current_users 。 再创建一个包含5个用户名的列表,将其命名为new_users ,并确保其中有一两个用户名也包含在列表current_users 中。 遍历列表new_users ,对于其中的每个用户名,都检查它是否已被使用。如果是这样,就打印一条消息,指出需要输入别的用户名;否则,打印一条消息,指 出这个用户名未被使用。 确保比较时不区分大消息;换句话说,如果用户名'John' 已被使用,应拒绝用户名'JOHN' 。
current_users=['a','b','c','d','e']
new_users=['A','e','C','g','t']
for new_user in new_users:
if new_user.lower() in current_users:
print("no")
else:
print("yes")
no
no
no
yes
yes
10.序数表示位置,如1st和2nd。大多数序数都以th结尾,只有1、2和3例外。 在一个列表中存储数字1~9。 遍历这个列表。 在循环中使用一个if-elif-else 结构,以打印每个数字对应的序数。输出内容应为1st 、2nd 、3rd 、4th 、5th 、6th 、7th 、8th 和9th ,但每个序数都独占一行。
numbers=range(1,10)
for number in numbers:
if number==1:
print(str(number)+ 'st')
elif number==2:
print(str(number)+ 'nd')
elif number==3:
print(str(number)+ 'rd')
else:
print(str(number)+ 'th')
1st
2nd
3rd
4th
5th
6th
7th
8th
9th