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

Loading editor…

Accessing Elements from bytes

We can access elements of a bytes object using indexes.

Both positive and negative indexing are supported.

Example - Accessing Elements

Loading editor…

Iterating bytes Data Type

We can iterate through a bytes object using a loop.

Example - Iteration

Loading editor…

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

Loading editor…

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

Loading editor…

Understanding Immutability

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

Example

Loading editor…

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

Loading editor…

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

Loading editor…

Checking bytes Data Type

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

Example - type() Function

Loading editor…

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

Loading editor…

Example - bytes

Loading editor…

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

Finished this lesson?

Mark it as complete to track your progress and see technology completion stats.