Skip to main content
MahiDev — Learn. Build. Become AI-ready.

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.

Beginner15 min

Visual reference

Binary code — data at the lowest levelBinary code — data at the lowest level
Computer memory and bytesComputer memory and bytes
Network packets and binary dataNetwork packets and binary data

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.

8 bits = 1 byte; binary 01000001 = decimal 65 = ASCII A

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.

Minimum 00000000 = 0, maximum 11111111 = 255
  • Valid: 0, 45, 128, 255
  • Invalid: -1, 256, 500 (from your notes: 256 and -34 in a list cause ValueError)

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:

python
numbers = [12, 0, 34, 255, 122, 34, 56]

b = bytes(numbers)

print(b)
print(type(b))
text
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).

A=65, B=66, C=67
python
b = bytes([65, 66, 67])
print(b)
text
b'ABC'

Printable ASCII often appears as letters in the output.

Chapter 6 — Iterating through bytes

python
b = bytes([65, 66, 67])

for value in b:
    print(value)
text
65
66
67

Important: 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.

Indexed bytes from your list example
python
b = bytes([12, 0, 34, 255, 122])

print(b[0])
print(b[-1])
print(b[1:4])
text
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.

python
b = bytes([65, 66, 67])
# b[0] = 90
# TypeError: 'bytes' object does not support item assignment

Think of bytes as a sealed envelope — read and copy, but do not change a byte in place.

Sealed envelope — immutable bytesSealed envelope — immutable bytes
Tamper-proof envelopeTamper-proof envelope
Manila envelopeManila envelope

Need mutable binary data? Use bytearray (next lesson).

Chapter 9 — Invalid byte values

python
# 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
python
text = "ABC"
data = b"ABC"

print(text[0])
print(data[0])
text
A
65

Practice exercises

  1. Build bytes([65, 66, 67]) and relate values to letters.
  2. Print b[0:4] for [12, 0, 34, 255, 122, 34, 56] (from your notes).
  3. Try bytes([256]) once to see ValueError.

Quick knowledge check

  1. Valid range per byte?
    0–255.

  2. Can you run b[0] = 1?
    No — immutable.

  3. Create bytes from a list?
    bytes(list_of_ints).

Summary

  • A byte is 8 bits; values 0–255.
  • bytes is an immutable sequence for binary data.
  • Create with bytes([...]); ASCII also allows b"..." literals.
  • Index → int; slice → bytes.
  • Used in files, networking, encryption, images; use bytearray to mutate in place.

Next lesson: bytearray — mutable byte sequences.

Loading playground…