Python学习日志 - 4.操作列表(习题解答)

4-1比萨

pizzas = ['chicken pizza', 'beef pizza', 'seafood pizza']
for pizza in pizzas:
    print(pizza)
for pizza in pizzas:
    print("I like {}".format(pizza))
print("I really love pizza")

4-2动物

pets = ['dog', 'cat', 'fish']
for pet in pets:
    print("A {} would make a great pet".format(pet))
print("Any of these animals would make a great pet")

4-3数到20

for i in range(1,20):
    print(i)

4-4一百万

li = range(1,1000000)
for dig in li:
    print(li)

4-5计算1~1000000的总和

li = range(1, 1000000)
print(min(li))
print(max(li))
print(sum(li))

4-6奇数

li = range(1,20,2)
for dig in li:
    print(dig)

4-7 3的倍数

li = range(3,30,3)
for dig in li:
    print(dig)

4-8立方

4-9立方解析

li = [value**3 for value in range(1,10)]
for dig in li:
    print(dig)

4-10切片

li = range(1, 20, 2)
print("The first three items in the list are:")
for dig in li[0,3]:
    print(dig)
print("Three items from the middle of the list are:")
for dig in li[1,4]:
    print(dig)
print("The Last three items in the list are:")
for dig in li[-3:]:
    print(dig)

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

pizzas = ['chicken pizza', 'beef pizza', 'seafood pizza']
fpizzas = pizzas
fpizzas.append('mashroom pizza')
print('my favorite pizza is:')
for pizza in pizzas:
    print(pizza)
print('my friends\' favorite pizza is:')
for pizza in fpizzas:
    print(pizza)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值