#使用一对小括号()来表示一个元组
#元组是不可变数据类型,不能进行增删改操作
#和列表一样,也是一个有序的存储数据的容器
#可以通过下标来获取元素
#列表是可变的,而元组是不可变的数据类型
words = ['hello','world','python']
nums = (5,3,4,3,8,6,1,4,2,0,6,7,9,4,5,9,4,3,7,9)
print(nums[3])
print(nums.index(7))
print(nums.count(9))
age = (30) #age是一个整数,并不是一个元组
print(type(age))
age = (30,) #如果元组里只有一个元素,要在最后加一个逗号
print(type(age))
#tuple是一个内置类
print(tuple('hello'))
#如何把列表转换为元组 tuple list set
print(tuple(words))
print(list(nums))
#元组也可以遍历
for i in nums:
print(i)
################
j = 0
while j < len(nums):
print(nums[j])
j += 1
结果
3
11
3
<class 'int'>
<class 'tuple'>
('h', 'e', 'l', 'l', 'o')
('hello', 'world', 'python')
[5, 3, 4, 3, 8, 6, 1, 4, 2, 0, 6, 7, 9, 4, 5, 9, 4, 3, 7, 9]
5
3
4
3
8
6
1
4
2
0
6
7
9
4
5
9
4
3
7
9
5
3
4
3
8
6
1
4
2
0
6
7
9
4
5
9
4
3
7
9
Process finished with exit code 0