*Memo:
- My post explains a bytearray (2).
- My post explains a bytearray (3).
- My post explains a bytearray (4).
- My post explains a bytearray (5).
- My post explains bytearray().
- My post explains a bytes (1).
- My post explains a string (1).
- My post explains string, bytes and bytearray functions.
- My post explains 10 collection types and their related posts.
A bytearray:
- is the ordered mutable(unhashable) collection of the zero or more bytes represented with ASCII characters and whose type is
bytearrayand 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.
- Each byte must be between
- can be used with len() to get the length.
- is
Trueif it's non-empty andFalseif it's empty, checking it with bool(). - is
Falseif it's non-empty andTrueif it's empty, inverting the truth value withnot. - can be checked if specific bytes are and aren't in the bytearray with
inandnot inrespectively. - can be checked if the bytearray is and isn't referred to by two variables with
isandis notrespectively. - and other bytearray can be checked if:
- all the bytes in them are and aren't equal with
==and!=respectively. - the bytearray is greater than other bytearray with
>. - the bytearray is greater than or equal to other bytearray with
>=. - the bytearray is less than other bytearray with
<. - the bytearray is less than or equal to other bytearray with
<=.
- all the bytes in them are and aren't equal with
- and other bytearray cannot be checked if they have and don't have their common bytes with
bool()and&and withnotand&respectively. - can be enlarged with
*and a number. - and other bytearrays can be concatenated with
+. - and other bytearray 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). - the bytes in either the bytearray or other bytearray but not both with
'^'(Symmetric Difference: A Δ B).
- all the bytes in them with
- can be iterated with a
forstatement. - can be unpacked with an assignment and
forstatement, 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.
- For
- 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.
- For
- 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.
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'))
print(bytearray(b'ABCDE') * 3)
print(bytearray(b'01234') * 3)
print(bytearray(b'') * 3)
print(bytearray(b'ABC') + bytearray(b'DE') + bytearray(b'FGHI'))
for v in bytearray(b'ABC'): print(v)
v1, v2, v3 = bytearray(b'ABC'); print(v1, v2, v3)
v1, *v2, v3 = bytearray(b'ABCDEF'); print(v1, v2, v3)
for v1, v2, v3 in [bytearray(b'ABC'),
bytearray(b'DEF')]: print(v1, v2, v3)
for v1, *v2, v3 in [bytearray(b'ABCDEF'),
bytearray(b'GHIJKL')]: print(v1, v2, v3)
print(*bytearray(b'ABCD'), *bytearray(b'EF'))
print([*bytearray(b'ABCD'), *bytearray(b'EF')])
# No error
print(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'))
print(bytearray(b'ABCD') & bytearray(b'ACE'))
print(bytearray(b'ABCD') - bytearray(b'ACE'))
print(bytearray(b'ABCD') ^ bytearray(b'ACE'))
print(bytearray(b'ABCDE') * 1000000000)
# Error
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'>
v = bytearray(b"Hello World")
print(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.""")
print(v)
# bytearray(b"I\'m John.")
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')
v = bytearray(b'''Apple
Orange
Banana
Kiwi''')
print(v)
# bytearray(b'Apple\nOrange\nBanana\nKiwi')
v = bytearray(b"""
Apple
Orange
Banana
Kiwi
""")
print(v)
# bytearray(b'\nApple\n Orange\n Banana\n Kiwi\n')
Top comments (2)
So, when we are talking about mutable sequences, it can be changed, maybe its a typo or misconception
Thank you. I corrected the mistake
cannottocan.