元组是不可变序列,没有增删改操作,是可迭代对象。
一、元组的创建
#方法一:使用小括号 若只有一个元素,必须加逗号,否则不是元组类型
tuple1=('hello','world',100)
tuple11=('hello',)
print(tuple1)
#方法二:使用tuple()函数
tuple2=(('hello','python',101))
print(tuple2)
结果:
('hello', 'world', 100)
('hello',)
('hello', 'python', 101)
二、元组的遍历
#方法一:使用索引
tuple1=('hello','world',100)
print(tuple1[0])
print(tuple1[1])
print(tuple1[2])
#方法二:使用for…in
for item in tuple1:
print(item)
结果:
hello
world
100
hello
world
100