内置函数
高级变量类型有一些公共的内置函数,如下
| 函数 | 描述 | 备注 |
|---|---|---|
len(items) |
计算容器中元素个数 | |
del item |
删除变量 | |
max(items) |
返回容器中元素最大值 | 字典只比较key |
min(items) |
返回容器中元素最小值 | 字典只比较key |
len 长度
str = 'hello'print(len(str))
结果:
5
del 删除
lst = [1,2,3]del lst[0]print(lst)
结果:
[2,3]
运算符
| 运算符 | 描述 | 支持的数据类型 |
|---|---|---|
+ |
合并 | 字符串、列表、元组 |
* |
重复 | 字符串、列表、元组 |
in |
是否存在(字典中判断键) | 字符串、列表、元组、集合、字典 |
not in |
是否不存在(字典中判断键) | 字符串、列表、元组、集合、字典 |
> >= == < <= |
比较(==以外的较少使用,逐个比较元素) | 字符串、列表、元组 |
in和not in
str = 'hello'# h是否在str中result = 'h' in strprint(result)result = 'h' not in strprint(result)
结果:
TrueFalse
+合并
只有字符串、列表、元组可以合并
字符串
# 字符串str1 = 'hello'str2 = 'world'str = str1 + str2
列表
lst1 = [1,2,3]lst2 = [4,5,6]lst = lst1 + lst2
元组
t1 = (1,2,3)t2 = (4,5,6)t = t1 + t2
*重复只有字符串、列表、元组可以 ```python str = ‘hello’ print(str*3)
l = [1,2,3] print(l * 3)
t = (1,2,3) print(t * 3)
结果:
hellohellohello [1, 2, 3, 1, 2, 3, 1, 2, 3] [1, 2, 3, 1, 2, 3, 1, 2, 3] ```
