《Python编程从入门到实践》第四章 操作列表 笔记及练习

知识点思维导图

练习


# 练习4.1
pizzas=['Chicago', 'California ', ' Thick', 'Cracker and Thin']
for pizza in pizzas:
    print(f"I like {pizza} pizza")
print('I really love pizza')
# 练习4.2
pets=['cat','dog','duck','turtle']
for pet in pets:
    print(f"A {pet} would make great pet")
print("Any of these animals would make a great pet!")
# 创建1-10的平方!!!!!!!!!!!!!!!!!简洁版本
squares=[]
for value in range(1,11) :
    squares.append(value**2)
print(squares)
# 练习4.3
for value in range(1,21):
   print(value)

# 练习4.4
numbers=range(1,1_000_001)
for number in numbers:
  print(number)
# 练习 4.5
print(min(numbers))
print(max(numbers))
print(sum(numbers))
# 练习4.6奇数
odds=range(1,21,2)
for odd in odds:
    print(odd)
# 练习4.7 3的倍数
numbers=range(3,30,3)
for number in numbers:
    print(number)
# 练习4.8 立方
cubes = [value**3 for value in range(1,11)]
print(cubes)
# 练习4.10 切片
print(f"The first three items in list are {names[0:3]}")
print(f"The items from the middle of the list are {names[1:4]}")
print(f"The last three items in list are {names[-3:]}")            # 打印末尾三个元素
# 练习4.11
pizzas=['Chicago', 'California ', ' Thick', 'Cracker and Thin']
friend_pizzas=pizzas[:]
pizzas.append('potato')
friend_pizzas.append('tomato')
print("My favorite pizzas are")
for pizza in pizzas:
    print(pizza)
print("My friend‘s favorite pizzas are")
for friends_pizza in friend_pizzas:
    print(friends_pizza)
# 练习4.13
buffets = ('water', 'chicken', 'pizza', 'beef')
for food in buffets:
    print(food)
# buffets[0]='potato'
buffets2 =  ('water', 'chicken', 'button', 'pock')
for food in buffets2:
    print(food)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值