Python学习 - 语法入门:https://blog.csdn.net/wanzijy/article/details/125287855
Python学习 - 数据类型:https://blog.csdn.net/wanzijy/article/details/125341568
Python学习 - 流程控制:https://blog.csdn.net/wanzijy/article/details/125400906
Python学习 - 运算符(操作符):https://blog.csdn.net/wanzijy/article/details/125387919
Python学习 - 列表:https://blog.csdn.net/wanzijy/article/details/125457990
Python学习 - 字典:https://blog.csdn.net/wanzijy/article/details/125567225
Python学习 -元组和集合:https://blog.csdn.net/wanzijy/article/details/125577609
Python学习 - 函数(1):https://blog.csdn.net/wanzijy/article/details/125669084
Python学习 - 函数(2):https://blog.csdn.net/wanzijy/article/details/125739981
Python学习 - 类与对象:https://blog.csdn.net/wanzijy/article/details/125826952
Python学习 - 面向对象之封装:https://blog.csdn.net/wanzijy/article/details/125902925
Python学习 - 面向对象之继承:https://blog.csdn.net/wanzijy/article/details/125963829
Python学习 - 面向对象之多态:https://blog.csdn.net/wanzijy/article/details/127140632
Python - 文件操作(1):https://blog.csdn.net/wanzijy/article/details/127099071
Python - 文件操作(2):https://blog.csdn.net/wanzijy/article/details/127150189
Python学习 - 垃圾回收、特殊方法和模块:https://blog.csdn.net/wanzijy/article/details/126202999
Python学习 - 异常处理:https://blog.csdn.net/wanzijy/article/details/126803672
1. 简介
列表是Python中的一个对象
列表可以保存多个有序的数据
列表中存储的数据,称之为元素
一个列表中可以存储多个元素,也可以在创建列表时,来指定列表中的元素
列表是用来存储对象的
可以保存任意的对象
会按照插入的顺序存储到列表中
创建:
通过“[ ]”创建列表
当向列表中添加多个元素时,多个元素之间使用“,”隔开
my_list = [] # 创建了一个空列表
my_list1 = [10, 20, 30, 40 ,50]
my_list2 = [10, 'hello', None, 40.12 ,[50, 60]] # 存储任意对象
操作列表中的数据:
可以通过索引来获取列表中的元素
索引是元素在列表中的位置,列表中的每一个元素都有一个索引
索引是从0开始的整数
在Python中,列表的索引可以是负数。如果索引是负数,则从后向前获取元素,-1表示倒数第一个,-2表示倒数第二个…
如果使用的索引超过了最大的范围,会抛出异常
my_list2 = [10, 'hello', None, 40.12 ,[50, 60]]
print(my_list2[0]) # 10
print(my_list2[-1]) # [50, 60]
2. 切片
指从现有列表中,获取一个子列表
语法: 列表[起始:结束:步长]
在获取元素时,会包括起始的元素,但不包括结束位置的元素
做切片操作时,总会返回一个新的列表,不会影响原来的列表
stus = ['a', 'b', 'c', 'd', 'e', 'f']
print(stus[0 : 1]) # ['a']
起始和结束位置的索引都可以省略不写
如果省略结束位置,则会一直截取到最后
如果省略开始位置,则会从第一个元素开始截取
如果开始位置和结束位置全部省略,则相当于创建了一个列表的副本
stus = ['a', 'b', 'c', 'd', 'e', 'f']
print(stus[1:]) # ['b', 'c', 'd', 'e', 'f']
print(stus[:3]) # ['a', 'b', 'c']
print(stus[:]) # ['a', 'b', 'c', 'd', 'e', 'f']
步长表示每次获取元素的间隔,默认值为1
步长不能是0,但是可以是负数。如果是负数,则会从列表的后部向前取元素
stus = ['a', 'b', 'c', 'd', 'e', 'f']
print(stus[0 : 6 : 2]) # ['a', 'c', 'e']
print(stus[ : : -1]) # ['f', 'e', 'd', 'c', 'b', 'a']
3. 通用操作
+ 可以将两个列表拼接为一个列表
* 可以将列表重复指定的次数
my_list = [1, 2, 3] + [4, 5, 6]
print(my_list) # [1, 2, 3, 4, 5, 6]
print(my_list * 2) # [1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6]
in 检查指定元素是否存在于列表中,存在返回True,否则返回False
not in 检查指定元素是否不存在于列表中,不存在返回True,否则返回False
stus = ['a', 'b', 'c', 'd', 'e', 'f', 'a']
print('a' in stus) # True
print('aa' not in stus) # True
min() 获取列表中的最小值
max() 获取列表中的最大值
my_list = [1, 2, 3, 4, 5, 6]
print(min(my_list)) # 1
print(max(my_list)) # 6
stus = ['a', 'b', 'c', 'd', 'e', 'f', 'a']
print(min(stus)) # a
print(max(stus)) # f
index()
获取指定元素在列表中第一次出现的索引
语法: 列表.index(元素, [查找的起始位置], [查找的结束位置])
存在的话,返回值是元素在列表中的索引,否则会抛出异常
当存在后面的两个参数时,包括开始位置,不包括结束位置
stus = ['a', 'b', 'c', 'd', 'e', 'f', 'a']
print(stus.index('a')) # 0
print(stus.index('b', 1)) # 1
count()
统计元素在列表中出现的次数
语法: 列表.count(元素)
stus = ['a', 'b', 'c', 'd', 'e', 'f', 'a']
print(stus.count('a')) # 2
4. 序列(sequence)
序列是Python中最基本的一种数据结构
数据结构是指计算机中数据存储的方式
用于保存一组有序的数据,所有的数据在序列当中都有一个唯一的位置(索引),并且序列中的数据会按照添加的顺序来分配索引
分类:
- 可变序列:序列中的元素可以改变
包括:列表(list,也是可变对象) - 不可变序列:序列中的元素不可以改变
包括:字符串(str),元组(tuple)
5. 修改列表中的元素
通过索引直接进行修改
stus = ['a', 'b', 'c', 'd', 'e', 'f', 'a']
stus[0] = 'aa'
print(stus) # ['aa', 'b', 'c', 'd', 'e', 'f', 'a']
使用del来删除列表中索引为n的某个元素
语法: del 列表[n]
stus = ['a', 'b', 'c', 'd', 'e', 'f', 'a']
del stus[6] # 删除索引为6的元素
print(stus) # ['aa', 'b', 'c', 'd', 'e', 'f']
使用切片来修改列表
语法:列表[起始索引 : 结束索引] = 序列
在给切片进行赋值时,只能使用序列
stus = ['a', 'b', 'c', 'd', 'e', 'f', 'a']
stus[0 : 2] = '12' # 字符串也是序列
print(stus) # ['1', '2', 'c', 'd', 'e', 'f']
stus[3 : 5] = ['aa', 'bb', 'cc', 'dd'] # 使用新的替换旧的,长度可以不一样
print(stus) # ['1', '2', 'c', 'aa', 'bb', 'cc', 'dd', 'f']
stus[0 : 0] = ['ee'] # 向索引为0的位置插入元素
print(stus) # ['ee', '1', '2', 'c', 'aa', 'bb', 'cc', 'dd', 'f']
stus[ : : 2] = ['切片1', '切片1', '切片1', '切片1', '切片1'] # 当设置步长时,序列中元素的个数必须和切片中元素的个数一致
print(stus) # ['切片1', '1', '切片1', 'c', '切片1', 'bb', '切片1', 'dd', '切片1']
del stus[0 : 2] # 删除索引为0和1的两个元素
print(stus) # ['切片1', 'c', '切片1', 'bb', '切片1', 'dd', '切片1']
del stus[::2] # 从头开始,步长为2的删除元素
print(stus) # ['c', 'bb', 'dd']
stus[1 : 3] = [] # 删除索引为1和2的元素
print(stus) # ['c']
注意:以上的操作,只适用于可变序列
list() 函数可以将其他的序列转换为list
str = 'hello'
# str[0] = '1' # 错误,不可变序列,无法通过索引进行修改
print(list(str)) # ['h', 'e', 'l', 'l', 'o']
6. 列表的方法
下面的所有方法都不能对字符串进行操作,只能对可变的列表进行操作
append() 在列表的后面添加一个元素
语法: 列表.append(元素)
stus = ['a', 'b', 'c', 'd', 'e', 'f']
stus.append('g')
print(stus) # ['a', 'b', 'c', 'd', 'e', 'f', 'g']
insert() 在列表的指定位置插入一个元素,如果后面还有元素的话,索引向后推移
语法: 列表.insert(索引,元素)
stus = ['a', 'b', 'c', 'd', 'e', 'f']
stus.insert(2, 'h')
print(stus) # ['a', 'b', 'h', 'c', 'd', 'e', 'f', 'g']
extend() 使用新的序列来扩展当前序列,参数必须是一个序列
会将序列中的元素添加到当前列表的尾部
和 “ 列表 += 序列 ”的效果是一样的,也会将序列中的元素添加到当前列表的尾部
语法: 列表.extend(索引,元素)
stus = ['a', 'b', 'c', 'd', 'e', 'f']
stus.extend(['i', 'j'])
print(stus) # ['a', 'b', 'h', 'c', 'd', 'e', 'f', 'g', 'i', 'j']
stus1 = ['a', 'b', 'c', 'd', 'e', 'f']
stus1 += ['i', 'j']
print(stus1) # ['a', 'b', 'h', 'c', 'd', 'e', 'f', 'g', 'i', 'j']
pop() 根据索引删除并返回指定元素
语法: 列表.pop([索引])
不写索引的话,是删除最后一个元素
会返回所删除的元素的值
stus = ['a', 'b', 'h', 'c', 'd', 'e', 'f', 'g', 'i', 'j', 'i', 'j']
print(stus.pop(2)) # h
print(stus.pop()) # j
print(stus) # ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'i', 'j', 'i']
remove() 删除指定元素
语法: 列表.remove(元素)
如果相同值的元素有多个,只会删除第一个
stus1 = ['a', 'b', 'h', 'c', 'd', 'e', 'f', 'g', 'a']
stus1.remove('a')
print(stus1) # ['b', 'h', 'c', 'd', 'e', 'f', 'g', 'a']
reverse() 反转列表
语法: 列表.reverse()
stus2 = ['a', 'b', 'h', 'c', 'd', 'e', 'f', 'g']
stus2.reverse()
print(stus2) # ['g', 'f', 'e', 'd', 'c', 'h', 'b', 'a']
sort() 对列表中的元素进行排序
语法: 列表.sort([reverse=True])
默认是升序排列,如果要降序排列的话,传入参数“reverse=True”即可变成降序排列
stus1 = ['a', 'b', 'h', 'c', 'd', 'e', 'f', 'g', 'a']
stus1.sort()
print(stus1) # ['a', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
stus1.sort(reverse=True)
print(stus1) # ['h', 'g', 'f', 'e', 'd', 'c', 'b', 'a', 'a']
clear() 清空列表中的所有元素
stus1 = ['a', 'b', 'h', 'c', 'd', 'e', 'f', 'g', 'a']
stus1 .clear()
print(stus) # []
7. 遍历列表
while 循环遍历
stus = ['a', 'b', 'c', 'd', 'e', 'f']
i = 0
while(i < len(stus)) :
print(stus[i])
i += 1
for 循环遍历
序列中的元素有多少个,就会执行多少次
语法:
for 变量 in 序列 :
代码块
语法中的“变量”可以随便写,每执行一次就会将序列中的一个元素赋值给变量
stus = ['a', 'b', 'c', 'd', 'e', 'f']
for s in stus :
print(s)
8. range
用来生成自然数的序列
语法: range([起始位置], 结束位置, [步长])
起始位置可以省略,默认是0
步长可以省略,默认是1
包括起始位置,不包括结束位置
stus = range(5)
print(stus) # range(0, 5)
print(list(stus)) # [0, 1, 2, 3, 4]
stu1 = range(0, 10, 2)
print(stu1) # range(0, 10, 2)
print(list(stu1)) # [0, 2, 4, 6, 8]
stu2 = range(10, 0, 1)
print(stu2) # range(10, 0)
print(list(stu2)) # []
stu2 = range(10, 0, -1)
print(stu2) # range(10, 0, -1)
print(list(stu2)) # [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
通过range可以创建一个执行指定次数的for循环
for 循环除了创建方式以外,其余的都和while一样。包括else、包括break、continue都可以在for循环中使用
for i in range(10) :
print(i)