Nearby lessons

20 of 159

Python - bytes Data Type

📌 What You Will Learn
  • 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

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

Accessing Elements from bytes

We can access elements of a bytes object using indexes.

Both positive and negative indexing are supported.

Example - Accessing Elements

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

Iterating bytes Data Type

We can iterate through a bytes object using a loop.

Example - Iteration

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

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

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

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

🐍Code Cell
1x = [10, 20, 30, 40]
2 
3b = bytes(x)
4 
5b[0] = 100
Output
TypeError: 'bytes' object does not support item assignment

Understanding Immutability

Immutability means the object's data cannot be changed after creation.

Example

🐍Code Cell
1b = bytes([1, 2, 3])
2 
3print(b)
Output
b'\x01\x02\x03'

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

🐍Code Cell
1data = bytes([65, 66, 67])
2 
3print(data)
Output
b'ABC'

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

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

Checking bytes Data Type

We can use the type() function to check the data type.

Example - type() Function

🐍Code Cell
1b = bytes([1, 2, 3])
2 
3print(type(b))
Output
No output captured.

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

🐍Code Cell
1l = [10, 20, 30]
2 
3l[0] = 100
4 
5print(l)
Output
[100, 20, 30]

Example - bytes

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

Important Notes

  • bytes is 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 bytes data type stores binary data.
  • Use the bytes() function to create a bytes object.
  • Only integer values from 0 to 255 are allowed.
  • The bytes object is immutable.
  • It supports indexing, iteration, len(), and type().
  • 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
📝 Key Takeaways
  • 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

🧠 Test Your Knowledge

8 Questions
Progress: 0 / 8