is 和 == 区别,id() ,回顾编码,encode(),decode()

1. is 和 == 区别      id()函数      == 判断两边的值      is 判断内存地址例
 s = "alex 是 大 xx"# abc = id(s)   # 得到内存地址# print(abc)

# lst = ["大阳哥", "佳琪哥", "小花生", "燃哥"]# print(id(lst))  # 就是一个内存地址. 毫无意义

# lst = ["周杰伦", "燃哥"]# lst1 = ["周杰伦", "燃哥"]# print(id(lst))# print(id(lst1))

# s = "燃哥"# s1 = "燃哥"# # 小数据池. 会对字符串进行缓存, 为了节省内存# print(id(s))# print(id(s1))

# tu = ("燃哥", "周杰伦")# tu1 = ("燃哥", "周杰伦")# print(id(tu), id(tu1))

# dic = {"a": "b", "c":"d"}# dic1 = {"a": "b", "c":"d"}# print(id(dic), id(dic1))

# a = 10# b = 10# print(id(a), id(b))

# 布尔也有,# a = True# b = True# print(id(a), id(b))

# -5# a = 257# b = 257# print(id(a), id(b))

# a = "小威"# b = "小威"# print(id(a), id(b))

# 1. id() 查看内存地址# 2. str 有小数据池的

# == is id# == 判断. 左右两端是否相等和一致, 比较的是内容# is 判断. 判断的是内存地址  id()的值来判断    内存地址

# lst = ["马化腾", "小威"]# lst2 = ["马化腾", "小威"]# print(lst == lst2)  # True# print(lst is lst2)  # False

# s = "alex"# print("1111111111")# print("1111111111")# print("1111111111")# print("1111111111")# print("1111111111")# print("1111111111")# print("1111111111")# print("1111111111")# print("1111111111")## s2 = "alex"# print(s == s2) # True# print(s is s2)  # True. 小数据池

# s1 = "@akljflkasdjklfjkasdlfjklsdajfklsdajfklasdjkflasdjklfjsdaklfjsdakljfklasdjfklsdajfklsdajfklsdajklfsjadklfjsadklfjasdkljfklsdjfklsdjfklsdjfklsdjfklasdjfklasdjklfjasdklakljflkasdjklfjkasdlfjklsdajfklsdajfklasdjkflasdjklfjsdaklfjsdakljfklasdjfklsdajfklsdajfklsdajklfsjadklfjsadklfjasdkljfklsdjfklsdjfklsdjfklsdjfklasdjfklasdjklfjasdkl"# s2 = "@akljflkasdjklfjkasdlfjklsdajfklsdajfklasdjkflasdjklfjsdaklfjsdakljfklasdjfklsdajfklsdajfklsdajklfsjadklfjsadklfjasdkljfklsdjfklsdjfklsdjfklsdjfklasdjfklasdjklfjasdklakljflkasdjklfjkasdlfjklsdajfklsdajfklasdjkflasdjklfjsdaklfjsdakljfklasdjfklsdajfklsdajfklsdajklfsjadklfjsadklfjasdkljfklsdjfklsdjfklsdjfklsdjfklasdjfklasdjklfjasdkl"# print(id(s1), id(s2))## s = "abc中def"# print(s.title())

回顾编码:    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()  编码. 获取到的是编码之后的结果. bytes3. decode()  解码. 把bytes编程我们熟悉的字符串例
# s = "alex马"# 想要存储.必须进行编码

# encode() 编码之后的内容是bytes类型的数据

# 30个字节 10个字. 每个字3个字节# b'\xe6\x9d\x8e\xe5\x98\x89\xe8\xaf\x9a\xe7\x9a\x84\xe5\x84\xbf\xe5\xad\x90\xe8\xa2\xab\xe7\xbb\x91\xe6\x9e\xb6\xe4\xba\x86'# bs = s.encode("UTF-8")   # 把字符串编码成UTF-8的形式# print(bs)

# 英文:编码之后的结果是英文本身# 中文:编码之后UTF-8 下. 一个中文3个字节

# s = "饿了么"# bs = s.encode("GBK")    # b'\xb6\xf6\xc1\xcb\xc3\xb4' GBK 一个中文:2个字节# print(bs)

# s = "中"# print(s.encode("utf-8"))# print(s.encode("GBK"))

# decode()解码

# bs = b'\xb6\xf6\xc1\xcb\xc3\xb4'    # 从别人那里读到的   GBK## # 编程人认识的东西# s = bs.decode("GBK")    # 解码之后是字符串, 用什么编码就用什么解码# print(s)

# GBK => utf-8bs = b'\xb6\xf6\xc1\xcb\xc3\xb4'# 先解码成unicode字符串s = bs.decode("GBK")# 在把字符串编码成UTF-8bss = s.encode("UTF-8")print(bss)
上一篇:python编码encode decode(解惑)


下一篇:pyhton字符编码问题--decode和encode方法