DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on • Edited on

Bytearray in Python (1)

Buy Me a Coffee

*Memo:

A bytearray:

  • is the ordered mutable(unhashable) collection of the zero or more bytes represented with ASCII characters and whose type is bytearray and which allows duplicated bytes:
    • Each byte must be between [0, 255] from 256 ASCII characters.
    • A bytearray is for computer to understand.
    • Ordered means that the order of each byte in a bytearray is kept so it guarantees that the order is always the same unless changed.
    • Mutable(Unhashable) means the bytes of a bytearray can be changed.
  • can be used with len() to get the length.
  • is True if it's non-empty and False if it's empty, checking it with bool().
  • is False if it's non-empty and True if it's empty, inverting the truth value with not keyword.
  • can be checked if specific bytes are and aren't in the bytearray with in keyword and with not and in keyword respectively.
  • can be checked if the bytearray is and isn't referred to by two variables with is keyword and with is and not keyword respectively.
  • and other bytearray can be checked if all the bytes in:
    • them are and aren't the same with == and != respectively.
    • the bytearray are in other bytearray with <=.
    • other bytearray are in the bytearray with >=.
    • the bytearray and other bytes are in other bytearray with <.
    • other bytearray and other bytes are in the bytearray with >.
  • and other bytearray cannot be checked if they have and don't have their common bytes with bool() and & and with not keyword and & respectively.
  • can be enlarged with * and a number.
  • and other bytearrays can be concatenated with +.
  • and other bytearrays cannot return:
    • all the bytes in them with '|' (Union: A ∪ B).
    • their common bytes with '&' (Intersection: A ∩ B).
    • the bytes in the bytearray which aren't in other bytearray with '-' (Difference: A - B).
  • and other bytearray cannot return the bytes in the bytearray but not in other bytearray or not in the bytearray but in other bytearray with '^' (Symmetric Difference: A Δ B).
  • can be iterated with a for statement.
  • can be unpacked with an assignment and for statement, function and * but not with **.
  • can be created by bytearray() with or without several types of values:
    • For bytearray(), the words type conversion are also suitable in addition to the word creation.
  • can be encoded to from a string by encode():
    • For encode(), the words creation and type conversion are also suitable in addition to the word encoding.
  • cannot be big because it gets MemoryError.
  • can be read by indexing and slicing.
  • can be changed by indexing, slicing and a del statement.
  • can be continuously used through multiple variables.
  • can be shallow-copied by bytearray.copy(), copy.copy(), bytearray() and slicing.
  • can be deep-copied by copy.deepcopy().

Be careful, a big bytearray gets MemoryError.


bytearray() can create a bytearray as shown below:

*Memo:

  • \' is the escape sequence to output '.
v = bytearray(b'') # Empty bytearray
v = bytearray(b"Hello World")
v = bytearray(B'I\'m John.')
v = bytearray(B"I'm John.")
v = bytearray(b'''I'm John.''')
v = bytearray(b"""I'm John.""")
v = bytearray(B'''Apple Orange Banana Kiwi''')
v = bytearray(b'Apple' b" Orange" b'''Banana''' B"""Kiwi""")
v = bytearray(b'''Apple
Orange
Banana
Kiwi''')
v = bytearray(b"""
Apple
   Orange
       Banana
           Kiwi
""")
# No error

print(len(bytearray(b"Let's go!")))
print(bool(bytearray(b'0')))
print(bool(bytearray(b' ')))
print(bool(bytearray(b'')))
print(not bytearray(b'0'))
print(not bytearray(b' '))
print(not bytearray(b''))
print(bytearray(b'A') in bytearray(b'ABC'))
print(bytearray(b'A') not in bytearray(b'ABC'))
print(bytearray(b'ABC') is bytearray(b'ABC'))
print(bytearray(b'ABC') is not bytearray(b'ABC'))
print(bytearray(b'ABC') != bytearray(b'ABC'))
print(bytearray(b'ABC') <= bytearray(b'ABC'))
print(bytearray(b'ABC') >= bytearray(b'ABC'))
print(bytearray(b'ABC') < bytearray(b'ABC'))
print(bytearray(b'ABC') > bytearray(b'ABC'))
v = bytearray(b'ABCDE') * 3
v = bytearray(b'01234') * 3
v = bytearray(b'') * 3
v = bytearray(b'ABC') + bytearray(b'DE') + bytearray(b'FGHI')
for v in bytearray(b'ABC'): pass
v1, v2, v3 = bytearray(b'ABC')
v1, *v2, v3 = bytearray(b'ABCDEF')
for v1, v2, v3 in [bytearray(b'ABC'), bytearray(b'DEF')]: pass
for v1, *v2, v3 in [bytearray(b'ABCDEF'), bytearray(b'GHIJKL')]: pass
print(*bytearray(b'ABCD'), *bytearray(b'EF'))
print([*bytearray(b'ABCD'), *bytearray(b'EF')])
# No error

v = b"Lёт's gφ!" # Let's go!
print(bool(bytearray(b'ABC') & bytearray(b'BD')))
print(not (bytearray(b'ABC') & bytearray(b'BD')))
print(bytearray(b'AE') | bytearray(b'ACE') | bytearray(b'ABDE'))
print(bytearray(b'AE') & bytearray(b'ACE') & bytearray(b'ABDE'))
print(bytearray(b'AE') - bytearray(b'ACE') - bytearray(b'ABDE'))
print(bytearray(b'ABCD') ^ bytearray(b'ACE'))
v = bytearray(b'ABCDE') * 1000000000
# Error
Enter fullscreen mode Exit fullscreen mode

A bytearray is the ordered mutable(unhashable) collection of the zero or more bytes represented with ASCII characters and whose type is bytearray and which allows duplicated bytes as shown below:

v = bytearray(b'') # Empty bytearray

print(v)
# bytearray(b'')

print(type(v))
# <class 'bytearray'>
Enter fullscreen mode Exit fullscreen mode
v = bytearray(b"Hello World")

print(v)
# bytearray(b'Hello World')
Enter fullscreen mode Exit fullscreen mode
v = bytearray(B"I'm John.")
v = bytearray(B'I\'m John.')
v = bytearray(b'''I'm John.''')
v = bytearray(b"""I'm John.""")

print(v)
# bytearray(b"I\'m John.")
Enter fullscreen mode Exit fullscreen mode
v = bytearray(B'''Apple Orange Banana Kiwi''')
v = bytearray(b'Apple' b" Orange" b''' Banana''' B""" Kiwi""")

print(v)
# bytearray(b'Apple Orange Banana Kiwi')
Enter fullscreen mode Exit fullscreen mode
v = bytearray(b'''Apple
Orange
Banana
Kiwi''')

print(v)
# bytearray(b'Apple\nOrange\nBanana\nKiwi')
Enter fullscreen mode Exit fullscreen mode
v = bytearray(b"""
Apple
   Orange
       Banana
           Kiwi
""")

print(v)
# bytearray(b'\nApple\n   Orange\n       Banana\n           Kiwi\n')
Enter fullscreen mode Exit fullscreen mode

Top comments (2)

Collapse
 
mrm4rc profile image
Marcelo Almeida

Mutable(Unhashable) means the bytes of a bytearray cannot be changed.

So, when we are talking about mutable sequences, it can be changed, maybe its a typo or misconception

Collapse
 
hyperkai profile image
Super Kai (Kazuya Ito)

Thank you. I corrected the mistake cannot to can.