Skip to content

Instantly share code, notes, and snippets.

@edisoncastro01
Created September 20, 2020 01:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edisoncastro01/15dd049e7afd053b64466cc70ff5454c to your computer and use it in GitHub Desktop.
Save edisoncastro01/15dd049e7afd053b64466cc70ff5454c to your computer and use it in GitHub Desktop.
Created on Skills Network Labs
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<center>\n",
" <img src=\"https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/Logos/organization_logo/organization_logo.png\" width=\"300\" alt=\"cognitiveclass.ai logo\" />\n",
"</center>\n",
"\n",
"# Sets in Python\n",
"\n",
"Estaimted time needed: **20** minutes\n",
"\n",
"## Objectives\n",
"\n",
"After complting this lab you will be able to:\n",
"\n",
"- Work with sets in Python, including operations and logic operations.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2>Table of Contents</h2>\n",
"<div class=\"alert alert-block alert-info\" style=\"margin-top: 20px\">\n",
" <ul>\n",
" <li>\n",
" <a href=\"#set\">Sets</a>\n",
" <ul>\n",
" <li><a href=\"content\">Set Content</a></li>\n",
" <li><a href=\"op\">Set Operations</a></li>\n",
" <li><a href=\"logic\">Sets Logic Operations</a></li>\n",
" </ul>\n",
" </li>\n",
" <li>\n",
" <a href=\"#quiz\">Quiz on Sets</a>\n",
" </li>\n",
" </ul>\n",
" <p>\n",
" Estimated time needed: <strong>20 min</strong>\n",
" </p>\n",
"</div>\n",
"\n",
"<hr>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2 id=\"set\">Sets</h2>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3 id=\"content\">Set Content</h3>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A set is a unique collection of objects in Python. You can denote a set with a curly bracket <b>{}</b>. Python will automatically remove duplicate items:\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'R&B', 'disco', 'hard rock', 'pop', 'rock', 'soul'}"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Create a set\n",
"\n",
"set1 = {\"pop\", \"rock\", \"soul\", \"hard rock\", \"rock\", \"R&B\", \"rock\", \"disco\"}\n",
"set1"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The process of mapping is illustrated in the figure:\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src=\"https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/PY0101EN/Chapter%202/Images/SetsUnique.png\" width=\"1100\" />\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" You can also create a set from a list as follows:\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"{'00:42:19',\n",
" 10.0,\n",
" 1982,\n",
" '30-Nov-82',\n",
" 46.0,\n",
" 65,\n",
" 'Michael Jackson',\n",
" None,\n",
" 'Pop, Rock, R&B',\n",
" 'Thriller'}"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Convert list to set\n",
"\n",
"album_list = [ \"Michael Jackson\", \"Thriller\", 1982, \"00:42:19\", \\\n",
" \"Pop, Rock, R&B\", 46.0, 65, \"30-Nov-82\", None, 10.0]\n",
"album_set = set(album_list) \n",
"album_set"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now let us create a set of genres:\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'R&B',\n",
" 'disco',\n",
" 'folk rock',\n",
" 'hard rock',\n",
" 'pop',\n",
" 'progressive rock',\n",
" 'rock',\n",
" 'soft rock',\n",
" 'soul'}"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Convert list to set\n",
"\n",
"music_genres = set([\"pop\", \"pop\", \"rock\", \"folk rock\", \"hard rock\", \"soul\", \\\n",
" \"progressive rock\", \"soft rock\", \"R&B\", \"disco\"])\n",
"music_genres"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3 id=\"op\">Set Operations</h3> \n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let us go over set operations, as these can be used to change the set. Consider the set <b>A</b>:\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'AC/DC', 'Back in Black', 'Thriller'}"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Sample set\n",
"\n",
"A = set([\"Thriller\", \"Back in Black\", \"AC/DC\"])\n",
"A"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" We can add an element to a set using the <code>add()</code> method: \n"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'AC/DC', 'Back in Black', 'NSYNC', 'Thriller'}"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Add element to set\n",
"\n",
"A.add(\"NSYNC\")\n",
"A"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" If we add the same element twice, nothing will happen as there can be no duplicates in a set:\n"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'AC/DC', 'Back in Black', 'NSYNC', 'Thriller'}"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Try to add duplicate element to the set\n",
"\n",
"A.add(\"NSYNC\")\n",
"A"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" We can remove an item from a set using the <code>remove</code> method:\n"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'AC/DC', 'Back in Black', 'Thriller'}"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Remove the element from set\n",
"\n",
"A.remove(\"NSYNC\")\n",
"A"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" We can verify if an element is in the set using the <code>in</code> command:\n"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Verify if the element is in the set\n",
"\n",
"\"AC/DC\" in A"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3 id=\"logic\">Sets Logic Operations</h3>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Remember that with sets you can check the difference between sets, as well as the symmetric difference, intersection, and union:\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" Consider the following two sets:\n"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"# Sample Sets\n",
"\n",
"album_set1 = set([\"Thriller\", 'AC/DC', 'Back in Black'])\n",
"album_set2 = set([ \"AC/DC\", \"Back in Black\", \"The Dark Side of the Moon\"])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src=\"https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/PY0101EN/Chapter%202/Images/SetsSamples.png\" width=\"650\" />\n"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"({'AC/DC', 'Back in Black', 'Thriller'},\n",
" {'AC/DC', 'Back in Black', 'The Dark Side of the Moon'})"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Print two sets\n",
"\n",
"album_set1, album_set2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"As both sets contain <b>AC/DC</b> and <b>Back in Black</b> we represent these common elements with the intersection of two circles.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src = \"https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/PY0101EN/Chapter%202/Images/SetsLogic.png\" width = \"650\" />\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can find the intersect of two sets as follow using <code>&</code>:\n"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'AC/DC', 'Back in Black'}"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Find the intersections\n",
"\n",
"intersection = album_set1 & album_set2\n",
"intersection"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can find all the elements that are only contained in <code>album_set1</code> using the <code>difference</code> method:\n"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'Thriller'}"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Find the difference in set1 but not set2\n",
"\n",
"album_set1.difference(album_set2) "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You only need to consider elements in <code>album_set1</code>; all the elements in <code>album_set2</code>, including the intersection, are not included.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src=\"https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/PY0101EN/Chapter%202/Images/SetsLeft.png\" width=\"650\" />\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The elements in <code>album_set2</code> but not in <code>album_set1</code> is given by:\n"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'The Dark Side of the Moon'}"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"album_set2.difference(album_set1) "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src = \"https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/PY0101EN/Chapter%202/Images/SetsRight.png\" width=\"650\" />\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can also find the intersection of <code>album_list1</code> and <code>album_list2</code>, using the <code>intersection</code> method:\n"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'AC/DC', 'Back in Black'}"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Use intersection method to find the intersection of album_list1 and album_list2\n",
"\n",
"album_set1.intersection(album_set2) "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" This corresponds to the intersection of the two circles:\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src=\"https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/PY0101EN/Chapter%202/Images/SetsIntersect.png\" width=\"650\" />\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The union corresponds to all the elements in both sets, which is represented by coloring both circles:\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src = \"https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/PY0101EN/Chapter%202/Images/SetsUnion.png\" width=\"650\" />\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" The union is given by:\n"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'AC/DC', 'Back in Black', 'The Dark Side of the Moon', 'Thriller'}"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Find the union of two sets\n",
"\n",
"album_set1.union(album_set2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"And you can check if a set is a superset or subset of another set, respectively, like this:\n"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Check if superset\n",
"\n",
"set(album_set1).issuperset(album_set2) "
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Check if subset\n",
"\n",
"set(album_set2).issubset(album_set1) "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here is an example where <code>issubset()</code> and <code>issuperset()</code> return true:\n"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Check if subset\n",
"\n",
"set({\"Back in Black\", \"AC/DC\"}).issubset(album_set1) "
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Check if superset\n",
"\n",
"album_set1.issuperset({\"Back in Black\", \"AC/DC\"}) "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<hr>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2 id=\"quiz\">Quiz on Sets</h2>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Convert the list <code>['rap','house','electronic music', 'rap']</code> to a set:\n"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'electronic music', 'house', 'rap'}"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Write your code below and press Shift+Enter to execute\n",
"set(['rap','house','electronic music', 'rap'])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Double-click **here** for the solution.\n",
"\n",
"<!-- Your answer is below:\n",
"set(['rap','house','electronic music','rap'])\n",
"-->\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<hr>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Consider the list <code>A = [1, 2, 2, 1]</code> and set <code>B = set([1, 2, 2, 1])</code>, does <code>sum(A) = sum(B)</code> \n"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"the sum of A is: 6\n",
"the sum of B is: 3\n"
]
}
],
"source": [
"# Write your code below and press Shift+Enter to execute\n",
"\n",
"A = [1, 2, 2, 1]\n",
"B = set([1, 2, 2, 1])\n",
"print(\"the sum of A is:\", sum(A))\n",
"print(\"the sum of B is:\", sum(B))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Double-click **here** for the solution.\n",
"\n",
"<!-- Your answer is below:\n",
"A = [1, 2, 2, 1] \n",
"B = set([1, 2, 2, 1])\n",
"print(\"the sum of A is:\", sum(A))\n",
"print(\"the sum of B is:\", sum(B))\n",
"-->\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<hr>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Create a new set <code>album_set3</code> that is the union of <code>album_set1</code> and <code>album_set2</code>:\n"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'AC/DC', 'Back in Black', 'The Dark Side of the Moon', 'Thriller'}"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Write your code below and press Shift+Enter to execute\n",
"\n",
"album_set1 = set([\"Thriller\", 'AC/DC', 'Back in Black'])\n",
"album_set2 = set([ \"AC/DC\", \"Back in Black\", \"The Dark Side of the Moon\"])\n",
"album_set3=album_set1.union(album_set2)\n",
"album_set3"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Double-click **here** for the solution.\n",
"\n",
"<!-- Your answer is below:\n",
"album_set3 = album_set1.union(album_set2)\n",
"album_set3\n",
"-->\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<hr>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Find out if <code>album_set1</code> is a subset of <code>album_set3</code>:\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Write your code below and press Shift+Enter to execute\n",
"album_set1"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Double-click **here** for the solution.\n",
"\n",
"<!-- Your answer is below:\n",
"album_set1.issubset(album_set3)\n",
"-->\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<hr>\n",
"<h2>The last exercise!</h2>\n",
"<p>Congratulations, you have completed your first lesson and hands-on lab in Python. However, there is one more thing you need to do. The Data Science community encourages sharing work. The best way to share and showcase your work is to share it on GitHub. By sharing your notebook on GitHub you are not only building your reputation with fellow data scientists, but you can also show it off when applying for a job. Even though this was your first piece of work, it is never too early to start building good habits. So, please read and follow <a href=\"https://cognitiveclass.ai/blog/data-scientists-stand-out-by-sharing-your-notebooks/\" target=\"_blank\">this article</a> to learn how to share your work.\n",
"<hr>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Author\n",
"\n",
"<a href=\"https://www.linkedin.com/in/joseph-s-50398b136/\" target=\"_blank\">Joseph Santarcangelo</a>\n",
"\n",
"## Other contributors\n",
"\n",
"<a href=\"www.linkedin.com/in/jiahui-mavis-zhou-a4537814a\">Mavis Zhou</a>\n",
"\n",
"## Change Log\n",
"\n",
"| Date (YYYY-MM-DD) | Version | Changed By | Change Description |\n",
"| ----------------- | ------- | ---------- | ---------------------------------- |\n",
"| 2020-08-26 | 2.0 | Lavanya | Moved lab to course repo in GitLab |\n",
"| | | | |\n",
"| | | | |\n",
"\n",
"## <h3 align=\"center\"> © IBM Corporation 2020. All rights reserved. <h3/>\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python",
"language": "python",
"name": "conda-env-python-py"
},
"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.6.11"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment