Lesson 06
bytes — Immutable Byte Sequence
Learn what bytes are, why computers store binary data, how Python builds immutable byte sequences, and how bytes power files, images, networking, and encryption.
Visual reference
What you will learn
- What is a byte? — the smallest practical storage unit (8 bits)
- Range 0–255 — why one byte has exactly 256 values
bytes()constructor — build immutable binary sequences- Indexing and slicing — read bytes like other sequences
Chapter 1 — Before Python: what is a byte?
Every image, video, PDF, password, and network message is ultimately stored as binary. Computers work with 0 and 1.
Important definitions
| Unit | Size |
|---|---|
| 1 bit | One binary digit (0 or 1) |
| 1 byte | 8 bits |
| 1 KB | 1024 bytes |
| 1 MB | 1024 KB |
| 1 GB | 1024 MB |
Think of a byte as a locker with 8 switches — each ON (1) or OFF (0).
Chapter 2 — Why is the range 0 to 255?
A byte has 8 bits. Each bit has 2 states, so:
2⁸ = 256 combinations → values 0 through 255.
- Valid:
0,45,128,255 - Invalid:
-1,256,500(from your notes:256and-34in a list causeValueError)
Chapter 3 — What is bytes in Python?
bytes is a pre-defined sequence type for raw binary data.
Purpose (your notes): support scenarios like end-to-end encryption and secure transmission of confidential data on networks — each stored value must be in [0, 256) i.e. 0–255.
Definition: a bytes object is an ordered collection of integers where every value is between 0 and 255.
Your notes also say there is no general symbolic notation for arbitrary byte data in programs; you usually build bytes with bytes(object). For ASCII text, Python also allows literals like b"ABC".
Real-world uses
| Area | Example |
|---|---|
| Images | PNG, JPEG pixel data |
| Files | PDF binary storage |
| Networking | Packets on the internet |
| Encryption | Secure binary messages |
Chapter 4 — Creating a bytes object
Syntax: varname = bytes(object) — commonly a list of integers:
numbers = [12, 0, 34, 255, 122, 34, 56]
b = bytes(numbers)
print(b)
print(type(b))b'\x0c\x00"\xffz"8'
<class 'bytes'>Why does it print strangely?
Python often displays bytes in hex escape form (\x0c, \xff). The data is the same — only the display changes.
| Decimal | Hex | In repr |
|---|---|---|
| 12 | 0c |
\x0c |
| 255 | ff |
\xff |
| 122 | 7a |
z (printable ASCII) |
Try it in the playground
Edit the numbers = [...] list (only 0–255), run again, and watch print(b) change.
Chapter 5 — Understanding ASCII
Many byte values map to characters (ASCII).
b = bytes([65, 66, 67])
print(b)b'ABC'Printable ASCII often appears as letters in the output.
Chapter 6 — Iterating through bytes
b = bytes([65, 66, 67])
for value in b:
print(value)65
66
67Important: iterating bytes yields integers, not one-character strings.
| Object | Iteration yields |
|---|---|
"ABC" |
'A', 'B', 'C' |
b"ABC" |
65, 66, 67 |
Your notes also show reversing with b[::-1] and printing each value.
Chapter 7 — Indexing and slicing
Insertion order is preserved — index and slice like other sequences.
b = bytes([12, 0, 34, 255, 122])
print(b[0])
print(b[-1])
print(b[1:4])12
122
b'\x00"\xff'| Operation | str |
bytes |
|---|---|---|
obj[0] |
Character ('A') |
Integer (65) |
obj[1:4] |
str slice |
bytes slice |
From your notes: b[0:4] → b'\x0c\x00"\xff'.
Chapter 8 — Bytes are immutable
bytes objects do not support item assignment.
b = bytes([65, 66, 67])
# b[0] = 90
# TypeError: 'bytes' object does not support item assignmentThink of bytes as a sealed envelope — read and copy, but do not change a byte in place.
Need mutable binary data? Use bytearray (next lesson).
Chapter 9 — Invalid byte values
# bytes([256])
# ValueError: bytes must be in range(0, 256)
# bytes([-1])
# ValueError: bytes must be in range(0, 256)Only 256 values per byte — negatives and 256 cannot be stored.
Chapter 10 — str vs bytes
| Feature | str |
bytes |
|---|---|---|
| Stores | Characters (text) | Raw 0–255 values |
| Example | "ABC" |
b"ABC" |
obj[0] |
Character | Integer |
| Mutable? | No | No |
| Typical use | UI text, messages | Files, images, network, crypto |
text = "ABC"
data = b"ABC"
print(text[0])
print(data[0])A
65Practice exercises
- Build
bytes([65, 66, 67])and relate values to letters. - Print
b[0:4]for[12, 0, 34, 255, 122, 34, 56](from your notes). - Try
bytes([256])once to seeValueError.
Quick knowledge check
-
Valid range per byte?
0–255. -
Can you run
b[0] = 1?
No — immutable. -
Create bytes from a list?
bytes(list_of_ints).
Summary
- A byte is 8 bits; values 0–255.
bytesis an immutable sequence for binary data.- Create with
bytes([...]); ASCII also allowsb"..."literals. - Index →
int; slice →bytes. - Used in files, networking, encryption, images; use
bytearrayto mutate in place.
Next lesson: bytearray — mutable byte sequences.
Loading playground…