*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 a bytes shallow and deep copy.
- My post explains a list (1).
- My post explains a tuple (1).
- My post explains a set (1).
- My post explains a frozenset (1).
- My post explains a dictionary (1).
- My post explains an iterator (1).
- My post explains a range (1).
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 withnotkeyword. - can be checked if specific bytes are and aren't in the bytes object with
inkeyword and withnotandinkeyword respectively. - can be checked if the bytes object is and isn't referred to by two variables with
iskeyword and withisandnotkeyword respectively:- Be careful, bytes literals with
iskeyword and withisandnotkeyword get 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 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
>.
- them are and aren't the same with
- and other bytes object cannot be checked if they have and don't have their common bytes with
bool()and&and withnotkeyword and&respectively. - can be enlarged with
*and a number. - and other bytes objects can be concatenated with
+. - and other bytes objects 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).
- all the bytes in them with
- and other bytes object cannot return the bytes in the bytes object but not in other bytes object or not in the bytes object but in other bytes object with
'^'(Symmetric Difference: A Δ B). - 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')
v = b'ABCDE' * 3
v = b'01234' * 3
v = b'' * 3
v = b'ABC' + b'DE' + b'FGHI'
for v in b'ABC': pass
v1, v2, v3 = b'ABC'
v1, *v2, v3 = b'ABCDEF'
for v1, v2, v3 in [b'ABC', b'DEF']: pass
for v1, *v2, v3 in [b'ABCDEF', b'GHIJKL']: pass
print(*b'ABCD', *b'EF')
print([*b'ABCD', *b'EF'])
# No error
v = b"Lёт's gφ!" # Let's go!
print(bool(b'ABC' & b'BD'))
print(not (b'ABC' & b'BD'))
print(b'AE' | b'ACE' | b'ABDE')
print(b'AE' & b'ACE' & b'ABDE')
print(b'AE' - b'ACE' - b'ABDE')
print(b'ABCD' ^ b'ACE')
v = 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)