*Memo:
- My post explains a bytes (1).
- My post explains a bytes (3).
- My post explains a bytes (4).
- My post explains bytes().
A bytes can be used with len() to get the length as shown below:
v = b"Let's go!"
print(len(v))
# 9
A non-empty bytes and empty bytes are:
-
TrueandFalse, checking them with bool() respectively. -
FalseandTrue, inverting their truth values withnotrespectively.
# Non-empty bytes
print(bool(b'0'))
print(bool(b' '))
# True
# Empty bytes
print(bool(b''))
# False
# Non-empty bytes
print(not b'0') # bytes
print(not b' ') # bytes
# False
# Empty bytes
print(not b'')
# True
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
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
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
isandis notget 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
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 "!="?
print(b'ABC' == b'ABC') # True
print(b'ABC' == b'AB') # False
print(b'ABC' != b'ABC') # False
print(b'ABC' != b'AB') # True
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'isFalse, comparingb'C' > b'D'. - E.g.
b'ABC' > b'AB'isTrue, comparingb'C' > an absent byte(lower value than the other). - E.g.
b'ABC' > b'AD'isFalse, comparingb'B' > b'D'.
- Lexicographical comparison:
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
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
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
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
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
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
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'
Top comments (0)