*Memo:
- My post explains a bytearray (1).
- My post explains a bytearray (3).
- My post explains a bytearray (4).
- My post explains a bytearray (5).
- My post explains bytearray().
A bytearray can be used with len() to get the length as shown below:
v = bytearray(b"Let's go!")
print(len(v))
# 9
A non-empty bytearray and empty bytearray are:
-
TrueandFalse, checking them with bool() respectively. -
FalseandTrue, inverting their truth values withnotkeyword respectively.
# Non-empty bytearray
print(bool(bytearray(b'0')))
print(bool(bytearray(b' ')))
# True
# Empty bytearray
print(bool(bytearray(b'')))
# False
# Non-empty bytearray
print(not bytearray(b'0'))
print(not bytearray(b' '))
# False
# Empty bytearray
print(not bytearray(b''))
# True
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
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
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
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
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
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
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
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
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
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'
Top comments (0)