Nearby lessons
21 of 159Python - bytearray Data Type
- 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
Iterating bytearray Elements
We can iterate through a bytearray object using a loop.
Example - Iteration
Modifying bytearray Values
Unlike the bytes data type, the bytearray data type allows modification of its values.
Example - Modifying Values
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
Accessing Elements
Elements can be accessed using positive and negative indexing.
Example - Accessing Elements
Updating Elements
Elements of a bytearray object can be updated directly.
Example - Updating Elements
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
Example Using Characters
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
Explanation
The ASCII value 90 represents the character Z.
Checking bytearray Type
Use the type() function to check the data type.
Example - type() Function
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
bytearray Example
Important Notes
bytearrayis 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(), andtype(). - 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 |
- 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