python中的數據結構

變量將數據存儲在內存中。這就意味着在建立變量時會在內存中開闢一個空間。基於變量的數據類型,解釋器會分配指定內存,並決定什麼數據能夠被存儲在內存中。python

Python中的變量不須要聲明變量的賦值操做既是變量聲明和定義的過程。所以,每一個變量在使用前都必須賦值,變量賦值之後該變量纔會被建立。
數據結構

python中的數據結構主要包括:序列(如列表、元祖、字符串)、映射(如字典)、集合。ide

其中,序列中的元祖和字符串爲不可變類型。緣由,官方解釋:性能

One is performance: knowing that a string is immutable means we can allocate space for it at creation time, and the storage requirements are fixed and unchanging. This is also one of the reasons for the distinction between tuples and lists.ui

Another advantage is that strings in Python are considered as 「elemental」 as numbers. No amount of activity will change the value 8 to anything else, and in Python, no amount of activity will change the string 「eight」 to anything else.spa

也就是說:第一個是性能方面會更好,在建立時就分配固定大小的內存,第二個就是字符串、數字爲基本類型,相對其餘數據類型,改變的狀況要少。orm

相關文章
相關標籤/搜索