微信第三方网站开发教程,W做网站,网页设计风格,去加网 wordpressCSDN 课程推荐#xff1a;《8小时Python零基础轻松入门》#xff0c;讲师齐伟#xff0c;苏州研途教育科技有限公司CTO#xff0c;苏州大学应用统计专业硕士生指导委员会委员#xff1b;已出版《跟老齐学Python#xff1a;轻松入门》《跟老齐学Python#xff1a;Django实…
CSDN 课程推荐《8小时Python零基础轻松入门》讲师齐伟苏州研途教育科技有限公司CTO苏州大学应用统计专业硕士生指导委员会委员已出版《跟老齐学Python轻松入门》《跟老齐学PythonDjango实战》、《跟老齐学Python数据分析》和《Python大学实用教程》畅销图书。Python3 基础学习笔记第三章【操作列表】目录【3.1】遍历整个列表 【3.1.1】在for循环中执行更多的操作 【3.2】range()函数 【3.2.1】对数字列表执行简单的统计计算 【3.2.2】列表解析 【3.3】使用列表的一部分 【3.3.1】切片 【3.3.2】遍历列表 【3.3.3】复制列表 【3.4】元组 【3.4.1】定义元组 【3.4.2】遍历元组中所有的值 【3.4.3 】修改元组变量 【3.1】遍历整个列表
使用 for 循环来遍历整个列表 names [alice , david , liwei]for name in names:print(name)输出结果如下
alice
david
liweifor循环让Python从列表names中取出一个名字并将其储存在变量name中最后 让Python打印前面储存到变量name中的名字对于列表中的每个名字Python都将 重复执行后两行代码将列表names中的每个名字都打印出来
【3.1.1】在for循环中执行更多的操作
在for循环中可对每个元素执行任何操作下面对前面的示例进行扩展
例一
names [alice , david , liwei]
for name in names:print(name.title() , that was a good man!)输出结果如下
Alice, that was a good man!
David, that was a good man!
Liwei, that was a good man!例二
names [alice , david , liwei]
for name in names:print(name.title() , that was a good man!)print(I cant wait to see you again, name.title() .\n)
print(Nice to meet you!)输出结果如下
Alice, that was a good man!
I cant wait to see you again,Alice.David, that was a good man!
I cant wait to see you again,David.Liwei, that was a good man!
I cant wait to see you again,Liwei.Nice to meet you!【3.2】range()函数 Python使用range()函数能够轻松地生成一系列的数字 Python3 range() 函数返回的是一个可迭代对象类型是对象而不是列表类型 所以打印的时候不会打印列表 Python3 list() 函数是对象迭代器可以把range()返回的可迭代对象转为一个列表返回的变量类型为列表 Python2 range() 函数返回的是列表 例一
for i in range(1,5):print(i)输出结果如下
1
2
3
4例二
for i in range(5):print(i)输出结果如下
0
1
2
3
4例三 list(range(5))
[0, 1, 2, 3, 4]list(range(0))
[]
list(range(0, 30, 5))
[0, 5, 10, 15, 20, 25]list(range(0, 10, 2))
[0, 2, 4, 6, 8]list(range(0, -10, -1))
[0, -1, -2, -3, -4, -5, -6, -7, -8, -9]list(range(1, 0))
[]例四
squares []
for value in range(1,11):square value ** 2squares.append(square)
print(squares)输出结果如下
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]【3.2.1】对数字列表执行简单的统计计算 digits [1, 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 0] min(digits)0max(digits)9sum(digits)45【3.2.2】列表解析
列表解析能够让比如3.2中的例四更加简化只需要一行代码就能生成这样的列表列表解析将for循环和创建新元素的代码合并成一行并自动附加新元素 squares [value ** 2 for value in range(1,11)]print(squares)在这个示例中for循环为for value in range(1,11)它将值1~10提供给表达式value ** 2 输出结果如下
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]【3.3】使用列表的一部分
处理列表的部分元素——Python称之为切片
【3.3.1】切片
list [a,b,c,d,e,f]
print(list[:]) #省略全部代表截取全部内容可以用来将一个列表拷给另一个列表
print(list[:3]) #省略起始位置的索引默认起始位置从头开始结束位置索引为2
print(list[3:]) #省略结束位置的索引默认结束位置为最后一个开始位置索引为3
print(list[1:4]) #开始位置索引为1结束位置索引为3顾头不顾尾
print(list[4:1]) #从左到右索引因此为空值
print(list[-1:-3]) #从左到右索引因此为空值
print(list[-3:-1]) #开始位置索引为倒数第三个结束位置索引为倒数第二个
print(list[1:5:2]) #开始位置索引为1结束位置索引为4间隔2
print(list[5:1:-1]) #反向取值开始位置索引为5结束位置索引为2
print(list[::-1]) #反向取值反向输出列表【3.3.2】遍历列表
players [charles , martina , michael , florence , eli]
print(Here are the first three players on my team:)
for player in players[:3]:print(player.title())输出结果如下
Here are the first three players on my team:
Charles
Martina
Michael【3.3.3】复制列表
要复制列表可以创建一个包含整个列表的切片方法是同时省略起始索引和终止索引[:]这让Python创建一个始于第一个元素终止于最后一个元素的切片即复制整个列表
my_foods [pizza , falafel , carrot cake]
friend_foods my_foods[:]
print(My favorite foods are:)
print(my_foods)
print(\nMy friends favorite foods are:)
print(friend_foods)输出结果如下
My favorite foods are:
[pizza, falafel, carrot cake]My friends favorite foods are:
[pizza, falafel, carrot cake]为核实我们的确有两个列表下面在每个列表中都添加一种食品并核实每个列表都记录了相应人员喜欢的食品
my_foods [pizza , falafel , carrot cake]
friend_foods my_foods[:]my_foods.append(cannoli)
friend_foods.append(ice cream)print(My favorite foods are:)
print(my_foods)
print(\nMy friends favorite foods are:)
print(friend_foods)输出结果如下
My favorite foods are:
[pizza, falafel, carrot cake, cannoli]My friends favorite foods are:
[pizza, falafel, carrot cake, ice cream]输出结果表明cannoli’包含在我喜欢的食品列表中而’ice cream’没有ice cream’包含在我朋友喜欢的食品中而’cannoli’没有假如我们只是简单的将my_foods赋给friend_foods就不能得到两个列表。下面是错误示例
my_foods [pizza , falafel , carrot cake]
friend_foods my_foods #错误写法my_foods.append(cannoli)
friend_foods.append(ice cream)print(My favorite foods are:)
print(my_foods)
print(\nMy friends favorite foods are:)
print(friend_foods)错误示例输出结果如下
My favorite foods are:
[pizza, falafel, carrot cake, cannoli, ice cream]My friends favorite foods are:
[pizza, falafel, carrot cake, cannoli, ice cream]【3.4】元组
Python将不能修改的值称为不可变的而不可变的列表被称为元组
【3.4.1】定义元组
元组看起来就像是列表但元组使用圆括号而不是方括号来标识定义元组后就可以使用索引来访问其元素就像访问列表元素一样
dimensions (200,50)
print(dimensions[0])
print(dimensions[1])输出结果如下
200
50如果尝试修改元组中元素的值将会导致Python返回类型错误消息由于试图修改元组的操作是被禁止的因此Python指出不能给元组的元素赋值
dimensions (200,50)
dimensions[0] 300将会报错
Traceback (most recent call last):File dimensions.py, line 2, in moduledimensions[0] 300
TypeError: tuple object does not support item assignment【3.4.2】遍历元组中所有的值
像列表一样元组也可以使用for循环来遍历元组中的所有值
例一
dimensions (200,100,50,6)
for dimension in dimensions:print(dimension)输出结果如下
200
100
50
6例二
dimensions (200,100,50,6)
for dimension in dimensions[:3]:print(dimension)输出结果如下
200
100
50【3.4.3 】修改元组变量
虽然不能修改元组元素但是可以给储存元组的变量赋值
dimensions (200,50)
print(Original dimensions:)
for dimension in dimensions:print(dimension)dimensions (400,100)
print(\nModified dimensions:)
for dimension in dimensions:print(dimension)输出结果如下
Original dimensions:
200
50Modified dimensions:
400
100我们首先定义了一个元组并将其储存的尺寸打印了出来然后将一个新元组储存到变量dimensions中打印新的尺寸相比于列表元组是更简单的数据结构。如果需要储存的一组值在程序的整个生命周期内都不变可使用元组