This tutorial explains two closely related but different Python data types: bytes and bytearray.
These types are not very commonly used in day-to-day Python programming, but they are extremely important when dealing with binary data such as images, audio files, video files, and network streams. Understanding these types is essential when working at a lower level with binary representations.
1. Introduction
Python provides two special data types to represent sequences of byte values:
• bytes
• bytearray
Both types represent a sequence of integers, where each integer must be in the range 0 to 255 (inclusive).
They behave similarly to lists but are optimized for binary data.
Use case examples:
• Handling image files
• Handling video or audio files
• Handling raw byte streams
• Network protocols
• Low-level binary operations
The primary difference is:
• bytes is immutable
• bytearray is mutable
2. Creating a Bytes Object
To create a bytes object, you must pass an iterable (like a list or tuple) of integers to the bytes() constructor.
Example:
L = [10, 20, 30, 40] B = bytes(L) print(type(B))
Output:
<class 'bytes'>
The values stored inside the bytes object can be printed by iterating over it:
for x in B:
print(x)
Output:
10 20 30 40
Thus, a bytes object stores each integer as a single byte.
3. Rules and Properties of Bytes
3.1 Values Must Be Between 0 and 255
The bytes data type only allows values between 0 and 255. Any value outside this range raises a ValueError.
Example:
L = [10, 20, 30, 256] B = bytes(L)
This results in:
ValueError: bytes must be in range(0, 256)
Since range(0, 256) means values 0 to 255, 256 is invalid.
If you use 255, it works:
L = [10, 20, 30, 255] B = bytes(L) # Valid
3.2 Bytes Is Immutable
Once a bytes object is created, you cannot modify its elements.
Example:
L = [10, 20, 30, 40] B = bytes(L) print(B[0]) # prints 10 B[0] = 77 # trying to modify
This raises:
TypeError: 'bytes' object does not support item assignment
Immutability is an important characteristic of bytes.
3.3 Indexing and Slicing Are Allowed
Even though bytes is immutable, it supports indexing and slicing because it is a sequence type.
Example:
print(B[0]) # 10 print(B[-1]) # 40
Slicing returns a new bytes object:
print(B[1:3]) # bytes([20, 30])
4. Introduction to Bytearray
The bytearray type is almost identical to bytes, except for one key difference:
bytearray is mutable.
This means we can change individual byte values after creation.
5. Creating a Bytearray Object
Creation is similar to bytes:
L = [10, 20, 30, 40] B = bytearray(L) print(type(B))
Output:
<class 'bytearray'>
Accessing values:
print(B[0]) # 10 print(B[-1]) # 40
6. Rules and Properties of Bytearray
6.1 Values Must Still Be Between 0 and 255
Just like bytes, bytearray enforces the same restriction.
L = [10, 20, 30, 256] B = bytearray(L)
This results in:
ValueError: byte must be in range(0, 256)
The interpretation is the same: only values 0 to 255 are allowed.
6.2 Bytearray Is Mutable
You can update values inside a bytearray:
L = [10, 20, 30, 40] B = bytearray(L) B[0] = 77
Now the first element changes to 77.
Check via iteration:
for x in B:
print(x)
Output:
77 20 30 40
This proves that bytearray is mutable.
6.3 Supports Indexing and Slicing
Just like bytes:
print(B[1]) # 20 print(B[-1]) # 40 print(B[1:3]) # bytearray(b'\x14\x1e') → values 20 and 30
Slicing returns a bytearray object.
7. Differences Between Bytes and Bytearray
| Feature | bytes | bytearray |
|---|---|---|
| Mutability | Immutable | Mutable |
| Creation | bytes(iterable) | bytearray(iterable) |
| Modification | Not allowed | Allowed |
| Use cases | Read-only binary data | Modifiable binary data |
| Values range | 0–255 | 0–255 |
In interviews, a common question is:
What is the difference between bytes and bytearray?
The exact answer:
Bytes is immutable, whereas Bytearray is mutable. Both represent binary data and allow only values from 0 to 255.
8. When to Use Bytes and Bytearray
Use bytes when:
• You want to store binary data that should not be modified
• Network data packets
• Binary file contents
• Read-only buffers
Use bytearray when:
• You need mutable binary data
• Editing or processing binary files
• Working with encryption algorithms
• Network protocols requiring modification
These types are critical when working with raw binary formats.
9. Summary
The important points covered in this tutorial are:
• Both bytes and bytearray represent a sequence of byte values.
• Only values between 0 and 255 are allowed.
• bytes is immutable.
• bytearray is mutable.
• Both support indexing and slicing because they are sequence types.
• These types are especially useful for handling binary data such as images, audio, and video files.
