{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "provenance": [] }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" } }, "cells": [ { "cell_type": "markdown", "metadata": { "id": "ScmVn_3NKDsY" }, "source": [ "#**Workshop 3: Lists**\n" ] }, { "cell_type": "markdown", "metadata": { "id": "qB4dgqEuKkcm" }, "source": [ "##**Objectives**\n", "At the end of this workshop, you will be able to:\n", "* Explain why programs need collections of values.\n", "* Write programs that create flat lists, index them, slice them, and modify them through assignment and method calls." ] }, { "cell_type": "markdown", "metadata": { "id": "rvHDmc-DKew9" }, "source": [ "##**Why should I bother using a list at all?**\n", "\n", "You could save every item in your list individually as a variable, so why bother using a list at all? Doing calculations with a hundred variables called pressure_001, pressure_002, etc., would be at least as slow as doing them by hand. It would also make your code very long and harder to debug. By using a list, you are able to store all of your values in one place, call upon or search for them when you need to and perform the same action over each item. We’ll get into repetition structures more in bite 4.\n" ] }, { "cell_type": "markdown", "metadata": { "id": "xhclgv7ZKwWv" }, "source": [ "##**Lists 101**\n", "\n", "A list is an object that contains multiple data items. Lists are mutable, which means their contents can be changed during a program’s execution.\n", "\n", "* Objects stored in a list are called elements\n", "* Brackets denote a list, python will allow you to create an empty list []\n", "* Commas separate list elements\n", "* Quotes tell python that something is a strong and not a number\n", "* Lists can contain different data types" ] }, { "cell_type": "code", "metadata": { "id": "tt2jg1SYJ0_9" }, "source": [ "even_numbers = [2,4,6,8,10]\n", "BennetGirls = ['Jane', 'Elizabeth', 'Mary', 'Lydia', 'Kitty']\n", "types_example = ['Paige', 3.12, 80]" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "mHELTG35L1I8" }, "source": [ "##**Use len to find the length of a list**\n" ] }, { "cell_type": "code", "metadata": { "id": "rbAm4yyJLujn" }, "source": [ "print(len(BennetGirls))" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "mDOyn6gkMAf8" }, "source": [ "##**Use an item’s index to fetch it from a list**" ] }, { "cell_type": "code", "metadata": { "id": "a2FtFTp6MFsT" }, "source": [ "pressures = [0.273, 0.275, 0.277, 0.275, 0.276]\n", "print('zeroth item of pressures:', pressures[0])\n", "print('fourth item of pressures:', pressures[4])" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "EY3jN_TQMJmL" }, "source": [ "##**You can replace values in a list by reassigning them**" ] }, { "cell_type": "code", "metadata": { "id": "G8qzJiUbMO6p" }, "source": [ "pressures[0] = 0.265\n", "print('pressures is now:', pressures)" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "zNvRPIstMUNT" }, "source": [ "##**Character strings can also be indexed, but cannot be changed**" ] }, { "cell_type": "code", "metadata": { "id": "pw4IeJryMfGX" }, "source": [ "element = 'carbon'\n", "print('zeroth character:', element[0])\n", "print('third character:', element[3])" ], "execution_count": null, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "_vvegi_kMgc5" }, "source": [ "element[0] = 'C'" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "V1_pXu11Mjcl" }, "source": [ "##**Appending items to a list adds them to the end**" ] }, { "cell_type": "code", "metadata": { "id": "-8qBFYD2MxMW" }, "source": [ "primes = [2, 3, 5]\n", "print('primes is initially:', primes)" ], "execution_count": null, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "MT-_uj1CM2ud" }, "source": [ "primes.append(7)\n", "print('primes has become:', primes)" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "1MwoQQkEM4-M" }, "source": [ "##**Appending a list to another list creates a list within a list**" ] }, { "cell_type": "code", "metadata": { "id": "qDcDBHOrM-EE" }, "source": [ "primes.append(even_numbers)\n", "print(primes)" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "32Q1HA0zNBYt" }, "source": [ "##**Use extend to combine two lists into one**" ] }, { "cell_type": "code", "metadata": { "id": "xJ7DPiiyNHML" }, "source": [ "primes.extend(even_numbers)\n", "print(primes)" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "nv5YZYCBNGMe" }, "source": [ "##**Use del listName(index value) to remove something from a list**" ] }, { "cell_type": "code", "metadata": { "id": "rGaZhGRXNO2M" }, "source": [ "del primes[4]\n", "print(primes)" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "eXSVlYFUNYCZ" }, "source": [ "##**Use + to join two lists together into a third list**" ] }, { "cell_type": "code", "metadata": { "id": "Ir1uoBSnNhAk" }, "source": [ "odd_numbers = [1,3,5,7]\n", "all_numbers = odd_numbers + even_numbers\n", "print(all_numbers)" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "gRQI2peENlbm" }, "source": [ "##**Create a mini list by slicing it**\n", "* Taking a slice of a list allows you to select a range\n", "* **:** denotes a range, not specifying the start or finish implies the beginning/end\n", "* Python will stop the slice at the value before the index value you gave as your end.\n" ] }, { "cell_type": "code", "metadata": { "id": "xVE-3gSQNucj" }, "source": [ "days = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday']\n", "mid_days = days[2:5]\n", "print(mid_days)" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "QXRQRjj8OJhd" }, "source": [ "##**Search for items in a list using the in operator**\n", "Use an if statement to receive comfirmation that something is in your list (see bite 2 for a refresher)" ] }, { "cell_type": "code", "metadata": { "id": "oop7hx73OPEG" }, "source": [ "prod_nums = ['V475', 'F987', 'Q143', 'R688']\n", "search = input('Enter a product number: ')\n", "if search in prod_nums:\n", " print(search, 'was found in the list.')\n", "else:\n", " print(search, 'was not found in the list.')" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "eptQ3vhMO0Ms" }, "source": [ "##**To make a copy of a list, copy the list’s elements**" ] }, { "cell_type": "code", "metadata": { "id": "JiCe_RvFO5Uo" }, "source": [ "list1 = [1,2,3,4]\n", "list2 = list1\n", "\n", "print(list1)\n", "print(list2)" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "dZt7w6QkPOZM" }, "source": [ "Reassigning a value in list 1 also updates list 2\n" ] }, { "cell_type": "code", "metadata": { "id": "rXnglPR3PApa" }, "source": [ "list1[0] = 99\n", "print(list1)\n", "print(list2)" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "twmhp6puPGII" }, "source": [ "List two is also list 1, so appending to list 2 also appends to list 1" ] }, { "cell_type": "code", "metadata": { "id": "KT-ihTQCPG90" }, "source": [ "list2.append(102)\n", "print(list1)\n", "print(list2)\n" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "l0cqR0mSPSJ-" }, "source": [ "##**To create a separate copy of a list, you must start with empty brackets**\n" ] }, { "cell_type": "code", "metadata": { "id": "-Jnr4y8ZPX4j" }, "source": [ "list2 = [] + list1\n", "print(list1)\n", "print(list2)" ], "execution_count": null, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "cY0-5FU7PbJZ" }, "source": [ "list2.append(47)\n", "print(list1)\n", "print(list2)" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "66qLvVhMPf7c" }, "source": [ "##**A two dimensional list is a list that has other lists as its elements**" ] }, { "cell_type": "code", "metadata": { "id": "ovrRVALxPiS3" }, "source": [ "Characters = [['Harry', \"Ginny\"],['Ron', 'Hermione'], ['Darcy', 'Elizabeth'], ['Jane', 'Bingley']]\n", "print(characters)" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "98AaH8oEP2km" }, "source": [ "Use indexes to call upon specific items in the list." ] }, { "cell_type": "code", "metadata": { "id": "CLGNVWQ8P2P9" }, "source": [ "print(characters[0])\n", "print(characters[1:2])\n", "print(characters[1][1])" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "CkrBQBYKqh1T" }, "source": [ "##**Try it yourself**\n", "\n", "There are many other commands that can be found in the python documentation and utilized when manipulating lists. Let’s take a look at how the sort command works. Run the two programs and compare the final output. What is the difference between:\n", "\n", "program A: sorted(letters)\n", "\n", "program B: letters.sort?" ] }, { "cell_type": "code", "metadata": { "id": "YP-EoEaPrJ_O" }, "source": [ "# Program A\n", "letters = list('gold')\n", "result = sorted(letters)\n", "print('letters is', letters, 'and result is', result)" ], "execution_count": null, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "E6bGvoc-rMk3" }, "source": [ "# Program B\n", "letters = list('gold')\n", "result = letters.sort()\n", "print('letters is', letters, 'and result is', result)" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "J1kq3MgCQHik" }, "source": [ "##**Looking for more practice?**\n", "\n", "W3Schools: https://www.w3schools.com/python/python_lists.asp\n", "\n", "Software Carpentry: http://swcarpentry.github.io/python-novice-gapminder/11-lists/index.html\n" ] }, { "cell_type": "markdown", "metadata": { "id": "v0dnIR8KQO0o" }, "source": [ "##**References**\n", "\n", "1. Gaddis T. Starting out with Python. Third edition. Pearson; 2014.\n", "\n", "2. Lists – plotting and programming in python. Accessed April 28, 2021. http://swcarpentry.github.io/python-novice-gapminder/11-lists/index.html" ] } ] }