高级编程技术 hw week2

#3-2
names=['Nicole','Kelly','Peter']
for name in names:
    print('Hello, '+name.title())
print('')

#3-7
names=['Steven Hawking','Cate Blanchett','Chris Hemsworth']
for name in names:
    print('Please come to the dinner tonight, '+name)
print(names[0]+" can't come to the dinner.")
names[0]='JJ Lin'
for name in names:
    print('Please come to the dinner tonight, '+name)
print('I have found a larger table to accommodate more people.')
names.insert(0,'Taylor Swift')
names.insert(2,'Tom Cruise')
names.append('Brad Pitt')
for name in names:
    print('Please come to the dinner tonight, '+name)
print("I'm sorry but my table can't arrive at time, so I can only invit two people.")
while len(names)!=2:
    name=names.pop()
    print(name+", I'm so sorry I could't invit you to the dinner this time.")
for name in names:
    print('You are still invited, '+name+'!')
del names[1]
del names[0]
print(names)
print('')

#3-8
places_to_travel=['Chicago','New York','Los Angeles','Tahidi','Santa Claus Village']
print(places_to_travel)
print(sorted(places_to_travel))
print(places_to_travel)
print(sorted(places_to_travel,reverse=True))
print(places_to_travel)
places_to_travel.reverse()
print(places_to_travel)
places_to_travel.reverse()
print(places_to_travel)
places_to_travel.sort()
print(places_to_travel)
places_to_travel.sort(reverse=True)
print(places_to_travel)
print('')

#4-2
animals=['dog','cat','rabbit']
for animal in animals:
    print('I like '+animal+"! They are cute!")
print('I like these animals! They are so cute!\n')

#4-5
num=range(1,1000001)
print(min(num))
print(max(num))
print(sum(num))
print('')

#4-7
nums=[]
for num in range(3,31):
    if num%3==0:
        nums.append(num)
for num in nums:
    print(num)
print('')

#4-8
nums=[]
for value in range(1,11):
    nums.append(value**3)
for value in nums:
    print(value)
print('')

#4-10
message1='The first three items in the list are:'
print(message1)
print(message1[:3])
message2='Three items from the middle of the list are:'
print(message2)
print(message2[21:24])
message3='The last three items in the list are:'
print(message3)
print(message3[-3:])

上述程序输出结果如下:

Hello, Nicole
Hello, Kelly
Hello, Peter

Please come to the dinner tonight, Steven Hawking
Please come to the dinner tonight, Cate Blanchett
Please come to the dinner tonight, Chris Hemsworth
Steven Hawking can't come to the dinner.
Please come to the dinner tonight, JJ Lin
Please come to the dinner tonight, Cate Blanchett
Please come to the dinner tonight, Chris Hemsworth
I have found a larger table to accommodate more people.
Please come to the dinner tonight, Taylor Swift
Please come to the dinner tonight, JJ Lin
Please come to the dinner tonight, Tom Cruise
Please come to the dinner tonight, Cate Blanchett
Please come to the dinner tonight, Chris Hemsworth
Please come to the dinner tonight, Brad Pitt
I'm sorry but my table can't arrive at time, so I can only invit two people.
Brad Pitt, I'm so sorry I could't invit you to the dinner this time.
Chris Hemsworth, I'm so sorry I could't invit you to the dinner this time.
Cate Blanchett, I'm so sorry I could't invit you to the dinner this time.
Tom Cruise, I'm so sorry I could't invit you to the dinner this time.
You are still invited, Taylor Swift!
You are still invited, JJ Lin!
[]

['Chicago', 'New York', 'Los Angeles', 'Tahidi', 'Santa Claus Village']
['Chicago', 'Los Angeles', 'New York', 'Santa Claus Village', 'Tahidi']
['Chicago', 'New York', 'Los Angeles', 'Tahidi', 'Santa Claus Village']
['Tahidi', 'Santa Claus Village', 'New York', 'Los Angeles', 'Chicago']
['Chicago', 'New York', 'Los Angeles', 'Tahidi', 'Santa Claus Village']
['Santa Claus Village', 'Tahidi', 'Los Angeles', 'New York', 'Chicago']
['Chicago', 'New York', 'Los Angeles', 'Tahidi', 'Santa Claus Village']
['Chicago', 'Los Angeles', 'New York', 'Santa Claus Village', 'Tahidi']
['Tahidi', 'Santa Claus Village', 'New York', 'Los Angeles', 'Chicago']

I like dog! They are cute!
I like cat! They are cute!
I like rabbit! They are cute!
I like these animals! They are so cute!

1
1000000
500000500000

3
6
9
12
15
18
21
24
27
30

1
8
27
64
125
216
343
512
729
1000

The first three items in the list are:
The
Three items from the middle of the list are:
mid
The last three items in the list are:
re:


猜你喜欢

转载自blog.csdn.net/weixin_41792764/article/details/79575707
今日推荐