Python基礎六 記憶體(id)編碼進階

來源:互聯網
上載者:User

標籤:blog   tuple   ice   bit   資料類型   alt   post   info   display   

一.記憶體(id)1.查詢記憶體位址(id)
name = ‘alex‘print(id(name))li = [1,2,3]print(id(li))每次輸出的結果都是不同的
2.判斷是否是同一記憶體位址(is)
Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:54:40) [MSC v.1900 64 bit (AMD64)] on win32Type "help", "copyright", "credits" or "license" for more information.>>> name1 = ‘[email protected]‘>>> name2 = ‘[email protected]‘>>> print(name1 == name2)True>>> print(name1 is name2)False>>>

 

二.小資料池    int str

int    -5 ---256

str   如果是全部由字母組成的字串 都是指向一個記憶體位址.
如果是數字與str(單個字母)想乘,則20以內(包含20)的是同一個記憶體位址.
其他資料類型 則沒有小資料池概念.

 

三.編碼進階

1.Unicode:萬國碼(32位)

2.utf—8:用最少8位表示一個字元

3.gbk:國標

1,不同編碼之間是不能互相識別對方的二進位,會報錯,或者產生亂碼.
2,在你的字串(檔案),儲存,傳輸時,必須使用非unicode的二進位(01010101).

py3中:
字串:編碼方式(在記憶體中的運行方式):預設都是unicode.

int

byte
對於非中文: 表現形式: b‘alex‘
內部編碼: utf-8 gbk,gb2312...(非unicode) 你設定的.

對於中文: 表現形式: b‘xe3\xf2\x36\xe3\xf2\x36\‘
內部編碼: utf-8 gbk,gb2312...(非unicode) 你設定的.

s = ‘alex‘s1 = b‘alex‘print(s.capitalize())             #輸出結果:Alexprint(s1.capitalize())            #輸出結果:b‘Alex‘str ---> bytes英文:s = ‘alex‘b = s.encode(‘utf-8‘)b1 = s.encode(‘gbk‘)print(s,type(s))            #輸出結果:alex <class ‘str‘>print(b,type(b))            #輸出結果:b‘alex‘ <class ‘bytes‘>print(b1,type(b1))          #輸出結果:b‘alex‘ <class ‘bytes‘>中文:s = ‘中國‘b = s.encode(‘utf-8‘)b1 = s.encode(‘gbk‘)print(s,type(s))           #輸出結果:中國 <class ‘str‘>print(b,type(b))           #輸出結果:b‘\xe4\xb8\xad\xe5\x9b\xbd‘ <class ‘bytes‘>print(b1,type(b1))         #輸出結果:b‘\xd6\xd0\xb9\xfa‘ <class ‘bytes‘>
View Code

 

str
對於非中文: 表現形式: ‘alex‘
內部編碼: unicode
對於中文: 表現形式: ‘中國‘
內部編碼: unicode

bool 

list

tuple

dict

 

Python基礎六 記憶體(id)編碼進階

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.