5-1 条件测试:
car = 'subaru'
print("Is car == 'subaru'? I predict True.")
print(car == 'subaru')
print("\nIs car == 'audi'? I predict False.")
print(car == 'audi')
print("\nIs car == 'A'? I predict False.")
print(car == 'A')
print("\nIs car == 'B'? I predict False.")
print(car == 'B')
print("\nIs car == 'C'? I predict False.")
print(car == 'C')
print("\nIs car == 'D'? I predict False.")
print(car == 'D')
print("\nIs car == 'E'? I predict False.")
print(car == 'E')
print("\nIs car == 'F'? I predict False.")
print(car == 'F')
print("\nIs car == 'G'? I predict False.")
print(car == 'G')
print("\nIs car == 'H'? I predict False.")
print(car == 'H')
运行结果:
5-2 更多的条件测试:
# 检查两个字符串相等和不等
c = "abc"
d = "Abc"
print("检查两个字符串相等和不等")
print(c == d)
print(c != d)
# 使用函数lower()的测试
print("\n使用函数lower()的测试")
print(c.lower() == d.lower())
print(c.lower() != d.lower())
# 检查两个数字相等、不等、大于、小于、大于等于和小于等于
a = 1
b = 1
print("\n检查两个数字相等、不等、大于、小于、大于等于和小于等于")
print(a == b)
print(a != b)
print(a > b)
print(a < b)
print(a >= b)
print(a <= b)
# 使用关键字and 和 or 的测试
print("\n使用关键字and 和 or 的测试")
print(a == b and c == d)
print(a == b or c == d)
# 测试特定的值是否包含在列表中
a = [1, 2, 3, 4]
print("\n测试特定的值是否包含在列表中")
print( 1 in a)
print( 5 in a)
# 测试特定的值是否未包含在列表中
print("\n测试特定的值是否未包含在列表中")
a = [1, 2, 3, 4]
print( 1 not in a)
print( 5 not in a)
运行结果:
5-4 外星人颜色#3:
alien_colors = ['green', 'yellow', 'red']
for alien_color in alien_colors:
if alien_color == 'green':
print("You get five points")
elif alien_color == 'yellow':
print("You get ten points")
else:
print("You get fifteen points")
运行结果:
5-6 人生的不同阶段:
age = 20
if age < 2:
print("He is a baby.")
elif age < 4:
print("He is toddler.")
elif age < 13:
print("He is a child.")
elif age < 20:
print("He is a teenager.")
elif age < 65:
print("He is an adult.")
else:
print("He is an elder.")
运行结果:
5-7 喜欢的水果:
favorite_fruits = ['pears', 'apples', 'bananas']
if 'strawberries' in favorite_fruits:
print("You really like strawberries!")
if 'pears' in favorite_fruits:
print("You really like pears.")
if 'apples' in favorite_fruits:
print("You really like apples!")
if 'lemons' in favorite_fruits:
print("You really like lemons!")
if 'bananas' in favorite_fruits:
print("You really like bananas!")
运行结果:
5-8 以特殊方式跟管理员打招呼:
users = ['cpp', 'hsy', 'ldw', 'lyw', 'admin']
for user in users:
if user == 'admin':
print("Hello " + user + ",would you like to see a status report?")
else:
print("Hello " + user + ",thank you for logging in again.")
运行结果:
5-9 处理没有用户的情形:
users = ['cpp', 'hsy', 'ldw', 'lyw', 'admin']
if len(users) == 0:
print("We need to find some users!")
for user in users:
if user == 'admin':
print("Hello " + user + ",would you like to see a status report?")
else:
print("Hello " + user + ",thank you for logging in again.")
while users:
del users[0]
if len(users) == 0:
print("We need to find some users!")
运行结果:
5-10 检查用户名:
new_users = ['cpp', 'hsy', 'ldw', 'lyw', 'fxn']
current_users = ['cpp', 'lyw', 'lwk', 'ygd', 'hjh']
yes_or_no = True;
for new_user in new_users:
for current_user in current_users:
if new_user.lower() == current_user.lower():
print("名字已被使用")
yes_or_no = False
break
if yes_or_no:
print("名字未被使用")
yes_or_no = True
运行结果:
5-11 序数:
numbers = list(range(1,10))
for number in numbers:
if number == 1:
print("1st")
elif number == 2:
print("2nd")
elif number == 3:
print("3st")
else:
print(str(number) + "th")
运行结果:
6-1 人:
people = {'first_name': 'siyuan', 'last_name': 'hong', 'age': 19, 'city': 'shenzhen'}
print(people)
运行结果:
6-2 喜欢的数字:
favorite_numbers ={'hsy': 3, 'lyw': 5, 'ldw': 4, 'cpp': 9, 'fxn': 5}
print(favorite_numbers)
运行结果:
6-4 词汇表2:
python_words={'for': "循环", 'in': "在", 'not': "不", 'if': "如果", 'else': "否则"}
for name, value in python_words.items():
print(name + ": " + value)
python_words['elif'] = "否则如果"
python_words['True'] = "真"
python_words['False'] = "假"
python_words['None'] = "空"
python_words['while'] = "循环"
运行结果:
6-5 河流:
river_to_country = {'nile': 'egypt', 'yellow river': 'china', 'changjiang river': 'china'}
for river, country in river_to_country.items():
print("The " + river.title() + " runs through " + country.title() + ".")
运行结果:
6-6 调查:
favorite_languages = {
'jen': 'python',
'sarah': 'c',
'edward': 'ruby',
'phil': 'python',
}
peoples = ['cpp', 'hsy', 'jen', 'sarah', 'edward', 'phil']
for people in peoples:
if people in favorite_languages.keys():
print("Thanks for " + people.title() + "'s help")
else:
print(people.title() + ", welcome to take part in survey.")
运行结果:
6-7 人:
people_1 = {'first_name': 'siyuan', 'last_name': 'hong', 'age': 19, 'city': 'shenzhen'}
people_2 = {'first_name': 'dongwei', 'last_name': 'lin', 'age': 20, 'city': 'guangzhou'}
people_3 = {'first_name': 'yingwei', 'last_name': 'li', 'age': 20, 'city': 'guangzhou'}
peoples = [people_1, people_2, people_3]
for people in peoples:
print(people)
运行结果:
6-8 喜欢的地方:
favorite_places = {
'cpp': ['shanghai', 'paris', 'lundun'],
'hsy': ['new york', 'beijing', 'shanghai'],
'ldw': ['chongqing', 'chengdu', 'xiamen'],
}
for key in favorite_places.keys():
print(key, favorite_places[key])
运行结果:
6-11 城市:
beijing = {
'country': 'china',
'population': '20,000,000',
'fact': "chinese capital",
}
shanghai = {
'country': 'china',
'population': '20,000,000',
'fact': "international country",
}
shenzhen = {
'country': 'china',
'population': '20,000,000',
'fact': "young city",
}
cities = {
'beijing': beijing,
'shanghai': shanghai,
'shenzhen': shenzhen,
}
for key,value in cities.items():
print(key.title())
for key_1, value_1 in value.items():
print(key_1+":", value_1)
print()
运行结果: