免费门户网站源码,自己制作音乐的软件免费,莱芜雪野湖风景区介绍,想做一个个人网站怎么做元组#xff08;Tuple#xff09;是Python中的一种数据类型#xff0c;它是一个有序的、不可变的序列。元组使用圆括号 () 来表示#xff0c;其中的元素可以是任意类型#xff0c;并且可以包含重复的元素。 与列表#xff08;List#xff09;不同#xff0c;元组是不可… 元组Tuple是Python中的一种数据类型它是一个有序的、不可变的序列。元组使用圆括号 () 来表示其中的元素可以是任意类型并且可以包含重复的元素。 与列表List不同元组是不可变的这意味着一旦创建它的元素就不能被修改、删除或添加。元组适合用于存储一组不可变的数据。例如
# 获取长度
info (index, 210, 900)
print(len(info))# 3
你可以使用元组来表示一个坐标点的 x 和 y 坐标值或者表示日期的年、月、日等。
元组也被称为只读列表。
所以元组除了查询的内置方法外其他列表的内置方法无法使用。
案例如下
1、获取长度
# 获取长度
info (index, 210, 900)
print(len(info))# 3
2、索引和切片
# 索引和切片
info (index, 210, 900)
print(info[2])
print(info[:2])# 900
# (index, 210)
3、成员判断
# 成员判断
info (index, 210, 900)
print(dex in info)
print(index in info)# False
# True
4、拼接
# 拼接
y1 (1, 2)
y2 (3, 4)
print(y1 y2)# (1, 2, 3, 4)
5、循环
# 循环
for i in info:print(i)# index
# 210
# 900
6、内置方法index()、count()
# 内置方法
t (index, 210, 900, 210, 58, 91)
print(t.index(210))
print(t.count(210))# 1
# 2
7、元组与列表的数据类型的差异
此外元组与列表的数据类型使用上会有一个区别例如 t1 (1)python会认为这个是一个 int 的 1从而 t1 的数据类型是 int
详细看代码
t0 ()
t1 (1)
t2 (1, )
l0 []
l1 [1]
l2 [1, ]
print(ft0{t0}, type(t0))
print(ft1{t1}, type(t1))
print(ft2{t2}, type(t2))
print(fl0{l0}, type(l0))
print(fl1{l1}, type(l1))
print(fl2{l2}, type(l2))# t0() class tuple
# t11 class int
# t2(1,) class tuple
# l0[] class list
# l1[1] class list
# l2[1] class list
以上是关于python之元组的介绍。