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, ['金', '木', '水', '火'])