【零基础Python专题(源码版)】篇章4·元组tuple
下面列举的Python的元组基础知识(三引号成对儿注释的内容为一个小知识点),可以直接在Pycharm运行学习:
#元组 元组是个不可变序列
'''#元组的创建
# 使用()
t1=('Python','world',98)
t1_1='Python','world',98
print(t1,type(t1))
print(t1,type(t1_1))
t2_1=('Python')
t2_2=('Python',)
print(t2_1,type(t2_1))
print(t2_2,type(t2_2)) #一个元祖时要加逗号
# 使用tuple
t3=tuple(('Python','world',98))
print(t3,type(t3))
# 空元祖的创建
# 空列表
lis1=[]
lis2=list[]
# 空字典
d1={}
d2=dict()
# 空元祖
t4=()
t5=tuple()'''
#可变与不可变序列
'''t1=(1,[2,3],4)
print(t1[0],type(t1[0]))
print(t1[1],type(t1[1]))
print(t1[2],type(t1[2]))
#上面结果表示t1中第0,2项是不可变序列int,第1项是可变序列list
##t1[1]=100 #不可行,因为元组是不可变序列,内部数据是能改变的,这句话相当于把t1[1]的地址改变为100,是不可行的t1[1]此时存储的是list的地址
#但是可以进行以下操作
t1[1].append(100)
print(t1[1],type(t1[1]))'''
#元组的遍历
'''t1=('Python','world',98)
for item in t1:
print(item)'''