列表介绍

列表的格式:变量A的类型为列表

namesList = [‘xiaoWang’,‘xiaoZhang’,‘xiaoHua’]

比C语言的数组强大的地方在于列表中的元素可以是不同类型的

testList = [1, ‘a’]

python 列表和字符串

列表循环遍历

python 列表和字符串

列表的相关操作

append
append可以向列表(尾部)添加元素,添加数据时候是直接添加整个数组的

extend
extend可以向列表(尾部)添加元素,添加数据的时候逐个添加的

python 列表和字符串

insert()insert(index, object) 在指定位置index前插入元素object

如果index没有超过列表范围,则在对应下表添加元素,如果超过列表范围则新增的元素放在列表尾部

修改元素

下标来确定要修改的是哪个元素

python 列表和字符串
index(有元素,返回下表,没有对应元素报错)
格式:index(元素,start,end)

修改元素
下标来确定要修改的是哪个元素

python 列表和字符串

自定义变量查询
python 列表和字符串

index(有元素,返回下表,没有对应元素报错)
格式:index(元素,start,end)

python 列表和字符串

删除

del 列表名[下标]
python 列表和字符串

pop()默认删除尾部数据,添加下标删除下标元素
python 列表和字符串

remove:根据元素的值进行删除
python 列表和字符串

排序

sort()默认升序,参数reverse=false不反转reverse=true反转 [::-1] 切片反转

python 列表和字符串
python 列表和字符串

list 案例

初始化一个空数组,往里面循环添加1 - 10 之间的所有整数
python 列表和字符串
在刚刚创建的表中添加一个字符串

python 列表和字符串

删除下标为0的元素
python 列表和字符串
将索引下标为1的元素改成66

python 列表和字符串

输出列表中的第三个元素

python 列表和字符串

列表嵌套

根据下标查询具体数据
python 列表和字符串

嵌套案例

list = [23,24,35,31,22,33,55,43,78,34]

查询list中所有的偶数添加到list1中,并将list1 排序

将排序完的list1通过extend添加list2 = [[[3,21],2],[4],3],并取出元素 21

python 列表和字符串

 

字符串介绍

python 列表和字符串

字符串输出

python 列表和字符串

字符串输入

python 列表和字符串

下标

python 列表和字符串

切片

python 列表和字符串
python 列表和字符串

python 列表和字符串

字符串常见操作

find()

检测 python是否包含在 name 中,如果包含则返回开始的索引值
python 列表和字符串

index()

和 Find() 两个方法都是一样的,但是
fingd() 没有找到的话 返回值是 -1
index() 没有找到的话 回填出错误信息

python 列表和字符串

python 列表和字符串

count()

python 列表和字符串

replace()

python 列表和字符串

split()

python 列表和字符串

startswith()

python 列表和字符串

python 列表和字符串

endswith()

python 列表和字符串

python 列表和字符串

upper()

python 列表和字符串

lower()

python 列表和字符串

title()

python 列表和字符串

capitalize()

python 列表和字符串

partition()

python 列表和字符串

rpartition()

和上面一个partition()放在一样都是根据字符切割成三部分
但是 partition()从左边开始分
rpartition() 从右边开始分
python 列表和字符串

splitlines()

python 列表和字符串

isalpha()

python 列表和字符串

isdigit()

python 列表和字符串

isalnum()

python 列表和字符串

isspace()

python 列表和字符串

rjust()

python 列表和字符串

center()

python 列表和字符串

lstrip()

python 列表和字符串

rstrip()

python 列表和字符串

strip()

python 列表和字符串

rfind()

python 列表和字符串

join()

python 列表和字符串

字符串案例

利用切片,反转字符串

python 列表和字符串
将空格替换成逗点

python 列表和字符串

将所有的大写字符改成小写

python 列表和字符串

插入字符串

python 列表和字符串

相关文章: