Fork me on GitHub

随笔 - 997  文章 - 5  评论 - 181  阅读 - 300万 

一 概念:

元组是有序且不可更改的集合。在 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   虚生  阅读(3670)  评论(0编辑  收藏  举报
编辑推荐:
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
阅读排行:
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
历史上的今天:
2018-11-22 声纹识别开源代码工具
2017-11-22 USB协议介绍
2017-11-22 Ubuntu 16.04 python和OpenCV安装
2017-11-22 一种基于python的人脸识别开源系统
点击右上角即可分享
微信分享提示