第二章 python 基礎資料類型

來源:互聯網
上載者:User

標籤:解釋   img   col   資料類型   1.3   1.2   color   ati   string   

變數及身份運算

id()定義:函數用於擷取對象的記憶體位址。

舉列:

name = ‘tangni‘
id(name)

輸出
(56156880)

1.1 兩個變數名一個值舉列:

由於‘tangni’是一個簡單的字串,因此python解譯器做了最佳化,記憶體裡只有一個‘tangni’,name1和name2都指向同一塊兒記憶體位址。

name1 = ‘tangni‘
name2 = ‘tangni‘
name1_id = id(name1)
name2_id = id(name2)
print(name1_id,name2_id)

輸出
(56156880 56156880)

1.2 如果是長字串就沒有最佳化機制!

>>> a = ‘this is a very long sentence‘>>> b = ‘this is a very long sentence‘>>> id(a)4394464720>>> id(b)4394464640
1.3 對於數字也是有最佳化機制的,範圍[-5,257)
列如範圍內:

a = 256
b = 256
id(a)
1502186480
id(b)
1502186480

範圍外就不行了:

a = 257

b = 257

id(a)
55420240
id(b)
55419312

身份運算





第二章 python 基礎資料類型

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.