操作列表
1.1遍历整个列表
格式如下:
magicians = ['alice','david','carolina']
for magician in magicians:
print(magician)
alice
david
carolina
1.1.1 在for循环中执行更多的操作
magicians = ['alice','david','carolina']
for magician in magicians:
print(magician.title()+",that was a great trick!")
Alice,that was a great trick!
David,that was a great trick!
Carolina,that was a great trick!
额,多执行了一条print语句
1.2 避免缩进错误
①忘记缩进
②忘记缩进额外的代码行
③不必要的缩进
④循环后不必要的缩进
⑤遗漏了冒号
1.3 创建数值列表
1.3.1 使用函数range()
python中函数range()可以让你生成一系列数字
for value in range(1,5):
print(value)
1
2
3
4
包左不包右,左闭右开区间
1.3.2 使用range()创建数字列表
-
要创建数字列表,可使用函数list()将range()的结果直接转换为列表
numbers = list(range(1,6)) print(numbers)
[1, 2, 3, 4, 5]
-
使用range()函数时还可以指定步长
例如:打印1~10之间的偶数
even_numbers = list(range(2,11,2)) print(even_numbers) #从2开始打印,每次加2
[2, 4, 6, 8, 10]
-
使用函数range()打印1~10整数的平方
squares = [] for value in range(1,11): square = value**2 #两个*表示乘方 square表示临时变量 squares.append(square) print(squares)
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
1.3.3 对数字列表执行简单的统计计算
max()求最大值;min()求最小值,sum()求和
digits = [1,2,3,4,5,6,7,8,9,0]
print(max(digits))
print(min(digits))
print(sum(digits))
9
0
45
1.3.4 列表解析
squares = [value**2 for value in range(1,11)] 表达式,for循环
print(squares)
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100] #结果跟上面一样
使代码更简洁
1.4 使用列表的一部分
1.4.1 切片
players = ['charles','martina','michael','florence','eli']
print(players[0:3]) #包左不包右,只包含第一个到第三个元素,也就是索引0,1,2
print(players[1:4]) #提取2~4个元素
print(players[:4]) #没有指定第一个索引将从开头开始提取
print(players[2:]) #没有指定第二个索引将从第三个元素提取到末尾
print(players[-3:]) #提取倒数第三个元素到末尾
['charles', 'martina', 'michael']
['martina', 'michael', 'florence']
['charles', 'martina', 'michael', 'florence']
['michael', 'florence', 'eli']
['michael', 'florence', 'eli']
1.4.2 遍历切片
players = ['charles','martina','michael','florence','eli']
for player in players[:3]: #提取前三个元素
print(player)
charles
martina
michael
1.4.3 复制列表
复制列表的方法:不指定开始索引和结束索引
my_foods = ['pizza','falafel','carrot cake']
friend_foods = my_foods[:]
print("my favorite foods are:")
print(my_foods)
print("\nmy friend favorite foods are:")
print(friend_foods)
my favorite foods are:
['pizza', 'falafel', 'carrot cake']
my friend favorite foods are:
['pizza', 'falafel', 'carrot cake']
1.5 元组
1.5.1 定义元组
python将不能修改的值称为不可变的,而不可变的列表称为元组
dimensions = (200,50)
print(dimensions[0])
200
跟列表一样,访问仍然需要索引,但是使用圆括号
dimensions = (200,50)
dimensions[0]=400
print(dimensions[0])
Traceback (most recent call last):
File "D:\Python\Python-project\bookTest\Demo\dimensions.py", line 2, in <module>
dimensions[0]=400
~~~~~~~~~~^^^
TypeError: 'tuple' object does not support item assignment
元组不能修改值
元组和列表的区别:
①列表的值可以修改,元组不能
②列表使用方括号,元组使用圆括号
1.5.2 遍历元组中的所有值
dimensions = (200,50)
for dimension in dimensions:
print(dimension)
200
50
遍历元组的值,跟遍历列表一样
1.5.3 修改元组的变量
虽然不能修改元组的元素,但可以给存储元组的变量赋值
dimensions = (200,50)
print("原来的元组:")
print(dimensions)
dimensions=(400,100)
print("后面的元组:")
print(dimensions)
原来的元组:
(200, 50)
后面的元组:
(400, 100)
1.6 设置代码格式
1.6.1 格式原则
易于编写,易于阅读
1.6.2 缩进
4个空格
1.6.3 行长
每行不超过80个字符
1.6.4 空行
要将程序的不同部分分开,可使用空行