Nearby lessons

21 of 159

Python - bytearray Data Type

📌 What You Will Learn
  • Define the bytearray data type and how it differs from bytes
  • Create a bytearray object with the bytearray() function
  • Modify elements of a bytearray after creation
  • Identify the range of values allowed in a bytearray
  • Compare bytearray with the immutable bytes type

Introduction

The bytearray data type is almost the same as the bytes data type.

The main difference is that bytearray is mutable.

This means its values can be changed after the object is created.

Difference Between bytes and bytearray

bytes bytearray
Immutable Mutable
Values cannot be modified. Values can be modified.

Creating a bytearray Object

We can create a bytearray object using the bytearray() function.

Example - Creating bytearray

Loading editor…

Iterating bytearray Elements

We can iterate through a bytearray object using a loop.

Example - Iteration

Loading editor…

Modifying bytearray Values

Unlike the bytes data type, the bytearray data type allows modification of its values.

Example - Modifying Values

Loading editor…

Explanation

Initially:

[10, 20, 30, 40]

After Modification:

[100, 20, 30, 40]

Allowed Values in bytearray

Each element in a bytearray object must be between 0 and 255.

If any value is outside this range, Python raises a ValueError.

Example - Invalid Value

Loading editor…

Accessing Elements

Elements can be accessed using positive and negative indexing.

Example - Accessing Elements

Loading editor…

Updating Elements

Elements of a bytearray object can be updated directly.

Example - Updating Elements

Loading editor…

Understanding the Output

The ASCII value of 100 is d.

Therefore, the output contains the character d.

Length of bytearray

Use the len() function to find the number of elements in a bytearray object.

Example - len() Function

Loading editor…

Example Using Characters

Loading editor…

Explanation

ASCII Value Character
65 A
66 B
67 C

bytearray Supports Modification

Since bytearray is mutable, we can modify its elements.

Example - Modifying Characters

Loading editor…

Explanation

The ASCII value 90 represents the character Z.

Checking bytearray Type

Use the type() function to check the data type.

Example - type() Function

Loading editor…

Real World Usage of bytearray

The bytearray data type is useful when binary data needs to be modified.

It is commonly used for:

  • Working with files
  • Networking applications
  • Image processing
  • Video processing

Difference Between bytes and bytearray with Example

The following examples show the difference between bytes and bytearray.

bytes Example

Loading editor…

bytearray Example

Loading editor…

Important Notes

  • bytearray is a mutable data type.
  • Allowed values are from 0 to 255.
  • It supports indexing.
  • It supports iteration using loops.
  • It is mainly used for modifiable binary data.

Key Points

  • bytearray() creates a mutable binary object.
  • Elements can be modified after creation.
  • Only integers from 0 to 255 are allowed.
  • Supports indexing, iteration, len(), and type().
  • Useful when binary data needs to be updated.

Quick Summary

Feature Description
Data Type bytearray
Mutable Yes
Allowed Values 0 to 255
Supports Indexing Yes
Supports Modification Yes
Main Usage Mutable Binary Data
📝 Key Takeaways
  • bytearray is the mutable version of bytes
  • Each element of a bytearray must be between 0 and 255
  • Elements of a bytearray can be modified after creation
  • bytearray() creates a mutable binary object
  • bytearray is useful for modifiable binary data in files and networking

🧠 Test Your Knowledge

8 Questions
Progress: 0 / 8

Finished this lesson?

Mark it as complete to track your progress and see technology completion stats.