Nearby lessons

23 of 108

Python - bytearray Data 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

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
No output captured.

Iterating bytearray Elements

We can iterate through a bytearray object using a loop.

Example - Iteration

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
10
20
30
40

Modifying bytearray Values

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

Example - Modifying Values

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
100
20
30
40

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

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
ValueError: byte must be in range(0, 256)

Accessing Elements

Elements can be accessed using positive and negative indexing.

Example - Accessing Elements

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
10
30

Updating Elements

Elements of a bytearray object can be updated directly.

Example - Updating Elements

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
bytearray(b'\x01d\x03')

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

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
3

Example Using Characters

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
bytearray(b'ABC')

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

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
bytearray(b'ZBC')

Explanation

The ASCII value 90 represents the character Z.

Checking bytearray Type

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

Example - type() Function

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
No output captured.

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

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
TypeError

bytearray Example

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
bytearray(b'd\x14\x1e')

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

🧠 Test Your Knowledge

8 Questions

Progress: 0 / 8
Keep Going!Python - Lists