tuple1=(1,2,3,4,5)
print(tuple1)
print(type(tuple1))
输出:
(1, 2, 3, 4, 5)
<class 'tuple'>
不可改变的类型,和列表在使用上相似
tuple1=(1,2,3,4,5)
tuple1[1]=22
image.png
想尝试改变它会报错
定义一个空元组
tuple1=()
print(type(tuple1))
tuple1=(1)
print(type(tuple1))
输出:<class 'int'>
tuple1=(1,)
print(type(tuple1))
输出:<class 'tuple'>
tuple1=1,
print(type(tuple1))
输出:<class 'tuple'>
元组关键不是括号而是逗号
print("8*(8):",8*(8))
print("8*(8,):",8*(8,))
输出
8(8): 64
8(8,): (8, 8, 8, 8, 8, 8, 8, 8)
添加元素
temp=("小明","喜欢","唱","跳")
temp=temp[2:]+("rap",)+temp[:2]
print(temp)
输出:('唱', '跳', 'rap', '小明', '喜欢')
删除元素
temp=("小明","喜欢","唱","跳")
del temp
print(temp)
image.png
网友评论