Fork me on GitHub

一 概念:

元组是有序且不可更改的集合。在 Python 中,元组是用圆括号编写的。

 

二 使用方法:

1  基本创建:

thistuple = ("apple", "banana", "cherry")
print(thistuple)

 

2 访问:

thistuple = ("apple", "banana", "cherry")
print(thistuple[1])

 

三 遍历方法:

  1 基本方法:

thetuple=(1,3,5,7,9)

#first method
print("first method:")
for item in thetuple:
    print(item)

#second method
print("second method:")
for value in iter(thetuple):
    print(value)

 2 高级方法:

thetuple=(1,3,5,7,9)

#method one
print("first method:")
for index in range(len(thetuple)):
    print("index:",index, "value:",thetuple[index])


#method two
print("second method:")
for index,value in enumerate(thetuple):
    print("index:",index, "value:",value)

 

posted on 2020-11-22 19:48  虚生  阅读(3643)  评论(0编辑  收藏  举报