1. is 和 == 区别
id()函数
== 判断两边的值
is 判断内存地址

test
编码:
1. ASCII: 英文, 特殊字符, 数字, 8bit, 1byte
2. GBK: 中文 16bit, 2byte. 兼容ASCII
3. unicode: 万国码, 32bit 4byte. 兼容ASCII
4. UTF-8: 长度可变的unicode. 英文:8bit, 欧洲:16bit, 中文:24bit 3byte
python2 只能用ASCII
python3 有unicode, 默认的编码就是unicode
内存里用的是unicode. 硬盘和网络传输用的是utf-8或者GBK
2. encode() 编码. 获取到的是编码之后的结果. bytes
3. decode() 解码. 把bytes编程我们熟悉的字符串

s = "alex马"
想要存储.必须进行编码
encode() 编码之后的内容是bytes类型的数据
30个字节 10个字. 每个字3个字节
b'xe6x9dx8exe5x98x89xe8xafx9axe7x9ax84xe5x84xbfxe5xadx90xe8xa2xabxe7xbbx91xe6x9exb6xe4xbax86'
bs = s.encode("UTF-8") # 把字符串编码成UTF-8的形式
print(bs)
英文:编码之后的结果是英文本身
中文:编码之后UTF-8 下. 一个中文3个字节
s = "饿了么"
bs = s.encode("GBK") # b'xb6xf6xc1xcbxc3xb4' GBK 一个中文:2个字节
print(bs)
s = "中"
print(s.encode("utf-8"))
print(s.encode("GBK"))
decode()解码
bs = b'xb6xf6xc1xcbxc3xb4' # 从别人那里读到的 GBK
# 编程人认识的东西
s = bs.decode("GBK") # 解码之后是字符串, 用什么编码就用什么解码
print(s)
GBK => utf-8
bs = b'xb6xf6xc1xcbxc3xb4'
# 先解码成unicode字符串
s = bs.decode("GBK")
print(s)
# 在把字符串编码成UTF-8
bss = s.encode("UTF-8")
print(bss)
View Code