*Memo:
- My post explains a bytes (2).
- My post explains bytes().
- My post explains a bytes shallow and deep copy.
- My post explains a bytearray (1).
- My post explains a string (1).
- My post explains string, bytes and bytearray functions.
- 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
bytes
and 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
False
if it's empty. - can be checked if zero or more specific bytes are in the bytes object with
in
keyword. - can be checked if the bytes object is referred to by two variables with
is
keyword. - and one or more bytes objects can be concatenated with
+
but not with|
. - can be enlarged with
*
and a number. - can be iterated with a
for
statement. - can be created by the bytes literal
b
orB
with''
,""
,''''''
or""""""
and by bytes() with or without several types of values:- A bytes literal cannot be used for a docstring because it gets
None
. -
b
orB
with''
or""
are for one line. -
b
orB
with''''''
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 unpacked with an assignment and
for
statement, function and*
but not with**
. - 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!"))
v = b'ABC' + b'DE' + b'FGHI'
v = b'ABCDE' * 3
v = b'01234' * 3
v = b'' * 3
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!
v = b'ABC' | b'DE' | b'FGHI'
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'
A bytes can be used with len()
to get the length as shown below:
v = b"Let's go!"
print(len(v))
# 9
An empty bytes is False
as shown below:
print(bool(b'')) # Empty bytes
# False
print(bool(b' ')) # bytes
# True
A bytes object can be checked if zero or more specific bytes are in the bytes object with in
keyword as shown below:
v = b'ABC'
print(b'' in v)
print(b'A' in v)
print(b'BC' in v)
print(b'ABC' in v)
# True
print(b'a' in v)
# False
A bytes object and one or more bytes objects can be concatenated with +
but not with |
as shown below:
v = b'ABC' + b'DE' + b'FGHI'
print(v)
# b'ABCDEFGHI'
v = b'ABC' | b'DE' | b'FGHI'
# TypeError: unsupported operand type(s) for |: 'bytes' and 'bytes'
A bytes can be enlarged with *
and a number as shown below:
v = b'ABCDE' * 3
print(v)
# b'ABCDEABCDEABCDE'
v = b'01234' * 3
print(v)
# b'012340123401234'
v = b'' * 3
print(v)
# b''
A bytes can be iterated with a for
statement as shown below:
for v in b'ABC':
print(v)
# 65
# 66
# 67
Be careful, a big bytes gets OverflowError
as shown below:
v = b'ABCDE' * 1000000000
print(v)
# OverflowError: repeated bytes are too long