Python 標準資料型別

2022-06-19 13:15:14 字數 2656 閱讀 6641

python中存在 數字、字串、列表、元組、字典、集合(集合不常用),資料型別。

數字資料型別,包括整數、浮點數、複數和布林型別。

整數 int 長整型:包括正數,負數,0。

浮點數 float 浮點型:帶有小數點的實數。

複數 complex :複數由 實部和虛部組成,例 3+4j 或 3+4j (虛部的 j )不區分大小寫。實部和虛部可以是浮點數和整數。

布林型別 bool:當判斷條件為真時,返回true。當條件為假時,返回false。

#

數字資料型別

#num_int = 123

#print(type(num_int))##

###num_float = 12.23

#print(type(num_float))##

##num_bool = true

#print(type(num_bool))##

###num_complex = 5+6j

#print(type(num_complex))##

字串:字串是由單引號 ' 或雙引號 " 括起來的。

#

字串資料型別

strs = "

hello world.

"print

(type(strs))

#

列表:列表是將元素寫在方括號中 [ ] ,使用,逗號將方括號 [ ] 中的元素分隔開所形成的的序列物件。

#

空列表lst =

print

(type(lst))

#建立空列表

lst = ['a'

]print

(type(lst))

#建立包含乙個元素的列表

lst = [1,2.3,8+9j,'

abc',(4,5),,]

print(lst)#

建立包含有多個元素的列表

#[1, 2.3, (8+9j), 'abc', (4, 5), , ]

#列表中可以存放 int、float、complex、字串、元組、集合、字典 資料型別

元組:元組與列表相似,只是將最外面的方括號[ ] 變成了括號( )。元組的元素不能夠進行修改。

#

空元組tuple_1 =()

print

(type(tuple_1))##

包含乙個元素的元組

tuple_2 = ('

abc'

,)print

(type(tuple_2))##

建立多個元素的元組

tuple_3 = (1,2,3,4,5,6)

print

(type(tuple_3))

#

字典: 對的元素的集合。字典內部的元素是無序的,通過鍵來獲取鍵所對應的值。字典中的鍵是不能夠改變的,並且是唯一的。

#

建立空字典

dic_1 =

print

(dic_1)#{}

print

(type(dic_1))

#dic_2 =

print

(dic_2)

#print

(type(dic_2))

#dic_3 = ,(4,5):'

str',123:[4,5,6]}

#鍵為不可變型別 字串、元組、數字

print

(dic_3)

#, (4, 5): 'str', 123: [4, 5, 6]}

print

(type(dic_3))

#

集合:集合主要是進行 刪除重複元素 和 測試兩個集合之間的關係 。

#

建立空集合

set_1 =set()

print

(set_1)

#set()

print

(type(set_1))##

建立乙個元素的集合,可以不使用 ,

set_2 =

print

(set_2)

#print

(type(set_2))

#set_3 =

print

(set_3)

#print

(type(set_3))##

建立多個元素的集合

set_4 =

print

(set_4)

#print

(type(set_4))##

集合中不能包含列表和字典物件

2020-02-04

python 標準資料型別

資料型別 set number string list tuple dict bool 標準資料型別 none number string bool 1.none 主要為了判斷存在與否 2.number int long float complex id 查詢記憶體位址 type 查詢資料型別 3....

Python標準資料型別 數字型別

python3中標準資料型別有 數字 number 字串 string 列表 list 元組 tuple 集合 set 字典 dictionary 其中不可變資料有3個 數字 number 字串 string 元組 tuple 可變資料有2個 列表 list 集合 set 字典 dictionary...

Python 標準資料型別 Bytes

bytes 物件是由單個位元組作為基本元素 8位,取值範圍 0 255 組成的序列,為不可變物件。bytes 物件只負責以二進位制位元組序列的形式記錄所需記錄的物件,至於該物件到底表示什麼 比如到底是什麼字元 則由相應的編碼格式解碼所決定。我們可以通過呼叫 bytes 類 沒錯,它是類,不是函式 生...