Skip to content

Commit 6b222f1

Browse files
add section on data structure methods and list comprehension along with dict. looping
1 parent 080ab4e commit 6b222f1

File tree

2 files changed

+112
-3
lines changed

2 files changed

+112
-3
lines changed

Data_Sturctures.ipynb

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"source": [
77
"## Data Structures\n",
88
"\n",
9-
"A \"Data Structure\" is just a fancy way of saying, \"I put some information in a container.\" Conveniently, it makes me seem more professional and fancy. Python has a few types of Data Structures, but the most common ones are lists, tuples, and dictionaries. These structures let you store and organize data in different ways, making it easier to work with.\n",
9+
"A \"Data Structure\" is just a fancy way of saying, \"I put some information in a container.\" Conveniently, it makes me seem more professional and fancy. Python has a few types of Data Structures, but the most common ones are lists, tuples, and dictionaries. These structures let you store and organize data in different ways, making it easier to work with. Sometimes, Data Structures are called iterables (because you can loop through them), but not all iterables are Data Structures. For example, strings are also iterables, but they are not Data Structures. You can think of Data Structures as containers that hold data, like a backpack or Batman's belt.\n",
1010
"\n",
1111
"### Lists\n",
1212
"Lists are ordered collections of items that can be changed (mutable). You can think of them as a shopping list where you can add, remove, or change items. Lists are defined using square brackets `[]`, and you can access items using their index (starting from 0), like this: `list[n]`. Each item in a list is called an \"element.\" You can have as many elements as you can fit in your array without running out of memory. By the way, this is called a \"Stack Overflow\" error, and it happens when you try to use more memory than your computer has available in the RAM. It's like trying to fit two gallons of milk into a one-gallon milk jug, it just doesn't work.\n",
@@ -326,6 +326,71 @@
326326
"print(\"Number of times 'John' appears:\", students.count(\"John\")) # how many times john is in the list"
327327
],
328328
"id": "1f326905b82a434b"
329+
},
330+
{
331+
"metadata": {},
332+
"cell_type": "markdown",
333+
"source": [
334+
"### list slicing\n",
335+
"\n",
336+
"list slicing is not what happens when i bring my shopping list to the kitchen and cut it up into pieces. List slicing is when you take little slices or portions of a list, and create a new list out of them. You can slice a list using the syntax `list[start:end:step]`, where `start` is the index to start from, `end` is the index to stop at (not inclusive), and `step` is the number of indices to skip. If you leave out any of these parameters, Python will use default values: `start=0`, `end=len(list)`, and `step=1`. run the following code block for an example:"
337+
],
338+
"id": "8166b0d9385c6c56"
339+
},
340+
{
341+
"metadata": {
342+
"ExecuteTime": {
343+
"end_time": "2025-08-20T14:46:43.215891Z",
344+
"start_time": "2025-08-20T14:46:43.210656Z"
345+
}
346+
},
347+
"cell_type": "code",
348+
"source": [
349+
"my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n",
350+
"print(my_list[2:5]) # (elements at indices 2, 3, and 4)\n",
351+
"print(my_list[:5]) # (elements from the start to index 4)\n",
352+
"print(my_list[5:]) # (elements from index 5 to the end)\n",
353+
"print(my_list[::2]) # (every second element)"
354+
],
355+
"id": "cc45dbab94ec0955",
356+
"outputs": [
357+
{
358+
"name": "stdout",
359+
"output_type": "stream",
360+
"text": [
361+
"[3, 4, 5]\n",
362+
"[1, 2, 3, 4, 5]\n",
363+
"[6, 7, 8, 9, 10]\n",
364+
"[1, 3, 5, 7, 9]\n"
365+
]
366+
}
367+
],
368+
"execution_count": 3
369+
},
370+
{
371+
"metadata": {},
372+
"cell_type": "markdown",
373+
"source": "funnily enough, you can also slice strings like this! lets say you wanted to get the first 5 characters of a string, so you could do `string[:5]`. You can still use negative indices like this, so let's try writing some code that gets the _last five_ characters of a string:",
374+
"id": "6ad9f3564906017d"
375+
},
376+
{
377+
"metadata": {},
378+
"cell_type": "code",
379+
"outputs": [],
380+
"execution_count": null,
381+
"source": [
382+
"my_string = \"Hello, world!\"\n",
383+
"print(my_string[:5])\n",
384+
"# now print out the last five characters of the string\n",
385+
"# your code here!"
386+
],
387+
"id": "522a670b3425c220"
388+
},
389+
{
390+
"metadata": {},
391+
"cell_type": "markdown",
392+
"source": "## pratical exercise : TO WRITE",
393+
"id": "95449c903c6b4dc1"
329394
}
330395
],
331396
"metadata": {

Loops_And_Logic.ipynb

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -372,14 +372,58 @@
372372
{
373373
"metadata": {},
374374
"cell_type": "markdown",
375-
"source": "In the last few lines in the code cell above, you can see that we used two loop variables and the shop.items() method. This method returns a tuple of the key-value pairs, which we can then unpack into two variables, `item` and `price`. This is a useful way to loop through dictionaries when you need both the key and the value.\n",
375+
"source": "In the last few lines in the code cell above, you can see that we used two loop variables and the shop.items() method. This method returns a tuple of the key-value pairs, which we can then unpack into two variables, `item` and `price`. This is a useful way to loop through dictionaries when you need both the key and the value.",
376376
"id": "c8ee33d12a560ace"
377377
},
378378
{
379379
"metadata": {},
380380
"cell_type": "markdown",
381-
"source": "",
381+
"source": [
382+
"## List Comprehension\n",
383+
"\n",
384+
"List comprehension is probably one of the most confusing yet useful things that you'll come accross in python. If you've ever used a filter in another language, it's pretty much that. List comprehension lets you create a new list by applying an expression to each item in an existing iterable (like a list or a string) in a single line of code. The syntax is `new_iterable = [expression for item in iterable if condition]`. The `if condition` part is optional, but it allows you to filter the items in the iterable based on a condition.\n",
385+
"\n",
386+
"Let's say I wanted to create a list of the squares of the numbers from 0 to 9. I could do it with list comprehension like this:"
387+
],
382388
"id": "2fb25b4b5f1c4fcb"
389+
},
390+
{
391+
"metadata": {
392+
"ExecuteTime": {
393+
"end_time": "2025-08-20T16:35:13.132352Z",
394+
"start_time": "2025-08-20T16:35:13.093430Z"
395+
}
396+
},
397+
"cell_type": "code",
398+
"source": [
399+
"squares = [x**2 for x in range(10)]\n",
400+
"print(squares)"
401+
],
402+
"id": "664e194baa78c948",
403+
"outputs": [
404+
{
405+
"name": "stdout",
406+
"output_type": "stream",
407+
"text": [
408+
"[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]\n"
409+
]
410+
}
411+
],
412+
"execution_count": 2
413+
},
414+
{
415+
"metadata": {},
416+
"cell_type": "markdown",
417+
"source": "However, lets say I only wanted the squares of the even numbers. I could add a condition to the list comprehension like this:",
418+
"id": "6460b1337be0f4da"
419+
},
420+
{
421+
"metadata": {},
422+
"cell_type": "code",
423+
"outputs": [],
424+
"execution_count": null,
425+
"source": "even_squares = [x**2 for x in range(10) if x % 2 == 0]\n",
426+
"id": "22c54f87234a5eb2"
383427
}
384428
],
385429
"metadata": {

0 commit comments

Comments
 (0)