一 概念:

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

 

相关文章:

  • 2021-10-27
  • 2022-12-23
  • 2022-12-23
  • 2021-05-18
  • 2022-12-23
  • 2021-12-26
  • 2021-12-11
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-09-26
  • 2022-02-08
  • 2022-12-23
  • 2021-11-14
  • 2021-10-08
  • 2021-10-31
相关资源
相似解决方案