第2周作业 #高级编程技术

3-1 姓名:将一些朋友的姓名存储在一个列表中,并将其命名为names。依次访问该列表中的每个元素,从而将每个朋友的姓名显示出来。

names = ['Tom', 'Peter', 'Alice']
for name in names:
    print(name)

3-2 问候语:继续使用练习3-1中的列表,但不打印每个朋友的姓名,而为每人打印一条消息。每条消息都包含相同的问候语,但抬头为相应朋友的姓名。

names = ['Tom', 'Peter', 'Alice']
greet = 'Nice to meet you, '
for name in names:
    print(greet + name)


3-3 自己的列表:想想你喜欢的通勤方式,如骑摩托车或开汽车,并创建一个包含多种通勤方式的列表。根据该列表打印一系列有关这些通勤方式的宣言,如“I would like to own a Honda motorcycle”。

commutings = ['public transport', 'carpool', 'self-driving']
declare = 'Among the ways of getting to work, I would like to choose '
for commuting in commutings:
    print(declare + commuting)


3-4 嘉宾名单:如果你可以邀请任何人一起共进晚餐(无论是在世的还是故去的),你会邀请哪些人?请创建一个列表,其中包含至少3个你想邀请的人;然后,使用这个列表打印消息,邀请这些人与你共进晚餐。

guests = ['Isaac Newton', 'Albert Einstein', 'Stephen William Hawking']
invitation = 'Would you like to have dinner with me, '
for guest in guests:
    print(invitation + guest + '?')

3-5 修改嘉宾名单

print(guests[0] + ' cannot join us.')
guests[0] = 'Max Karl Ernst Ludwig Planck'
for guest in guests:
    print(invitation + guest + '?')

3-6 添加嘉宾

print('I am lucky to find a larger dining table.')
guests.insert(0, 'Galileo Galilei')
guests.insert(2, 'Charles Robert Darwin')
guests.append('Marie Curie')
for guest in guests:
    print(invitation + guest + '?')

3-7 缩减名单

print('I would like to extend my sincere apology to my friends that I can only invite two guests.')
while len(guests) != 2:
    print('I am sorry to tell you that I cannot invite you to my dinner, ' + guests.pop())
for guest in guests:
    print(invitation + guest + '?')
del guests[0]
del guests[0]
for guest in guests:
    print(guest)

3-4 ~ 3-7 的输出结果:

 
 
Would you like to have dinner with me, Isaac Newton?
Would you like to have dinner with me, Albert Einstein?
Would you like to have dinner with me, Stephen William Hawking?
Isaac Newton cannot join us.
Would you like to have dinner with me, Max Karl Ernst Ludwig Planck?
Would you like to have dinner with me, Albert Einstein?
Would you like to have dinner with me, Stephen William Hawking?
I am lucky to find a larger dining table.
Would you like to have dinner with me, Galileo Galilei?
Would you like to have dinner with me, Max Karl Ernst Ludwig Planck?
Would you like to have dinner with me, Charles Robert Darwin?
Would you like to have dinner with me, Albert Einstein?
Would you like to have dinner with me, Stephen William Hawking?
Would you like to have dinner with me, Marie Curie?
I would like to extend my sincere apology to my friends that I can only invite two guests.
I am sorry to tell you that I cannot invite you to my dinner, Marie Curie
I am sorry to tell you that I cannot invite you to my dinner, Stephen William Hawking
I am sorry to tell you that I cannot invite you to my dinner, Albert Einstein
I am sorry to tell you that I cannot invite you to my dinner, Charles Robert Darwin
Would you like to have dinner with me, Galileo Galilei?
Would you like to have dinner with me, Max Karl Ernst Ludwig Planck?

Process finished with exit code 0


3-8 放眼世界:想出至少5个你渴望去的地方。

将这些地方存储在一个列表中,并确保其中的元素不是按字母顺序排列的。

places = ['Phuket', 'Blackpool', 'Monaco', 'The Great Wall', 'Hawaii']

按原始排列顺序打印该列表。

print(places)

使用sorted()按字母顺序打印这个列表,同时不要修改它。并在此打印该列表,核实排列顺序未变。

print(sorted (places))
print(places)

使用sorted()按与字母顺序相反的顺序打印这个列表,同时不要修改它。并在此打印该列表,核实排列顺序未变。

print(sorted(places, reverse=True))
print(places)

使用reverse()修改列表元素的排列顺序。打印该列表,核实排列顺序确实变了。

places.reverse()
print(places)

使用reverse()再次修改列表元素的排列顺序。打印该列表,核实已恢复到原来的排列顺序。

places.reverse()
print(places)

使用sort()修改该列表,使其元素按字母顺序排列。打印该列表,核实排列顺序确实变了。

places.sort()
print(places)

使用sort()修改该列表,使其元素与字母顺序相反的顺序排列。打印该列表,核实排列顺序确实变了。

places.sort(reverse=True)
print(places)

输出结果:

['Phuket', 'Blackpool', 'Monaco', 'The Great Wall', 'Hawaii']
['Blackpool', 'Hawaii', 'Monaco', 'Phuket', 'The Great Wall']
['Phuket', 'Blackpool', 'Monaco', 'The Great Wall', 'Hawaii']
['The Great Wall', 'Phuket', 'Monaco', 'Hawaii', 'Blackpool']
['Phuket', 'Blackpool', 'Monaco', 'The Great Wall', 'Hawaii']
['Hawaii', 'The Great Wall', 'Monaco', 'Blackpool', 'Phuket']
['Phuket', 'Blackpool', 'Monaco', 'The Great Wall', 'Hawaii']
['Blackpool', 'Hawaii', 'Monaco', 'Phuket', 'The Great Wall']
['The Great Wall', 'Phuket', 'Monaco', 'Hawaii', 'Blackpool']

3-9 晚餐嘉宾

print(len(guests))


4-1 比萨

pizzas = ['Pepperoni pizza', 'Hawaii pizza', 'Mexican pizza']

for pizza in pizzas:
    print(pizza)

for pizza in pizzas:
    print('I like ' + pizza)

print(pizzas[0] + 'is a pizza topped with coins of pepperoni.')
print(pizzas[1] + 'is a pizza topped with tomato sauce, cheese, pineapple, and Canadian bacon or ham.')
print(pizzas[2] + 'is a pizza made with ingredients typical of Mexican cuisine.')
print('I really love pizza!')

4-2 动物

birds = ['ostrich', 'penguin', 'kiwi']

for bird in birds:
    print(bird)

print('A ' + birds[0] + 'is a type of very large African bird with a long neck and long legs, that cannot fly but can run very fast')
print('A ' + birds[1] + 'is a type of large black and white sea bird found mainly in the Antarctic.')
print('A ' + birds[2] + 'is a type of bird that lives in New Zealand.')
print('None of these birds can fly!')


4-3 数到20:使用一个for循环打印数字1~20(含)。

numbers = list(range(1, 21))
for number in numbers:
    print(number)

4-4 一百万:创建一个列表,使其中包含数字1~1000000,再使用一个for循环将这些数字打印出来。

numbers = list(range(1, 1000001))
for number in numbers:
    print(number)

4-5 计算1~1000000的总和

numbers = list(range(1, 1000001))
print(min(numbers))
print(max(numbers))
print(sum(numbers))

输出结果:

1
1000000
500000500000

用时:非常短


4-6 奇数

numbers = list(range(1, 20, 2))
for number in numbers:
    print(number)

4-7 3的倍数

numbers = list(range(3, 31, 3))
for number in numbers:
    print(number)

4-8 立方

numbers = []
for value in range(1, 11):
    numbers.append(value ** 3)

for number in numbers:
    print(number)

4-9 列表解析

numbers = [value ** 3 for value in range(1,11)]
print(numbers)

输出结果:

[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]


4-10 切片

numbers = [value ** 3 for value in range(1,11)]
print("The first three items in the list are")
print(numbers[:3])
print("Three items from the middle of the list are")
print(numbers[int(len(numbers) / 2 - 1): int(len(numbers) / 2 + 2)])
print("The last three items in the list are")
print(numbers[-3:])

输出结果:

The first three items in the list are
[1, 8, 27]
Three items from the middle of the list are
[125, 216, 343]
The last three items in the list are
[512, 729, 1000]

4-11 你的比萨和我的比萨

pizzas = ['Pepperoni pizza', 'Hawaii pizza', 'Mexican pizza']
friend_pizzas = pizzas[:]

pizzas.append('pizza Margherita')
friend_pizzas.append('Kebabpizza')
print("My favorite pizzas are:\n", pizzas)
print("My friend's favorite pizzas are:\n", friend_pizzas)

输出结果:

My favorite pizzas are:
 ['Pepperoni pizza', 'Hawaii pizza', 'Mexican pizza', 'pizza Margherita']
My friend's favorite pizzas are:
 ['Pepperoni pizza', 'Hawaii pizza', 'Mexican pizza', 'Kebabpizza']


猜你喜欢

转载自blog.csdn.net/yyyyzxc1234/article/details/79576287
今日推荐