Skip to content

Commit cf4d7e7

Browse files
committed
Included Section on Sets
1 parent 8bc4c53 commit cf4d7e7

File tree

1 file changed

+163
-4
lines changed

1 file changed

+163
-4
lines changed

03 List and Tuples and Sets.ipynb

Lines changed: 163 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4942,20 +4942,179 @@
49424942
"A set object is an unordered collection of distinct hashable objects. Common uses include membership testing, removing duplicates from a sequence, and computing mathematical operations such as intersection, union, difference, and symmetric difference."
49434943
]
49444944
},
4945+
{
4946+
"cell_type": "code",
4947+
"execution_count": 1,
4948+
"metadata": {
4949+
"collapsed": false
4950+
},
4951+
"outputs": [
4952+
{
4953+
"data": {
4954+
"text/plain": [
4955+
"{'circle', 'square', 'triangle'}"
4956+
]
4957+
},
4958+
"execution_count": 1,
4959+
"metadata": {},
4960+
"output_type": "execute_result"
4961+
}
4962+
],
4963+
"source": [
4964+
"shapes = ['circle','square','triangle','circle']\n",
4965+
"set_of_shapes = set(shapes)\n",
4966+
"set_of_shapes"
4967+
]
4968+
},
4969+
{
4970+
"cell_type": "code",
4971+
"execution_count": 7,
4972+
"metadata": {
4973+
"collapsed": false
4974+
},
4975+
"outputs": [
4976+
{
4977+
"name": "stdout",
4978+
"output_type": "stream",
4979+
"text": [
4980+
"circle\n",
4981+
"square\n",
4982+
"polygon\n",
4983+
"triangle\n"
4984+
]
4985+
}
4986+
],
4987+
"source": [
4988+
"shapes = {'circle','square','triangle','circle'}\n",
4989+
"for shape in set_of_shapes:\n",
4990+
" print(shape)"
4991+
]
4992+
},
4993+
{
4994+
"cell_type": "code",
4995+
"execution_count": 6,
4996+
"metadata": {
4997+
"collapsed": false
4998+
},
4999+
"outputs": [
5000+
{
5001+
"name": "stdout",
5002+
"output_type": "stream",
5003+
"text": [
5004+
"{'circle', 'square', 'polygon', 'triangle'}\n"
5005+
]
5006+
}
5007+
],
5008+
"source": [
5009+
"set_of_shapes.add('polygon') \n",
5010+
"print(set_of_shapes)"
5011+
]
5012+
},
49455013
{
49465014
"cell_type": "markdown",
49475015
"metadata": {},
49485016
"source": [
4949-
"<a name='set_operations'></a>Defining tuples, and accessing elements\n",
4950-
"---"
5017+
"## Exists (Check)"
5018+
]
5019+
},
5020+
{
5021+
"cell_type": "code",
5022+
"execution_count": 2,
5023+
"metadata": {
5024+
"collapsed": false
5025+
},
5026+
"outputs": [
5027+
{
5028+
"name": "stdout",
5029+
"output_type": "stream",
5030+
"text": [
5031+
"Circle is in the set: True\n",
5032+
"Rhombus is in the set: False\n"
5033+
]
5034+
}
5035+
],
5036+
"source": [
5037+
"# Test if circle is IN the set (i.e. exist)\n",
5038+
"print('Circle is in the set: ', ('circle' in set_of_shapes))\n",
5039+
"print('Rhombus is in the set:', ('rhombus' in set_of_shapes))"
49515040
]
49525041
},
49535042
{
49545043
"cell_type": "markdown",
49555044
"metadata": {},
49565045
"source": [
4957-
"<a name='exercise_set'></a>Exercise\n",
4958-
"---"
5046+
"## Operations"
5047+
]
5048+
},
5049+
{
5050+
"cell_type": "code",
5051+
"execution_count": 3,
5052+
"metadata": {
5053+
"collapsed": false
5054+
},
5055+
"outputs": [
5056+
{
5057+
"data": {
5058+
"text/plain": [
5059+
"{'circle', 'triangle'}"
5060+
]
5061+
},
5062+
"execution_count": 3,
5063+
"metadata": {},
5064+
"output_type": "execute_result"
5065+
}
5066+
],
5067+
"source": [
5068+
"favourites_shapes = set(['circle','triangle','hexagon'])\n",
5069+
"\n",
5070+
"# Intersection\n",
5071+
"set_of_shapes.intersection(favourites_shapes)"
5072+
]
5073+
},
5074+
{
5075+
"cell_type": "code",
5076+
"execution_count": 4,
5077+
"metadata": {
5078+
"collapsed": false
5079+
},
5080+
"outputs": [
5081+
{
5082+
"data": {
5083+
"text/plain": [
5084+
"{'circle', 'hexagon', 'square', 'triangle'}"
5085+
]
5086+
},
5087+
"execution_count": 4,
5088+
"metadata": {},
5089+
"output_type": "execute_result"
5090+
}
5091+
],
5092+
"source": [
5093+
"# Union\n",
5094+
"set_of_shapes.union(favourites_shapes)"
5095+
]
5096+
},
5097+
{
5098+
"cell_type": "code",
5099+
"execution_count": 5,
5100+
"metadata": {
5101+
"collapsed": false
5102+
},
5103+
"outputs": [
5104+
{
5105+
"data": {
5106+
"text/plain": [
5107+
"{'square'}"
5108+
]
5109+
},
5110+
"execution_count": 5,
5111+
"metadata": {},
5112+
"output_type": "execute_result"
5113+
}
5114+
],
5115+
"source": [
5116+
"# Difference\n",
5117+
"set_of_shapes.difference(favourites_shapes)"
49595118
]
49605119
},
49615120
{

0 commit comments

Comments
 (0)