You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
669 lines
16 KiB
669 lines
16 KiB
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "spread-midwest",
|
|
"metadata": {},
|
|
"source": [
|
|
"<!--\n",
|
|
"title: Intro to Python Lists\n",
|
|
"type: lesson\n",
|
|
"duration: \"00:30\"\n",
|
|
"creator: Brandi Butler\n",
|
|
"Private gist location: https://gist.github.com/brandiw/621c35f1987e5ab680e7de7b05dfe039\n",
|
|
"Presentation URL: https://presentations.generalassemb.ly/621c35f1987e5ab680e7de7b05dfe039#/\n",
|
|
"-->\n",
|
|
"\n",
|
|
"<h1>Python Programming: Lists</h1>\n",
|
|
"\n",
|
|
"<!--\n",
|
|
"\n",
|
|
"## Overview\n",
|
|
"This lesson introduces students to the concept of lists. This begins as basic list operations - accessing elements, `len`, `insert`, `append`, and `pop`. After an exercise to recap that, it segues into operations on numerical lists - `sum`, `min`, and `max`. It ends with a longer exercise recapping the list operations.\n",
|
|
"\n",
|
|
"## Learning Objectives\n",
|
|
"In this lesson, students will:\n",
|
|
"- Create lists in Python.\n",
|
|
"- Print out specific elements in a list.\n",
|
|
"- Perform common list operations.\n",
|
|
"\n",
|
|
"## Duration\n",
|
|
"30 minutes\n",
|
|
"\n",
|
|
"### Notes on Timing\n",
|
|
"\n",
|
|
"A 30 minute interval has been allotted for this lesson. You may finish up early due to the fact that this lesson doesn't get into loops or ranges. If you have extra time, put it on the activities or start the next lesson early so students do have buffer time later, when they need it.\n",
|
|
"\n",
|
|
"That said, at the point you give this lesson, students are still on day one. They will require more time than you probably expect to poke around the code.\n",
|
|
"\n",
|
|
"## Suggested Agenda\n",
|
|
"\n",
|
|
"| Time | Activity |\n",
|
|
"| --- | --- |\n",
|
|
"| 0:00 - 0:03 | Welcome |\n",
|
|
"| 0:03 - 0:15 | Basic List Operations |\n",
|
|
"| 0:15 - 0:25 | Numerical List Operations |\n",
|
|
"| 0:25 - 0:30 | Summary |\n",
|
|
"\n",
|
|
"## In Class: Materials\n",
|
|
"- Projector\n",
|
|
"- Internet connection\n",
|
|
"- Python3\n",
|
|
"-->\n",
|
|
"\n",
|
|
"---\n",
|
|
"\n",
|
|
"## Lesson Objectives\n",
|
|
"*After this lesson, you will be able to...*\n",
|
|
"\n",
|
|
"- Create lists in Python.\n",
|
|
"- Print out specific elements in a list.\n",
|
|
"- Perform common list operations.\n",
|
|
"\n",
|
|
"---\n",
|
|
"\n",
|
|
"## What is a List?\n",
|
|
"\n",
|
|
"Variables hold one item.\n",
|
|
"\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "simple-snapshot",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"my_color = \"red\"\n",
|
|
"my_peer = \"Brandi\""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "double-daily",
|
|
"metadata": {},
|
|
"source": [
|
|
"\n",
|
|
"- **Lists** hold multiple items - and lists can hold anything.\n",
|
|
"- A list is a data structure in Python, which is a fancy way of saying we can put data inside of it. In the same way you recognize strings by the quotation marks surround them, you can recognize lists by square brackets that surround them.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "moved-warner",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Declaring lists\n",
|
|
"colors = [\"red\", \"yellow\", \"green\"]\n",
|
|
"my_class = [\"Brandi\", \"Zoe\", \"Steve\", \"Aleksander\", \"Dasha\"]\n",
|
|
"\n",
|
|
"# Strings\n",
|
|
"colors = [\"red\", \"yellow\", \"green\"]\n",
|
|
"\n",
|
|
"# Numbers\n",
|
|
"my_nums = [4, 7, 9, 1, 4]\n",
|
|
"\n",
|
|
"# Both!\n",
|
|
"my_nums = [\"red\", 7, \"yellow\", 1, 4]"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "noble-rover",
|
|
"metadata": {},
|
|
"source": [
|
|
"\n",
|
|
"---\n",
|
|
"\n",
|
|
"\n",
|
|
"## Accessing Elements\n",
|
|
"\n",
|
|
"\n",
|
|
"**List Index** means the location of something (an *element*) in the list.\n",
|
|
"\n",
|
|
"List indexes start counting at 0!\n",
|
|
"\n",
|
|
"| List | \"Brandi\" | \"Zoe\" | \"Steve\" | \"Aleksander\" | \"Dasha\" |\n",
|
|
"|:-----:|:--------:|:-----:|:-------:|:------:|:------:|\n",
|
|
"| Index | 0 | 1 | 2 | 3 | 4 |\n",
|
|
"\n",
|
|
"\n",
|
|
"In our previous example, let's print a few specific items. We can access an item by counting from 0 and using square brackets to tell the list which item we want."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "impaired-darwin",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"my_class = [\"Brandi\", \"Zoe\", \"Steve\", \"Aleksander\", \"Dasha\"]\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "global-resort",
|
|
"metadata": {},
|
|
"source": [
|
|
"---\n",
|
|
"\n",
|
|
"## We Do: Lists"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "running-medium",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# 1. Create a **list** with the names `\"Holly\"`, `\"Juan\"`, and `\"Ming\"`.\n",
|
|
"\n",
|
|
"# 2. Print the third name.\n",
|
|
"\n",
|
|
"# 3. Create a **list** with the numbers `2`,`4`, `6`, and `8`.\n",
|
|
"\n",
|
|
"# 4. Print the first number.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "racial-municipality",
|
|
"metadata": {},
|
|
"source": [
|
|
"---\n",
|
|
"## List Indexing - Slicing\n",
|
|
"\n",
|
|
"- You can use the list indices to grab more than one item from a list using slicing.\n",
|
|
"- Similar syntax to indexing (using square brackets) after the list name, but allows for a range: `my_list[start:stop:step]`\n",
|
|
"- The `end` index used _will not_ be included in the output.\n",
|
|
"- If the start or end of the range is unspecified (left blank), it's assumed you want to use the beginning or end of the list, respectively.\n",
|
|
"- A step size of 1 will be assumed if you don't give it a value.\n",
|
|
"- You can use negative values to indicate that you want to count from the end of the list rather than the beginning.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "adjusted-solution",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"nums = [4, 9, 3, 6, 8, 2]\n",
|
|
"\n",
|
|
"print(nums[0:3])\n",
|
|
"\n",
|
|
"print(nums[:3])\n",
|
|
"\n",
|
|
"print(nums[3:])\n",
|
|
"\n",
|
|
"print(nums[::2])\n",
|
|
"\n",
|
|
"print(nums[-2:])"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "recent-phase",
|
|
"metadata": {},
|
|
"source": [
|
|
"---\n",
|
|
"\n",
|
|
"## List Operations - Length\n",
|
|
"\n",
|
|
"\n",
|
|
"`len()`:\n",
|
|
"\n",
|
|
"- A built in `list` operation.\n",
|
|
"- How long is the list?"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "broken-sucking",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"my_class = [\"Brandi\", \"Zoe\", \"Steve\", \"Aleksander\", \"Dasha\"]\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "round-bruce",
|
|
"metadata": {},
|
|
"source": [
|
|
"\n",
|
|
"---\n",
|
|
"\n",
|
|
"## Adding Elements: Append\n",
|
|
"\n",
|
|
"`.append()`:\n",
|
|
"\n",
|
|
"- A built in `list` operation.\n",
|
|
"- Adds to the end of the list.\n",
|
|
"- Takes any element.\n",
|
|
"\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "lined-coupon",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"my_class = [\"Brandi\", \"Zoe\", \"Steve\", \"Aleksander\", \"Dasha\"]\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "listed-house",
|
|
"metadata": {},
|
|
"source": [
|
|
"\n",
|
|
"---\n",
|
|
"\n",
|
|
"## Adding Elements: Insert\n",
|
|
"\n",
|
|
"`.insert()`:\n",
|
|
"\n",
|
|
"- A built in `list` operation.\n",
|
|
"- Adds to any point in the list\n",
|
|
"- Takes any element and an index.\n",
|
|
"\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "exciting-caribbean",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"my_class = [\"Brandi\", \"Zoe\", \"Steve\", \"Aleksander\", \"Dasha\", \"Sonyl\"]\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "silver-bridal",
|
|
"metadata": {},
|
|
"source": [
|
|
"\n",
|
|
"---\n",
|
|
"\n",
|
|
"## Removing elements - Pop\n",
|
|
"\n",
|
|
"\n",
|
|
"`.pop()`:\n",
|
|
"\n",
|
|
"- A built in `list` operation.\n",
|
|
"- Removes an item from the end of the list.\n",
|
|
"\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "african-weapon",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"my_class = [\"Brandi\", \"Zoe\", \"Steve\", \"Aleksander\", \"Dasha\", \"Sonyl\"]\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "specified-soundtrack",
|
|
"metadata": {},
|
|
"source": [
|
|
"\n",
|
|
"---\n",
|
|
"\n",
|
|
"## Removing elements - Pop(index)\n",
|
|
"\n",
|
|
"`.pop(index)`:\n",
|
|
"\n",
|
|
"- A built in `list` operation.\n",
|
|
"- Removes an item from the list.\n",
|
|
"- Can take an index.\n",
|
|
"\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "different-instrumentation",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"my_class = [\"Brandi\", \"Zoe\", \"Steve\", \"Aleksander\", \"Dasha\", \"Sonyl\"]\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "grave-poetry",
|
|
"metadata": {},
|
|
"source": [
|
|
"\n",
|
|
"---\n",
|
|
"\n",
|
|
"## Partner Exercise: Pop, Insert, and Append\n",
|
|
"\n",
|
|
"Partner up! Choose one person to be the driver and one to be the navigator, and see if you can do the prompts:\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "extra-saint",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# 1. Declare a list with the names of your classmates\n",
|
|
"\n",
|
|
"# 2. Print out the length of that list\n",
|
|
"\n",
|
|
"# 3. Print the 3rd name on the list\n",
|
|
"\n",
|
|
"# 4. Delete the first name on the list\n",
|
|
"\n",
|
|
"# 5. Re-add the name you deleted to the end of the list\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "piano-relevance",
|
|
"metadata": {},
|
|
"source": [
|
|
"\n",
|
|
"---\n",
|
|
"\n",
|
|
"## !! List Mutation: Warning !!\n",
|
|
"\n",
|
|
"This won't work as expected - don't do this!\n",
|
|
"\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "suspended-sauce",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"colors = [\"red\", \"yellow\", \"green\"]\n",
|
|
"print(colors.append(\"blue\"))\n",
|
|
"# returns None"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "referenced-occasions",
|
|
"metadata": {},
|
|
"source": [
|
|
"\n",
|
|
"This will work - do this!\n",
|
|
"\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "working-server",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"colors = [\"red\", \"yellow\", \"green\"]\n",
|
|
"colors.append(\"blue\")\n",
|
|
"print(colors)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "opponent-continent",
|
|
"metadata": {},
|
|
"source": [
|
|
"\n",
|
|
"---\n",
|
|
"\n",
|
|
"## Quick Review: Basic List Operations\n",
|
|
"\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "handed-commissioner",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# List Creation\n",
|
|
"my_list = [\"red\", 7, \"yellow\", 1]\n",
|
|
"\n",
|
|
"# List Length\n",
|
|
"list_length = len(my_list) # 4\n",
|
|
"\n",
|
|
"# List Index\n",
|
|
"print(my_list[0]) # red\n",
|
|
"\n",
|
|
"# List Append\n",
|
|
"my_list.append(\"Yi\") # [\"red\", 7, \"yellow\", 1, \"Yi\"]\n",
|
|
"\n",
|
|
"# List Insert at Index\n",
|
|
"my_list.insert(1, \"Sanju\") # [\"red\", \"Sanju\", 7, \"yellow\", 1, \"Yi\"]\n",
|
|
"\n",
|
|
"# List Delete\n",
|
|
"student_that_left = my_list.pop() # \"Yi\"; [\"red\", \"Sanju\", 7, \"yellow\", 1]\n",
|
|
"\n",
|
|
"# List Delete at Index\n",
|
|
"student_that_left = my_list.pop(2) # 7; [\"red\", \"Sanju\", \"yellow\", 1]\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "abstract-adaptation",
|
|
"metadata": {},
|
|
"source": [
|
|
"\n",
|
|
"---\n",
|
|
"\n",
|
|
"\n",
|
|
"## Numerical List Operations - Sum\n",
|
|
"\n",
|
|
"Some actions can only be performed on lists with numbers.\n",
|
|
"\n",
|
|
"`sum()`:\n",
|
|
"\n",
|
|
"- A built in `list` operation.\n",
|
|
"- Adds the list together.\n",
|
|
"- Only works on lists with numbers!\n",
|
|
"\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "domestic-huntington",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"team_batting_avgs = [.328, .299, .208, .301, .275, .226, .253, .232, .287]\n",
|
|
"sum_avgs = sum(team_batting_avgs)\n",
|
|
"print(\"The total of all the batting averages is\", sum_avgs)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "intermediate-interstate",
|
|
"metadata": {},
|
|
"source": [
|
|
"\n",
|
|
"---\n",
|
|
"\n",
|
|
"## List Operations - Max/Min\n",
|
|
"\n",
|
|
"\n",
|
|
"`max()` or `min()`:\n",
|
|
"\n",
|
|
"- Built in `list` operations.\n",
|
|
"- Finds highest, or lowest, in the list.\n",
|
|
"\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "reflected-arthur",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# max(your_numeric_list)\n",
|
|
"# min(your_numeric_list)\n",
|
|
"\n",
|
|
"print(\"The highest batting average is\", max(team_batting_avgs))\n",
|
|
"print(\"The lowest batting average is\", min(team_batting_avgs))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "insured-treat",
|
|
"metadata": {},
|
|
"source": [
|
|
"---\n",
|
|
"\n",
|
|
"## You Do: Lists\n",
|
|
"\n",
|
|
"Complete the following prompts:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "pretty-energy",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# 1. Save a list with the numbers `2`, `4`, `6`, and `8` into a variable called `numbers`.\n",
|
|
"\n",
|
|
"# 2. Print the max of `numbers`.\n",
|
|
"\n",
|
|
"# 3. Pop the last element in `numbers` off; re-insert it at index `2`.\n",
|
|
"\n",
|
|
"# 4. Pop the second number in `numbers` off.\n",
|
|
"\n",
|
|
"# 5. Append `3` to `numbers`.\n",
|
|
"\n",
|
|
"# 6. Print out the average number (divide the sum of `numbers` by the length).\n",
|
|
"\n",
|
|
"# 7. Print `numbers`.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "relative-battlefield",
|
|
"metadata": {},
|
|
"source": [
|
|
"\n",
|
|
"---\n",
|
|
"\n",
|
|
"## Summary and Q&A\n",
|
|
"\n",
|
|
"We accomplished quite a bit!"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "radio-flashing",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# List Creation\n",
|
|
"my_list = [\"red\", 7, \"yellow\", 1]\n",
|
|
"# List Length\n",
|
|
"list_length = len(my_list) # 4\n",
|
|
"# List Index\n",
|
|
"print(my_list[0]) # red\n",
|
|
"# List Append\n",
|
|
"my_list.append(\"Yi\") # [\"red\", 7, \"yellow\", 1, \"Yi\"]\n",
|
|
"# List Insert at Index\n",
|
|
"my_list.insert(1, \"Sanju\") # [\"red\", \"Sanju\", 7, \"yellow\", 1, \"Yi\"]\n",
|
|
"# List Delete\n",
|
|
"student_that_left = my_list.pop() # \"Yi\"; [\"red\", \"Sanju\", 7, \"yellow\", 1]\n",
|
|
"# List Delete at Index\n",
|
|
"student_that_left = my_list.pop(2) # 7; [\"red\", \"Sanju\", \"yellow\", 1]"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "confident-instrumentation",
|
|
"metadata": {},
|
|
"source": [
|
|
"\n",
|
|
"---\n",
|
|
"\n",
|
|
"## Summary and Q&A\n",
|
|
"\n",
|
|
"And for numerical lists only...\n",
|
|
"\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "forced-mercury",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Sum all numbers in list\n",
|
|
"print(sum(team_batting_avgs))\n",
|
|
"# Find minimum value of list\n",
|
|
"print(min(team_batting_avgs))\n",
|
|
"# Find maximum value of list\n",
|
|
"print(max(team_batting_avgs))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "surprising-station",
|
|
"metadata": {},
|
|
"source": [
|
|
"\n",
|
|
"---\n",
|
|
"\n",
|
|
"## Additional Resources\n",
|
|
"\n",
|
|
"- [Python Lists - Khan Academy Video](https://www.youtube.com/watch?v=zEyEC34MY1A)\n",
|
|
"- [Google For Education: Python Lists](https://developers.google.com/edu/python/lists)\n",
|
|
"- [Python-Lists](https://www.tutorialspoint.com/python/python_lists.htm)\n"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "Python 3",
|
|
"language": "python",
|
|
"name": "python3"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 3
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython3",
|
|
"version": "3.7.6"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|