Skip to content
Open
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
309 changes: 112 additions & 197 deletions Lessons/01-Introduction.ipynb

Large diffs are not rendered by default.

546 changes: 196 additions & 350 deletions Lessons/02-Data types.ipynb

Large diffs are not rendered by default.

815 changes: 163 additions & 652 deletions Lessons/03-Strings.ipynb

Large diffs are not rendered by default.

342 changes: 93 additions & 249 deletions Lessons/04-Lists.ipynb

Large diffs are not rendered by default.

114 changes: 51 additions & 63 deletions Lessons/05-Dictionaries.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,28 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## Dictionaries"
"# Dictionaries"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Dictionaries in Python are powerful data structures used to store and manage data in a key-value pair format. Each key-value pair maps the key to its associated value. Dictionaries are mutable, which means you can modify them after their creation. They are indexed by keys, which must be immutable types (e.g., strings, numbers, or tuples with immutable elements) and unique within a dictionary.\n",
"\n",
"### Basic Syntax\n",
"Dictionaries in Python are powerful data structures used to store and manage data in a key-value pair format. Each key-value pair maps the key to its associated value. Dictionaries are mutable, which means you can modify them after their creation. They are indexed by keys, which must be immutable types (e.g., strings, numbers, or tuples with immutable elements) and unique within a dictionary."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Basic Syntax\n",
"\n",
"A dictionary in Python can be created using curly braces `{}` with pairs separated by commas, and a colon `:` separates each key from its value:"
"A dictionary in Python can be created using curly braces `{}` with pairs separated by commas, and a colon `:` separating each key from its value:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -34,31 +39,23 @@
"Alternatively, you can use the `dict()` constructor:\n",
"\n",
"\n",
"my_dict = dict(key1='value1', key2='value2')"
"> `my_dict = dict(key1='value1', key2='value2')`"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Accessing Elements\n",
"## Accessing Elements\n",
"\n",
"You can access a value by referencing its key in square brackets `[]`:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"value1\n"
]
}
],
"outputs": [],
"source": [
"print(my_dict['key1']) # Output: value1"
]
Expand All @@ -72,18 +69,9 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"None\n",
"default_value\n"
]
}
],
"outputs": [],
"source": [
"print(my_dict.get('nonexistent_key'))\n",
"print(my_dict.get('nonexistent_key', 'default_value'))"
Expand All @@ -93,14 +81,14 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"### Adding and Modifying Elements\n",
"## Adding and Modifying Elements\n",
"\n",
"You can add a new key-value pair or modify an existing one by assigning a value to a key:"
]
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -112,14 +100,14 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"### Removing Elements\n",
"## Removing Elements\n",
"\n",
"Use the `del` statement to remove a key-value pair by specifying its key:"
]
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -135,7 +123,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -146,24 +134,33 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"### Iterating Over a Dictionary\n",
"We can use popitems to pop 2-tuple values in LIFO order"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"ex1 = {\"a\": 12, \"b\": 23, \"c\": [1,2,3,], (1,2): {}}\n",
"print(ex1.popitem())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Iterating Over a Dictionary\n",
"\n",
"You can iterate over a dictionary using a loop. By default, looping over a dictionary iterates over its keys:"
]
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"new_key new_value\n"
]
}
],
"outputs": [],
"source": [
"for key in my_dict:\n",
" print(key, my_dict[key])"
Expand All @@ -178,17 +175,9 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"new_key new_value\n"
]
}
],
"outputs": [],
"source": [
"for key, value in my_dict.items():\n",
" print(key, value)"
Expand All @@ -198,14 +187,14 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"### Dictionary Comprehensions\n",
"## Dictionary Comprehensions\n",
"\n",
"Similar to list comprehensions, dictionary comprehensions allow you to create dictionaries using an expressive and concise syntax:"
]
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -216,15 +205,15 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"This creates a dictionary where each key is a number from 0 to 5, and each value is the square of the key.\n",
"\n",
"### Useful Dictionary Methods"
"This creates a dictionary where each key is a number from 0 to 5, and each value is the square of the key."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Useful Dictionary Methods\n",
"\n",
"- `keys()`: Returns a view object containing the dictionary's keys.\n",
"- `values()`: Returns a view object containing the dictionary's values.\n",
"- `items()`: Returns a view object containing tuples of (key, value) pairs.\n",
Expand Down Expand Up @@ -255,8 +244,7 @@
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.5"
"pygments_lexer": "ipython3"
},
"toc": {
"base_numbering": 1,
Expand All @@ -278,5 +266,5 @@
}
},
"nbformat": 4,
"nbformat_minor": 2
"nbformat_minor": 4
}
Loading