Question
教材中课后的练习,3-1到3-11,选一些写到你的博客上
Answer
'''
SYSU - [专选]高级编程技术 - Week2, Lecture1 HW
by Duan
2018.03.12
'''
'''
3-3 自己的列表:
想想你喜欢的通勤方式,如骑摩托车或开汽车,并创建一个包含多种通勤方式的列表。根据该列表打印
一系列有关这些通勤方式的宣言,如"I would like to won a Honda motorcycle."
'''
commuting = ['walk', 'run', 'ride bike']
for ways in commuting:
print("I like to", ways, "to university sometimes.")
#Output:
#I like to walk to university sometimes.
#I like to run to university sometimes.
#I like to ride bike to university sometimes.
'''
3-4 嘉宾名单:
如果你可以邀请任何人一起共进晚餐(无论是在世的还是故去的),你会邀请哪些人?请创建一个列
表,其中包含至少3个你想邀请的人;然后,使用这个列表打印消息,邀请这些人来与你共进晚餐。
'''
name_list = ['Feynman', 'Gauss', 'Landau']
for name in name_list:
print(str(name_list.index(name) + 1) + ".", "\nDear", name)
print("\tI'd like to invite you to my dinner.")
print("\t\t\t\t\tYours\n\t\t\t\t\tDuan\n")
#Output:
#1.
#Dear Feynman
# I'd like to invite you to my dinner.
# Yours
# Duan
#
#2.
#Dear Gauss
# I'd like to invite you to my dinner.
# Yours
# Duan
#
#3.
#Dear Landau
# I'd like to invite you to my dinner.
# Yours
# Duan
'''
3-7 缩减名单:
略
'''
name_list = ['Feynman', 'Gauss', 'Landau']
print("Name list:", name_list, '\n')
print("Unfortunately, Gauss says he can't come, so we have to change the list.")
name_list[1] = 'Dirichlet'
print("New name list:", name_list, '\n')
print("WE FIND A LARGER TABLE FOR DINNER!!So we can invite another 3 people.")
name_list.insert(0, 'Newton')
name_list.insert(int(len(name_list) / 2), 'Li Bai')
name_list.append('Planck')
print('New name list:', name_list, '\n')
print("Unfortunately, the new table can't arrive punctually, so only 2 guests will be invited.")
while len(name_list) > 2:
name_list.pop()
print('New name list:', name_list, '\n')
for name in name_list:
print(str(name_list.index(name) + 1) + ".", "\nDear", name)
print("\tI'd like to invite you to my dinner.")
print("\t\t\t\t\tYours\n\t\t\t\t\tDuan\n")
while len(name_list) != 0:
del name_list[0]
print("name list:", name_list)
#Output:
# Name list: ['Feynman', 'Gauss', 'Landau']
#
# Unfortunately, Gauss says he can't come, so we have to change the list.
# New name list: ['Feynman', 'Dirichlet', 'Landau']
#
# WE FIND A LARGER TABLE FOR DINNER!!So we can invite another 3 people.
# New name list: ['Newton', 'Feynman', 'Li Bai', 'Dirichlet', 'Landau', 'Planck']
#
# Unfortunately, the new table can't arrive punctually, so only 2 guests will be invited.
# New name list: ['Newton', 'Feynman']
#
# 1.
# Dear Newton
# I'd like to invite you to my dinner.
# Yours
# Duan
#
# 2.
# Dear Feynman
# I'd like to invite you to my dinner.
# Yours
# Duan
#
# name list: []
'''
3-8 放眼世界:
略
'''
destination = ['Fiji', 'Norway', 'Paris', 'Yellowstone', 'Home']
print("Original list is", destination)
print("1. sorted():\t\t\t", sorted(destination))
print("2. After sorted():\t\t", destination)
print("3. sorted(reverse = Ture):\t", sorted(destination, reverse = True))
print("4. After sorted(reverse = Ture):", destination)
destination.reverse()
print("5. reverse():\t\t\t", destination)
destination.reverse()
print("6. Again reverse():\t\t", destination)
destination.sort()
print("7. sort():\t\t\t", destination)
destination.sort(reverse = True)
print("8. sort(reverse = True):\t", destination)
#Output:
#Original list is ['Fiji', 'Norway', 'Paris', 'Yellowstone', 'Home']
# 1. sorted(): ['Fiji', 'Home', 'Norway', 'Paris', 'Yellowstone']
# 2. After sorted(): ['Fiji', 'Norway', 'Paris', 'Yellowstone', 'Home']
# 3. sorted(reverse = Ture): ['Yellowstone', 'Paris', 'Norway', 'Home', 'Fiji']
# 4. After sorted(reverse = Ture): ['Fiji', 'Norway', 'Paris', 'Yellowstone', 'Home']
# 5. reverse(): ['Home', 'Yellowstone', 'Paris', 'Norway', 'Fiji']
# 6. Again reverse(): ['Fiji', 'Norway', 'Paris', 'Yellowstone', 'Home']
# 7. sort(): ['Fiji', 'Home', 'Norway', 'Paris', 'Yellowstone']
# 8. sort(reverse = True): ['Yellowstone', 'Paris', 'Norway', 'Home', 'Fiji']