*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 bytearray shallow and deep copy.
- My post explains a bytes (1).
- My post explains a string (1).
- My post explains string, bytes and bytearray functions.
- My post explains a list (1).
- My post explains a tuple (1).
- My post explains a set (1).
- My post explains a frozenset (1).
- My post explains a dictionary (1).
- My post explains an iterator (1).
- My post explains a range (1).
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 withnotkeyword. - can be checked if specific bytes are and aren't in the bytearray with
inkeyword and withnotandinkeyword respectively. - can be checked if the bytearray is and isn't referred to by two variables with
iskeyword and withisandnotkeyword 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
>.
- them are and aren't the same with
- and other bytearray cannot be checked if they have and don't have their common bytes with
bool()and&and withnotkeyword 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).
- all the bytes in them with
- 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
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'))
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
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.