Nearby lessons

22 of 108

Python - bytes 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

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
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

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

Iterating bytes Data Type

We can iterate through a bytes object using a loop.

Example - Iteration

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
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

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
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

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
TypeError: 'bytes' object does not support item assignment

Understanding Immutability

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

Example

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
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

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
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

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

Checking bytes Data Type

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

Example - type() Function

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
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

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
[100, 20, 30]

Example - bytes

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
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

🧠 Test Your Knowledge

8 Questions

Progress: 0 / 8
Keep Going!Python - bytearray Data Type