1.元组
- python中的元组和列表类似,不同之处在于元组的元素不能修改(增删改)
- 元组使用小括号(),列表使用方括号[]
- list 列表
- tuple 元组
- 定义: 名 = ()
- 注意:如果元组中只有一个元素,必须添加逗号,(’aaa,‘)、(1,)
1 # 下标和切片
2 t3 = ('a', 'b', 'c', 'a')
3 print(t3[0]) # >>a
4 print(t3[1:]) # >> ('b', 'c')
5 print(t3[::-1]) # >>('c', 'b', 'a')
6 # 计数
7 n = t3.count('a')
8 print(n) # >>2
9 index = t3.index('a', 0, 3) # 根据元素获取下标位置 在0和3(不含)之间找
10 print(index) # >> 0
11
12 # in ,not in
13 if 'c' in t3:
14 print('存在') # >> 存在
15 else:
16 print('不存在')
17 # 支持for---in 循环
18 for i in t3:
19 print(i)
20
2122 # list(tuple) --> 元组转成列表
23 # tuple(list) -->列表转成元组
24
25 t3 = list(t3)
26 print(t3) # >>['a', 'b', 'c', 'a']
27
28 t3 = tuple(t3)
29 print(t3) # >>('a', 'b', 'c', 'a')