# 元组的遍历
t =("python","world",98)print(t,type(t),id(t))print("-------------------第1种获取元组元素的方式:使用索引-----------------------")print(t[0])print(t[1])print(t[2])# print(t[3]) # 如果索引超出范围,则报错:IndexError: tuple index out of range# 第1中获取元组元素的方式:使用索引print("-------------------第2种获取元组元素的方式:for……in…… 遍历-----------------------")for item in t:print(item)
运行结果:
D:\Environment\Python\Python311\python.exe D:\Environment\PythonWorks\learnpython\马士兵Python\第8章_是排还是散\P70_69.元组的遍历.py
('python','world',98)<class'tuple'>1502492519424-------------------第1种获取元组元素的方式:使用索引-----------------------
python
world
98-------------------第2种获取元组元素的方式:for……in…… 遍历-----------------------
python
world
98
Process finished with exit code 0