zoukankan      html  css  js  c++  java
  • 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)
  • 相关阅读:
    安卓中期小作业
    安卓大作业UI预定搞
    实验3
    实验一总结
    实验8 SQLite数据库操作
    实验6 在应用程序中播放音频和视频
    实验4 颜色、字符串资源的使用
    实验四
    实验三
    实验二
  • 原文地址:https://www.cnblogs.com/dylancao/p/14020924.html
Copyright © 2011-2022 走看看