python3 id()、is 內存地址


id()、is 內存地址

1,id() 內存地址
2.== 比較的是值
3.is 比較的是內存地址
數字,字符串,有小數據池
int -5--256
str:1,不能有空格
2.長度不能超過20個字符
3.不能有特殊字符。

 

1.id() 內存地址
 1 # id() 內存地址
 2 s = 'alex'
 3 print(s,type(s),id(s))
 4 # alex <class 'str'> 35619648
 5 # alex <class 'str'> 32146240
 
        
2.== 比較的是值
3.is 比較的是內存地址
  3.1列表、字典、元祖
  
1 # 列表、字典、元祖
2 l1 = [1,2,3]
3 l2 = [1,2,3]
4 print(l1 == l2)  # True
5 print(id(l1),id(l2))  # 35680008 35679368
6 print(l1 is l2)  #False
 字符串、數字的特殊的。比較內存地址,pc看不出來,用cmd來查看s1 = 'alex  '
3.2對於str 小數據池
范圍
1.不能有空格。
2.長度不能超過20位。
3.不能有特殊字符。
cmd ---------》str
 1 # 有空格指向的是False
 2 >>> i = 'a'
 3 >>> i1= 'a'
 4 >>> print(i is i1)
 5 True
 6 
 7 >>> s1 = 'alex '
 8 >>> s2 = 'alex '
 9 >>> print(s1 == s2)
10 True
11 >>> print(s1 is s2)
12 False
13 
14 >>> i = 'a  b'
15 >>> i1= 'a  b'
16 >>> print(i is i1)
17 False

字符串中單個*20以內他們的內存地址一樣,單個字符*20以上內存地址不一樣。

1 # 超過20位則為False
2 >>> i = 'a'*20
3 >>> j = 'a'*20
4 >>> print(i is j)
5 True
6 >>> i = 'a'*21
7 >>> j = 'a'*21
8 >>> print(i is j)
9 False

字符串中不能包含特殊符號如:+,- @,* /

1 # 有特殊字符也為False
2 >>> i = 'a@'
3 >>> j = 'a@'
4 >>> print(i is j)
5 False
 3.3對於int 小數據池
范圍:
  -5---256 創建的相同的數字,都指向同一個內存地址。-5/256都包含
 1 >>> i = 6
 2 >>> i1= 6
 3 >>> print(i is i1)
 4 True
 5 >>> i = 258
 6 >>> i1= 258
 7 >>> print(i is i1)
 8 False
 9 >>> i = -5
10 >>> i1= -5
11 >>> print(i is i1)
12 True
4.編碼與解碼
py3:
str: 表現形式: s = 'alex' 實際編碼方式:unicode
bytes: 表現形式: s = b'alex' 實際編碼方式:utf-8,gbk,gb2312....

unicode : 所有字符(無論英文,中文等) 1個字符=4個字節

gbk : 一個字符,英文1個字節。中文兩個字節。

utf-8 : 英文1個字節。歐洲:2個字節。亞洲:3個字節。

 
        
    py3:
unicode A :00000000 00000000 00000000 00001001 四個字節
中 :00000000 00001000 00000000 00000001 四個字節

utf-8 A :00000001 一個字節
歐洲@ : 00000010 00000001 兩個字節
亞洲 '中' : 00001000 00000000 00000001 三個字節
'中國':00001000 00000000 00000001 00001001 00000000 00000001

gbk A :00001001 1個字節
中 :00000000 00100001 兩個字節
不同的編碼之間是不能相互識別,會產生亂碼。

存儲,傳輸的:utf-8,或者 gbk,或者是gb2312,或者是其他(絕對不是unicode)。
 
        
1 s1 = '曉曉'
2 b11 = s1.encode('utf-8')
3 print(b11)  # b'\xe6\x99\x93\xe6\xa2\x85' 6個字節
4 s2 = '曉曉'
5 b22 = s2.encode('gbk')
6 print(b22)  # b'\xcf\xfe\xc3\xb7' 4個字節
 
        

 

 
 
 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM