python 元组

  • 可变序列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)
    
# 元组没有生成式
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值