Skip to content

Commit

Permalink
Cleanup duplicate information
Browse files Browse the repository at this point in the history
  • Loading branch information
aalexmmaldonado committed Nov 19, 2024
1 parent 11b9f49 commit 833d477
Show file tree
Hide file tree
Showing 5 changed files with 2 additions and 424 deletions.
2 changes: 1 addition & 1 deletion docs/basics/.pages
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
nav:
- index.md
- colab.md
- Display: display
- Printing: printing
- Comments: comments
- Arithmetic: arithmetic
- Variables: variables
Expand Down
14 changes: 0 additions & 14 deletions docs/basics/collections/dict.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -140,20 +140,6 @@
"person_favorites[\"food\"][2] = \"Italian\"\n",
"print(person_favorites)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Iterables\n",
"\n",
"You may hear people say \"iterable\" and \"sequence\" interchangeably.\n",
"They are not the same!\n",
"An iterable is any object that can be iterated over, meaning you can go through its data one element at a time.\n",
"Sequences, on the other hand, can be iterated over **and** get specific values based on an index.\n",
"Every sequence is an iterable, but not every iterable is a sequence.\n",
"This is not really important for this course, but becomes crucial for [type hints](https://peps.python.org/pep-0484/) and efficient algorithm design."
]
}
],
"metadata": {
Expand Down
253 changes: 0 additions & 253 deletions docs/basics/collections/list.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -428,259 +428,6 @@
"print(in_my_pocket)\n",
"print(len(in_my_pocket))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Tuples\n",
"\n",
"A tuple is just like a list except the content cannot change, meaning I cannot do any mutation (i.e., replace, append, or delete)."
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(21, False, None, 'apple', 3.14)\n"
]
}
],
"source": [
"in_my_closed_pocket = (21, False, None, \"apple\", 3.14)\n",
"print(in_my_closed_pocket)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"apple\n"
]
}
],
"source": [
"print(in_my_closed_pocket[3])"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"ename": "AttributeError",
"evalue": "'tuple' object has no attribute 'append'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[16], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43min_my_closed_pocket\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mappend\u001b[49m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mwallet\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n",
"\u001b[0;31mAttributeError\u001b[0m: 'tuple' object has no attribute 'append'"
]
}
],
"source": [
"in_my_closed_pocket.append(\"wallet\")"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"ename": "TypeError",
"evalue": "'tuple' object doesn't support item deletion",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[17], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;28;01mdel\u001b[39;00m \u001b[43min_my_closed_pocket\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m3\u001b[39;49m\u001b[43m]\u001b[49m\n",
"\u001b[0;31mTypeError\u001b[0m: 'tuple' object doesn't support item deletion"
]
}
],
"source": [
"del in_my_closed_pocket[3]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"So why would you want to use a tuple instead of a list?\n",
"\n",
"- If you need a collection of items that should not be changed or modified throughout the program, using a tuple provides immutability.\n",
" This prevents accidental modifications and ensures data consistency.\n",
"- Tuples are generally more memory-efficient than lists because of their immutability.\n",
" If your data does not need to change, using a tuple can result in better performance."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Dictionaries\n",
"\n",
"Lists allow us to store ordered collections of data which can be flexibly accessed via indices.\n",
"However, frequently we need an alternative access pattern—looking up values by a descriptive key rather than numerical index.\n",
"This is enabled in Python using dictionaries.\n",
"\n",
"Dictionaries provide a flexible mapping of unique keys to associated values, like a real world dictionary maps words to definitions.\n",
"Defining a dictionary uses braces with colons separating keys and values."
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'color': 'blue', 'food': ['Chinese', 'Thai', 'American'], 'number': 32}\n"
]
}
],
"source": [
"person_favorites = {\n",
" \"color\": \"blue\",\n",
" \"food\": [\"Chinese\", \"Thai\", \"American\"],\n",
" \"number\": 32,\n",
"}\n",
"print(person_favorites)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Dictionaries have some key capabilities:\n",
"\n",
"- Store mappings of objects to easy retrieval by descriptive keys;\n",
"- High performance lookup time even for large data sets;\n",
"- Keys can use many immutable types: strings, numbers, tuples;\n",
"- Values can be any Python object;\n",
"- Extensible structure allowing easy growth.\n"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"dict_keys(['color', 'food', 'number'])\n"
]
}
],
"source": [
"print(person_favorites.keys())"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"blue\n",
"['Chinese', 'Thai', 'American']\n",
"32\n"
]
}
],
"source": [
"print(person_favorites[\"color\"])\n",
"print(person_favorites[\"food\"])\n",
"print(person_favorites[\"number\"])"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'color': 'red', 'food': ['Chinese', 'Thai', 'American'], 'number': 32}\n"
]
}
],
"source": [
"person_favorites[\"color\"] = \"red\"\n",
"print(person_favorites)"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'color': 'red', 'food': ['Chinese', 'Thai', 'American'], 'number': 32, 'city': 'Pittsburgh'}\n"
]
}
],
"source": [
"person_favorites[\"city\"] = \"Pittsburgh\"\n",
"print(person_favorites)"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'color': 'red', 'food': ['Chinese', 'Thai', 'Italian'], 'number': 32, 'city': 'Pittsburgh'}\n"
]
}
],
"source": [
"person_favorites[\"food\"][2] = \"Italian\"\n",
"print(person_favorites)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Iterables\n",
"\n",
"You may hear people say \"iterable\" and \"sequence\" interchangeably.\n",
"They are not the same!\n",
"An iterable is any object that can be iterated over, meaning you can go through its data one element at a time.\n",
"Sequences, on the other hand, can be iterated over **and** get specific values based on an index.\n",
"Every sequence is an iterable, but not every iterable is a sequence.\n",
"This is not really important for this course, but becomes crucial for [type hints](https://peps.python.org/pep-0484/) and efficient algorithm design."
]
}
],
"metadata": {
Expand Down
Loading

0 comments on commit 833d477

Please sign in to comment.