*Memo:
- My post explains a bytes (2).
- My post explains a bytes (3).
- My post explains a bytes (4).
- My post explains bytes().
- My post explains a bytearray (1).
- My post explains a string (1).
- My post explains string, bytes and bytearray functions.
- My post explains 10 collection types and their related posts.
A bytes (object):
- is the ordered immutable(hashable) collection of the zero or more bytes represented with ASCII characters whose type is
bytesand which allows duplicated bytes:- Each byte must be between
[0, 255]from 256 ASCII characters. - A bytes is for computer to understand.
- A bytes is also called a byte string.
- Ordered means that the order of each byte in a bytes object is kept so it guarantees that the order is always the same.
- Immutable(Hashable) means the bytes of a bytes object cannot be changed.
- Each byte must be between
- can be used with len() to get the length.
- is
Trueif it's non-empty andFalseif it's empty, checking it with bool(). - is
Falseif it's non-empty andTrueif it's empty, inverting the truth value withnot. - can be checked if specific bytes are and aren't in the bytes object with
inandnot inrespectively. - can be checked if the bytes object is and isn't referred to by two variables with
isandis notrespectively:- Be careful, bytes literals with
isandis notget warnings so use==and!=respectively.
- Be careful, bytes literals with
- 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
<=.
- all the bytes in them are and aren't equal with
- and other bytes object cannot be checked if they have and don't have their common bytes with
bool()and&and withnotand&respectively. - can be enlarged with
*and a number. - and other bytes objects can be concatenated with
+. - and other bytes object cannot return:
- all the bytes in them with
'|'(Union: A ∪ B). - their common bytes with
'&'(Intersection: A ∩ B). - the bytes in the bytes object which aren't in other bytes object with
'-'(Difference: A - B). - the bytes in either the bytes object or other bytes object but not both with
'^'(Symmetric Difference: A Δ B).
- all the bytes in them with
- can be iterated with a
forstatement. - can be unpacked with an assignment and
forstatement, function and*but not with**. - can be created by the bytes literal
borBwith'',"",''''''or""""""and by bytes() with or without several types of values:- A bytes literal cannot be used for a docstring because it gets
None. -
borBwith''or""are for one line. -
borBwith''''''or""""""are for one or more lines. - For
bytes(), the words type conversion are also suitable in addition to the word creation.
- A bytes literal cannot be used for a docstring because it gets
- can be encoded to from a string by encode():
- For
encode(), the words creation and type conversion are also suitable in addition to the word encoding.
- For
- cannot be big because it gets
OverflowError. - can be read by indexing and slicing.
- cannot be changed by indexing, slicing and a del statement.
- can be continuously used through multiple variables.
- cannot be shallow-copied by copy.copy(),
bytes()and slicing. - cannot be deep-copied and even shallow-copied by copy.deepcopy().
Be careful, a big bytes gets OverflowError.
OverflowError.b or B with '', "", '''''' or """""" can create a bytes as shown below:
*Memo:
-
\'is the escape sequence to output'.
v = b'' # Empty bytes
v = b"Hello World"
v = B'I\'m John.'
v = B"I'm John."
v = b'''I'm John.'''
v = b"""I'm John."""
v = B'''Apple Orange Banana Kiwi'''
v = b'Apple' b" Orange" b'''Banana''' B"""Kiwi"""
v = b'''Apple
Orange
Banana
Kiwi'''
v = b"""
Apple
Orange
Banana
Kiwi
"""
b'These above get no error'
b"These above get no error"
b'''These above get no error'''
b"""These above get no error"""
b'''
These above
get no error
'''
b"""
These above
get no error
"""
# No error
print(len(b"Let's go!"))
print(bool(b'0'))
print(bool(b' '))
print(bool(b''))
print(not b'0')
print(not b' ')
print(not b'')
print(b'A' in b'ABC')
print(b'A' not in b'ABC')
print(b'ABC' is b'ABC') # It gets warning so use `==`.
print(b'ABC' is not b'ABC') # It gets warning so use `==`.
print(b'ABC' == b'ABC')
print(b'ABC' != b'ABC')
print(b'ABC' > b'ABC')
print(b'ABC' >= b'ABC')
print(b'ABC' < b'ABC')
print(b'ABC' <= b'ABC')
print(b'ABCDE' * 3)
print(b'01234' * 3)
print(b'' * 3)
print(b'ABC' + b'DE' + b'FGHI')
for v in b'ABC': print(v)
v1, v2, v3 = b'ABC'; print(v1, v2, v3)
v1, *v2, v3 = b'ABCDEF'; print(v1, v2, v3)
for v1, v2, v3 in [b'ABC', b'DEF']: print(v1, v2, v3)
for v1, *v2, v3 in [b'ABCDEF', b'GHIJKL']: print(v1, v2, v3)
print(*b'ABCD', *b'EF')
print([*b'ABCD', *b'EF'])
# No error
print(b"Lёт's gφ!") # Let's go!
print(bool(b'ABC' & b'BD'))
print(not (b'ABC' & b'BD'))
print(b'AE' | b'ACE')
print(b'ABCD' & b'ACE')
print(b'ABCD' - b'ACE')
print(b'ABCD' ^ b'ACE')
print(b'ABCDE' * 1000000000)
# Error
A bytes object is the ordered immutable(hashable) collection of the zero or more bytes represented with ASCII characters whose type is bytes and which allows duplicated bytes as shown below:
v = b"Hello World"
print(v)
# b'Hello World'
print(type(v))
# <class 'bytes'>
v[1] = b'X'
v[3] = b'Y'
# TypeError: 'bytes' object does not support item assignment
v = b'' # Empty bytes
print(v)
# b''
v = b"Lёт's gφ!" # Let's go!
# SyntaxError: bytes can only contain ASCII literal characters
v = B"I'm John."
v = B'I\'m John.'
v = b'''I'm John.'''
v = b"""I'm John."""
print(v)
# b"I'm John."
v = B'''Apple Orange Banana Kiwi'''
v = b'Apple' b" Orange" b''' Banana''' B""" Kiwi"""
print(v)
# b'Apple Orange Banana Kiwi'
v = b'''Apple
Orange
Banana
Kiwi'''
print(v)
# b'Apple\nOrange\nBanana\nKiwi'
v = b"""
Apple
Orange
Banana
Kiwi
"""
print(v)
# b'\nApple\n Orange\n Banana\n Kiwi\n'
Top comments (0)