第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']


1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看rEADME.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看rEADME.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值