第4章--操作列表

本文介绍了Python中的迭代技巧,包括for循环遍历列表、字符串和范围,以及列表推导式。涵盖了基本的列表操作、条件语句应用和数据结构的更新。从练习4-1到4-15,逐步展示了变量赋值、条件判断和数据处理的实战例子。
部署运行你感兴趣的模型镜像
#练习 4-1:
pizzas = ['banana','coco','beef']
for pizza in pizzas:
    print(pizza)
for pizza in pizzas:
    print(f'I own a {pizza} pizza.')
print('I really like pizzas')
#练习 4-2:
#和练习4-1相同
#练习 4-3:
for digital in range(1,21):
    print(digital,end = ' ')
#练习 4-4:
#自作主张改到一百了
list_1 = list(range(1,11))
for i in list_1:
    print(i,end = ' ')
#练习 4-5:    
print(min(list_1))
print(max(list_1))    
print(sum(list_1))
#练习 4-6:    
for odd in range(1,21,2):    
    print(odd,end = ' ')
#练习 4-7:    
for number in range(3,31):
    if number%3 == 0:
        print(number,end = ' ')
#练习 4-8:    
cubic = []
for value in range(1,11):
    cubic.append(value**3)
print(cubic)
#练习 4-9:    
cubics = [value**3 for value in range(1,11)]    
print(cubics)
#练习 4-10:   
print(f'The first three items in the list are:{cubics[:3]}')   
print(f'Three items from the middle of the list are:{cubics[3:6]}')    
print(f'The last three items in the list are:{cubics[-3:]}')    
#练习 4-11:
friend_pizzas = pizzas[:]
pizzas.append('bili')
friend_pizzas.append('pili')
print('My favorite pizzas are:')
for pizza in pizzas:
    print(pizza)
print('My friend’s favorite pizzas are:')
for pizza in friend_pizzas:
    print(pizza)
#练习 4-12:
#没找到程序foods.py在哪
#练习 4-13:
old_foods = ('可乐','炸鸡','鱼丸','五花肉','羊肉串')
for food in old_foods:
    print(food)
#foods[0] = '牛肉串'
new_foods = ('可乐','炸鸡','芬达','五花肉','羊肉串')
for food in new_foods:
    print(food)
#练习 4-14:
#练习 4-15:
# I'll got it later

您可能感兴趣的与本文相关的镜像

Kotaemon

Kotaemon

AI应用

Kotaemon 是由Cinnamon 开发的开源项目,是一个RAG UI页面,主要面向DocQA的终端用户和构建自己RAG pipeline

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

-阿呆-

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值