python homework——The second week

本文通过一系列Python代码示例展示了如何使用for循环进行列表遍历、更新及操作,包括邀请客人共进晚餐的情景模拟,以及对列表进行排序和反转的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

3-1

names=['Mike','John','Jack']

for name in names:

    print(name)

3-4

guests=['Stark','Mike','Einstein']

for guest in guests:

    print("Could you come to have a dinner with me,"+guest+"?")

3-5

print('Mike can't come')

guests[1]='Kate'

for guest in guests:

    print("Could you come to have dinner with me,"+guest+"?")

3-6

print('I have found a bigger table.')

guests.insert(0,'Mask')

guests.insert(2,'Obama')

guests.append('lening')

for guest in guests:

    print('Could you come to have dinner with me,'+guest+'?')

3-7

print('Sorry, I can only invite 2 guests')

for value in range(0,4):

    guest=guests.pop()

    print('Sorry, '+guest)

for guest in guests:

    print('You are still in the list')

del guests[0]

del guests[0]

print(guests)

3-8

places=['The Great Wall','The peking University','London']

print(places)

print(sorted(places))

print(places)

print(sorted(places,reverse=True))

print(places)

places.reverse()

print(places)

places.reverse()

print(places)

places.sort()

print(places)

places.sort(reverse=True)

print(places)

3-9

print('There are '+str(len(guests))+'guests to come to my dinner.')

4-1

pizzas=['a','b','c']

for pizza in pizzas:

    print(pizza)

for pizza in pizzas:

    print('I like '+pizza+' pizza')

print('I really love pizza')

4-2

animals=['dog','cat,'bird']

for animal in animals:

    print('A '+animal+' would make a great pet')

print('Any of these animals would make a great pet')

4-3

for num in range(1,21):

    print(num)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值