Why This Matters
Most DSA practice uses lists and strings, but real systems also move raw bytes. Hashing, parsers, file formats, network protocols, compression, and low-level encodings all need byte-level thinking.
Use this page when a problem stops being "characters in a string" and becomes "bytes in a buffer."
Core Idea
bytes is immutable. bytearray is mutable. Both are sequences of integers from 0 to 255.
| Tool | Mutable? | Main use |
|---|---|---|
bytes | no | fixed binary data, encoded text, hash inputs |
bytearray | yes | editable byte buffer |
memoryview | view | slice buffers without copying in many cases |
encode | creates bytes | text to bytes |
decode | creates str | bytes to text |
int.to_bytes | creates bytes | pack integer into fixed byte width |
int.from_bytes | creates int | read integer from bytes |
Endianness
Endianness is byte order. Big-endian stores the most significant byte first. Little-endian stores the least significant byte first.
(258).to_bytes(2, "big") == bytes([0x01, 0x02])
(258).to_bytes(2, "little") == bytes([0x02, 0x01])
State the byte order whenever bytes represent a number.
Non-Example or Failure Mode
A Python str is text, not raw bytes. Indexing "A" gives a one-character string, while indexing b"A" gives integer 65. Confusing those two models causes parsing and encoding bugs.
Worked Example
You receive two bytes representing a length field in big-endian order:
packet = bytes([0x01, 0x2C])
length = int.from_bytes(packet, "big")
The result is 300 because 0x012C equals decimal 300.
Common Mistakes
| Mistake | Correction |
|---|---|
| Treating text and bytes as the same thing. | Use encode and decode at the boundary. |
Forgetting that bytes[i] is an integer. | Compare to 65 or use bytes([65]) for a one-byte object. |
Mutating bytes. | Use bytearray for editable buffers. |
| Omitting byte order. | Pass "big" or "little" to integer packing methods. |
| Assuming slicing always stays zero-copy. | Use memoryview when avoiding copies matters. |
Diagnostic Questions
| Question type | Question | Answer signal |
|---|---|---|
| Definition | What values can a byte hold? | Integers from 0 to 255. |
| Example / non-example | Is b"abc" a Python str? | No. It is bytes. |
| Computation | What is b"A"[0]? | 65. |
| Transfer | Why does endianness matter? | The same bytes can represent different numbers under different byte orders. |
Runnable Drill
Python bytes and bytearray drill
Checks encoding, mutation, memoryview, hex, and integer byte order.
Output will appear here.
Exercises
Beginner:
- Convert
"heap"to UTF-8 bytes and back. - Explain why
b"A"[0]is65. - Mutate a
bytearrayin place.
Intermediate:
- Pack and unpack
65535using two bytes. - Compare big-endian and little-endian output for the same integer.
Challenge:
- Write a parser for a toy packet format: two bytes of length followed by that many payload bytes.
Diagram Recommendation
Type: byte-order diagram.
Caption: "The integer 258 as two bytes: big-endian 01 02, little-endian 02 01."
Purpose: Make endianness concrete before learners parse binary data.
Next Topics
References
- Python documentation. Binary sequence types: https://docs.python.org/3/library/stdtypes.html#binary-sequence-types-bytes-bytearray-memoryview
- Python documentation.
int.to_bytesandint.from_bytes: https://docs.python.org/3/library/stdtypes.html#int.to_bytes - Python documentation. Text encodings: https://docs.python.org/3/library/codecs.html#standard-encodings