不可变数据类型tuple的增删改
元组增加数据:
只能通过拼接的方式“+”来增加数据,
tup1 = ('hello',11,23.3)
tup2 = ('world!',)
print(tup1+tup2)#('hello',11,23.3,'world!')
如果元组中只打算存一个数据,需要在数据末尾加“,”否则会被认为是带括号的普通对应类型元素
tup1,tup2,tup3,tup4,tup5 = ('hello'),(11),(23.3),('world',),(55,)
print(tup1,type(tup1))#hello <class 'str'>
print(tup2,type(tup2))#11 <class 'int'>
print(tup3,type(tup3))#23.3 <class 'float'>
print(tup4,type(tup4))#('world',) <class 'tuple'>
print(tup5,type(tup5))#(55,) <class 'tuple'>
元组删除数据:
元组不支持单个数据删除,但可以“del”删除整个元组
tup1 = ('hello',11,23.3)
del tup1
元组复制数据:
用*复制print(tup1*2),将元组tup1复制2次
元组求长度:
len(tup1)
对列表进行增删改
增加数据:3个函数都是一次只能传1个参
append():往末尾增加一条数据,将要插入的所有数据当做1个数据对象插入到末尾,append不会分割字典
insert():在指定下标位置插入数据
extend():往末尾增加一条数据,将要插入的所有数据,元组类型数据按“,”分割开插入,str类型数据按单个字符分割开插入,list类型数据按“,”分割后插入,字典类型数据只能插入键,值无法被插入
#list1.append(参值)
list1 = [1,2,3]
list1.append({'name':'xiaoming'})
print(list1)#[1, 2, 3, {'name': 'xiaoming'}]
#list1.extend(参数)
list1 = [1,2,3]
list1.extend((4,'hello',23.3))#[1, 2, 3, 4, 'hello', 23.3]
list1.extend('hello')#[1, 2, 3, 'h', 'e', 'l', 'l', 'o']
list1.extend([4,5,(6,'hello')])#[1, 2, 3, 4, 5, (6, 'hello')]
list1.extend({'name':'xiaoming'})#[1, 2, 3, 'name']
print(list1)