《Python编程——从入门到实践》第三章部分习题解

# 3-3
traffic_tools = ['bicycle' , 'car' , 'motorcycle']
print('I would like to own a ' + traffic_tools[0])
print('I would like to own a ' + traffic_tools[1])
print('I would like to own a ' + traffic_tools[2])

# 3-7
guests = ['Mike' , 'Wilt' , 'Bill']
print(guests[0] + ',welcome to have a dinner with me tonight!')
print(guests[1] + ',welcome to have a dinner with me tonight!')
print(guests[2] + ',welcome to have a dinner with me tonight!')
print('Sorry ' + guests[2] + ", I'm sorry to tell you that you can't eat dinner with me tonight.")
guests = ['Mike' , 'Wilt']
print(guests[0] + ',welcome to have a dinner with me tonight!')
print(guests[1] + ',welcome to have a dinner with me tonight!')
print("I'm glad to tell you all that I've found a bigger table for the dinner and I can invite more guests!")
guests.insert(0,'Shaq')
guests.insert(1,'Oscar')
guests.append('Elvin')
print(guests[0] + ',welcome to have a dinner with me tonight!')
print(guests[1] + ',welcome to have a dinner with me tonight!')
print(guests[2] + ',welcome to have a dinner with me tonight!')
print(guests[3] + ',welcome to have a dinner with me tonight!')
print(guests[4] + ',welcome to have a dinner with me tonight!')
print("I'm sorry to tell you all that I could only invite two guests.")
popped_guest = guests.pop()
print(popped_guest + ',sorry.')
popped_guest = guests.pop()
print(popped_guest + ',sorry.')
popped_guest = guests.pop()
print(popped_guest + ',sorry.')
print(guests[0] + ',welcome to have a dinner with me tonight!')
print(guests[1] + ',welcome to have a dinner with me tonight!')
del guests[0]
del guests[0]
print(guests)

# 3-10
elements = ['mountain' , 'river' , 'country' , 'city' , 'language']
elements.append('sport')
elements.insert(0,'comic')
print(elements)
del elements[6]
popped = elements.pop()
print(popped)
elements.remove('country')
print(elements)
temp = sorted(elements)
print(temp)
print(elements)
elements.sort()
print(elements)
elements.reverse()
print(elements)
print(len(elements))

你可能感兴趣的:(作业)