Nearby lessons
20 of 159Python - bytes Data Type
- Define the bytes data type and what it stores
- Create a bytes object with the bytes() function
- Identify the range of values allowed in a bytes object
- Access and iterate through bytes elements
- Explain why the bytes data type is immutable
- Compare bytes with the list data type
Introduction
The bytes data type is used to store a group of byte values.
It is mainly used to represent binary data.
A bytes object is similar to an array of byte values.
What is bytes Data Type?
The bytes data type stores binary data.
Each element in a bytes object must be in the range 0 to 255.
The bytes data type is immutable.
This means its values cannot be changed after the object is created.
Creating a bytes Object
We can create a bytes object by using the bytes() function.
Example - Creating bytes Object
Accessing Elements from bytes
We can access elements of a bytes object using indexes.
Both positive and negative indexing are supported.
Example - Accessing Elements
Iterating bytes Data Type
We can iterate through a bytes object using a loop.
Example - Iteration
Important Conclusion 1
The allowed values for a bytes object are 0 to 255.
If any value is outside this range, Python raises a ValueError.
Example - Invalid Value
Important Conclusion 2
Once a bytes object is created, its values cannot be modified.
This is because the bytes data type is immutable.
Example - Invalid Modification
Understanding Immutability
Immutability means the object's data cannot be changed after creation.
Example
Example of Binary Data
The bytes data type is commonly used to represent binary information such as:
- Images
- Video files
- Audio files
- Network packets
Example - ASCII Values
Explanation
| ASCII Value | Character |
|---|---|
| 65 | A |
| 66 | B |
| 67 | C |
Length of bytes Object
We can use the len() function to find the number of elements in a bytes object.
Example - len() Function
Checking bytes Data Type
We can use the type() function to check the data type.
Example - type() Function
Difference Between list and bytes
| list | bytes |
|---|---|
| Mutable | Immutable |
| Stores any type of values. | Stores only integers from 0 to 255. |
Example: [1, 2, 3] |
Example: bytes([1, 2, 3]) |
Example - list
Example - bytes
Important Notes
bytesis an immutable data type.- Each element must be between 0 and 255.
- It is mainly used to store binary data.
- It supports indexing.
- It supports iteration using loops.
Key Points
- The
bytesdata type stores binary data. - Use the
bytes()function to create a bytes object. - Only integer values from 0 to 255 are allowed.
- The
bytesobject is immutable. - It supports indexing, iteration,
len(), andtype(). - It is commonly used for images, audio, videos, and network data.
Quick Summary
| Feature | Description |
|---|---|
| Data Type | bytes |
| Mutable | No (Immutable) |
| Allowed Values | 0 to 255 |
| Supports Indexing | Yes |
| Supports Iteration | Yes |
| Main Usage | Binary Data Storage |
- The bytes data type stores binary data
- Each element of a bytes object must be between 0 and 255
- A value outside 0 to 255 raises a ValueError
- The bytes data type is immutable
- Use len() to count elements and type() to check the data type
- bytes is used to represent images, audio, video, and network data