Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Created on Cognitive Class Labs
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"alert alert-block alert-info\" style=\"margin-top: 20px\">\n",
" <a href=\"https://cocl.us/topNotebooksPython101Coursera\">\n",
" <img src=\"https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/PY0101EN/Ad/TopAd.png\" width=\"750\" align=\"center\">\n",
" </a>\n",
"</div>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a href=\"https://cognitiveclass.ai/\">\n",
" <img src=\"https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/PY0101EN/Ad/CCLog.png\" width=\"200\" align=\"center\">\n",
"</a>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h1>Sets in Python</h1>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<p><strong>Welcome!</strong> This notebook will teach you about the sets in the Python Programming Language. By the end of this lab, you'll know the basics set operations in Python, including what it is, operations and logic operations.</p> "
]
},
{
"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>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2 id=\"set\">Sets</h2>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3 id=\"content\">Set Content</h3>"
]
},
{
"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:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"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:"
]
},
{
"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\" />"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" You can also create a set from a list as follows:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false,
"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": 3,
"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:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"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": 4,
"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> "
]
},
{
"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>:"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"{'AC/DC', 'Back in Black', 'Thriller'}"
]
},
"execution_count": 5,
"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: "
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"{'AC/DC', 'Back in Black', 'NSYNC', 'Thriller'}"
]
},
"execution_count": 6,
"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": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"{'AC/DC', 'Back in Black', 'NSYNC', 'Thriller'}"
]
},
"execution_count": 7,
"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:"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"{'AC/DC', 'Back in Black', 'Thriller'}"
]
},
"execution_count": 8,
"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:"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 9,
"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>"
]
},
{
"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:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" Consider the following two sets:"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": false
},
"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\" />"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": false,
"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": 12,
"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."
]
},
{
"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\" />"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can find the intersect of two sets as follow using <code>&</code>:"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"{'AC/DC', 'Back in Black'}"
]
},
"execution_count": 13,
"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:"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"{'Thriller'}"
]
},
"execution_count": 14,
"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."
]
},
{
"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\" />"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The elements in <code>album_set2</code> but not in <code>album_set1</code> is given by:"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"{'The Dark Side of the Moon'}"
]
},
"execution_count": 15,
"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\" />"
]
},
{
"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:"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"{'AC/DC', 'Back in Black'}"
]
},
"execution_count": 16,
"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:"
]
},
{
"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\" />"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The union corresponds to all the elements in both sets, which is represented by coloring both circles:"
]
},
{
"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\" />"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" The union is given by:"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": [
"{'AC/DC', 'Back in Black', 'The Dark Side of the Moon', 'Thriller'}"
]
},
"execution_count": 17,
"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:"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Check if superset\n",
"\n",
"set(album_set1).issuperset(album_set2) "
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 19,
"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:"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 20,
"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": 21,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 21,
"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>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2 id=\"quiz\">Quiz on Sets</h2>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Convert the list <code>['rap','house','electronic music', 'rap']</code> to a set:"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'electronic music', 'house', 'rap'}"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Write your code below and press Shift+Enter to execute\n",
"set1=set(['rap','house','electronic music','rap'])\n",
"set1"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Double-click __here__ for the solution.\n",
"<!-- Your answer is below:\n",
"set(['rap','house','electronic music','rap'])\n",
"-->"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<hr>"
]
},
{
"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> "
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"the sum of A is 6\n",
"the sum of A is 6\n"
]
}
],
"source": [
"# Write your code below and press Shift+Enter to execute\n",
"A=[1,2,2,1]\n",
"B=[1,2,2,1]\n",
"print(\"the sum of A is \", sum(A))\n",
"print(\"the sum of A is \",sum(B))\n",
"\n"
]
},
{
"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",
"-->"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<hr>"
]
},
{
"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>:"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"{'AC/DC', 'Back in Black', 'The Dark Side of the Moon', 'Thriller'}"
]
},
"execution_count": 29,
"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",
"-->"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<hr>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Find out if <code>album_set1</code> is a subset of <code>album_set3</code>:"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Write your code below and press Shift+Enter to execute\n",
"album_set1.issubset(album_set3)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Double-click __here__ for the solution.\n",
"\n",
"<!-- Your answer is below:\n",
"album_set1.issubset(album_set3)\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>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"alert alert-block alert-info\" style=\"margin-top: 20px\">\n",
"<h2>Get IBM Watson Studio free of charge!</h2>\n",
" <p><a href=\"https://cocl.us/bottemNotebooksPython101Coursera\"><img src=\"https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/PY0101EN/Ad/BottomAd.png\" width=\"750\" align=\"center\"></a></p>\n",
"</div>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3>About the Authors:</h3> \n",
"<p><a href=\"https://www.linkedin.com/in/joseph-s-50398b136/\" target=\"_blank\">Joseph Santarcangelo</a> is a Data Scientist at IBM, and holds a PhD in Electrical Engineering. His research focused on using Machine Learning, Signal Processing, and Computer Vision to determine how videos impact human cognition. Joseph has been working for IBM since he completed his PhD.</p>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Other contributors: <a href=\"www.linkedin.com/in/jiahui-mavis-zhou-a4537814a\">Mavis Zhou</a>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<hr>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<p>Copyright &copy; 2018 IBM Developer Skills Network. This notebook and its source code are released under the terms of the <a href=\"https://cognitiveclass.ai/mit-license/\">MIT License</a>.</p>"
]
}
],
"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.6.6"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.