python元组和元组的方法

元组使用括号

tup = (1,2,3)     

tup2 = ([1,2],[3,4])  # 元组里的元素可以是数组

tup3 = (1,"a")   # 元组的元素可以是不同类型的

tup4 = ()  # 空元组

元组的方法:

1.元组的长度 len()

tup = (1,2,3)
print(len(tup))  # 3

2. 元组相加,合并成一个新的元组

tup = (1,2,3)
tup2 = (4,5,6)
print(tup + tup2) # (1,2,3,4,5,6)

3.元组和数字相乘

tup = ("")
print(tup * 4) # 哈哈哈哈

4.判断某个元素是否在元组中

方法一:

if  in

tup = (1,2,3)
if 1 in tup:
    print("")

方法二:

in

tup  = (1,2,3print(1 in tup) # True

5. 迭代元组

for  in 

tup = (1,2,3)
for item in tup:
    print(item)
原文地址:https://www.cnblogs.com/luguankun/p/11902131.html