Python bytearray 函數 - Python零基礎入門教程

目錄html

基礎 Python 學習路線推薦 : Python 學習目錄 >> Python 基礎入門python

Python 除了 bytes 字節序列 以外,還有 bytearray 可變的字節序列,具體區別在哪呢?顧名思義,bytes 是不可變的,而 bytearray 是可變的!具體本文會有詳細的講解!編程

一.Python 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 是不可變的字節序列;學習

二.Python bytearray 函數使用

# !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'>
************************************************************
'''

三.bytearray 與 bytes 區別

  • 相同點:bytearray 與 bytes 取值範圍都是 0 ~ 256 ;
  • 不一樣點:bytearray 可變的字節序列,bytes 是不可變的字節序列 ;

1. 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
'''

2.bytearray 可變字節序列

# !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')
'''

四.猜你喜歡

  1. Python for 循環
  2. Python 字符串
  3. Python 列表 list
  4. Python 元組 tuple
  5. Python 字典 dict
  6. Python 條件推導式
  7. Python 列表推導式
  8. Python 字典推導式
  9. Python 函數聲明和調用
  10. Python 不定長參數 *argc/**kargcs
  11. Python 匿名函數 lambda
  12. Python return 邏輯判斷表達式
  13. Python 字符串/列表/元組/字典之間的相互轉換
  14. Python 局部變量和全局變量
  15. Python type 函數和 isinstance 函數區別
  16. Python is 和 == 區別
  17. Python 可變數據類型和不可變數據類型
  18. Python 淺拷貝和深拷貝

未經容許不得轉載:猿說編程 » Python bytearray 函數ui

本文由博客 - 猿說編程 猿說編程 發佈!code

相關文章
相關標籤/搜索