目錄html
基礎 Python 學習路線推薦 : Python 學習目錄 >> Python 基礎入門python
Python 除了 bytes 字節序列 以外,還有 bytearray 可變的字節序列,具體區別在哪呢?顧名思義,bytes 是不可變的,而 bytearray 是可變的!具體本文會有詳細的講解!編程
# !usr/bin/env python # -*- coding:utf-8 _*- """ @Author:猿說編程 @Blog(我的博客地址): www.codersrc.com @File:Python bytearray 函數.py @Time:2021/05/04 07:37 @Motto:不積跬步無以致千里,不積小流無以成江海,程序人生的精彩須要堅持不懈地積累! """ # 1.定義空的字節序列bytearray bytearray() -> empty bytearrayarray # 2.定義指定個數的字節序列bytes,默認以0填充,不能是浮點數 bytearray(int) -> bytes array of size given by the parameter initialized with null bytes # 3.定義指定內容的字節序列bytes bytearray(bytes_or_buffer) -> mutable copy of bytes_or_buffer # 4.定義指定內容的字節序列bytes bytearray(string, encoding[, errors]) -> bytearray # 5.定義指定內容的字節序列bytes,只能爲int 類型,不能含有float 或者 str等其餘類型變量 bytearray(iterable_of_ints) -> bytearray
返回值 : 返回一個新的可變字節序列,可變字節序列 bytearray 有一個明顯的特徵,輸出的時候最前面會有一個字符 b 標識,舉個例子:函數
b'\x64\x65\x66' b'i love you' b'https://www.codersrc.com'
凡是輸出前面帶有字符 b 標識的都是字節序列 ;bytearray 可變的字節序列,bytes 是不可變的字節序列;學習
# !usr/bin/env python # -*- coding:utf-8 _*- """ @Author:猿說編程 @Blog(我的博客地址): www.codersrc.com @File:Python bytearray 函數.py @Time:2021/05/04 07:37 @Motto:不積跬步無以致千里,不積小流無以成江海,程序人生的精彩須要堅持不懈地積累! """ if __name__ == "__main__": # 定義空的字節序列bytearray b1 = bytearray() print(b1) print(type(b1)) print("***"*20) # 定義指定個數的字節序列bytes,默認以0填充,不能是浮點數 b2 = bytearray(10) print(b2) print(type(b2)) print("***" * 20) # 定義指定內容的字節序列bytes b3 = bytes('abc', 'utf-8') print(b3) print(type(b3)) print("***" * 20) # 正常輸出 b1 = bytearray([1, 2, 3, 4]) >> > b'\x01\x02\x03\x04' # bytes字節序列必須是 0 ~ 255 之間的整數,不能含有float類型 b1 = bytearray([1.1, 2.2, 3, 4]) >> > TypeError: an integer is required # bytes字節序列必須是 0 ~ 255 之間的整數,不能含有str類型 b1 = bytearray([1, 'a', 2, 3]) >> > TypeError: an integer is required # bytes字節序列必須是 0 ~ 255 之間的整數,不能大於或者等於256 b1 = bytearray([1, 257]) >> > ValueError: bytes must be in range(0, 256) ''' 輸出結果: bytearray(b'') <class 'bytearray'> ************************************************************ bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00') <class 'bytearray'> ************************************************************ b'abc' <class 'bytes'> ************************************************************ '''
# !usr/bin/env python # -*- coding:utf-8 _*- """ @Author:猿說編程 @Blog(我的博客地址): www.codersrc.com @File:Python bytearray 函數.py @Time:2021/05/04 07:37 @Motto:不積跬步無以致千里,不積小流無以成江海,程序人生的精彩須要堅持不懈地積累! """ if __name__ == "__main__": # bytes不可變字節序列 b1 = b"abcd" for i in b1: print(i,end=" ") print() b1[0] = "A" ''' 輸出結果: 97 98 99 100 Traceback (most recent call last): File "E:/Project/python/python_project/untitled10/123.py", line 22, in <module> b1[0] = "A" TypeError: 'bytes' object does not support item assignment '''
# !usr/bin/env python # -*- coding:utf-8 _*- """ @Author:猿說編程 @Blog(我的博客地址): www.codersrc.com @File:Python bytearray 函數.py @Time:2021/05/04 07:37 @Motto:不積跬步無以致千里,不積小流無以成江海,程序人生的精彩須要堅持不懈地積累! """ if __name__ == "__main__": # bytearray可變字節序列 b1 = b"abcd" b2 = bytearray(b1) print("修改以前:",b2) b2[0] = 65 print("修改以後:", b2) ''' 輸出結果: 修改以前: bytearray(b'abcd') 修改以後: bytearray(b'Abcd') '''
未經容許不得轉載:猿說編程 » Python bytearray 函數ui
本文由博客 - 猿說編程 猿說編程 發佈!code