*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 withnotkeyword respectively.
# 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 keyword and with not and in keyword 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 keyword and with is and not keyword respectively as shown below:
*Memo:
- Be careful, bytes literals with
iskeyword and withisandnotkeyword 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
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 the same with
==and!=respectively. - the bytes object are in other bytes object with
<=. - other bytes object are in the bytes object with
>=. - the bytes object and other bytes are in other bytes object with
<. - other bytes object and other bytes are in the bytes object with
>.
v = b'ABC'
print(v == b'ABC') # True
print(v == b'AB') # False
print(v == b'AD') # False
print(v == b'ABCD') # False
print(v == b'ABDE') # False
print(v == b'') # False
v = b'ABC'
print(v != b'ABC') # False
print(v != b'AB') # True
print(v != b'AD') # True
print(v != b'ABCD') # True
print(v != b'ABDE') # True
print(v != b'') # True
v = b'ABC'
print(v <= b'ABC') # True
print(v <= b'AB') # False
print(v <= b'AD') # True
print(v <= b'ABCD') # True
print(v <= b'ABDE') # True
print(v <= b'') # False
v = b'ABC'
print(v >= b'ABC') # True
print(v >= b'AB') # True
print(v >= b'AD') # False
print(v >= b'ABCD') # False
print(v >= b'ABDE') # False
print(v >= b'') # True
v = b'ABC'
print(v < b'ABC') # False
print(v < b'AB') # False
print(v < b'AD') # True
print(v < b'ABCD') # True
print(v < b'ABDE') # True
print(v < b'') # False
v = b'ABC'
print(v > b'ABC') # False
print(v > b'AB') # True
print(v > b'AD') # False
print(v > b'ABCD') # False
print(v > b'ABDE') # False
print(v > b'') # True
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 keyword 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)