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 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 and not in 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 and is not 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 equal with == and != respectively.
  • the bytearray is greater than other bytearray with >.
  • the bytearray is greater than or equal to other bytearray with >=.
  • the bytearray is less than other bytearray with <.
  • the bytearray is less than or equal to other bytearray with <=.

*Memo:

  • Lexicographical comparison is used with their bytearrays:
    • Lexicographical comparison:
      • compares each byte's Unicode points in their bytearrays from their 1st bytes one by one:
      • finishes the comparison:
        • just after the current comparison which has a difference between two bytes in their bytearrays is done.
        • or just after the current comparison of the byte and absent byte and vice versa in their bytearrays is done:
        • An absent byte is evaluated to a lower value than the other.
        • or if both of their bytearrays are exhausted.
    • E.g. bytearray(b'ABC') > bytearray(b'ABD') is False, comparing bytearray(b'C') > bytearray(b'D').
    • E.g. bytearray(b'ABC') > bytearray(b'AB') is True, comparing bytearray(b'C') > an absent byte(lower value than the other).
    • E.g. bytearray(b'ABC') > bytearray(b'AD') is False, comparing bytearray(b'B') > bytearray(b'D').
v = bytearray(b'ABC') # 65 66 67

print(v == bytearray(b'ABC'))  # 65 66 67     # True
print(v == bytearray(b'CBA'))  # 67 66 65     # False
print(v == bytearray(b'ABD'))  # 65 66 68     # False
print(v == bytearray(b'AB@'))  # 65 66 64     # False
print(v == bytearray(b'AB'))   # 65 66        # False
print(v == bytearray(b'AD'))   # 65 68        # False
print(v == bytearray(b'A@'))   # 65 64        # False
print(v == bytearray(b'ABCD')) # 65 66 67 68  # False
print(v == bytearray(b'ABDE')) # 65 66 68 69  # False
print(v == bytearray(b'AB@?')) # 65 66 64 63  # False
print(v == bytearray(b''))                    # False
Enter fullscreen mode Exit fullscreen mode
v = bytearray(b'ABC') # 65 66 67

print(v != bytearray(b'ABC'))  # 65 66 67     # False
print(v != bytearray(b'CBA'))  # 67 66 65     # True
print(v != bytearray(b'ABD'))  # 65 66 68     # True
print(v != bytearray(b'AB@'))  # 65 66 64     # True
print(v != bytearray(b'AB'))   # 65 66        # True
print(v != bytearray(b'AD'))   # 65 68        # True
print(v != bytearray(b'A@'))   # 65 64        # True
print(v != bytearray(b'ABCD')) # 65 66 67 68  # True
print(v != bytearray(b'ABDE')) # 65 66 68 69  # True
print(v != bytearray(b'AB@?')) # 65 66 64 63  # True
print(v != bytearray(b''))                    # True
Enter fullscreen mode Exit fullscreen mode
v = bytearray(b'ABC')

print(v > bytearray(b'ABC'))  # 65 66 67     # False
print(v > bytearray(b'CBA'))  # 67 66 65     # False
print(v > bytearray(b'ABD'))  # 65 66 68     # False
print(v > bytearray(b'AB@'))  # 65 66 64     # True
print(v > bytearray(b'AB'))   # 65 66        # True
print(v > bytearray(b'AD'))   # 65 68        # False
print(v > bytearray(b'A@'))   # 65 64        # True
print(v > bytearray(b'ABCD')) # 65 66 67 68  # False
print(v > bytearray(b'ABDE')) # 65 66 68 69  # False
print(v > bytearray(b'AB@?')) # 65 66 64 63  # True
print(v > bytearray(b''))                    # True
Enter fullscreen mode Exit fullscreen mode
v = bytearray(b'ABC')

print(v >= bytearray(b'ABC'))  # 65 66 67     # True
print(v >= bytearray(b'CBA'))  # 67 66 65     # False
print(v >= bytearray(b'ABD'))  # 65 66 68     # False
print(v >= bytearray(b'AB@'))  # 65 66 64     # True
print(v >= bytearray(b'AB'))   # 65 66        # True
print(v >= bytearray(b'AD'))   # 65 68        # False
print(v >= bytearray(b'A@'))   # 65 64        # True
print(v >= bytearray(b'ABCD')) # 65 66 67 68  # False
print(v >= bytearray(b'ABDE')) # 65 66 68 69  # False
print(v >= bytearray(b'AB@?')) # 65 66 64 63  # True
print(v >= bytearray(b''))                    # True
Enter fullscreen mode Exit fullscreen mode
v = bytearray(b'ABC')

print(v < bytearray(b'ABC'))  # 65 66 67     # False
print(v < bytearray(b'CBA'))  # 67 66 65     # True
print(v < bytearray(b'ABD'))  # 65 66 68     # True
print(v < bytearray(b'AB@'))  # 65 66 64     # False
print(v < bytearray(b'AB'))   # 65 66        # False
print(v < bytearray(b'AD'))   # 65 68        # True
print(v < bytearray(b'A@'))   # 65 64        # False
print(v < bytearray(b'ABCD')) # 65 66 67 68  # True
print(v < bytearray(b'ABDE')) # 65 66 68 69  # True
print(v < bytearray(b'AB@?')) # 65 66 64 63  # False
print(v < bytearray(b''))                    # False
Enter fullscreen mode Exit fullscreen mode
v = bytearray(b'ABC')

print(v <= bytearray(b'ABC'))  # 65 66 67     # True
print(v <= bytearray(b'CBA'))  # 67 66 65     # True
print(v <= bytearray(b'ABD'))  # 65 66 68     # True
print(v <= bytearray(b'AB@'))  # 65 66 64     # False
print(v <= bytearray(b'AB'))   # 65 66        # False
print(v <= bytearray(b'AD'))   # 65 68        # True
print(v <= bytearray(b'A@'))   # 65 64        # False
print(v <= bytearray(b'ABCD')) # 65 66 67 68  # True
print(v <= bytearray(b'ABDE')) # 65 66 68 69  # True
print(v <= bytearray(b'AB@?')) # 65 66 64 63  # False
print(v <= bytearray(b''))                    # False
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 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)