[Course] Advanced Computer Programming, Homework, week 2

xiaoxiao2021-02-28  51

Chapter 3Chapter 4


Chapter 3

# 3-1 姓名 names = ['Tom', 'Jerry', 'Jack'] for name in names: print(name, end=', ') Tom, Jerry, Jack, # 3-6 添加嘉宾 persons = ['Tom', 'Jerry', 'Jack'] persons.insert(0, 'QQ') persons.insert(len(persons)//2, 'HH') persons.append('PP') for person in persons: print('Welcome,' + person + '!') Welcome,QQ! Welcome,Tom! Welcome,HH! Welcome,Jerry! Welcome,Jack! Welcome,PP! # 3-7 缩减名单 print('Just 2.') while(len(persons)>2): print("Sorry,", persons[-1]) persons.pop() for person in persons: print('Welcome again,', person) del persons[:] print(persons) Just 2. Sorry, PP Sorry, Jack Sorry, Jerry Sorry, HH Welcome again, QQ Welcome again, Tom [] # 3-9 晚餐嘉宾 print(len(persons)) 0


Chapter 4

# 4-2 动物 animals = ['dog', 'cat', 'bird'] for animal in animals: print("A " + animal + " would make a great pet") print('Any of these animals would make a great pet!') A dog would make a great pet A cat would make a great pet A bird would make a great pet Any of these animals would make a great pet! # 4-3 数到20 for i in range(1,21): print(i, end=" ") 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 # 4-9 列表解析 [x**3 for x in range(1,11)] [1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]
转载请注明原文地址: https://www.6miu.com/read-2627403.html

最新回复(0)