-
Hajipur, Bihar, 844101
Hajipur, Bihar, 844101
Introduction to Python
Python Basics
Python Syntax
Python Comments
Python Variables
Python Data Types
Python Casting
Python I/O
Python Operators
Cotrol Structures
Data Structures
Python Strings
Python Lists
Python Tuples
Python Dictionaries
Python Sets
Python Arrays
Python Bytes and Bytearray
Date and Time
Functions and Module
File Handling
Python OOP
Advanced Concepts
Python Scope
Python Modules
Python JSON
Python RegEx
Python PIP
Python Try...Except
Python String Formatting
Python User Input
Python VirtualEnv
Python Math
Python DSA
Python DSA
Lists and Arrays
Python Stacks
Python Queues
Linked Lists
Python Hash Tables
Python Trees
Python Binary Trees
Binary Search Trees
Python AVL Trees
Python Graphs
Searching Algorithms
Sorting Algorithms
In Python, bytes
and bytearray
are used to handle binary data.
They look like lists of integers but represent data in bytes form (0 to 255).
data = bytes([65, 66, 67])
print(data) # b'ABC'
print(data[0]) # 65
✅ You cannot change values in a bytes
object.
data = bytearray([65, 66, 67])
print(data) # bytearray(b'ABC')
data[1] = 90
print(data) # bytearray(b'AZC')
✅ You can change values in a bytearray
.
text = "hello"
b = bytes(text, 'utf-8')
print(b) # b'hello'
b = b'hello'
text = b.decode('utf-8')
print(text) # hello
b = b"data"
ba = bytearray(b)
Method | Works On | Description |
---|---|---|
.decode() |
bytes | Converts to string |
.append(x) |
bytearray | Adds one byte to the end |
.pop() |
bytearray | Removes the last byte |
.reverse() |
bytearray | Reverses byte order |
.extend([]) |
bytearray | Adds multiple bytes |
with open("file.bin", "rb") as f:
content = f.read()
print(content) # bytes
Convert string to bytes
Modify bytearray data
Decode bytes into text
Use .append()
or .reverse()
on bytearray
Q1. Write a Python program to create a bytes object with values [65, 66, 67]
and print it.
Q2. Write a Python program to print the second byte from a bytes object.
Q3. Write a Python program to try changing a byte in a bytes object and observe the error (bytes are immutable).
Q4. Write a Python program to create a bytearray and modify the first element.
Q5. Write a Python program to convert the string "Python"
into bytes using UTF-8 encoding.
Q6. Write a Python program to decode b'Hello'
into a string using .decode()
.
Q7. Write a Python program to append the value 88
to a bytearray using append()
.
Q8. Write a Python program to use .reverse()
on a bytearray and print the result.
Q9. Write a Python program to convert a bytes object to a bytearray and modify its content.
Q10. Write a Python program to read a binary file and print its content in bytes.
Introduction to Python
Python Basics
Python Syntax
Python Comments
Python Variables
Python Data Types
Python Casting
Python I/O
Python Operators
Cotrol Structures
Data Structures
Python Strings
Python Lists
Python Tuples
Python Dictionaries
Python Sets
Python Arrays
Python Bytes and Bytearray
Date and Time
Functions and Module
File Handling
Python OOP
Advanced Concepts
Python Scope
Python Modules
Python JSON
Python RegEx
Python PIP
Python Try...Except
Python String Formatting
Python User Input
Python VirtualEnv
Python Math
Python DSA
Python DSA
Lists and Arrays
Python Stacks
Python Queues
Linked Lists
Python Hash Tables
Python Trees
Python Binary Trees
Binary Search Trees
Python AVL Trees
Python Graphs
Searching Algorithms
Sorting Algorithms