python 小知识总结汇整

一、=、==、is、id()

1、=(赋值)

1 a = 'yang'
2 print(a)
3 >>> yang
View Code

2、==(比较值是否相等)

1 a = 'yang'
2 b = 'yang'
3 print(a == b)
4 >>> True
View Code

3、is(比较内存地址是否相等)

1 a = 666
2 b = 666
3 print(a is b)
4 >>> False
View Code

4、id(内存地址)

1 a = 666
2 b = 888
3 print(id(a))
4 print(id(b))
5 
6 >>> 6049680
7 >>> 10522416
View Code

二、小数字池

  1. 数字,字符串 小数据池

    • 数字的范围 -5 – 256
  2. 字符串:

  3. (1).不能有特殊字符
      (2).s*20 还是同一个地址,s*21以后都是两个地址

 

三、python3 编码

1、知识回顾

 1 ascii
 2             A : 00000010  8位 一个字节
 3 
 4 unicode     A : 00000000 00000001 00000010 00000100 32位  四个字节
 5             中:00000000 00000001 00000010 00000110 32位  四个字节
 6 
 7 
 8 utf-8      A :  00100000 8位 一个字节
 9           中 :  00000001 00000010 00000110 24位 三个字节
10 
11 
12 gbk        A : 00000110  8位 一个字节
13          中  : 00000010 00000110 16位 两个字节
View Code

1、各个编码之间的二进制,是不能互相识别的,会产生乱码。

2、文件的储存,传输,不能是unicode(只能是utf-8 utf-16 gbk,gb2312,asciid等)

2、python3中的编码

(1).string类型在内存中是用Unicode编码

(2).bytes类型:

 1 英文:
 2 str 
 3     表现形式: s = 'yang'
 4     编码方式:unicode
 5 bytes
 6     表现形式:b'yang'
 7     编码方式:除unicode外的其他编码
 8 
 9 中文:
10 str 
11     表现形式: s = '中国'
12     编码方式:unicode 
13 bytes
14     表现形式:b'\xe4\xb8\xad\xe5\x9b\xbd'
15     编码方式:除unicode外的其他编码
View Code

3、python3种str类型与bytes类型的互相转换

① str ——>bytes

 1 # encode()方法,编码
 2 
 3 s = 'yang'
 4 print(s,type(s))
 5 print(s.encode('utf-8'),type(s.encode('utf-8')))
 6 
 7 >>> yang <class 'str'>
 8 >>> b'yang' <class 'bytes'>
 9 
10 # -------------------------------------------------
11 c = '中国'
12 print(c,type(c))
13 print(c.encode('utf-8'),type(c.encode('utf-8')))
14 
15 >>> 中国 <class 'str'>
16 >>> b'\xe4\xb8\xad\xe5\x9b\xbd' <class 'bytes'>
View Code

① bytes ——>str

 1 # decode()方法,解码
 2 
 3 s = b'yang'
 4 print(s,type(s))
 5 print(s.decode('utf-8'),type(s.decode('utf-8')))
 6 
 7 >>> b'yang' <class 'bytes'>
 8 >>> yang <class 'str'>
 9 
10 # -------------------------------------------------
11 c = b'\xe4\xb8\xad\xe5\x9b\xbd'
12 print(c,type(c))
13 print(c.decode('utf-8'),type(c.decode('utf-8')))
14 
15 >>> b'\xe4\xb8\xad\xe5\x9b\xbd' <class 'bytes'>
16 >>> 中国 <class 'str'>
View Code

 

posted @ 2020-04-04 16:31  kylindemi  阅读(191)  评论(0)    收藏  举报