*Memo:
My post explains a bytearray (1).
My post explains bytearray().
My post explains a bytes (1).
My post explains a string (1).
A bytearray can be read by indexing and slicing as shown below:
*Memo:
- Indexing can be done with one or more
[index]in the range[The 1st element index, The last element index]:
index(Required-Type:int):
- Don't use
index=.
- Don't use
- Error occurs if
indexis out of range.
- Slicing can be done with one or more
[start:end:step]in the range[start, end):
start(Optional-Default:None-Type:intand NoneType):
- It's a start index(inclusive).
- If it's
None, it's the 1st element index. - Don't use
start=.
end(Optional-Default:None-Type:intand NoneType):
- It's an end index(exclusive).
- If it's
None, it's the next index to the last element index. - Don't use
end=.
step(Optional-Default:None-Type:intand 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. - Error doesn't occur even if
[start, end)is out of the range[The 1st element index, The last element index].
index,startandendcan be signed indices(zero and positive and negative indices).
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])
# 97 98 99 100 101 102 103 104
v[1] = ord('B')
v[-7] = ord('B')
print(v)
# bytearray(b'aBcdefgh')
v[3] = ord('D')
v[-5] = ord('D')
print(v)
# bytearray(b'aBcDefgh')
v[6] = ord('G')
v[-2] = ord('G')
v[1], v[3], v[6] = ord('B'), ord('D'), ord('G')
v[-7], v[-5], v[-2] = ord('B'), ord('D'), ord('G')
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])
# IndexError: bytearray index out of range
v = bytearray(b'ABCDEFGH')
print(v[:])
print(v[::])
print(v[None:None:None])
print(v[0: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'')
v = bytearray(b'ABCDEFGH')
print(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])
# 65 66 67 68 69 70 71 72
print(v[:])
print(v[::])
print(v[None:None:None])
print(v[0: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'')
v = bytearray(b'ABCDEFGH')
print(v[-9], v[8])
# IndexError: bytearray index out of range
A bytearray can be changed by indexing, slicing and a del statement as shown below:
*Memo:
- An iterable must be assigned to a sliced variable.
- A
delstatement can remove zero or more elements from a bytearray by indexing and slicing and can remove one or more variables themselves.
v = bytearray(b'abcdef')
v[1] = ord(b'X')
v[3:5] = [ord(b'Y'), ord(b'Z')]
print(v)
# bytearray(b'aXcYZf')
v = bytearray(b'abcdef')
del v[1], v[3:5]
print(v)
# bytearray(b'acd')
v = bytearray(b'abcdef')
del v
print(v)
# NameError: name 'v' is not defined
A bytearray can be continuously used through multiple variables as shown below:
v1 = v2 = v3 = bytearray(b'abcde') # Equivalent
# v1 = bytearray(b'abcde')
v1[0] = ord(b'X') # v2 = v1
v2[3:5] = [ord(b'Y'), ord(b'Z')] # v3 = v2
del v3[1:3]
print(v1) # bytearray(b'XYZ')
print(v2) # bytearray(b'XYZ')
print(v3) # bytearray(b'XYZ')
A bytearray can be shallow-copied but cannot be deep-copied as shown below:
<Shallow & Deep copy>:
*Memo:
v1andv2refer to different bytearrays and each same byte.
iskeyword can check ifv1andv2refer to the same bytearray and each same byte.
bytearray.copy(), copy.copy(), bytearray() and slicing can shallow-copy a bytearray:
bytearray.copy()has no arguments.
copy.deepcopy() cannot deep-copy but can shallow-copy a bytearray.
import copy
v1 = bytearray(b'abcde')
v2 = v1.copy()
v2 = copy.copy(v1)
v2 = bytearray(v1)
v2 = v1[:]
v2 = copy.deepcopy(v1)
print(v1, v1[2]) # bytearray(b'abcde') 99
print(v2, v2[2]) # bytearray(b'abcde') 99
print(v1 is v2, v1[2] is v2[2])
# False True
v2[1]= ord('X')
v2[3]= ord('Y')
# ↓ ↓
print(v1) # bytearray(b'abcde')
print(v2) # bytearray(b'aXcYe')
# ↑ ↑
SOCIAL SHARE CARD GENERATOR