python 基础6-元组

元组   tuple

相当于是列表的加工

列表相同点:

1、可索引、切片
2、能用for循环,可迭代对象
3、元素是有序的
列表不同点:
1、格式()
2、元组的一级元素不可被修改(增加、删除)

# 字符串、列表、元素 3者可互相转换
test1 = "hello word"
test2 = [123,"hello"]
test3 = (123,"word")
v1 = tuple(test1)
v2 = tuple(test2)
print(v1,v2)


方法:

test = (11,22,33,"qq")
test.count(11) 获取指定元素在元组中出现的次数
test.index("qq") 获取指定元素的位置

猜你喜欢

转载自www.cnblogs.com/zhuanfang/p/12483210.html
今日推荐