python元组(tuple)循环遍历实例分析


一 概念:

元组是有序且不可更改的集合。在 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)

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM