运算符 | 描述 | 支持的容器类型 |
---|---|---|
+ | 合并 | 字符串、列表、元组 |
* | 复制 | 字符串、列表、元组 |
in | 元素是否存在 | 字符串、列表、元组、字典 |
not in | 元素是否不存在 | 字符串、列表、元组、字典 |
# 1. 字符串
str1 = 'lcj'
str2 = 'nb'
str3 = str1 + str2
print(str3)
# 2. 列表
list1 = [1,2,3]
list2 = [100,999,333]
list3 = list1 + list2
print(list3)
# 3. 元组
t1 = (1,2)
t2 = (10,20)
t3 = t1 + t2
print(t3)
lcjnb
[1, 2, 3, 100, 999, 333]
(1, 2, 10, 20)
# 1. 字符串
print('--' * 10)
# 2. 列表
list4 = ['lcj']
print(list4 * 4)
# 3. 元组
t4 = ('decade',)
print(t4)
print(t4 * 4)
--------------------
['lcj', 'lcj', 'lcj', 'lcj']
('decade',)
('decade', 'decade', 'decade', 'decade')
# 1. 字符串
print('d' in 'decade')
print('dcd' not in 'decade')
# 2. 列表
list0 = ['decade','hibiki','zio','w']
print('ooo' in list0)
print('decade' not in list0)
# 3. 元组
t0 = ('ghost','ex-aid','build','rabbit')
print('tank' in t0)
print('tank' not in t0)
True
True
False
False
False
True
函数 | 描述 |
---|---|
len() | 计算容器中元素个数 |
del 或 del() | 删除 |
max() | 返回容器中元素最大值 |
min() | 返回容器中元素最小值 |
range(start, end, step) | 生成从start到end的数字,步长为 step,供for循环使用 |
enumerate() | 函数用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在 for 循环当中。 |
# 1. 字符串
str1 = 'abcdefg'
print(len(str1)) # 7
# 2. 列表
list1 = [10, 20, 30, 40]
print(len(list1)) # 4
# 3. 元组
t1 = (10, 20, 30, 40, 50)
print(len(t1)) # 5
# 4. 集合
s1 = {
10, 20, 30}
print(len(s1)) # 3
# 5. 字典
dict1 = {
'name': 'Rose', 'age': 18}
print(len(dict1)) # 2
7
4
5
3
2
# 2. 列表
list1 = [10, 20, 30, 40]
del(list1[0])
print(list1) # [20, 30, 40]
# 1. 字符串
str1 = 'abcdefg'
del str1
print(str1)
[20, 30, 40]
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
in
7 str1 = 'abcdefg'
8 del str1
----> 9 print(str1)
NameError: name 'str1' is not defined
# max()
# 字符串
str1 = 'abcdefg'
print(max(str1)) # g
# 列表
list1 = [10, 20, 30, 40]
print(max(list1)) # 40
# min()
print(min(str1))
print(min(list1))
g
40
a
10
for i in range(1,10,1):
print(i,end=' ')
print()
for i in range(1,10,2):
print(i,end=' ')
print()
for i in range(10):
print(i,end=' ')
1 2 3 4 5 6 7 8 9
1 3 5 7 9
0 1 2 3 4 5 6 7 8 9
·语法:enumerate(可遍历对象,start=0)
·注意:start参数用来设置遍历数据的下标起始值,默认为0
list01 = [1,2,3,10,99,888]
for i in enumerate(list01):
print(i,end=' ')
print()
for i in enumerate(list01,start=1):
print(i,end=' ')
(0, 1) (1, 2) (2, 3) (3, 10) (4, 99) (5, 888)
(1, 1) (2, 2) (3, 3) (4, 10) (5, 99) (6, 888)
·作用:将某个序列转换成列表
t1 = ('a','b','b','n')
s1 = {
1,2,30,200}
list_t = list(t1)
list_s = list(s1)
print(list_t)
print(type(list_t))
print(list_s)
print(type(list_s))
['a', 'b', 'b', 'n']
[200, 1, 2, 30]
·作用:将某个序列转换成元组
list1 = [10,20,30,40]
set1 = set([1,2,3,4,5])
tuple_l = tuple(list1)
tuple_s = tuple(set1)
print(tuple_l)
print(type(tuple_l))
print(tuple_s)
print(type(tuple_s))
(10, 20, 30, 40)
(1, 2, 3, 4, 5)
·作用:将某个序列转换成集合
·注意:集合可以快速完成列表去重,集合不支持下标
list2 = [1,3,5,7,9,9,9]
tuple1 = ('decade','zio','ohma','decade')
set_l = set(list2)
set_t = set(tuple1)
print(set_l)
print(type(set_l))
print(set_t)
print(type(set_t))
{1, 3, 5, 7, 9}
{'decade', 'zio', 'ohma'}