*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 withnotrespectively.
# 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 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
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 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
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')isFalse, comparingbytearray(b'C') > bytearray(b'D'). - E.g.
bytearray(b'ABC') > bytearray(b'AB')isTrue, comparingbytearray(b'C') > an absent byte(lower value than the other). - E.g.
bytearray(b'ABC') > bytearray(b'AD')isFalse, comparingbytearray(b'B') > bytearray(b'D').
- Lexicographical comparison:
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
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
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
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
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
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
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'
Top comments (0)