From aa25f2831792794649f2c80176aa609850c2a413 Mon Sep 17 00:00:00 2001 From: Louis Oliphant <ltoliphant@wisc.edu> Date: Sat, 5 Oct 2024 08:57:24 -0500 Subject: [PATCH] working on lec 14 --- .../14_Lists/Lec_14_Lists.ipynb | 68 ++++++++++++++++--- 1 file changed, 58 insertions(+), 10 deletions(-) diff --git a/f24/Louis_Lecture_Notes/14_Lists/Lec_14_Lists.ipynb b/f24/Louis_Lecture_Notes/14_Lists/Lec_14_Lists.ipynb index 2854404..72af2de 100644 --- a/f24/Louis_Lecture_Notes/14_Lists/Lec_14_Lists.ipynb +++ b/f24/Louis_Lecture_Notes/14_Lists/Lec_14_Lists.ipynb @@ -8,8 +8,10 @@ ] }, { - "cell_type": "markdown", + "cell_type": "code", + "execution_count": null, "metadata": {}, + "outputs": [], "source": [ "# Warmup 0: Complete the 5 TODOs!\n", "def get_wordle_results(guess):\n", @@ -127,9 +129,9 @@ "- Create a list and use sequence operations on a list.\n", "- Write loops that process lists\n", "- Explain key differences between strings and lists: type flexibility, mutability\n", - "- Mutate a list using \n", - " - indexing and double indexing, \n", - " - methods such as append, extend, sort, and pop\n", + "- Mutate a list using\n", + " - indexing and double indexing, \n", + " - methods such as append, extend, sort, and pop\n", "- split() a string into a list\n", "- join() list elements into a string" ] @@ -138,14 +140,45 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Create a list and use sequence operations on a list." + "### Creating Lists\n", + "\n", + "A **list** is used to store multiple items into a single data structure. To create a list you use square brackets `[]`.\n", + "\n", + "**Be Careful** -- notice that square brackets now have two meanings in Python. They are used for indexing and slicing (i.e. accessing the items within a sequence) and they are use for creating. Let's see the difference:\n", + "\n", + "**Indexing and Slicing:**\n", + "\n", + "```python\n", + "name = 'rumplestiltskin'\n", + "last_part = name[-4:] # <----slicing the value 'skin'\n", + "first_ch = name[0] # <----indexing the value 'r'\n", + "```\n", + "\n", + "**Creating a List:**\n", + "\n", + "```python\n", + "nothing = [] # <-- creating an empty list\n", + "greetings = ['hello','hola','dag','Kon\\'nichiwa'] # <-- creating a list of 4 strings\n", + "primes = [23,27,29] # <-- creating a list of 3 integers\n", + "rand_items = ['flugelhorn',23.17, True, 973] # <-- creating a list of 4 items (mixed types)\n", + "```\n", + "\n", + "Do you see the difference? If the square brackets *come right after a sequence* then you are accessing an item or items in the sequence. If the square brackets *stand alone* then you are creating a list." ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['bread', 'milk', 'eggs', 'apples', 'macNcheese']\t5\n" + ] + } + ], "source": [ "# A list is a sequence seperated by commas\n", "\n", @@ -154,6 +187,17 @@ "print(grocery_list, len(grocery_list), sep='\\t')" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "A list is a sequence so you can use all of the sequencing techniques you used with strings when you are working with lists. Namely, you can use the `len()` function, indexing, slicing, and `for` loops to work with the items in a list.\n", + "\n", + "### You Try It\n", + "\n", + "Change the code in the cell below to access the elements of the `grocery_list`." + ] + }, { "cell_type": "code", "execution_count": null, @@ -170,8 +214,12 @@ "# TODO: print the last item in grocery_list\n", "print(grocery_list) \n", "\n", - "# TODO: slice the list!\n", - "print(grocery_list) \n" + "# TODO: slice to get the eggs and apples\n", + "print(grocery_list) \n", + "\n", + "## TODO: finish the for loop to print out every item in the list\n", + "for ??? in ???:\n", + " pass\n" ] }, { @@ -624,7 +672,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.6" + "version": "3.10.12" } }, "nbformat": 4, -- GitLab