操作列表
4.1遍历整个列表
遍历整个列表,可以使用 for 循环
magicians = ['alice', 'dadvid', 'corolina']
for magician in magicians:
print(magician)
'''
输出:
alice
dadvid
corolina
'''
4.1.1 深入研究循环
- 理解 for 循环语句的执行过程
- 推荐这样写循环,如: for cat in cats:
4.1.2 在 for 循环中执行更多操作
如何区别代码在循环内还是循环外,就是看该代码是否缩进
magicians = ['alice', 'dadvid', 'corolina']
for magician in magicians:
print(f"{magician.title()}, that was a great trick!")
print(f"I can't wait to see your next trick, {magician.title()}.\n")
'''
输出:
Alice, that was a great trick!
I can't wait to see your next trick, Alice.
Dadvid, that was a great trick!
I can't wait to see your next trick, Dadvid.
Corolina, that was a great trick!
I can't wait to see your next trick, Corolina.
'''
4.1.3 在 for 循环结束后执行一些操作
for循环结束后,需要提供总结性输出
magicians = ['alice', 'dadvid', 'corolina']
for magician in magicians:
print(f"{magician.title()}, that was a great trick!")
print(f"I can't wait to see your next trick, {magician.title()}.\n")
print("Thank you, everyone. That was a great magic show!")
'''
输出:
Alice, that was a great trick!
I can't wait to see your next trick, Alice.
Dadvid, that was a great trick!
I can't wait to see your next trick, Dadvid.
Corolina, that was a great trick!
I can't wait to see your next trick, Corolina.
Thank you, everyone. That was a great magic show!
'''
4.2 避免缩进错误
python一定要严格缩进
- 忘记缩进
- 忘记缩进额外的代码行
- 不必要的缩进
- 循环后不必要的缩进
- 遗忘了冒号
4.3创建数字列表
4.3.1 使用函数range()
函数 rang([ ,]x[, ]) 有三个参数,[ ]内为可选参数,三个参数依次为起始、结束(不包括该值)、步长,起始默认为 0 ,步长默认为 1
for i in range(1, 6, 2):
print(i)
'''
输出:
1
3
5
'''
4.3.2 使用 range() 创建数字列表
squares = []
for value in range(1, 11, 2):
print(value)
squares.append(value ** 2)
print(squares)
'''
输出:
1
3
5
7
9
[1, 9, 25, 49, 81]
'''
4.3.3 对数字列表执行简单的统计计算
digits = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
print(min(digits)) #求最小值
print(max(digits)) #求最大值
print(sum(digits)) #求和
'''
输出:
0
9
45
'''
4.3.4 列表解析
列表解析是一种高级方法,将用一行代码完成 4.3.2 代码的工作
squares = [value ** 2 for value in range(1, 11, 2)]
print(squares)
'''
输出:
[1, 9, 25, 49, 81]
'''
4.4 使用列表的一部分
简言之就是列表的切片
4.4.1切片
切片的方式为 [[x ]:[y][: z ]],三个参数与函数 range()类似,依次分别为起始位置(默认为0)、结束位置(不包括该下标,默认为取完所有值)、步长(默认为1)
除了第一个冒号不能省略,其它都可以省略
players = ['charles', 'martina', 'michael', 'florence', 'eli']
print(players[:]) #输出开始到结束
print(players[1:3]) #输出下标1、2,但不包括3
print(players[1:4:2]) #输出下标1, 3,很明显步长是2
‘’’
输出:
[‘charles’, ‘martina’, ‘michael’, ‘florence’, ‘eli’]
[‘martina’, ‘michael’]
[‘martina’, ‘florence’]
‘’’
4.4.2 遍历切片
如果要遍历列表的部分元素,可以用切片方法
players = ['charles', 'martina', 'michael', 'florence', 'eli']
print('Here are the first three players on my team:')
for player in players[:3]: #注意:下标是从 0 开始的
print(player.title())
'''
输出:
Here are the first three players on my team:
Charles
Martina
Michael
'''
4.4.3 复制列表
复制列表的语法是:
friend_foods = my_foods[:] #这里是列表的一个副本
而不是
friend_foods = my_foods #这里是将两个变量产生关联,指向同一个列表
要确定两个列表是否为同一个,可以向 my_foods 的列表添加一个新元素,然后观察 friend_food 是否发生相同变化,如果没有变化,说明复制成功,不是同一个副本;如果与 my_food 变化相同,则没有复制成功,是同一个列表
注:关于列表复制,有很多陷阱,如果想完全搞懂,建议学习下小甲鱼的深拷贝、浅拷贝
4.5 元组
元组可以理解为不可变的列表,用 圆括号() 表示
注:严格的说,元组是由逗号标志的,圆括号不是必须的
4.5.1 定义元组
定义元组后,可以用下标进行访问(也可以用切片方法访问),但不允许修改
dimensions = (200, 50)
print(dimensions[0])
print(dimensions[1])
'''
输出:
200
50
'''
4.5.2 遍历元组中的元素
和列表类似,用 for 语句遍历元组
dimensions = (200, 50)
for dimension in dimensions:
print(dimension)
'''
输出:
200
50
'''
4.5.3 修改元组变量
虽然不能修改元组得元素,但可以修改元组变量
dimensions = (200, 50) # 开始变量关联得是元组(200,50)
print('Original dimensions:')
for dimension in dimensions:
print(dimension)
dimensions = (400, 100) # 现在变量关联得是元组(400,100)
print('\nModified dimensions:')
for dimension in dimensions:
print(dimension)
'''
输出:
Original dimensions:
200
50
Modified dimensions:
400
100
'''
4.6设置代码格式
PEP8
- 使用4个空格缩进
- 每行代码长不超过72字符
- 合理使用空行
4.7 小结
略