DEV Community

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

Posted on • Edited on

Bytearray in Python (2)

Buy Me a Coffee

*Memo:

A bytearray can be used with len() to get the length as shown below:

v = bytearray(b"Let's go!")

print(len(v))
# 9
Enter fullscreen mode Exit fullscreen mode

A non-empty bytearray and empty bytearray are:

  • True and False, checking them with bool() respectively.
  • False and True, inverting their truth values with not keyword respectively.
# Non-empty bytearray
print(bool(bytearray(b'0')))
print(bool(bytearray(b' ')))
# True

# Empty bytearray
print(bool(bytearray(b'')))
# False
Enter fullscreen mode Exit fullscreen mode
# Non-empty bytearray
print(not bytearray(b'0'))
print(not bytearray(b' '))
# False

# Empty bytearray
print(not bytearray(b''))
# True
Enter fullscreen mode Exit fullscreen mode

A bytearray can be checked if specific bytes are and aren't in the bytearray with in keyword and with not and in keyword respectively as shown below:

v = bytearray(b'ABC')

print(bytearray(b'A') in v)
print(bytearray(b'BC') in v)
print(bytearray(b'ABC') in v)
print(bytearray(b'') in v)
# True

print(bytearray(b'a') in v)
# False
Enter fullscreen mode Exit fullscreen mode
v = bytearray(b'ABC')

print(bytearray(b'A') not in v)
print(bytearray(b'BC') not in v)
print(bytearray(b'ABC') not in v)
print(bytearray(b'') not in v)
# False

print(bytearray(b'a') not in v)
# True
Enter fullscreen mode Exit fullscreen mode

A bytearray can be checked if the bytearray is and isn't referred to by two variables with is keyword and with is and not keyword respectively as shown below:

v1 = bytearray(b'ABC')
v2 = bytearray(b'ABC')
v3 = v1

print(v1 is v2) # False
print(v1 is v3) # True

print(v1 is not v2) # True
print(v1 is not v3) # False
Enter fullscreen mode Exit fullscreen mode

A bytearray 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 >.
v = bytearray(b'ABC')

print(v == bytearray(b'ABC'))  # True
print(v == bytearray(b'AB'))   # False
print(v == bytearray(b'AD'))   # False
print(v == bytearray(b'ABCD')) # False
print(v == bytearray(b'ABDE')) # False
print(v == bytearray(b''))     # False
Enter fullscreen mode Exit fullscreen mode
v = bytearray(b'ABC')

print(v != bytearray(b'ABC'))  # False
print(v != bytearray(b'AB'))   # True
print(v != bytearray(b'AD'))   # True
print(v != bytearray(b'ABCD')) # True
print(v != bytearray(b'ABDE')) # True
print(v != bytearray(b''))     # True
Enter fullscreen mode Exit fullscreen mode
v = bytearray(b'ABC')

print(v <= bytearray(b'ABC'))  # True
print(v <= bytearray(b'AB'))   # False
print(v <= bytearray(b'AD'))   # True
print(v <= bytearray(b'ABCD')) # True
print(v <= bytearray(b'ABDE')) # True
print(v <= bytearray(b''))     # False
Enter fullscreen mode Exit fullscreen mode
v = bytearray(b'ABC')

print(v >= bytearray(b'ABC'))  # True
print(v >= bytearray(b'AB'))   # True
print(v >= bytearray(b'AD'))   # False
print(v >= bytearray(b'ABCD')) # False
print(v >= bytearray(b'ABDE')) # False
print(v >= bytearray(b''))     # True
Enter fullscreen mode Exit fullscreen mode
v = bytearray(b'ABC')

print(v < bytearray(b'ABC'))  # False
print(v < bytearray(b'AB'))   # False
print(v < bytearray(b'AD'))   # True
print(v < bytearray(b'ABCD')) # True
print(v < bytearray(b'ABDE')) # True
print(v < bytearray(b''))     # False
Enter fullscreen mode Exit fullscreen mode
v = bytearray(b'ABC')

print(v > bytearray(b'ABC'))  # False
print(v > bytearray(b'AB'))   # True
print(v > bytearray(b'AD'))   # False
print(v > bytearray(b'ABCD')) # False
print(v > bytearray(b'ABDE')) # False
print(v > bytearray(b''))     # True
Enter fullscreen mode Exit fullscreen mode

A bytearray and other bytearray cannot be checked if they have and don't have their common bytes with bool() and & and with not keyword and & respectively as shown below:

v = bytearray(b'ABC')

print(bool(v & bytearray(b'BD')))
print(not (v & bytearray(b'BD')))
# TypeError: unsupported operand type(s) for &: 'bytearray' and 'bytearray'
Enter fullscreen mode Exit fullscreen mode

Top comments (0)