DEV Community

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

Posted on • Edited on

Bytes in Python (2)

Buy Me a Coffee

*Memo:

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

v = b"Let's go!"

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

A non-empty bytes and empty bytes are:

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

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

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

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

v = b'ABC'

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

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

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

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

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

*Memo:

  • Be careful, bytes literals with is and is not get warnings so use == and != respectively.
v1 = b'ABC'
v2 = 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
print(b'ABC' is b'ABC') # True
print(b'ABC' is b'AB')  # False
# SyntaxWarning: "is" with 'bytes' literal. Did you mean "=="?

print(b'ABC' is not b'ABC') # False
print(b'ABC' is not b'AB')  # True
# SyntaxWarning: "is not" with 'bytes' literal. Did you mean "!="?
Enter fullscreen mode Exit fullscreen mode
print(b'ABC' == b'ABC') # True
print(b'ABC' == b'AB')  # False

print(b'ABC' != b'ABC') # False
print(b'ABC' != b'AB')  # True
Enter fullscreen mode Exit fullscreen mode

A bytes object and other bytes object can be checked if:

  • all the bytes in them are and aren't equal with == and != respectively.
  • the bytes object is greater than other bytes object with >.
  • the bytes object is greater than or equal to other bytes object with >=.
  • the bytes object is less than other bytes object with <.
  • the bytes object is less than or equal to other bytes object with <=.

*Memo:

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

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

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

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

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

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

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

A bytes object and other bytes object 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 = b'ABC'

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

Top comments (0)