python基礎資料型別 (Elementary Data Type)

來源:互聯網
上載者:User

標籤:python   資料類型   列表   字典   字串   

python標準資料類型

有六個標準的資料類型:
1、Number(數字)
2、String(字串)
3、tuple(元組)
4、list(列表)
5、dict(字典)
6、Sets(集合)

python資料類型分類

可變資料:
字典、列表
不可變資料:
數字、字串、元組、集合

python資料類型介紹Number

python3支援float、int、complex、bool
在python3中只有一種類型int,表示長整型,沒有python2中的long
樣本:

a,b,c,d = 1.1,1,1+3j,True

可以用type查看:

>>> map(lambda x:type(x),[1,1.1,1+3j,True])[<type ‘int‘>, <type ‘float‘>, <type ‘complex‘>, <type ‘bool‘>]

也可以用instance:

>>> isinstance(1.1,float)True
String字串

字串或串(String)是由數字、字母、底線組成的一串字元。
以字母、數字、底線組成的一串字元
它是程式設計語言中表示文本的類型
樣本:

定義字串:>>>a = ‘stringtype‘取出字串:>>>>>>a‘stringtype‘截取前5位字元:>>>a[0:5]   從0開始,但截止到5之前‘strin‘截取最後一位字元:>>>a[-1:]‘e‘
列表

列表用“[]”賦值
列表是使用最頻繁的一種資料類型。
截取的方式也可以用【開始:結束】來截取
樣本:

>>> data = [1,2,3,5,6,2,1,4]>>> data[0:2][1, 2]
元組

元組用"()"標識。內部元素用逗號隔開。但是元組不能二次賦值,相當於唯讀列表。
樣本:

>>> a = (1,2,3,4,5)>>>>>> a[0:3](1, 2, 3)嘗試改變,但是是失敗的:>>> a[2] = 5Traceback (most recent call last):  File "<stdin>", line 1, in <module>TypeError: ‘tuple‘ object does not support item assignment
字典

列表是有序的對象集合,字典是無序的對象集合。
兩者之間的區別在於:字典當中的元素是通過鍵來存取的,而不是通過位移存取。
字典用"{ }"標識。字典由索引(key)和它對應的值value組成。
樣本:

>>> a = {‘a‘:‘b‘,‘c‘:‘d‘,‘b‘:‘a‘}>>> a{‘a‘: ‘b‘, ‘c‘: ‘d‘, ‘b‘: ‘a‘}也可以用dict函數賦值:>>> a = dict([(‘a‘,‘b‘)])>>> a{‘a‘: ‘b‘}

建立空字典使用:

a = {}或a = dict()

python基礎資料型別 (Elementary Data Type)

聯繫我們

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