'''不可变序列'''
'''可变序列 列表,字典'''
lst = [10,20,45]
print(id(lst))
lst.append(300)
print(id(lst))
''' 不可变序列 ,字符串 元组'''
s = 'hello'
print(id(s))
s = s+'world'
print(id(s))
print(s)
'''元组的创建方式'''
'''第一种 使用()'''
t = ('Python','world',98)
print(t)
print(type(t))
t2 = 'Ptyhon','world',98
print(t2)
print(type(t2))
# 只有一个元素要使用,逗号来进行区分
t3 = ('python',)
print(t3)
print(type(t3))
'''第二种创建方式,使用内置函数typle()'''
t1 = tuple(('Python', 'world', 98))
print(type(t1))
'''空元组'''
'''空列表'''
lst1 = []
lst2 = list()
d = {}
d2 = dict()
#空元组
t4 = ()
t5 = tuple()
print('空列表', lst1, lst2)
print('空字典', d, d2)
print('空元组', t4, t5)
#为什么元组要定义成不可变的
t = (10, [20, 30], 9)
print(t)
print(type(t))
print(t[0], type(t[0]), id(t[0]))
print(t[1], type(t[1]), id(t[1]))
print(t[2], type(t[2]), id(t[2]))
'''尝试将t[1]修改为100'''
print(id(100))
# t[1] = 100 #元组是不允许修改成元素的
t[1].append(100)
print(t, id(t[1]))
'''元组的遍历'''
t = ('Python', 'world', 98)
'''第一种获取元组的方式,使用索引'''
print(t[0])
print(t[1])
print(t[2])
'''遍历元组'''
for item in t:
print(item)
python 元组
最新推荐文章于 2022-10-04 00:05:49 发布