元组


主要是元组的学习:

# 编写人:刘钰琢
# 编写日期:2021/1/16 11:00
#不可变序列,可变序列 可变:列表,字典  两次的输出id是一样的
lst=[10,20,30]
print(id(lst))
lst.append(300)
print(id(lst))
#不可变序列:字符串,元组  两次输出的id是不一样的
s='hello'
print(id(s))
s=s+'world'
print(id(s))
#元组的创建
t=('python','hello',90)#括号是可以省略不写的
t3='python','hello',90
t4=('python',)
print('第一种创建',t,type(t))
print('第二次创建,省略小括号',t3,type(t3))
print('第三次创建,单单一个元素时',t4,type(t4))
t1=tuple(('python','hello',90))#要有两重括号,
print('第四次创建,用内置函数:',t1,type(t1))
t2=(10,)#只有一个元素的时候要添加逗号
print('第五次创建,单单一个元素时',t2,type(t2))
#空列表,空字典,空元组的创建
#空列表
lst=[]
lst1=list()
#空字典
d={}
d1=dict()
#空元组
t6=()
t7=tuple()
print('空列表',lst,lst1)
print('空字典',d,d1)
print('空元组',t6,t7)
#元组的可变和不可变的部分
t=(10,[20,30],40)#10,和40属于不可变部分,但是在【20,30】是一个可变的列表
print(t,type(t))#所以说对[20,30]属于是可以进行增删改查操作的
print(t[0])
t[1].append(100)#增加还有extend
print('中间列表后面添加一个元素',t)
t[1].sort(reverse=True)#排序,还有删除pop()填入索引,remove()填入
print('对中间的列表进行逆序排序',t)


文章作者: 毛豆不逗比
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 毛豆不逗比 !
  目录
{% include '_third-party/exturl.swig' %} {% include '_third-party/bookmark.swig' %} {% include '_third-party/copy-code.swig' %} + {% include '_custom/custom.swig' %}