Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "aa32ab4f",
"metadata": {},
"source": [
"> Note: The following content is `AI Generated` (Exactly copy-pasted from `Google Gemini Search` result, nothing modified)\n",
"\n",
"# bytearray\n",
"\n",
"The `bytearray` type is a mutable sequence of integers ranging from 0 to 255. It functions almost identically to [`bytes`](../bytes/bytes.ipynb), but with one major difference: you can modify its elements in place without creating a new object in memory."
]
},
{
"cell_type": "markdown",
"id": "a2609f1a",
"metadata": {
"vscode": {
"languageId": "bat"
}
},
"source": [
"---"
]
},
{
"cell_type": "markdown",
"id": "559da6a3",
"metadata": {},
"source": [
"# 1. Creating a `bytearray` Object\n",
"\n",
"You can initialize a `bytearray` using the `bytearray()` constructor in several ways:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "1a3bce9a",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"bytearray(b'Hello')\n",
"bytearray(b'World')\n",
"bytearray(b'Hello')\n",
"bytearray(b'\\x00\\x00\\x00\\x00\\x00')\n"
]
}
],
"source": [
"# 1. From a string (must specify an encoding)\n",
"ba1: bytearray = bytearray(\"Hello\", \"utf-8\")\n",
"print(ba1) # Output: bytearray(b'Hello')\n",
"\n",
"# 2. From an existing bytes object\n",
"ba2: bytearray = bytearray(b\"World\")\n",
"print(ba2)\n",
"\n",
"# 3. From an iterable of integers (0-255)\n",
"ba3: bytearray = bytearray([72, 101, 108, 108, 111]) # ASCII for 'Hello'\n",
"print(ba3)\n",
"\n",
"# 4. With a specific size (initializes with null bytes)\n",
"ba4: bytearray = bytearray(5) # Output: bytearray(b'\\x00\\x00\\x00\\x00\\x00')\n",
"print(ba4)"
]
},
{
"cell_type": "markdown",
"id": "4b8a258f",
"metadata": {},
"source": [
"---"
]
},
{
"cell_type": "markdown",
"id": "1f171720",
"metadata": {},
"source": [
"# 2. Modifying a `bytearray` (In-Place Mutability)\n",
"\n",
"Because it is mutable, bytearray supports list-like operations such as item assignment, appending, popping, and extending."
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "5ad91bac",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"bytearray(b'Jython')\n",
"bytearray(b'Jython!')\n",
"bytearray(b'Jython! Rules')\n",
"bytearray(b'JythonRules')\n"
]
}
],
"source": [
"ba: bytearray = bytearray(b\"Python\")\n",
"\n",
"# 1. Change an element using an integer (ASCII 74 = 'J')\n",
"ba[0] = 74\n",
"print(ba) # Output: bytearray(b'Jython')\n",
"\n",
"# 2. Append a single byte value\n",
"ba.append(33) # ASCII 33 = '!'\n",
"print(ba) # Output: bytearray(b'Jython!')\n",
"\n",
"# 3. Extend with another byte sequence\n",
"ba.extend(b\" Rules\")\n",
"print(ba) # Output: bytearray(b'Jython! Rules')\n",
"\n",
"# 4. Delete elements using slicing\n",
"del ba[6:8]\n",
"print(ba) # Output: bytearray(b'JythonRules')"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.14.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading
Loading