if语句
Python中,if语句让你能够检查程序的当前状态,并据此采取相应的措施。
5-1
coffers = ['拿铁','美式','卡布奇诺','coco' ]
for coffer in coffers:
if coffer == '拿铁':
print(coffer)
else:
print(coffer)
5 − 2 5-2 5−2
message = 'aAshovemend'
if message.lower() != 'aashovemend':
print("hellow python")
else:
print(message)
aAshovemend
num = 22
if num == 24:
print("please try again!")
else:
num !=23 or num<=23
print("jiejinle")
jiejinle
num = ['a','s','w']
if 'a' in num:
print('duide')
if 'q' not in num:
print('you are right')
duide
you are right
request = ['a','b','d']
'a' in request
True
5 − 3 5-3 5−3
aline_color = 'green'
if aline_color == 'green':
print("恭喜你获得五点积分")
恭喜你获得五点积分
5 − 4 5-4 5−4
aline_color = 'red'
if aline_color == 'green':
print("恭喜你获得五点积分")
else:
print("恭喜你获得十点积分")
恭喜你获得十点积分
5 − 5 5-5 5−5
aline_color = 'red'
if aline_color == 'green':
print("5分")
elif aline_color == 'yello':
print("10fen")
else:
print("15fen")
15fen
5 − 6 5-6 5−6
age = 20
if age < 2:
print("婴儿")
elif age < 4:
print("学习走路阶段")
elif age < 13:
print("儿童")
elif age < 20:
print("青少年")
elif age < 65:
print("成年人")
else:
print("老人")
成年人
5 − 7 5-7 5−7
favorite_fruits = ['orange','dragon fruit','apple']
if 'orange' in favorite_fruits:
print("You really like orange")
if 'dragon fruit' in favorite_fruits:
print("You really like drage")
You really like orange
You really like drage
5 − 8 5-8 5−8
names = ['admin','reic','lucy','jone','nade']
for name in names:
if name == 'admin':#赋值和等号
print("Hello " + name.title() + '.')
else:
print("Hello " + name.title() + ",thank you for logging in again.")
Hello Admin.
Hello Reic,thank you for logging in again.
Hello Lucy,thank you for logging in again.
Hello Jone,thank you for logging in again.
Hello Nade,thank you for logging in again.
5 − 9 5-9 5−9
names = []
if names:
for nume in names:
print("have " + name)
else:
print("We need to find some users")
for name in names:
if name == 'admin':
print("Hello " + name.title() + '.')
else:
print("Hello " + name.title() + ",thank you for logging in again.")
We need to find some users
5 − 10 5-10 5−10
current_users = ['A','b','c','d','e','f','g']
new_users = ['h','i','g','A','r']
for new in new_users:
if new in current_users:
print(new + "已经被使用")
elif new.lower() == 'a':
print("yibeishiyong")
else:
print(new + "没有使用")
h没有使用
i没有使用
g已经被使用
A已经被使用
r没有使用
5 − 11 5-11 5−11
nums = list(range(1,10))
for num in nums:
if str(num) == '1':
print(str(num) + "st")
if str(num) == '2':
print(str(num) + "nd")
1st
2nd