首页 > 其他 > 详细

元组和元组的方法

时间:2019-11-21 01:03:01      阅读:110      评论:0      收藏:0      [点我收藏+]

元组使用括号

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!