set()
>>>x = set('runoob') >>> y = set('google')
>>> x, y (set(['b', 'r', 'u', 'o', 'n']), set(['e', 'o', 'g', 'l']))
# 重复的被删除
>>> x & y
# 交集 set(['o'])
>>> x | y
# 并集 set(['b', 'e', 'g', 'l', 'o', 'n', 'r', 'u'])
>>> x - y
# 差集
set(['r', 'b', 'u', 'n'])
with
with open as file
# 相当于
try
final file.close()
assert
assert true/false ,"string"
# 如果false ,抛出错误 提示 为 string
下划线
单下划线函数,外部无法import *
类里的双下划线,无法被外部利用,
前后双下划线可以,但是不要用,因为python版本更新后作者会用
_ 表示没有赋值的最后一个值
数据结构 列表优化
列表推导特别之处
列表推导更快
range不占内存
“,”.join(str(n) for n in range(100))
使用列表推导而不是map(),filter()
def is_odd_number(number: int) -> bool:
return number % 2 == 1
odd_numbers = filter(is_odd_number,nums)
odd_numbers_doubled = list(map(lambda x: x*2,odd_numbers))
# https://www.runoob.com/python/python-func-map.html
负索引
确定可迭代性 all 和 any
# 内置函数
sum([1,2,3])
# all和any,iterable 是可迭代对象
def has_zero(iterable):
return not all(iterable)
使用 * 运算符操作剩余序列
![](https://img-blog.csdnimg.cn/img_convert/080ed9735482dc22330bcd6fd54f2f3e.png)
array.array 获取基本类型数组
强类型(即不可以改变元素类型)
![](https://img-blog.csdnimg.cn/img_convert/65494b85f048d7cb430a50f72bbce2e6.png)
带str的不可变unicode 字符数组
![](https://img-blog.csdnimg.cn/img_convert/197fc92fa7a716ac668eb977f7ecbe92.png)
带有bytearray的单字节可变序列
bytes(二进制形式存储数据) 不可变
![[Pasted image 20230206163729.png]]
![](https://img-blog.csdnimg.cn/img_convert/da5fcf5b41382fb1d8585eee84f56916.png)
![](https://img-blog.csdnimg.cn/img_convert/93c2f2eeb11df6a983ef76f06700d910.png)
使用字节作为不可变的单字节序列
高效字典
默认参数
my_car = cars_config.get("volvo123","CCC")
使用defaultdict
from collections import defaultdict
_dict = defaultdict(list)
![[Pasted image 20230206170239.png]]
![](https://img-blog.csdnimg.cn/img_convert/2a82d958fda271785a1115cd0183dfc4.png)
![](https://img-blog.csdnimg.cn/img_convert/c47ebfa9f46726da11c9935a3f553252.png)
字典模拟 Switch-case(实现多路复用)
面向对象开发原则:修改要封闭,扩展要开放
![](https://img-blog.csdnimg.cn/img_convert/df7553e78bc1acc6f148d3dd0a28e265.png)
python 3.10
![](https://img-blog.csdnimg.cn/img_convert/572b1464edcf521b30ce8fc5d1c4c56b.png)
字典推导优化dict构造