Skip to content

Commit 14613a1

Browse files
committed
updated python_basics
1 parent 8956360 commit 14613a1

File tree

1 file changed

+184
-50
lines changed

1 file changed

+184
-50
lines changed

python_basics.ipynb

Lines changed: 184 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -8,50 +8,6 @@
88
"To get the most out of this course, follow along with Andrei and code along with the videos. The course will go over how to install Python on your computer later. To start, you can use Python in [Repl.it](https://replit.com/)."
99
]
1010
},
11-
{
12-
"cell_type": "markdown",
13-
"id": "6a723c1e",
14-
"metadata": {},
15-
"source": [
16-
"- Python Basics\n",
17-
"- Learning Python\n",
18-
"- Python Data Types\n",
19-
"- How to Succeed\n",
20-
"- Numbers\n",
21-
"- Math Functions\n",
22-
"- DEVELOPER FUNDAMENTALS I\n",
23-
"- Operator Precedence\n",
24-
"- Optional: Bin and Complex\n",
25-
"- Variables\n",
26-
"- Expressions vs. Statements\n",
27-
"- Augmented Assignment Operator\n",
28-
"- Strings\n",
29-
"- String Concatenation\n",
30-
"- Type Conversion\n",
31-
"- Escape Sequence\n",
32-
"- Formatted Strings\n",
33-
"- String Indexes\n",
34-
"- Immutability\n",
35-
"- Built-in Functions and Methods\n",
36-
"- Booleans\n",
37-
"- DEVELOPER FUNDAMENTALS II\n",
38-
"- Lists\n",
39-
"- List Slicing\n",
40-
"- Matrix\n",
41-
"- List Methods\n",
42-
"- Common List Patterns\n",
43-
"- List Unpacking\n",
44-
"- None\n",
45-
"- Dictionaries\n",
46-
"- DEVELOPER FUNDAMENTALS III\n",
47-
"- Dictionary Keys\n",
48-
"- Dictionary Methods\n",
49-
"- Dictionary Methods 2\n",
50-
"- Tuples\n",
51-
"- Sets\n",
52-
"- Sets 2"
53-
]
54-
},
5511
{
5612
"cell_type": "markdown",
5713
"id": "dbdd5340",
@@ -1196,7 +1152,7 @@
11961152
},
11971153
{
11981154
"cell_type": "code",
1199-
"execution_count": 61,
1155+
"execution_count": 3,
12001156
"id": "95b42394",
12011157
"metadata": {},
12021158
"outputs": [
@@ -1217,7 +1173,7 @@
12171173
},
12181174
{
12191175
"cell_type": "code",
1220-
"execution_count": 62,
1176+
"execution_count": 4,
12211177
"id": "4fcb6cd0",
12221178
"metadata": {},
12231179
"outputs": [
@@ -1250,7 +1206,7 @@
12501206
},
12511207
{
12521208
"cell_type": "code",
1253-
"execution_count": 63,
1209+
"execution_count": 5,
12541210
"id": "05a8d266",
12551211
"metadata": {},
12561212
"outputs": [
@@ -1270,7 +1226,7 @@
12701226
},
12711227
{
12721228
"cell_type": "code",
1273-
"execution_count": 64,
1229+
"execution_count": 6,
12741230
"id": "806f9334",
12751231
"metadata": {},
12761232
"outputs": [
@@ -1299,7 +1255,7 @@
12991255
},
13001256
{
13011257
"cell_type": "code",
1302-
"execution_count": 68,
1258+
"execution_count": 7,
13031259
"id": "fcb09c53",
13041260
"metadata": {},
13051261
"outputs": [
@@ -1323,8 +1279,186 @@
13231279
"metadata": {},
13241280
"source": [
13251281
"## Common List Patterns\n",
1326-
"Common list patterns in Python."
1282+
"We'll now learn some useful tricks with lists. One that we've seen is len(), to figure out the length of a string or a list.\n",
1283+
"\n",
1284+
"Another is how to reverse a list, which we can do with the reverse() method or with list slicing."
1285+
]
1286+
},
1287+
{
1288+
"cell_type": "code",
1289+
"execution_count": 10,
1290+
"id": "1b8f9af7",
1291+
"metadata": {},
1292+
"outputs": [
1293+
{
1294+
"name": "stdout",
1295+
"output_type": "stream",
1296+
"text": [
1297+
"['x', 'e', 'd', 'd', 'c', 'b', 'a']\n",
1298+
"['a', 'b', 'c', 'd', 'd', 'e', 'x']\n"
1299+
]
1300+
}
1301+
],
1302+
"source": [
1303+
"print(basket)\n",
1304+
"basket.sort() # sort the list\n",
1305+
"basket.reverse() # reverse the sorted list\n",
1306+
"print(basket[::-1]) # reverse the last order of the sorted list with list slicing - creates new list"
13271307
]
1308+
},
1309+
{
1310+
"cell_type": "markdown",
1311+
"id": "cd2e95b0",
1312+
"metadata": {},
1313+
"source": [
1314+
"If we want to generate a numbered list quickly, we can use range."
1315+
]
1316+
},
1317+
{
1318+
"cell_type": "code",
1319+
"execution_count": 13,
1320+
"id": "e0765348",
1321+
"metadata": {},
1322+
"outputs": [
1323+
{
1324+
"name": "stdout",
1325+
"output_type": "stream",
1326+
"text": [
1327+
"range(1, 10)\n",
1328+
"[1, 2, 3, 4, 5, 6, 7, 8, 9]\n"
1329+
]
1330+
}
1331+
],
1332+
"source": [
1333+
"print(range(1, 10))\n",
1334+
"print(list(range(1, 10))) # start, stop (not inclusive)"
1335+
]
1336+
},
1337+
{
1338+
"cell_type": "markdown",
1339+
"id": "ec79474f",
1340+
"metadata": {},
1341+
"source": [
1342+
"join() takes an iterable data structure like a list and joins it together."
1343+
]
1344+
},
1345+
{
1346+
"cell_type": "code",
1347+
"execution_count": 17,
1348+
"id": "0d091ba3",
1349+
"metadata": {},
1350+
"outputs": [
1351+
{
1352+
"name": "stdout",
1353+
"output_type": "stream",
1354+
"text": [
1355+
"hi my name is Andrei\n"
1356+
]
1357+
}
1358+
],
1359+
"source": [
1360+
"sentence = \" \"\n",
1361+
"new_sentence = sentence.join([\"hi\", \"my\", \"name\", \"is\", \"Andrei\"])\n",
1362+
"print(new_sentence)"
1363+
]
1364+
},
1365+
{
1366+
"cell_type": "markdown",
1367+
"id": "9ae59879",
1368+
"metadata": {},
1369+
"source": [
1370+
"## List Unpacking\n",
1371+
"In Python, we can use list unpacking to assign elements of a list to multiple variables. Try out the examples below in your Python environment."
1372+
]
1373+
},
1374+
{
1375+
"cell_type": "code",
1376+
"execution_count": 21,
1377+
"id": "08399771",
1378+
"metadata": {},
1379+
"outputs": [
1380+
{
1381+
"name": "stdout",
1382+
"output_type": "stream",
1383+
"text": [
1384+
"1\n",
1385+
"2\n",
1386+
"3\n",
1387+
"[4, 5, 6, 7, 8]\n",
1388+
"9\n"
1389+
]
1390+
}
1391+
],
1392+
"source": [
1393+
"a,b,c, *rest, d = [1, 2, 3, 4, 5, 6, 7, 8, 9]\n",
1394+
"print(a)\n",
1395+
"print(b)\n",
1396+
"print(c)\n",
1397+
"print(rest)\n",
1398+
"print(d)"
1399+
]
1400+
},
1401+
{
1402+
"cell_type": "markdown",
1403+
"id": "6db20452",
1404+
"metadata": {},
1405+
"source": [
1406+
"## None\n",
1407+
"With None, we saw that some methods worked on lists but didn't output anything. None is a special data type that exists in Python. Most programming languages have a data type like this to represent an absence of value. We'll see this being used a lot more as we get deeper into the course.\n",
1408+
"\n",
1409+
"In a video game for example, when a user is first created, it won't have any weapons at the beginning of the game, so we can use None to represent this."
1410+
]
1411+
},
1412+
{
1413+
"cell_type": "code",
1414+
"execution_count": 22,
1415+
"id": "58e5d1e6",
1416+
"metadata": {},
1417+
"outputs": [
1418+
{
1419+
"name": "stdout",
1420+
"output_type": "stream",
1421+
"text": [
1422+
"None\n"
1423+
]
1424+
}
1425+
],
1426+
"source": [
1427+
"weapons = None\n",
1428+
"print(weapons)"
1429+
]
1430+
},
1431+
{
1432+
"cell_type": "markdown",
1433+
"id": "ed0762ac",
1434+
"metadata": {},
1435+
"source": [
1436+
"## Dictionaries"
1437+
]
1438+
},
1439+
{
1440+
"cell_type": "markdown",
1441+
"id": "34b1beac",
1442+
"metadata": {},
1443+
"source": [
1444+
"## DEVELOPER FUNDAMENTALS III"
1445+
]
1446+
},
1447+
{
1448+
"cell_type": "markdown",
1449+
"id": "c93a6ed0",
1450+
"metadata": {},
1451+
"source": [
1452+
"## Dictionary Keys"
1453+
]
1454+
},
1455+
{
1456+
"cell_type": "code",
1457+
"execution_count": null,
1458+
"id": "21319ef5",
1459+
"metadata": {},
1460+
"outputs": [],
1461+
"source": []
13281462
}
13291463
],
13301464
"metadata": {

0 commit comments

Comments
 (0)