Python Reference Library →

Python bytearray() Function – Create and Modify Binary Data

Let’s look at bytearray() function – create and modify binary data

  1. Notice

    bytearray() creates a mutable sequence of bytes. Each element must be an integer from 0 through 255.

  2. Try

    Follow the example, change its sample, and compare what happens.

  3. Check

    Explain the result and test your understanding.

Watch how it works

Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.

Explore the idea

Follow the code and watch its state change. Guided simulation.

Code
Python
data = bytearray([3, 66])
data[0] = 65
print(list(data))
Live state

Top-level scope

data→Object 1
Object 1 · bytearraybytearray(b'AB')
This step

bytearray() creates a mutable sequence of bytes. Each element must be an integer from 0 through 255.

Picture bytearray() function – create and modify binary data

bytearray() creates a mutable sequence of bytes. Each element must be an integer from 0 through 255.

Image brief

Alt text
A diagram for Python bytearray() Function – Create and Modify Binary Data. Instructions: data = bytearray([3, 66]) data[0] = 65 print(list(data)). Displayed result: [65, 66]. The final example bindings are data --> object 1 (bytearray). object 1: bytearray(b'AB')
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python bytearray() Function – Create and Modify Binary Data". Teaching focus: bytearray() creates a mutable sequence of bytes. Each element must be an integer from 0 through 255. Depict exactly the relationships and labels in this reference diagram: EXAMPLE INSTRUCTIONS data = bytearray([3, 66]) data[0] = 65 print(list(data)) | v DISPLAYED RESULT [65, 66] FINAL BINDINGS data --> object 1 (bytearray) object 1: bytearray(b'AB') Use a flat vector style, generous spacing, clear arrows and legible labels, with a restrained blue/teal palette on a plain background. The diagram must explain the programming idea accurately. Do not depict variables as boxes containing copied objects when they share a reference. Avoid mascots, decorative screenshots, logos, gradients and extra text. Landscape composition, approximately 3:2, suitable for a course card. Accessible description: A diagram for Python bytearray() Function – Create and Modify Binary Data. Instructions: data = bytearray([3, 66]) data[0] = 65 print(list(data)). Displayed result: [65, 66]. The final example bindings are data --> object 1 (bytearray). object 1: bytearray(b'AB')

Follow the execution

Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.

Execution workspace

Choose a sample and follow its execution. Guided simulation.

Code
Python
data = bytearray([3, 66])
data[0] = 65
print(list(data))
Live state

Top-level scope

data→Object 1
Object 1 · bytearraybytearray(b'AB')
This step

The example has finished. Compare the final output with the values and references above.

Terminal
Output
[65, 66]

What do you expect this program to print?

What does this example display?

data = bytearray([3, 66])
data[0] = 65
print(list(data))
What does this example display?
Choose one answer. You can try again.

Answer and explanation

[65, 66]

Follow the printed values in order. The output is: [65, 66]

Which explanation matches what you learned?

Which explanation matches what you learned?
Choose one answer. You can try again.

Answer and explanation

bytearray() creates a mutable sequence of bytes. Each element must be an integer from 0 through 255.

bytearray() creates a mutable sequence of bytes. Each element must be an integer from 0 through 255.

More examples and reference

Python bytearray() Function

The bytearray() function in Python is used to create a mutable sequence of bytes. It’s like a list, but each item is a byte (0 to 255). This is useful when you want to manipulate binary data such as files, images, or streams.

Syntax

bytearray([source[, encoding[, errors]]])

Parameters:

  • source (optional) – can be a string, bytes, iterable of integers, or an integer size.
  • encoding (optional) – required if the source is a string.
  • errors (optional) – how to handle encoding errors ('strict', 'ignore', etc.).

Returns:

  • A new bytearray object (mutable bytes).

Example 1: Create an Empty Bytearray

b = bytearray()
print(b)
bytearray(b'')

Example 2: From a List of Integers

b = bytearray([65, 66, 67])
print(b)
bytearray(b'ABC')

Example 3: From a String with Encoding

b = bytearray("hello", "utf-8")
print(b)
bytearray(b'hello')

Example 4: Modify Bytearray

b = bytearray("hello", "utf-8")
b[0] = 72  # Replace 'h' with 'H'
print(b)
bytearray(b'Hello')

Use Cases of bytearray()

  • Modify byte-level data (e.g., in file I/O or network sockets)
  • Efficiently handle binary data (images, audio, etc.)
  • Useful for encryption, hashing, and compression tasks

Common Mistakes

  • Forgetting encoding: When creating from a string, always specify encoding.
  • Invalid integers: Values in iterable must be between 0 and 255.

Interview Tip

In low-level system or network programming, you may be asked to handle binary protocols. Knowing bytearray() is essential when you need mutable binary structures.

Summary

  • bytearray() creates a mutable array of bytes.
  • Supports multiple input types: string, list, bytes, or integer.
  • Commonly used for working with binary files and buffers.

Practice Problem

Write a program that converts a user-entered string into a bytearray, modifies the first character, and prints the result.

text = input("Enter a word: ")
b = bytearray(text, "utf-8")
b[0] = b[0] - 32  # Make first character uppercase
print("Modified:", b)

What to remember

Connect what you noticed in the example with the idea below.

Remember

bytearray() creates a mutable sequence of bytes. Each element must be an integer from 0 through 255.

Watch out for this idea

A bytearray cannot be changed after creation.

This is the misconception to avoid.

Reference checks are optional and do not count toward course progress.

    Reference library →