You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
1004 lines
28 KiB
1004 lines
28 KiB
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "efficient-shoot",
|
|
"metadata": {},
|
|
"source": [
|
|
"<!--\n",
|
|
"title: Further Data Structures\n",
|
|
"type: lesson\n",
|
|
"duration: 00:40\n",
|
|
"creator: Steve Peters\n",
|
|
"-->\n",
|
|
"\n",
|
|
"\n",
|
|
"<h1>Python Programming: Sets and Tuples</h1>\n",
|
|
"\n",
|
|
"<!--\n",
|
|
"\n",
|
|
"## Overview\n",
|
|
"This lesson focuses on sets and tuples. It starts with sets, encompassing We Dos as new functions are introduced, then goes into tuples. It ends with the `type` function and a You Do. If there's time, after the \"Additional Reading\" slides, there is a sets exercise. This exercises are also in the `xx-additional-exercises` folder, if you don't get to it.\n",
|
|
"\n",
|
|
"## Important Notes or Prerequisites\n",
|
|
"- The students will be asked to build upon their knowledge of the `list`, so a solid understanding of this concept will be vital.\n",
|
|
"- The \"why\" of each datatype is essential to impart to students and your checks for understanding may keep circling back around to the \"why\".\n",
|
|
"- Much of this lesson contrasts the three data types (tuples, sets, lists) to each other, so that students can see them side by side and start to internalize the differences.\n",
|
|
"\n",
|
|
"## Learning Objectives\n",
|
|
"In this lesson, students will:\n",
|
|
"\n",
|
|
"- Perform common actions with sets.\n",
|
|
"- Perform common actions with tuples.\n",
|
|
"- Know when to use each of the different collection structures.\n",
|
|
"\n",
|
|
"\n",
|
|
"## Duration\n",
|
|
"40 minutes\n",
|
|
"\n",
|
|
"### Timing note:\n",
|
|
"- If there's time remaining at the end, giving exercises involving multiple contrasting sets, tuples, and lists would be great.\n",
|
|
"- There is, in the `xx-additional-exercises` folder in the parent folder, a set equals challenge that you should give in class if there's time, and if not, give as homework.\n",
|
|
"\n",
|
|
"## Suggested Agenda\n",
|
|
"\n",
|
|
"| Time | Activity |\n",
|
|
"| --- | --- |\n",
|
|
"| 0:00 - 0:02 | Welcome |\n",
|
|
"| 0:02 - 0:20 | Sets |\n",
|
|
"| 0:20 - 0:37 | Tuples |\n",
|
|
"| 0:37 - 0:40 | Summary |\n",
|
|
"\n",
|
|
"## In Class: Materials\n",
|
|
"- Projector\n",
|
|
"- Internet connection\n",
|
|
"- Python3\n",
|
|
"-->\n",
|
|
"\n",
|
|
"---\n",
|
|
"\n",
|
|
"## Learning Objectives\n",
|
|
"\n",
|
|
"*After this lesson, you will be able to:*\n",
|
|
"\n",
|
|
"- Perform common actions with sets.\n",
|
|
"- Perform common actions with tuples.\n",
|
|
"- Know when to use different data structures.\n",
|
|
"\n",
|
|
"---\n",
|
|
"\n",
|
|
"## Discussion: Lists\n",
|
|
"\n",
|
|
"Here are some lists:\n",
|
|
"\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "christian-coverage",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"unique_colors = [\"red\", \"yellow\", \"red\", \"green\", \"red\", \"yellow\"]\n",
|
|
"subscribed_emails = [\"mary@gmail.com\", \"opal@gmail.com\", \"mary@gmail.com\", \"sayed@gmail.com\"]\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "leading-pizza",
|
|
"metadata": {},
|
|
"source": [
|
|
"\n",
|
|
"What could be a problem here?\n",
|
|
"\n",
|
|
"<aside class=\"notes\">\n",
|
|
"\n",
|
|
"**Talking Points**:\n",
|
|
"\n",
|
|
"- \"Why, in the `subscribed_emails` list, would duplicate entries be a problem? Or unique colors having duplicates?\"\n",
|
|
"\n",
|
|
"**Teaching Tip**:\n",
|
|
"\n",
|
|
"- You can guide students to think about deduplication and the need to ensure unique values, thus dovetailing into sets\n",
|
|
"\n",
|
|
"</aside>\n",
|
|
"\n",
|
|
"---\n",
|
|
"\n",
|
|
"## Introducing Sets\n",
|
|
"\n",
|
|
"\n",
|
|
"Lists:\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "crazy-vinyl",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"unique_colors_list = [\"red\", \"yellow\", \"red\", \"green\", \"red\", \"yellow\"]\n",
|
|
"subscribed_emails_list = [\"mary@gmail.com\", \"opal@gmail.com\", \"mary@gmail.com\", \"sayed@gmail.com\"]\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "utility-spirit",
|
|
"metadata": {},
|
|
"source": [
|
|
"\n",
|
|
"Sets: an unordered collection of unique elements. These are powerful because set theory operations can be applied to them in O(1) time.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "sweet-overview",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"unique_colors_set = {\"green\", \"yellow\", \"red\"}\n",
|
|
"subscribed_emails_set = {\"mary@gmail.com\", \"opal@gmail.com\", \"sayed@gmail.com\"}\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "parental-style",
|
|
"metadata": {},
|
|
"source": [
|
|
"\n",
|
|
"- Notice the `{}` versus the `[]`.\n",
|
|
"\n",
|
|
"---\n",
|
|
"\n",
|
|
"\n",
|
|
"## How Can We Make a Set?\n",
|
|
"\n",
|
|
"If we explicitly cast a set from a list, Python removes duplicates automatically.\n",
|
|
"\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "peripheral-overhead",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"my_set = set(a_list_to_convert)\n",
|
|
"\n",
|
|
"# In action:\n",
|
|
"unique_colors_list = [\"red\", \"yellow\", \"red\", \"green\", \"red\", \"yellow\"]\n",
|
|
"unique_colors_set = set(unique_colors_list)\n",
|
|
"# => {\"green\", \"yellow\", \"red\"}\n",
|
|
"\n",
|
|
"# In action:\n",
|
|
"unique_colors_set_2 = set([\"red\", \"yellow\", \"red\", \"green\", \"red\", \"yellow\"])\n",
|
|
"# => {\"green\", \"yellow\", \"red\"}\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "novel-elevation",
|
|
"metadata": {},
|
|
"source": [
|
|
"\n",
|
|
"We can make a set directly using curly braces:\n",
|
|
"\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "associate-bikini",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"colors = {\"red\", \"orange\", \"yellow\", \"green\", \"blue\", \"indigo\", \"violet\"}\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "informed-express",
|
|
"metadata": {},
|
|
"source": [
|
|
"---\n",
|
|
"\n",
|
|
"## Important Note: Sets\n",
|
|
"\n",
|
|
"Lists are always in the same order:\n",
|
|
"\n",
|
|
"- `my_list = [\"green\", \"yellow\", \"red\"]` is always going to be`[\"green\", \"yellow\", \"red\"]`\n",
|
|
"- `my_list[0]` is always `\"green\"`; `my_list[1]` is always `\"yellow\"`; `my_list[2]` is always `\"red\"`.\n",
|
|
"\n",
|
|
"Sets are not like this! Similar to dictionaries, they're not ordered.\n",
|
|
"\n",
|
|
"- `my_set = {\"green\", \"yellow\", \"red\"}` could later be `{\"red\", \"yellow\", \"green\"}`!\n",
|
|
"- Note that python defaults to displaying an ascending sort of a set. Although this is displayed to the user when the variable is called via the interpreter, _the order cannot be relied upon_.\n",
|
|
"\n",
|
|
"We **cannot** do: `my_set[0]` - sets are unordered, and a `TypeError`\n",
|
|
"exception will be thrown.\n",
|
|
"\n",
|
|
"---\n",
|
|
"\n",
|
|
"## We Do: Creating a Set from a List\n",
|
|
"\n",
|
|
"Let's make some sets!\n",
|
|
"\n",
|
|
"- Make a list `clothing_list` containing the main color of your classmates' clothing.\n",
|
|
"- Using `clothing_list`, make a set named `clothing_set`.\n",
|
|
"- Use a `for` loop to print out both `clothing_list` and `clothing_set`.\n",
|
|
"- Try to print an index!"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 5,
|
|
"id": "intended-flesh",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"green\n",
|
|
"blue\n",
|
|
"white\n",
|
|
"black\n",
|
|
"\n",
|
|
"white\n",
|
|
"black\n",
|
|
"green\n",
|
|
"blue\n"
|
|
]
|
|
},
|
|
{
|
|
"ename": "TypeError",
|
|
"evalue": "'set' object is not subscriptable",
|
|
"output_type": "error",
|
|
"traceback": [
|
|
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
|
|
"\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)",
|
|
"\u001b[1;32m<ipython-input-5-1e34bcc782f1>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m 10\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mitem\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 11\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 12\u001b[1;33m \u001b[0mclothing_set\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
|
|
"\u001b[1;31mTypeError\u001b[0m: 'set' object is not subscriptable"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"clothing_list = ['green', 'blue', 'white', 'black']\n",
|
|
"clothing_set = set(clothing_list)\n",
|
|
"\n",
|
|
"for item in clothing_list:\n",
|
|
" print(item)\n",
|
|
" \n",
|
|
"print()\n",
|
|
"\n",
|
|
"for item in clothing_set:\n",
|
|
" print(item)\n",
|
|
" \n",
|
|
"clothing_set[0]"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "clean-brown",
|
|
"metadata": {},
|
|
"source": [
|
|
"---\n",
|
|
"\n",
|
|
"## We Do: Adding to a Set\n",
|
|
"\n",
|
|
"How do we add more to a set?"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 6,
|
|
"id": "literary-numbers",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# In a list:\n",
|
|
"clothing_list.append(\"red\")\n",
|
|
"\n",
|
|
"# In a set\n",
|
|
"clothing_set.add(\"red\")\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "prime-security",
|
|
"metadata": {},
|
|
"source": [
|
|
"\n",
|
|
"`add` vs `append` - this is because we can't guarantee it's going at the end!\n",
|
|
"\n",
|
|
"Let's add a few colors to `clothing_list` and `clothing_set` and then print them.\n",
|
|
"\n",
|
|
"- What happens if you add a duplicate?\n",
|
|
"\n",
|
|
"---\n",
|
|
"\n",
|
|
"## We Do: Removing from a List and a Set\n",
|
|
"\n",
|
|
"Remember, lists are always the same order and sets are not!\n",
|
|
"\n",
|
|
"- With the set `{\"green\", \"yellow\", \"red\"}`, `my_set[0]` could be `green`, `red`, or `yellow`.\n",
|
|
"\n",
|
|
"So thus, we need to be careful about removal:\n",
|
|
"\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 7,
|
|
"id": "impaired-happening",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# In a list:\n",
|
|
"clothing_list.pop() # Removes and returns the last item in the list.\n",
|
|
"clothing_list.pop(0) # Removes and returns a specific (here, the first) item in the list.\n",
|
|
"\n",
|
|
"# In a set\n",
|
|
"clothing_set.pop() # Removes and returns an arbitrary element. This is probably not what you want to do in most cases.\n",
|
|
"# clothing_set.pop(0) # Python throws a TypeError error (pop takes no arguments)\n",
|
|
"clothing_set.remove('red') # This removes an element from the set directly, and raises KeyError if the element is not present. This is the most common use.\n",
|
|
"clothing_set.discard('red') # This removes an element from the set directly, but does NOT throw a KeyError if the element is not present in the set.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "solved-niger",
|
|
"metadata": {},
|
|
"source": [
|
|
"\n",
|
|
"---\n",
|
|
"\n",
|
|
"## We Do: Set Intersection\n",
|
|
"\n",
|
|
"- One thing that sets are _really_ good at is _relational algebra_ (set theory). This is a fancy word for a SQL join, or comparing elements between two sets.\n",
|
|
"- Sets are really good at this because they use the same [hashing trick](https://en.wikipedia.org/wiki/Hash_table) under the hood that dictionaries use and makes them so fast.\n",
|
|
"\n",
|
|
"Let's start by making two sets:\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 8,
|
|
"id": "moral-friend",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"set1 = {1, 2, 3, 4, 5}\n",
|
|
"set2 = {4, 5, 6, 7, 8}\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "intensive-reflection",
|
|
"metadata": {},
|
|
"source": [
|
|
"\n",
|
|
"---\n",
|
|
"\n",
|
|
"## We Do: Set Intersection\n",
|
|
"\n",
|
|
"\n",
|
|
"Now let's use the `.intersection()` set method to find out _what elements these two sets have in common_:\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "adjacent-virus",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"set1.intersection(set2)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "regulation-match",
|
|
"metadata": {},
|
|
"source": [
|
|
"\n",
|
|
"Yields the result: `{4, 5}`\n",
|
|
"\n",
|
|
"- This makes sense, the two sets share the elements `4` and `5`.\n",
|
|
"- Note that this is commutative, meaning we could also write `set2.intersection(set1)` and receive the same result.\n",
|
|
"\n",
|
|
"---\n",
|
|
"\n",
|
|
"## We Do: Set Differences\n",
|
|
"\n",
|
|
"- Instead of finding _elements in common_ between two sets, we can also find _their differences_.\n",
|
|
"- To do this, we use `.difference()`.\n",
|
|
"- Note that this method _is not commutative_, meaning _order matters_.\n",
|
|
"- The difference is what is contained in the _first_ set, _but not in the second set_.\n",
|
|
"\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "loved-correspondence",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"set1.difference(set2)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "federal-sensitivity",
|
|
"metadata": {},
|
|
"source": [
|
|
"\n",
|
|
"Yields the result: `{1, 2, 3}`\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "amateur-possession",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"set2.difference(set1)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "elementary-opening",
|
|
"metadata": {},
|
|
"source": [
|
|
"Yields the result: `{6, 7, 8}`\n",
|
|
"\n",
|
|
"---\n",
|
|
"\n",
|
|
"## Quick Review: Sets vs. Lists\n",
|
|
"\n",
|
|
"**Lists**:\n",
|
|
"\n",
|
|
"- The original, normal object.\n",
|
|
"- Created with `[]`.\n",
|
|
"- `append()`, `insert(index)`, `pop()`, `pop(index)`.\n",
|
|
"- Duplicates and mutable.\n",
|
|
"\n",
|
|
"**Sets**:\n",
|
|
"\n",
|
|
"- Lists without duplicates.\n",
|
|
"- Created with `{}` or with `set(my_list)`.\n",
|
|
"- `add()` and `remove(element)`.\n",
|
|
"\n",
|
|
"---\n",
|
|
"## Quick Review: Sets vs. Lists"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "victorian-simple",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"### Creation ###\n",
|
|
"# List\n",
|
|
"my_list = [\"red\", \"yellow\", \"green\", \"red\"]\n",
|
|
"# Sets\n",
|
|
"my_set = {\"red\", \"yellow\", \"green\"}\n",
|
|
"my_set2 = set(my_list)\n",
|
|
"my_set = set(a_list_to_convert)\n",
|
|
"\n",
|
|
"### Appending a New Value ###\n",
|
|
"my_list.append(\"blue\")\n",
|
|
"my_set.add(\"blue\")\n",
|
|
"\n",
|
|
"### Appending a Duplicate ###\n",
|
|
"my_list.append(\"blue\")\n",
|
|
"# => my_list = [\"red\", \"yellow\", \"green\", \"red\", \"blue\", \"blue\"]\n",
|
|
"my_set.add(\"blue\")\n",
|
|
"# => my_set = {\"red\", \"yellow\", \"green\", \"blue\"}\n",
|
|
"\n",
|
|
"### Removing items: ###\n",
|
|
"my_list.pop(1)\n",
|
|
"my_set.remove(\"red\")\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "serial-sunglasses",
|
|
"metadata": {},
|
|
"source": [
|
|
"\n",
|
|
"---\n",
|
|
"\n",
|
|
"## Discussion: Immutability Thoughts\n",
|
|
"\n",
|
|
"A set is a type of list which doesn't allow duplicates.\n",
|
|
"\n",
|
|
"What if, instead, we have a list we don't want to change?\n",
|
|
"\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "killing-agenda",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"rainbow_colors = [\"red\", \"orange\", \"yellow\", \"green\", \"blue\", \"indigo\", \"violet\"]\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "executive-bunny",
|
|
"metadata": {},
|
|
"source": [
|
|
"\n",
|
|
"We **don't** want:\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "taken-celtic",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"rainbow_colors[0] = \"gray\"\n",
|
|
"## Gray's not in the rainbow!\n",
|
|
"rainbow_colors.pop()\n",
|
|
"## We can't lose violet!\n",
|
|
"rainbow_colors.append(\"pink\")\n",
|
|
"# Pink's not in the rainbow!\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "premier-notebook",
|
|
"metadata": {},
|
|
"source": [
|
|
"\n",
|
|
"We want `rainbow_colors` to be **immutable** - the list _cannot_ be changed.\n",
|
|
"\n",
|
|
"How we do that in Python?\n",
|
|
"\n",
|
|
"---\n",
|
|
"\n",
|
|
"## Introducing: Tuples\n",
|
|
"\n",
|
|
"**Sets** have the following properties:\n",
|
|
"\n",
|
|
"- No duplicates\n",
|
|
"- Mutable\n",
|
|
"\n",
|
|
"**Tuples** are similar to a list with the following differences:\n",
|
|
"\n",
|
|
"- Allows duplicates (same as a list)\n",
|
|
"- Once assigned, the tuple _cannot be changed_\n",
|
|
"\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "lined-exclusive",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"rainbow_colors_tuple = (\"red\", \"orange\", \"yellow\", \"green\", \"blue\", \"indigo\", \"violet\")\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "through-inflation",
|
|
"metadata": {},
|
|
"source": [
|
|
"\n",
|
|
"When should you use a tuple?\n",
|
|
"\n",
|
|
"- When you need data protection through immutability (note: all Python variables are public).\n",
|
|
"- When you never want to change the list.\n",
|
|
"- Tuples are sometimes wrapped in a class namespace to simulate what would be done with a [const structure](https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/how-to-define-constants) in a C-based language.\n",
|
|
"- This is a way of holding appconstants, or constants your program will use (like the API endpoint of a server, credentials, etc.)\n",
|
|
"\n",
|
|
"---\n",
|
|
"\n",
|
|
"## Tuple Syntax\n",
|
|
"\n",
|
|
"- Created with parentheses `()`.\n",
|
|
"- Access values via indices (like regular lists, but *not* like sets).\n",
|
|
"\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "changed-incident",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"rainbow_colors_tuple = (\"red\", \"orange\", \"yellow\", \"green\", \"blue\", \"indigo\", \"violet\")\n",
|
|
"print(rainbow_colors[1])"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "genetic-tragedy",
|
|
"metadata": {},
|
|
"source": [
|
|
"\n",
|
|
"- Tuples can be printed with a `for` loop (just like a set or list!).\n",
|
|
"\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "roman-mississippi",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"rainbow_colors_tuple = (\"red\", \"orange\", \"yellow\", \"green\", \"blue\", \"indigo\", \"violet\")\n",
|
|
"\n",
|
|
"for color in rainbow_colors_tuple:\n",
|
|
" print(color)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "conceptual-baker",
|
|
"metadata": {},
|
|
"source": [
|
|
"---\n",
|
|
"\n",
|
|
"## We Do: Tuples\n",
|
|
"\n",
|
|
"Let's declare a tuple named `seasons` and set it to have the values `fall`, `winter`, `spring`, and `summer`. We'll print the tuple and each value. Then we'll try to reassign them (we can't)!"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 9,
|
|
"id": "coral-channel",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"('fall', 'winter', 'spring', 'summer')\n",
|
|
"fall\n",
|
|
"winter\n",
|
|
"spring\n",
|
|
"summer\n"
|
|
]
|
|
},
|
|
{
|
|
"ename": "TypeError",
|
|
"evalue": "'tuple' object does not support item assignment",
|
|
"output_type": "error",
|
|
"traceback": [
|
|
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
|
|
"\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)",
|
|
"\u001b[1;32m<ipython-input-9-551911a28e22>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m 6\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mseason\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 7\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 8\u001b[1;33m \u001b[0mseasons\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m'more summer, please'\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
|
|
"\u001b[1;31mTypeError\u001b[0m: 'tuple' object does not support item assignment"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"seasons = ('fall', 'winter', 'spring', 'summer')\n",
|
|
"\n",
|
|
"print(seasons)\n",
|
|
"\n",
|
|
"for season in seasons:\n",
|
|
" print(season)\n",
|
|
"\n",
|
|
"seasons[0] = 'more summer, please'"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "unlimited-spoke",
|
|
"metadata": {},
|
|
"source": [
|
|
"---\n",
|
|
"\n",
|
|
"## Quick Review: Sets, Tuples, Lists\n",
|
|
"\n",
|
|
"**List**:\n",
|
|
"\n",
|
|
"- The original, normal object: `[\"red\", \"red\", \"yellow\", \"green\"]`.\n",
|
|
"- Has duplicates; mutable: `append()`, `insert(index)`, `pop()`, `pop(index)`\n",
|
|
"\n",
|
|
"**Set**:\n",
|
|
"\n",
|
|
"- List without duplicates: `{\"red\", \"yellow\", \"green\"}`.\n",
|
|
"- Mutable: `add()` and `remove(element)`\n",
|
|
"\n",
|
|
"**Tuple**:\n",
|
|
"\n",
|
|
"- Has duplicates, but immutable: You can't change it!\n",
|
|
"- `(\"red\", \"red\", \"yellow\", \"green\")` will *always* be `(\"red\", \"red\", \"yellow\", \"green\")`.\n",
|
|
"\n",
|
|
"---\n",
|
|
"\n",
|
|
"## Quick Review: Sets, Tuples, Lists"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "genuine-thirty",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"### Creation ###\n",
|
|
"# List\n",
|
|
"my_list = [\"red\", \"yellow\", \"green\", \"red\"]\n",
|
|
"# Sets\n",
|
|
"my_set = {\"red\", \"yellow\", \"green\"}\n",
|
|
"my_set2 = set(my_list))\n",
|
|
"my_set = set(a_list_to_convert)\n",
|
|
"# Tuples\n",
|
|
"my_tuple = (\"red\", \"yellow\", \"green\")\n",
|
|
"\n",
|
|
"### Appending a New Value ###\n",
|
|
"my_list.append(\"blue\")\n",
|
|
"my_set.add(\"blue\")\n",
|
|
"# Tuples -> You can't!\n",
|
|
"\n",
|
|
"### Removing items: ###\n",
|
|
"my_list.pop(1)\n",
|
|
"my_set.remove(\"red\")\n",
|
|
"# Tuples -> You can't!\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "fatty-church",
|
|
"metadata": {},
|
|
"source": [
|
|
"\n",
|
|
"---\n",
|
|
"\n",
|
|
"## Introducing Types\n",
|
|
"\n",
|
|
"Variables certainly can hold a lot!\n",
|
|
"\n",
|
|
"- Sets, tuples, and lists are easily confused.\n",
|
|
"- `type()` tells us what a variable is: set, tuple, list, dictionary, integer, string - anything!\n",
|
|
"\n",
|
|
"Try it:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 10,
|
|
"id": "lyric-adams",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"unique_colors is <class 'set'>\n",
|
|
"unique_colors_2 is <class 'list'>\n",
|
|
"unique_colors_3 is <class 'tuple'>\n",
|
|
"my_number is <class 'int'>\n",
|
|
"my_string is <class 'str'>\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"unique_colors = set([\"red\", \"yellow\", \"green\", \"red\"])\n",
|
|
"print(\"unique_colors is\", type(unique_colors))\n",
|
|
"# --\n",
|
|
"unique_colors_2 = [\"red\", \"yellow\", \"green\", \"red\"]\n",
|
|
"print(\"unique_colors_2 is\", type(unique_colors_2))\n",
|
|
"# --\n",
|
|
"unique_colors_3 = (\"red\", \"yellow\", \"green\", \"red\")\n",
|
|
"print(\"unique_colors_3 is\", type(unique_colors_3))\n",
|
|
"# --\n",
|
|
"my_number = 2\n",
|
|
"print(\"my_number is\", type(my_number))\n",
|
|
"# --\n",
|
|
"my_string = \"Hello!\"\n",
|
|
"print(\"my_string is\", type(my_string))\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "departmental-client",
|
|
"metadata": {},
|
|
"source": [
|
|
"\n",
|
|
"---\n",
|
|
"\n",
|
|
"## You Do: List Types Practice\n",
|
|
"\n",
|
|
"- Create a list (`[]`), set (`{}`), and tuple (`()`) of some of your favorite foods.\n",
|
|
"\n",
|
|
"Next, in every list type that you can:\n",
|
|
"\n",
|
|
"1. Add `\"pizza\"` anywhere; append `\"eggs\"` to the end.\n",
|
|
"2. Remove `\"pizza\"`.\n",
|
|
"3. Re-assign the element at index `1` to be `\"popcorn\"`.\n",
|
|
"4. Remove the element at index `2` and re-insert it at index `0`.\n",
|
|
"5. Print the element at index `0`.\n",
|
|
"\n",
|
|
"Print your final \"lists\" using a loop, then print their types. Don't throw an error!"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 14,
|
|
"id": "european-ivory",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"chicken\n",
|
|
"ice cream\n",
|
|
"\n",
|
|
"<class 'list'>\n",
|
|
"chicken\n",
|
|
"ice cream\n",
|
|
"popcorn\n",
|
|
"tacos\n",
|
|
"eggs\n",
|
|
"<class 'set'>\n",
|
|
"ice cream\n",
|
|
"chicken\n",
|
|
"cheeseburger\n",
|
|
"tacos\n",
|
|
"<class 'tuple'>\n",
|
|
"ice cream\n",
|
|
"cheeseburger\n",
|
|
"chicken\n",
|
|
"tacos\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"food_list = ['ice cream', 'cheeseburger', 'chicken', 'tacos']\n",
|
|
"food_set = {'ice cream', 'cheeseburger', 'chicken', 'tacos'}\n",
|
|
"food_tuple = ('ice cream', 'cheeseburger', 'chicken', 'tacos')\n",
|
|
"\n",
|
|
"# 1.\n",
|
|
"food_list.append('pizza')\n",
|
|
"food_set.add('pizza')\n",
|
|
"food_list.append('eggs')\n",
|
|
"\n",
|
|
"# 2.\n",
|
|
"food_list.pop(food_list.index('pizza'))\n",
|
|
"food_set.remove('pizza')\n",
|
|
"\n",
|
|
"# 3.\n",
|
|
"food_list[1] = 'popcorn'\n",
|
|
"\n",
|
|
"# 4. \n",
|
|
"food_list.insert(0, food_list.pop(2))\n",
|
|
"\n",
|
|
"# 5.\n",
|
|
"print(food_list[0])\n",
|
|
"print(food_tuple[0])\n",
|
|
"\n",
|
|
"print()\n",
|
|
"\n",
|
|
"print(type(food_list))\n",
|
|
"for item in food_list:\n",
|
|
" print(item)\n",
|
|
" \n",
|
|
"print(type(food_set)) \n",
|
|
"for item in food_set:\n",
|
|
" print(item)\n",
|
|
" \n",
|
|
"print(type(food_tuple)) \n",
|
|
"for item in food_tuple:\n",
|
|
" print(item)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "coordinate-walker",
|
|
"metadata": {},
|
|
"source": [
|
|
"---\n",
|
|
"\n",
|
|
"## Summary and Q&A\n",
|
|
"\n",
|
|
"We've learned two new types of lists:\n",
|
|
"\n",
|
|
"Sets:\n",
|
|
"\n",
|
|
"- A mutable list without duplicates.\n",
|
|
"- Handy for storing emails, usernames, and other unique elements."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "later-details",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"email_set = {'my_email@gmail.com', 'second_email@yahoo.com', \"third_email@hotmail.com\"}\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "smart-pursuit",
|
|
"metadata": {},
|
|
"source": [
|
|
"\n",
|
|
"Tuples:\n",
|
|
"\n",
|
|
"- An immutable list that allows duplicates.\n",
|
|
"- Handy for storing anything that won't change.\n",
|
|
"\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "numeric-fence",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"rainbow_tuple = (\"red\", \"orange\", \"yellow\", \"green\", \"blue\", \"indigo\", \"violet\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "christian-ready",
|
|
"metadata": {},
|
|
"source": [
|
|
"\n",
|
|
"---\n",
|
|
"\n",
|
|
"## Additional Reading\n",
|
|
"\n",
|
|
"- [Repl.it that recaps Tuples](https://repl.it/@GAcoding/python-programming-tuple-practice?lite=true)\n",
|
|
"- [Python Count Occurrences of Letters, Words and Numbers in Strings and Lists-Video](https://www.youtube.com/watch?v=szIFFw_Xl_M)\n",
|
|
"- [Storing Multiple Values in Lists](https://swcarpentry.github.io/python-novice-inflammation/03-lists/)\n",
|
|
"- [Sets and Frozen Sets](https://www.python-course.eu/sets_frozensets.php)\n",
|
|
"- [Sets](https://www.learnpython.org/en/Sets)\n",
|
|
"- [Python Tuple](https://www.programiz.com/python-programming/tuple)\n",
|
|
"- [Tuples](http://openbookproject.net/thinkcs/python/english3e/tuples.html)\n",
|
|
"- [Strings, Lists, Tuples, and Dictionaries Video](https://www.youtube.com/watch?v=19EfbO5D_8s)\n",
|
|
"- [Python Data Structures: Lists, Tuples, Sets, and Dictionaries Video](https://www.youtube.com/watch?v=R-HLU9Fl5ug)\n"
|
|
]
|
|
}
|
|
],
|
|
"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.7.6"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|