python 元组学习

#元组的值不可以改变

#1.空元组
tup1 = ()
print(tup1)

#2.一个元素的元组, 逗号不可省略
tup2 = (4,)
print(tup2)

#3. 通过下标访问元组的值
tup1 = (1, 34, 8, 23, 24, 99)
print(tup1[3])
print(tup1[1:])

#4.拼接元组
tup3 = tup1 + tup2
print(tup3)

#5.删除元组
del tup3

#fun
#6. 元组的长度
print(len(tup2))

#7. 元组 *
tup3 = (12,) * 4
print(tup3)

#8. 判断元素在元组中
if 8 in tup1:
    print("8 in tup1")

#9. 遍历元组
for i in tup2:
    print(i)

#10. 比较元组的大小
if tup1 > tup2:
    print("tup1 > tup2")
else:
    print("tup1 < tup2")

#11. 元组中元素的个数
print(len(tup1))

#12. 元组中的最大值和最小值
print(min(tup1))
print(max(tup1))

#13. 列表转元组
list_1 = [1, 23, 34]
print(tuple(list_1))

你可能感兴趣的:(python 元组学习)