python元组整合-5

it2022-05-05  133

文章目录

2.元组2.1特点2.2 元组的创建2.3拼接、重复、索引(偏移)、切片2.4增删改查查删改 2.5基本函数2.6遍历 复习总结(字符串、列表、元组) 关于列表方面请看: https://blog.csdn.net/weixin_41736752/article/details/96437875

2.元组

2.1特点

有序可重复不可更改符号使用()

2.2 元组的创建

空元组的创建,不能添加元素单元素元组的创建,需要在后面加逗号 >>> a=("a") >>> type(a) <class 'str'> >>> a=('a',) >>> type(a) <class 'tuple'> 多元素元组的创建,包含多种数据类型

2.3拼接、重复、索引(偏移)、切片

同列表一样,不做详细测试 https://blog.csdn.net/weixin_41736752/article/details/96437875

2.4增删改查

索引查切片查index() 返回位置

只能全部删除del 元组名即可

不能改元组,但是可以改元组中的列表

tp=('a','b','c',['a','b','c']) tp[3][1]='nice' #('a', 'b', 'c', ['a', 'nice', 'c'])

2.5基本函数

最大值最小值

max(tp) min(tp)

tp=(1,5,9,4,5,10) print(max(tp),min(tp)) #10 1

2.6遍历

元素遍历

tp=('one','two','and','a','b') for i in tp: print(i,end=' ')

索引遍历

tp=('one','two','and','a','b') for i in range(len(tp)): print(tp[i],end=' ')

枚举enumerate

tp=('one','two','and','a','b') for i in enumerate(tp,1): print(i,end=' ') #(1, 'one') (2, 'two') (3, 'and') (4, 'a') (5, 'b')

复习总结(字符串、列表、元组)

字符串、列表、元组的相同点和不同点

符号引号“ ”方括号[ ]小括号()特征字符串列表元组类型stringlisttuple顺序\有序有序重复、索引、切片TTT增Tappend()、extend()、insert(位置,元素)F删s.strip(“指定字符”)pop()、remove()、del、clear()del改s.replace([现有],[替新],替换数量)TF查s.find()、s.index()、s.rindex()、Tindex()特点简单易操作拷贝注意三种情况不能对创建好的元组操作

最新回复(0)