*Memo:
- My post explains a bytearray (1).
- My post explains a bytearray (2).
- My post explains a bytearray (3).
- My post explains a bytearray (5).
- My post explains bytearray().
A bytearray can be read by indexing or changed by indexing and a del statement as shown below:
*Memo:
- Indexing can be done with one or more
[index]in the range[The 1st index, The last index]:-
index(Required-Type:int):- Don't use
index=.
- Don't use
-
indexcan be signed indices(zero and positive and negative indices). - Error occurs if
indexis out of range. -
[slice][index]can be used.
-
- A
delstatement can remove one or more bytes from a bytearray by indexing and can remove one or more variables themselves.
v = bytearray(b'abcdefgh')
print(v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7])
print(v[-8], v[-7], v[-6], v[-5], v[-4], v[-3], v[-2], v[-1])
print(v[0:1][0], v[1:2][0], v[2:3][0], v[3:4][0],
v[4:5][0], v[5:6][0], v[6:7][0], v[7:8][0])
print(v[-8:-7][0], v[-7:-6][0], v[-6:-5][0], v[-5:-4][0],
v[-4:-3][0], v[-3:-2][0], v[-2:-1][0], v[-1:8][0])
# 97 98 99 100 101 102 103 104
v[1] = ord('B')
v[-7] = ord('B')
v[1] = 66
v[-7] = 66
print(v)
# bytearray(b'aBcdefgh')
v[3] = ord('D')
v[-5] = ord('D')
v[3] = 68
v[-5] = 68
print(v)
# bytearray(b'aBcDefgh')
v[6] = ord('G')
v[-2] = ord('G')
v[6] = 71
v[-2] = 71
v[1], v[3], v[6] = ord('B'), ord('D'), ord('G')
v[-7], v[-5], v[-2] = ord('B'), ord('D'), ord('G')
v[1], v[3], v[6] = 66, 68, 71
v[-7], v[-5], v[-2] = 66, 68, 71
print(v)
# bytearray(b'aBcDefGh')
del v[1], v[2], v[4]
# del v[-7], v[-5], v[-2]
print(v)
# bytearray(b'acefh')
del v
print(v)
# NameError: name 'v' is not defined
v = bytearray(b'abcdefgh')
print(v[-9], v[8])
v[-9], v[8] = ord('X'), ord('Y')
v[-9], v[8] = 88, 89
del v[-9], v[8]
# IndexError: bytearray index out of range
A bytearray can be read by slicing as shown below:
*Memo:
- Slicing can be done with one or more
[start:end:step]in the range[start, end):-
start(Optional-Default:None-Type:int/NoneType):- It's a start index(inclusive).
- If it's
None, it's the 1st index. - Don't use
start=.
-
end(Optional-Default:None-Type:int/NoneType):- It's an end index(exclusive).
- If it's
None, it's the bytearray length. - Don't use
end=.
-
step(Optional-Default:None-Type:int/NoneType):- It's the interval of indices.
- If it's
None, it's1. - It cannot be zero.
- Don't use
end=.
- The
[]with at least one:is slicing. -
startandendcan be signed indices(zero and positive and negative indices). - Error doesn't occur even if
[start, end)is out of the range[The 1st index, The bytearray length).
-
v = bytearray(b'ABCDEFGH')
print(v[:])
print(v[::])
print(v[None:None:None])
print(v[0:8:1])
print(v[-8:8:1])
print(v[-100:100:1])
# bytearray(b'ABCDEFGH')
print(v[::2])
# bytearray(b'ACEG')
print(v[::-2])
# bytearray(b'HFDB')
print(v[2:])
print(v[-6:])
print(v[2::])
print(v[-6::])
# bytearray(b'CDEFGH')
print(v[2::2])
print(v[-6::2])
# bytearray(b'CEG')
print(v[2::-2])
print(v[-6::-2])
# bytearray(b'CA')
print(v[:6])
print(v[:-2])
print(v[:6:])
print(v[:-2:])
# bytearray(b'ABCDEF')
print(v[:6:2])
print(v[:-2:2])
# bytearray(b'ACE')
print(v[:6:-2])
print(v[:-2:-2])
# bytearray(b'H')
print(v[2:6])
print(v[-6:-2])
print(v[2:6:])
print(v[-6:-2:])
# bytearray(b'CDEF')
print(v[2:6:2])
print(v[-6:-2:2])
# bytearray(b'CE')
print(v[2:6:-2])
print(v[-6:-2:-2])
# bytearray(b'')
Top comments (0)