DEV Community

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

Posted on

Bytearray in Python (3)

Buy Me a Coffee

*Memo:

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 byte index, The last byte index]:
    • index(Required-Type:int):
      • Don't use index=.
    • Error occurs if index is out of range.
  • index can be signed indices(zero and positive and negative indices).
  • A del statement 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])
# 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
Enter fullscreen mode Exit fullscreen mode
v = bytearray(b'abcdefgh')

print(v[-9], v[8])
v[-9] = ord('X')
v[8] = ord('Y')
del v[-9], v[8]
# IndexError: bytearray index out of range
Enter fullscreen mode Exit fullscreen mode

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 byte 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 next index to the last byte index.
      • Don't use end=.
    • step(Optional-Default:None-Type:int/NoneType):
      • It's the interval of indices.
      • If it's None, it's 1.
      • It cannot be zero.
      • Don't use end=.
    • The [] with at least one : is slicing.
    • start and end can be signed indices(zero and positive and negative indices).
    • Error doesn't occur even if [start, end) is out of the range [The 1st byte index, The last byte index].
  • Indexing and slicing can be used together.
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'')
Enter fullscreen mode Exit fullscreen mode

Top comments (0)