tuple(“元组“)

tuple()元组

特点:不可变的列表     

*固定了某些数据,不允许外界修改 

t = ("李白","黑塞","悉达多","在轮下")
print(t[1])
print(t[1:3])
print(t[::-1])

t[0] = "1984"  //不可以修改
print(t[0])

//terminal
黑塞
('黑塞', '悉达多')
('在轮下', '悉达多', '黑塞', '李白')
Traceback (most recent call last):
  File "C:\Users\86138\Desktop\pachong\start.py", line 5, in <module>
    t[0] = "1984"
    ~^^^
TypeError: 'tuple' object does not support item assignment

Process finished with exit code 1

*元组如果只有一个元素(*),需要在元素的末尾添加一个逗号

t = ("早上好")  #str
print(type(t))
t1 = ("Good Morning",)  #tuple
print(type(t1))

//termianl
<class 'str'>
<class 'tuple'>

元组不可变:(元组地址不可变)

#元组中的列表可以追加,发生改变
t = (1,2,3,4,5,["金","木","水"])
t[5].append("火")
print(t)

//terminal
(1, 2, 3, 4, 5, ['金', '木', '水', '火'])

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值