(Python基礎(chǔ)教程之五)Python中的數(shù)據(jù)類型

  1. Python基礎(chǔ)教程
  2. 在SublimeEditor中配置Python環(huán)境
  3. Python代碼中添加注釋
  4. Python中的變量的使用
  5. Python中的數(shù)據(jù)類型
  6. Python中的關(guān)鍵字
  7. Python字符串操作
  8. Python中的list操作
  9. Python中的Tuple操作
  10. Pythonmax()和min()–在列表或數(shù)組中查找最大值和最小值
  11. Python找到最大的N個(前N個)或最小的N個項目
  12. Python讀寫CSV文件
  13. Python中使用httplib2–HTTPGET和POST示例
  14. Python將tuple開箱為變量或參數(shù)
  15. Python開箱Tuple–太多值無法解壓
  16. Pythonmultidict示例–將單個鍵映射到字典中的多個值
  17. PythonOrderedDict–有序字典
  18. Python字典交集–比較兩個字典
  19. Python優(yōu)先級隊列示例

數(shù)據(jù)類型定義變量的類型。由于所有內(nèi)容都是Python中的對象,因此數(shù)據(jù)類型實際上是類。變量是類的實例。

在任何編程語言中,可以對不同類型的數(shù)據(jù)類型執(zhí)行不同的操作,其中某些數(shù)據(jù)類型與其他數(shù)據(jù)類型相同,而某些數(shù)據(jù)類型非常特定于該特定數(shù)據(jù)類型。

1. Python中的內(nèi)置數(shù)據(jù)類型

Python默認具有以下內(nèi)置數(shù)據(jù)類型。

Category Data types / Class names
Text/String str
Numeric int, float, complex
List list, tuple, range
Map dict
Set set, frozenset
Boolean bool
Binary bytes, bytearray, memoryview

2.詳細的數(shù)據(jù)類型

2.1。字符串

字符串可以定義為用單引號,雙引號或三引號引起來的字符序列。三引號(“””)可用于編寫多行字符串。

str數(shù)據(jù)類型

x = 'A'

y = "B"

z = """

C

"""

print(x) # prints A

print(y) # prints B

print(z) # prints C

print(x + y) # prints AB - concatenation

print(x*2) # prints AA - repeatition operator

name = str('john') # Constructor

sumOfItems = str(100) # type conversion from int to string

2.2。整數(shù),浮點數(shù),復(fù)雜

這些是數(shù)字類型。它們是在將數(shù)字分配給變量時創(chuàng)建的。

  • int 保留長度不受限制的帶符號整數(shù)。
  • float 保留浮點精度數(shù)字,并且它們的精度最高為15個小數(shù)位。
  • complex –復(fù)數(shù)包含實部和虛部。

數(shù)值類型

x = 2                   # int

x = int(2) # int  

x = 2.5                 # float

x = float(2.5) # float

x = 100+3j              # complex

x = complex(100+3j) # complex

2.3。列表,元組,范圍

在Python中,list是使用方括號()和逗號()編寫的一些數(shù)據(jù)的有序序列。列表可以包含不同類型的數(shù)據(jù)。[ ],

Slice [ :]運算符可用于訪問列表中的數(shù)據(jù)。

所述并置運算符(+)重復(fù)操作符(*的工作原理類似的str數(shù)據(jù)類型。

范圍可以被認為是sublist,一個的取出list使用切片運算符。

一個元組是類似list的-除了tuple是一個只讀的數(shù)據(jù)結(jié)構(gòu),我們不能修改一個元組的項目的規(guī)模和價值。另外,項目用括號括起來(, )。

清單類型

randomList = [1, "one", 2, "two"]

print (randomList); # prints [1, 'one', 2, 'two']

print (randomList + randomList); # prints [1, 'one', 2, 'two', 1, 'one', 2, 'two']

print (randomList * 2); # prints [1, 'one', 2, 'two', 1, 'one', 2, 'two']

alphabets = ["a", "b", "c", "d", "e", "f", "g", "h"] 

print (alphabets[3:]); # range - prints ['d', 'e', 'f', 'g', 'h']

print (alphabets[0:2]); # range - prints ['a', 'b']

randomTuple = (1, "one", 2, "two")

print (randomTuple[0:2]); # range - prints (1, 'one')

randomTuple[0] = 0      # TypeError: 'tuple' object does not support item assignment

2.4。字典

字典或字典是項的鍵值對有序集合。鍵可以保存任何原始數(shù)據(jù)類型,而值是任意的Python對象。

字典中的條目用逗號分隔并括在花括號中{, }。

字典類型

charsMap = {1:'a', 2:'b', 3:'c', 4:'d'};  

print (charsMap); # prints {1: 'a', 2: 'b', 3: 'c', 4: 'd'}

print("1st entry is " + charsMap[1]); # prints 1st entry is a

print (charsMap.keys()); # prints dict_keys([1, 2, 3, 4])

print (charsMap.values()); # prints dict_values(['a', 'b', 'c', 'd'])

2.5。設(shè)置,frozenset

python中的set可以定義為花括號中包含的各種項目的無序集合{, }。

集合中的元素不能重復(fù)。python set的元素必須是不可變的。

不同于list,沒有indexset元素。這意味著我們只能循環(huán)訪問的元素set。

凍結(jié)套是正常集的不變形式。這意味著我們無法刪除任何項目或?qū)⑵涮砑拥絻鼋Y(jié)集中。

設(shè)置類型

digits = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}  

print(digits) # prints {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}

print(type(digits)) # prints <class 'set'>

print("looping through the set elements ... ") 

for i in digits: 

print(i) # prints 0 1 2 3 4 5 6 7 8 9 in new lines

digits.remove(0) # allowed in normal set

print(digits) # {1, 2, 3, 4, 5, 6, 7, 8, 9}

frozenSetOfDigits = frozenset({0, 1, 2, 3, 4, 5, 6, 7, 8, 9})  

frozenSetOfDigits.remove(0) # AttributeError: 'frozenset' object has no attribute 'remove'

2.6。布爾

布爾值是兩個恒定的對象False和True。它們用于表示真值。在數(shù)字上下文中,它們的行為分別類似于整數(shù)0和1。

x = True

y = False

print(x) #True

print(y) #False

print(bool(1)) #True

print(bool(0)) #False

2.7。字節(jié),字節(jié)數(shù)組,內(nèi)存視圖

bytesbytearray用于處理二進制數(shù)據(jù)。所述memoryview使用緩沖協(xié)議來訪問其他二進制對象的存儲器,而無需進行復(fù)印。

字節(jié)對象是單個字節(jié)的不可變序列。僅在處理與ASCII兼容的數(shù)據(jù)時,才應(yīng)使用它們。

bytes文字的語法與文字的語法相同string,只是'b'添加了前綴。

bytearray對象總是通過調(diào)用構(gòu)造函數(shù)來創(chuàng)建的bytearray()。這些是可變的對象。

字節(jié),內(nèi)存視圖類型

x = b'char_data'

x = b"char_data"

y = bytearray(5)

z = memoryview(bytes(5))

print(x) # b'char_data'

print(y) # bytearray(b'\x00\x00\x00\x00\x00')

print(z) # <memory at 0x014CE328>

3. type()函數(shù)

該type()函數(shù)可用于獲取任何對象的數(shù)據(jù)類型。

獲取類型

x = 5

print(type(x)) # <class 'int'>

y = 'howtodoinjava.com'

print(type(y)) # <class 'str'>

將您的問題留在我的評論中。

學(xué)習(xí)愉快!

參考:Python文檔

作者:分布式編程
出處:https://zthinker.com/
如果你喜歡本文,請長按二維碼,關(guān)注 分布式編程
.

分布式編程

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容