第四章 操作列表

a = ['alice', 'dvaid', 'koko']
for b in a:
    print(b)

for b in a:
    print(b.title() + ' that was a  get trike.')

#创建数字列表
for value in range(1,6):
    print(value)

numbers=list(range(1,6))
print(numbers)

#指定步长,只打印偶数
enen_numbers=list(range(2,11,2))
print(enen_numbers)

#前10个整数的平方
squares=[]
for value in range(1,11):
    square=value**2
    squares.append(square)
print(squares)

#列表解析:for循环直接放在列表里边重新定义
squares=[value**2 for value in range(1,11)]
print(squares)

#求一组数中的最小值、最大值、总和
digits=[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
print(min(digits))
print(max(digits))
print(sum(digits))

#切片:其列表中的一部分
print(squares[0:3])
print(squares[:4])
print(squares[2:])
print(squares[-3:])    #[64, 81, 100]

#遍历:也就是循环列表中的一部分
players=['Kim','Tom','Amy','Freya']
print('Here are the first three players on my team:')
for player in players[:3]:
    print(player.title())

#复制列表:通过切片赋值给新的列表,原列表不变
my_foods=['Tomatoes', 'potatoes', 'watermelon', 'apples',]
friend_foods=my_foods[:]

my_foods.append('bananas')
friend_foods.append('ice crean')
print("My favorite food are:")
print(my_foods)
print("\nMy friend favorite food are:")
print(friend_foods)

#直接a列表=b列表,ab相关联,a变b变,b变a变
a=[0,1,2,3]
b=a
a.append(5)
b.append(4)
print(a)
print(b)

#元祖:不可变的列表
dimensions=(200,500,1)
print(dimensions)
print(dimensions[0])

for dimension in dimensions:
    print(dimension)

dimensions=(10,80,66)
print("\nModified dimensions:")
for dimension in dimensions:
    print(dimension)

#PEP 8代码格式规范

#4-1 比萨名称列表,for循环打印。末尾添加一行代码,指出有多喜欢比萨。
pizzas=['草莓披萨','海鲜披萨','牛肉披萨']
for pizza in pizzas:
    print("我喜欢吃"+pizza)
print("I really love pizza!\n")


#4-2 动物名称列表,for循环打印。程序末尾指出共同之处。
animals=['dog','cat','bear']
for animal in animals:
    print('A '+animal+' would make a great pet”')
print('Any of these animals would make a great pet!')

#4-3 数到20:使用一个for循环打印数字1~20(含)。
digits=[]
for digit in range(1,21):
    digits.append(digit)
print(digits)

#!!!:数字太大,不好看结果,我们用30来代替验证学习哈!
# 4-4 一百万:创建列表,for循环打印(如果输出的时间太长,按Ctrl + C 停止输出,或关闭输出窗口)。
figures=[]
for figure in range(1,31):
    figures.append(figure)
print(figures)

#4-5 计算总和:创列表,使用min()和max()核实该列表从1开始,30结束的。调用函数sum()
print(min(figures))
print(max(figures))
print(sum(figures))

# 4-6 奇数:通过给函数range()指定第三个参数来创建一个列表, 其中包含1~20的奇数;再使用一个for 循环将这些数字都打印出 来。
odd_numbers=[]
for odd_number in range(1,21,2):
    odd_numbers.append(odd_number)
    print(odd_number)
print(odd_numbers)

#4-7 3的倍数:创建一个列表,其中包含3~30内能被3整除的数字; 再使用一个for循环将这个列表中的数字都打印出来。
list=[]
for number in range(3,31,3):
    if(number%3==0):
        list.append(number)
print(list)


#4-8 立方:创建列表,包含即1~10的立方,再使用一个for循环将这些立方数都打印出来。
list=[]
for number in range(1,11):
        list.append(number**3)
print(list)

#4-9 立方解析:使用列表解析生成一个列表,其中包含前10个整数 的立方。
cublic=[n**3 for n in range(1,11)]
print(cublic)

#4-10 切片:打印消息“The first three items in the list are:” ,再使用切片来打印列表的前三个元素
#打印消息“Three items from the middle of the list are:”, 再使用切片来打印列表中间的三个元素。
# 打印消息“The last three items in the list are:”,再使用切片来打 印列表末尾的三个元素。
#方式一
a=range(0,11)
print("\nThe first three items in the list are:")
# print(a[0:3])  #range(0, 3)
for n in range(0,3):
    print(n)
print("\nThree items from the middle of the list are:")
# print(a[4:7])  #range(4, 7)
for n in range(4,7):
    print(n)
print("\nThe last three items in the list are:")
# print(a[-3:])   #range(8, 11)
for n in range(8,11):
    print(n)

#方式二
a=[0,1,2,3,4,5,6,7,8,9,10]
print("\nThe first three items in the list are:")
print(a[0:3])
print("\nThree items from the middle of the list are:")
print(a[4:7])
print("\nThe last three items in the list are:")
print(a[-3:])  #想打印最后一个元素只能用‘空’,用-1不会打印最后一个值

#4-11 你的比萨和我的比萨:通过4-1,创建比萨列表的副本,并将其存储到变量friend_pizzas中。 原来比萨列表中添加一种比萨。 在列表friend_pizzas中添加另一种比萨。
# 核实你有两个不同的列表:打印“”,再使用一个for 循环来打印第一个列表;
# 打印“My friend's favorite pizzas are:”,再使用一个for循环来打印第二个列表。核实新增的比萨被添加到了正确的列表中。
my_pizzas=['草莓披萨','海鲜披萨','牛肉披萨']
friend_pizzas=my_pizzas[:]
my_pizzas.append('蒜蓉披萨')
friend_pizzas.append('西瓜披萨')

print("My favorite pizzas are:")
for my_pizza in my_pizzas:
    print(my_pizza)
print("My friend's favorite pizzas are:")
for friend_pizza in friend_pizzas:
    print(friend_pizza)

# 4-12 使用多个循环:在本节中,为节省篇幅,程序foods.py的每个版本都没有使用for循环来打印列表。
# 请选择一个版本的foods.py, 在其中编写两个for循环,将各个食品列表都打印出来。
my_foods=['Tomatoes', 'potatoes', 'watermelon', 'apples',]
friend_foods=my_foods[:]
my_foods.append('bananas')
friend_foods.append('ice crean')
print("My favorite food are:")
for my_food in my_foods:
    print(my_food)
print("\nMy friend favorite food are:")
for friend_food in friend_foods:
    print(friend_food)

# 4-13 自助餐:自助式餐馆,只提供五种简单的食品,并将其存储在一个元组中。
# for循环打印, 尝试修改其中的一个元素,核实Python确实会拒绝你这样做。
# 调整了菜单,替换了其中两种食品。
# 请编写一个这样的代码块:给元组变量赋值,并使用一个for 循环将新元 组的每个元素都打印出来
option_foods=('Pork','ice cream', 'pizza', 'roast duck', 'milk')
for option_food in option_foods:
    print(option_food)
# option_foods[3]='eggs'----->error:'tuple' object does not support item assignment

option_foods=('Pork','ice cream', 'eggs', 'apple', 'milk')   #修改元祖只能重新
for option_food in option_foods:
    print(option_food)



#4-14 PEP 8 :请访问https://python.org/dev/peps/pep-0008/ ,阅读 PEP 8格式设置指南。当前,这些指南适用的不多,但你可以大致 浏览一下。
# 三个具体问题:
# 1、设置tab为 4 个空格
# 2、行长度小于等于80
# 3、空格


# 4-15 代码审核 :从本章编写的程序中选择三个,根据PEP 8指南对 它们进行修改。 每级缩进都使用四个空格。
# 对你使用的文本编辑器进行设置, 使其在你按Tab键时都插入四个空格;如果你还没有这样做, 现在就去做吧(有关如何设置,请参阅附录B)。
# 每行都不要超过80字符。对你使用的编辑器进行设置,使其在 第80个字符处显示一条垂直参考线。 不要在程序文件中过多地使用空行。
alice
dvaid
koko
Alice that was a  get trike.
Dvaid that was a  get trike.
Koko that was a  get trike.
1
2
3
4
5
[1, 2, 3, 4, 5]
[2, 4, 6, 8, 10]
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
1
100
385
[1, 4, 9]
[1, 4, 9, 16]
[9, 16, 25, 36, 49, 64, 81, 100]
[64, 81, 100]
Here are the first three players on my team:
Kim
Tom
Amy
My favorite food are:
['Tomatoes', 'potatoes', 'watermelon', 'apples', 'bananas']

My friend favorite food are:
['Tomatoes', 'potatoes', 'watermelon', 'apples', 'ice crean']
[0, 1, 2, 3, 5, 4]
[0, 1, 2, 3, 5, 4]
(200, 500, 1)
200
200
500
1

Modified dimensions:
10
80
66
我喜欢吃草莓披萨
我喜欢吃海鲜披萨
我喜欢吃牛肉披萨
I really love pizza!

A dog would make a great pet”
A cat would make a great pet”
A bear would make a great pet”
Any of these animals would make a great pet!
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30]
1
30
465
1
3
5
7
9
11
13
15
17
19
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
[3, 6, 9, 12, 15, 18, 21, 24, 27, 30]
[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]
[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]

The first three items in the list are:
0
1
2

Three items from the middle of the list are:
4
5
6

The last three items in the list are:
8
9
10

The first three items in the list are:
[0, 1, 2]

Three items from the middle of the list are:
[4, 5, 6]

The last three items in the list are:
[8, 9, 10]
My favorite pizzas are:
草莓披萨
海鲜披萨
牛肉披萨
蒜蓉披萨
My friend's favorite pizzas are:
草莓披萨
海鲜披萨
牛肉披萨
西瓜披萨
My favorite food are:
Tomatoes
potatoes
watermelon
apples
bananas

My friend favorite food are:
Tomatoes
potatoes
watermelon
apples
ice crean
Pork
ice cream
pizza
roast duck
milk
Pork
ice cream
eggs
apple
milk

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值