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

🐍Code Cell
1x = [10, 20, 30, 40]
2 
3b = bytearray(x)
4 
5print(type(b))
Output
No output captured.

Iterating bytearray Elements

We can iterate through a bytearray object using a loop.

Example - Iteration

🐍Code Cell
1x = [10, 20, 30, 40]
2 
3b = bytearray(x)
4 
5for i in b:
6 print(i)
Output
10
20
30
40

Modifying bytearray Values

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

Example - Modifying Values

🐍Code Cell
1x = [10, 20, 30, 40]
2 
3b = bytearray(x)
4 
5b[0] = 100
6 
7for i in b:
8 print(i)
Output
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

🐍Code Cell
1x = [10, 256]
2 
3b = bytearray(x)
Output
ValueError: byte must be in range(0, 256)

Accessing Elements

Elements can be accessed using positive and negative indexing.

Example - Accessing Elements

🐍Code Cell
1x = [10, 20, 30]
2 
3b = bytearray(x)
4 
5print(b[0])
6print(b[-1])
Output
10
30

Updating Elements

Elements of a bytearray object can be updated directly.

Example - Updating Elements

🐍Code Cell
1b = bytearray([1, 2, 3])
2 
3b[1] = 100
4 
5print(b)
Output
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

🐍Code Cell
1b = bytearray([10, 20, 30])
2 
3print(len(b))
Output
3

Example Using Characters

🐍Code Cell
1data = bytearray([65, 66, 67])
2 
3print(data)
Output
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

🐍Code Cell
1data = bytearray([65, 66, 67])
2 
3data[0] = 90
4 
5print(data)
Output
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

🐍Code Cell
1b = bytearray([1, 2, 3])
2 
3print(type(b))
Output
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

🐍Code Cell
1b = bytes([10, 20, 30])
2 
3b[0] = 100
Output
TypeError

bytearray Example

🐍Code Cell
1b = bytearray([10, 20, 30])
2 
3b[0] = 100
4 
5print(b)
Output
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
📝 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