Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit a315965

Browse filesBrowse files
committed
Set
1 parent 1d9afb3 commit a315965
Copy full SHA for a315965

File tree

Expand file treeCollapse file tree

1 file changed

+253
-16
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+253
-16
lines changed
Open diff view settings
Collapse file

‎tutorial.ipynb‎

Copy file name to clipboardExpand all lines: tutorial.ipynb
+253-16Lines changed: 253 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
"\n",
2323
"#### 1) [Args and Kwargs](#ch1)\n",
2424
"#### 2) [Generators](#ch2)\n",
25-
"#### 2) [Map, Reduce, Filter](#ch3)"
25+
"#### 3) [Map, Reduce, Filter](#ch3)\n",
26+
"#### 4) [Sets](#ch4)"
2627
]
2728
},
2829
{
@@ -1048,12 +1049,12 @@
10481049
"source": [
10491050
"### Filter\n",
10501051
"As the name suggests, a filter method literally filters out the elements defined.\n",
1051-
"Let's filter "
1052+
"Let's filter only the negative numbers."
10521053
]
10531054
},
10541055
{
10551056
"cell_type": "code",
1056-
"execution_count": 73,
1057+
"execution_count": 81,
10571058
"metadata": {},
10581059
"outputs": [],
10591060
"source": [
@@ -1063,7 +1064,7 @@
10631064
},
10641065
{
10651066
"cell_type": "code",
1066-
"execution_count": 74,
1067+
"execution_count": 82,
10671068
"metadata": {},
10681069
"outputs": [
10691070
{
@@ -1072,7 +1073,7 @@
10721073
"-5"
10731074
]
10741075
},
1075-
"execution_count": 74,
1076+
"execution_count": 82,
10761077
"metadata": {},
10771078
"output_type": "execute_result"
10781079
}
@@ -1083,7 +1084,7 @@
10831084
},
10841085
{
10851086
"cell_type": "code",
1086-
"execution_count": 75,
1087+
"execution_count": 83,
10871088
"metadata": {},
10881089
"outputs": [
10891090
{
@@ -1092,7 +1093,7 @@
10921093
"-4"
10931094
]
10941095
},
1095-
"execution_count": 75,
1096+
"execution_count": 83,
10961097
"metadata": {},
10971098
"output_type": "execute_result"
10981099
}
@@ -1103,7 +1104,7 @@
11031104
},
11041105
{
11051106
"cell_type": "code",
1106-
"execution_count": 76,
1107+
"execution_count": 84,
11071108
"metadata": {},
11081109
"outputs": [
11091110
{
@@ -1112,7 +1113,7 @@
11121113
"-3"
11131114
]
11141115
},
1115-
"execution_count": 76,
1116+
"execution_count": 84,
11161117
"metadata": {},
11171118
"output_type": "execute_result"
11181119
}
@@ -1123,7 +1124,7 @@
11231124
},
11241125
{
11251126
"cell_type": "code",
1126-
"execution_count": 77,
1127+
"execution_count": 85,
11271128
"metadata": {},
11281129
"outputs": [
11291130
{
@@ -1132,7 +1133,7 @@
11321133
"-2"
11331134
]
11341135
},
1135-
"execution_count": 77,
1136+
"execution_count": 85,
11361137
"metadata": {},
11371138
"output_type": "execute_result"
11381139
}
@@ -1143,7 +1144,7 @@
11431144
},
11441145
{
11451146
"cell_type": "code",
1146-
"execution_count": 78,
1147+
"execution_count": 86,
11471148
"metadata": {},
11481149
"outputs": [
11491150
{
@@ -1152,7 +1153,7 @@
11521153
"-1"
11531154
]
11541155
},
1155-
"execution_count": 78,
1156+
"execution_count": 86,
11561157
"metadata": {},
11571158
"output_type": "execute_result"
11581159
}
@@ -1163,7 +1164,7 @@
11631164
},
11641165
{
11651166
"cell_type": "code",
1166-
"execution_count": 79,
1167+
"execution_count": 87,
11671168
"metadata": {},
11681169
"outputs": [
11691170
{
@@ -1173,7 +1174,7 @@
11731174
"traceback": [
11741175
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
11751176
"\u001b[0;31mStopIteration\u001b[0m Traceback (most recent call last)",
1176-
"\u001b[0;32m<ipython-input-79-373734c2136e>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mnext\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mless_than_zero\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
1177+
"\u001b[0;32m<ipython-input-87-373734c2136e>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mnext\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mless_than_zero\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
11771178
"\u001b[0;31mStopIteration\u001b[0m: "
11781179
]
11791180
}
@@ -1189,12 +1190,248 @@
11891190
"Cause that's it. The filtered list only contains `[-5, -4, -3, -2, -1]`"
11901191
]
11911192
},
1193+
{
1194+
"cell_type": "markdown",
1195+
"metadata": {},
1196+
"source": [
1197+
"<a id=\"ch4\"></a>\n",
1198+
"## Chapter 4 - Sets\n",
1199+
"\n",
1200+
"A set is an unordered collection with no duplicate elements. Basic uses include membership testing and eliminating duplicate entries. Set objects also support mathematical operations like union, intersection, difference, and symmetric difference.<br>\n",
1201+
"Curly braces or the `set()` function can be used to create sets. Note: to create an empty set you have to use `set()`, not {}; the latter creates an empty dictionary"
1202+
]
1203+
},
1204+
{
1205+
"cell_type": "code",
1206+
"execution_count": 75,
1207+
"metadata": {},
1208+
"outputs": [
1209+
{
1210+
"data": {
1211+
"text/plain": [
1212+
"{'apple', 'banana', 'orange', 'pear'}"
1213+
]
1214+
},
1215+
"execution_count": 75,
1216+
"metadata": {},
1217+
"output_type": "execute_result"
1218+
}
1219+
],
1220+
"source": [
1221+
"basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}\n",
1222+
"basket"
1223+
]
1224+
},
1225+
{
1226+
"cell_type": "code",
1227+
"execution_count": 84,
1228+
"metadata": {},
1229+
"outputs": [],
1230+
"source": [
1231+
"# Demonstrate set operations on unique letters from two words\n",
1232+
"a = set('abracadabra')\n",
1233+
"b = set('alacazam')"
1234+
]
1235+
},
1236+
{
1237+
"cell_type": "code",
1238+
"execution_count": 85,
1239+
"metadata": {},
1240+
"outputs": [
1241+
{
1242+
"data": {
1243+
"text/plain": [
1244+
"{'a', 'b', 'c', 'd', 'r'}"
1245+
]
1246+
},
1247+
"execution_count": 85,
1248+
"metadata": {},
1249+
"output_type": "execute_result"
1250+
}
1251+
],
1252+
"source": [
1253+
"a"
1254+
]
1255+
},
1256+
{
1257+
"cell_type": "code",
1258+
"execution_count": 86,
1259+
"metadata": {},
1260+
"outputs": [
1261+
{
1262+
"data": {
1263+
"text/plain": [
1264+
"{'a', 'c', 'l', 'm', 'z'}"
1265+
]
1266+
},
1267+
"execution_count": 86,
1268+
"metadata": {},
1269+
"output_type": "execute_result"
1270+
}
1271+
],
1272+
"source": [
1273+
"b"
1274+
]
1275+
},
1276+
{
1277+
"cell_type": "code",
1278+
"execution_count": 87,
1279+
"metadata": {},
1280+
"outputs": [
1281+
{
1282+
"data": {
1283+
"text/plain": [
1284+
"{'b', 'd', 'r'}"
1285+
]
1286+
},
1287+
"execution_count": 87,
1288+
"metadata": {},
1289+
"output_type": "execute_result"
1290+
}
1291+
],
1292+
"source": [
1293+
"a-b # letters in a but not in b"
1294+
]
1295+
},
1296+
{
1297+
"cell_type": "code",
1298+
"execution_count": 88,
1299+
"metadata": {},
1300+
"outputs": [
1301+
{
1302+
"data": {
1303+
"text/plain": [
1304+
"{'l', 'm', 'z'}"
1305+
]
1306+
},
1307+
"execution_count": 88,
1308+
"metadata": {},
1309+
"output_type": "execute_result"
1310+
}
1311+
],
1312+
"source": [
1313+
"b-a # letters in b but not in a"
1314+
]
1315+
},
1316+
{
1317+
"cell_type": "code",
1318+
"execution_count": 89,
1319+
"metadata": {},
1320+
"outputs": [
1321+
{
1322+
"data": {
1323+
"text/plain": [
1324+
"{'a', 'c'}"
1325+
]
1326+
},
1327+
"execution_count": 89,
1328+
"metadata": {},
1329+
"output_type": "execute_result"
1330+
}
1331+
],
1332+
"source": [
1333+
"a & b # letters in both a and b"
1334+
]
1335+
},
1336+
{
1337+
"cell_type": "code",
1338+
"execution_count": 90,
1339+
"metadata": {},
1340+
"outputs": [
1341+
{
1342+
"data": {
1343+
"text/plain": [
1344+
"{'a', 'b', 'c', 'd', 'l', 'm', 'r', 'z'}"
1345+
]
1346+
},
1347+
"execution_count": 90,
1348+
"metadata": {},
1349+
"output_type": "execute_result"
1350+
}
1351+
],
1352+
"source": [
1353+
"a | b # letters in a or b or both"
1354+
]
1355+
},
1356+
{
1357+
"cell_type": "code",
1358+
"execution_count": 91,
1359+
"metadata": {},
1360+
"outputs": [
1361+
{
1362+
"data": {
1363+
"text/plain": [
1364+
"{'b', 'd', 'l', 'm', 'r', 'z'}"
1365+
]
1366+
},
1367+
"execution_count": 91,
1368+
"metadata": {},
1369+
"output_type": "execute_result"
1370+
}
1371+
],
1372+
"source": [
1373+
"a ^ b # letters in a or b but not both"
1374+
]
1375+
},
1376+
{
1377+
"cell_type": "code",
1378+
"execution_count": 93,
1379+
"metadata": {},
1380+
"outputs": [
1381+
{
1382+
"data": {
1383+
"text/plain": [
1384+
"{'d', 'r'}"
1385+
]
1386+
},
1387+
"execution_count": 93,
1388+
"metadata": {},
1389+
"output_type": "execute_result"
1390+
}
1391+
],
1392+
"source": [
1393+
"# Set comprehension\n",
1394+
"a = {x for x in 'abracadabra' if x not in 'abc'}\n",
1395+
"a"
1396+
]
1397+
},
1398+
{
1399+
"cell_type": "code",
1400+
"execution_count": 94,
1401+
"metadata": {},
1402+
"outputs": [
1403+
{
1404+
"data": {
1405+
"text/plain": [
1406+
"{'a', 'h', 'k', 's'}"
1407+
]
1408+
},
1409+
"execution_count": 94,
1410+
"metadata": {},
1411+
"output_type": "execute_result"
1412+
}
1413+
],
1414+
"source": [
1415+
"a = \"akash\"\n",
1416+
"set(a)"
1417+
]
1418+
},
1419+
{
1420+
"cell_type": "markdown",
1421+
"metadata": {},
1422+
"source": [
1423+
"Remove duplicates from the list."
1424+
]
1425+
},
11921426
{
11931427
"cell_type": "code",
11941428
"execution_count": null,
11951429
"metadata": {},
11961430
"outputs": [],
1197-
"source": []
1431+
"source": [
1432+
"duplicated_list = [1,2,2,4,6,3,9,6,9,7]\n",
1433+
"final_list = set([x for x in duplicated_list if duplicated_list.co])"
1434+
]
11981435
}
11991436
],
12001437
"metadata": {

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.