Skip to content

Commit 1408a93

Browse files
move data struture stuff from loops to data structures
1 parent 9441475 commit 1408a93

File tree

1 file changed

+159
-0
lines changed

1 file changed

+159
-0
lines changed

Data_Structures.ipynb

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,39 @@
461461
],
462462
"id": "522a670b3425c220"
463463
},
464+
{
465+
"metadata": {},
466+
"cell_type": "markdown",
467+
"source": [
468+
"## Looping through Data Structures\n",
469+
"\n",
470+
"There are several ways to loop through data structures in Python, some of which make a decent amount of sense, others which are a bit more confusing. The most common way to loop through data structures is a `for` loop. You can use a `for` loop to iterate over the elements of a list, tuple, dictionary or string. The formula for this is `for item in data_structure:` where `data_structure` is the list, tuple, dictionary, or string you want to loop through. The `item` variable will take on the value of each element in the data structure as you iterate through it. Remember, you can only use this variable in its scope, which is in the loop.\n",
471+
"\n",
472+
"\n",
473+
"Run the code block below to see how it works with with a list, tuple, and string."
474+
],
475+
"id": "b1fd5bf54b16365d"
476+
},
477+
{
478+
"metadata": {},
479+
"cell_type": "code",
480+
"outputs": [],
481+
"execution_count": null,
482+
"source": [
483+
"for char in \"hello\":\n",
484+
" print(char) # This will print each character in the string \"hello\", because strings are like arrays of characters\n",
485+
"\n",
486+
"backpack = [\"laptop\", \"slightly broken TV\", \"water cooler\", \"monster energy\", \"a pencil stub that has only an eraser left\", \"Ace of Spades\", \"Rules Card\"]\n",
487+
"\n",
488+
"for item in backpack:\n",
489+
" print(item) # loops through each item in the backpack list and prints it\n",
490+
"\n",
491+
"PANTONE_448_C = (74, 65, 42) # the ugliest color in the world, used on cigarette packaging\n",
492+
"for color in PANTONE_448_C:\n",
493+
" print(color) # loops through each element in the tuple and prints it\n"
494+
],
495+
"id": "48fa92706c02d466"
496+
},
464497
{
465498
"metadata": {},
466499
"cell_type": "markdown",
@@ -469,6 +502,132 @@
469502
"\n"
470503
],
471504
"id": "95449c903c6b4dc1"
505+
},
506+
{
507+
"metadata": {},
508+
"cell_type": "markdown",
509+
"source": [
510+
"## Looping through Dictionaries\n",
511+
"Dictionaries are a bit different from lists and tuples, as they are key-value pairs. You can either loop through the keys, the values, or both. Here's how you can do that:"
512+
],
513+
"id": "fda4609996d1db8"
514+
},
515+
{
516+
"metadata": {},
517+
"cell_type": "code",
518+
"outputs": [],
519+
"execution_count": null,
520+
"source": [
521+
"shop = {\n",
522+
" \"apples\": 1.5,\n",
523+
" \"bananas\": 0.5,\n",
524+
" \"oranges\": 0.75,\n",
525+
" \"milk\": 2.0,\n",
526+
" \"eggs\": 999, # eggs are expensive these days, all the tariffs :pensive:\n",
527+
"}\n",
528+
"\n",
529+
"# Looping through keys\n",
530+
"for item in shop:\n",
531+
" print(item) # This will print each key in the dictionary\n",
532+
"# Looping through values\n",
533+
"for price in shop.values():\n",
534+
" print(price) # This will print each value in the dictionary\n",
535+
"# alternative way to loop through prices\n",
536+
"for item in shop:\n",
537+
" print(shop[item]) # access the price for each item using the key\n",
538+
"\n",
539+
"# Looping through both keys and values\n",
540+
"for item, price in shop.items():\n",
541+
" print(f\"{item}: ${price}\") # This will print each key-value pair in the dictionary"
542+
],
543+
"id": "dc42f1c14d649a5a"
544+
},
545+
{
546+
"metadata": {},
547+
"cell_type": "markdown",
548+
"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.",
549+
"id": "1289bd77c7fc2130"
550+
},
551+
{
552+
"metadata": {},
553+
"cell_type": "markdown",
554+
"source": [
555+
"## List Comprehension\n",
556+
"\n",
557+
"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",
558+
"\n",
559+
"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:"
560+
],
561+
"id": "703e7267cc4a55a6"
562+
},
563+
{
564+
"metadata": {},
565+
"cell_type": "code",
566+
"outputs": [],
567+
"execution_count": null,
568+
"source": [
569+
"squares = [x**2 for x in range(10)]\n",
570+
"print(squares)"
571+
],
572+
"id": "f316e36ecfa9e63c"
573+
},
574+
{
575+
"metadata": {},
576+
"cell_type": "markdown",
577+
"source": "However, lets say I only wanted the squares of the even numbers. I could add a condition to the list comprehension like this:",
578+
"id": "f4c28e448480ec5a"
579+
},
580+
{
581+
"metadata": {},
582+
"cell_type": "code",
583+
"outputs": [],
584+
"execution_count": null,
585+
"source": [
586+
"even_squares = [x**2 for x in range(10) if x % 2 == 0]\n",
587+
"print(even_squares)\n",
588+
"\n",
589+
"odd_squares = [x**2 for x in range(10) if x % 2]\n",
590+
"print(odd_squares)"
591+
],
592+
"id": "1f1f4726034ef609"
593+
},
594+
{
595+
"metadata": {},
596+
"cell_type": "markdown",
597+
"source": [
598+
"Wondering why the second one works lmao? In Python, any non-zero number is considered `True`, and zero is considered `False`. So, when we use `if x % 2`, it evaluates to `True` for odd numbers (which give a remainder of 1 when divided by 2) and `False` for even numbers (which give a remainder of 0). the list comprehension `[x**2 for x in range(10) if x % 2]` filters out the even numbers and only includes the squares of the odd numbers.\n",
599+
"\n",
600+
"### some other handy oneliners\n",
601+
"\n",
602+
"if you want to apply a single function to all of a list, or even just part of a list you can do something like this:\n",
603+
"\n",
604+
"`[expression for item in items if condition]`\n",
605+
"\n",
606+
"If you want an if/else clause, put that first in the list comprehension like this:\n",
607+
"\n",
608+
"`[expression if condition else other_expression for item in items]`\n",
609+
"\n",
610+
"here's a few examples:"
611+
],
612+
"id": "eefafe9c0546c6bd"
613+
},
614+
{
615+
"metadata": {},
616+
"cell_type": "code",
617+
"outputs": [],
618+
"execution_count": null,
619+
"source": [
620+
"words = [\"hello\", \"world\", \"this\", \"is\", \"a\", \"test\"]\n",
621+
"uppercased_words = [word.upper() for word in words]\n",
622+
"print(\" \".join(uppercased_words))\n",
623+
"\n",
624+
"inputs = [\"five\", \"seventy two\", None, \"twenty three\", \"forty two\"]\n",
625+
"inputs = [x for x in inputs if x is not None] # completely strip out the None values\n",
626+
"\n",
627+
"inputs = [\"five\", \"seventy two\", None, \"twenty three\", \"forty two\"]\n",
628+
"inputs = [x if x is not None else \"invalid\" for x in inputs] # replace None values with invalid\n"
629+
],
630+
"id": "4343bd7ddb8fcfb7"
472631
}
473632
],
474633
"metadata": {

0 commit comments

Comments
 (0)