DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on • Edited on

Bytes in Python (1)

Buy Me a Coffee

*Memo:

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.
  • can be used with len() to get the length.
  • is True if it's non-empty and False if it's empty, checking it with bool().
  • is False if it's non-empty and True if it's empty, inverting the truth value with not keyword.
  • can be checked if specific bytes are and aren't in the bytes object with in keyword and with not and in keyword respectively.
  • can be checked if the bytes object is and isn't referred to by two variables with is keyword and with is and not keyword respectively:
    • Be careful, bytes literals with is keyword and with is and not keyword get warnings so use == and != respectively.
  • 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 >.
  • and other bytes object cannot be checked if they have and don't have their common bytes with bool() and & and with not keyword 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).
  • 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 for statement.
  • can be unpacked with an assignment and for statement, function and * but not with **.
  • can be created by the bytes literal b or B 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 or B with '' or "" are for one line.
    • b or B with '''''' or """""" are for one or more lines.
    • For bytes(), the words type conversion are also suitable in addition to the word creation.
  • 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.
  • 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.


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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
v = b'' # Empty bytes

print(v)
# b''
Enter fullscreen mode Exit fullscreen mode
v = b"Lёт's gφ!" # Let's go!
# SyntaxError: bytes can only contain ASCII literal characters
Enter fullscreen mode Exit fullscreen mode
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."
Enter fullscreen mode Exit fullscreen mode
v = B'''Apple Orange Banana Kiwi'''
v = b'Apple' b" Orange" b''' Banana''' B""" Kiwi"""

print(v)
# b'Apple Orange Banana Kiwi'
Enter fullscreen mode Exit fullscreen mode
v = b'''Apple
Orange
Banana
Kiwi'''

print(v)
# b'Apple\nOrange\nBanana\nKiwi'
Enter fullscreen mode Exit fullscreen mode
v = b"""
Apple
   Orange
       Banana
           Kiwi
"""

print(v)
# b'\nApple\n   Orange\n       Banana\n           Kiwi\n'
Enter fullscreen mode Exit fullscreen mode

Top comments (0)