- 可变序列vs不可变序列(地址相关)
- 创建元组
- 元组中存储的是对象的引用(地址)
- 元组遍历
# ==================== 可变序列 vs 不可变序列
# 可变序列:列表,字典 增删改地址不变
lst = [10,20,30]
print(id(lst)) # 2203020448136
lst.append(40)
print(id(lst)) # 2203020448136
# 不可变序列:字符串,元组
s = 'hello'
print(id(s)) # 1619015870512
s = s+'world'
print(id(s)) # 1619016475888
# ==================== 创建元组
# 使用()创,如果只有一个元素,用,隔开
t = ('black','stones',98)
print(t) # ('black', 'stones', 98)
print(type(t)) # <class 'tuple'>
s = ('hello')
print(type(s)) # <class 'str'>
t1 = ('hello', )
print(type(t)) # <class 'tuple'>
# 使用内置函数 tuple()创
t1 = tuple(('black', 'stones', 98))
print(t1)
# 空元组
t2 = ()
t3 = tuple()
# ==================== 元组中存储的是对象的引用(地址)
# 即:如果元组中的对象本身是可变对象,它的引用(地址)不变,值可变
t = (88,[10,30],66)
print(t[0]) # 不可修改
print(t[1], id(t[1])) # 1798432183176
t[1].append(100) # 列表可修改
print(t[1],id(t[1])) # 1798432183176
# ==================== 元组的遍历
t = tuple(('black', [10,20], 66))
for i in t:
print(i)
# 元组没有生成式