5-2
a = 'Abc' print("Is a == 'Abc'? I predict True") print(a == 'Abc') print("Is a.lower() == 'Abc'? I predict False") print(a.lower() == 'Abc') b = 1 c = 2 print("Is b >= c? I predict False") print(b >= c) print("Is b < c and c > 0? I predict True") print(b < c and c > 0) print("Is b < c or b <= 0? I predict True") print(b < c or b <= 0) d = 'D' e = ['A', 'B', 'C', 'D'] print("Is d in e? I predict True") print(d in e) f = 'F' print("Is f in e? I predict False") print(f in e)
5-6
age = 20 if age < 2 and age > 0: print("You are an infant.") elif age < 4: print("You are an toddler") elif age < 13: print("You are a child") elif age < 20: print("You are a teenager") elif age < 65: print("You are an adult") else: print("You are an elder")
5-7
favorite_fruits = ['apple', 'pear', 'banana'] if 'apple' in favorite_fruits: print("You really like apple") if 'banana' in favorite_fruits: print("You really like banana") if 'pear' in favorite_fruits: print("You really like pear") if 'pineapple' in favorite_fruits: print("You really like pineapple") if 'watermelon' in favorite_fruits: print("You really like watermelon")
5-9
users = ['admin', 'Algernon', 'Steve', 'George', 'Antetokounmpo'] users.clear() name = 'Algernon' if name == 'admin': print("Hello admin, would you like to see a status report?") elif name in users: print("Hello " + name + ", thank you for logging in again") else: print("We need to find some users!")
6-2
numbers = {'Curry': 30, 'Klay': 11, 'Durant': 35, 'Livingston': 34, 'Young': 6} for name, number in numbers.items(): print(name + " love the number " + str(number))
扫描二维码关注公众号,回复:
1062786 查看本文章
6-5
rivers = {'nile': 'egypt', 'changjiang river': 'china', 'mississippi river': 'america'} for river, contry in rivers.items(): print("The " + river.title() + " runs through " + contry.title()) for river in rivers.keys(): print(river.title()) for contry in rivers.values(): print(contry.title())
6-7
stu1 = {'name': 'A', 'score': 90} stu2 = {'name': 'B', 'score': 89} stu3 = {'name': 'C', 'score': 91} people = [stu1, stu2, stu3] for stu in people: print(stu['name'] + " score: " + str(stu['score']))
6-10
numbers = {'Curry': [30, 3], 'Klay': [11, 3], 'Durant': [35, 3, 2], 'Livingston': [34, 2], 'Young': [6, 2, 0]} for name, numbers in numbers.items(): print(name + " love the numbers:", end=' ') for number in numbers: print(number, end=', ') print()