公共方法
运算符
对于str, list 和 tuple 这三个类型, 算数运算符 +号 和 *号 属于公有方法
算数运算符 | 用途 |
---|---|
+号 | 拼接 |
*号 | 复制 (倍增) |
并不能用于 dict 和 set 类型的数据!
对于所有的5个容器 str, list, tuple, dict 和 set
判断是否存在的方法in/not in都通用
- 注意: 对于字典dict来说, in/not in 仅仅判断key是否存在
Python内置函数
函数 | 用途 |
---|---|
len() | 计算容器中元素的个数 |
max() | 返回容器中元素的最大值 |
min() | 返回容器中元素的最小值 |
del() | 删除变量 |
对于多维度list和tuple的访问
变量名[x][y][...]
带索引的遍历结果
alist = ["a", "b", "c"]
i = 0
for item in alist:
print(i, item)
i += 1
结果
o a
1 b
2 c
枚举函数 enumerate()
- 将一个可遍历的数据对象组合成一个索引序列, 并同时列出下标和数据
alist = ["a", "b", "c"]
for i, item in enumerate(alist):
print(i, item)
结果
o a
1 b
2 c