directory

  • Python bytearray functions
  • Python bytearray function
  • Differences between bytearray and bytes
    • 1. Bytes Immutable sequence of bytes
    • 2. Bytearray Mutable byte sequence
  • Four. Guess you like it

Recommended courses for learning Basic Python: Python Learning Directory >> Getting Started with Python

Python also has mutable byte sequences called bytearray. What is the difference? As the name implies, bytes are immutable, while bytearray is mutable! This article will have a detailed explanation!

Python bytearray functions

#! Usr /bin/env python # -* -coding :utf-8 _*- "" www.codersrc.com @file :Python bytearray function. py @time :2021/05/04 07:37 @motto: A thousand miles without a small step, a river without a small stream, the wonderful life of the program needs to be accumulated unrelentingly! Bytearray () -> empty bytearrayArray # 2. Defines a sequence of bytes with a specified number of bytes, populated with 0 by default, Cannot be a floating point number bytearray(int) -> bytes array of size given by the parameter initialized with null bytes # 3. Bytearray (bytes_or_buffer) -> mutable copy of bytes_or_buffer # 4. Bytes bytearray(string, encoding[, errors]) -> bytearray # 5. Defines a sequence of bytes that contains the specified contents. Bytes must be of type int and cannot contain variables of other types such as float or STR. Bytearray (iterable_of_ints) -> bytearrayCopy the code

Return value: returns a new mutable byte sequence. The mutable bytearray is preceded by a character b, for example:

b'\x64\x65\x66'
b'i love you'
b'https://www.codersrc.com'
Copy the code

Any output preceded by the character B is a sequence of bytes; Bytearray Is a mutable sequence of bytes. Bytes is an immutable sequence of bytes.

Python bytearray function

#! Usr /bin/env python # -* -coding :utf-8 _*- "" www.codersrc.com @file :Python bytearray function. py @time :2021/05/04 07:37 @motto: A thousand miles without a small step, a river without a small stream, the wonderful life of the program needs to be accumulated unrelentingly! """ if __name__ == "__main__": Bytearray b1 = bytearray() print(b1) print(type(b1)) print("***"*20) print("***"*20) B2 = bytearray(10) print(b2) print(type(b2)) print("***" * 20) Utf-8) print(b3) print(type(b3)) print("*** *" * 20) B1 = ByteArray ([1.1, 2.2, 3, 4]) >> > TypeError: float b1 = ByteArray ([1.1, 2.2, 3, 4]) >> > TypeError: An INTEGER is required # bytes the byte sequence must be an integer between 0 and 255 and cannot contain STR type B1 = ByteArray ([1, 'a', 2, 3]) >> > TypeError: An INTEGER is required # bytes The byte sequence must be an integer between 0 and 255 and cannot be greater than or equal to 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'> * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * "'Copy the code

Differences between bytearray and bytes

  • Similarity: Bytearray and bytes ranges from 0 to 256.
  • Differences: Bytearray is a mutable sequence of bytes. Bytes is an immutable sequence of bytes.

1. Bytes Immutable sequence of bytes

#! Usr /bin/env python # -* -coding :utf-8 _*- "" www.codersrc.com @file :Python bytearray function. py @time :2021/05/04 07:37 @motto: A thousand miles without a small step, a river without a small stream, the wonderful life of the program needs to be accumulated unrelentingly! """ if __name__ == "__main__": # bytes immutable byte sequence b1 = b"abcd" for I in b1: Print (I,end=" ") print() b1[0] =" " File "E:/Project/python/python_project/untitled10/123.py", line 22, in <module> b1[0] = "A" TypeError: 'bytes' object does not support item assignment '''Copy the code

2. Bytearray Mutable byte sequence

#! Usr /bin/env python # -* -coding :utf-8 _*- "" www.codersrc.com @file :Python bytearray function. py @time :2021/05/04 07:37 @motto: A thousand miles without a small step, a river without a small stream, the wonderful life of the program needs to be accumulated unrelentingly! """ if __name__ == "__main__": B2 = bytearray(b1) print(" ",b2) b2[0] = 65 print(" ",b2) Bytearray (b'abcd') ""Copy the code

Four.Guess you like

  1. The Python for loop
  2. The Python string
  3. The Python list
  4. The Python tuple tuple
  5. Python dictionary dict
  6. Python conditional derivations
  7. Python list derivations
  8. Python dictionary derivations
  9. Python function declarations and calls
  10. Python variable argument *argc/**kargcs
  11. Python anonymous function lambda
  12. Python return logic determines expressions
  13. Python string/list/tuple/dictionary conversions
  14. Python local and global variables
  15. The Python type function is different from the isinstance function
  16. Python is differs from ==
  17. Python mutable and immutable data types
  18. Shallow and deep copies of Python

Python bytearray functions

This article is published by the blog – Ape Say Programming Ape Say programming!