{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Module 2\n", "\n", "## Video 6: Lists and List Operations\n", "**Python for the Energy Industry**\n", "\n", "## Lists\n", "\n", "We often want to keep track of many bits of data at once. One way of doing this in python is with a 'list'. Lists are defined by enclosing multiple comma-separated values inside square brackets." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# list of strings\n", "vessels = ['ABIOLA', 'ACACIA']\n", "\n", "# list of floats\n", "prices = [1.23, 1.98, 1.45, 1.67]\n", "\n", "# lists of integers\n", "quantities1 = [18, 22, 21, 32]\n", "quantities2 = [12, 11, 16, 18]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Looping Over Lists" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1.23\n", "1.98\n", "1.45\n", "1.67\n" ] } ], "source": [ "# loop over prices\n", "for price in prices:\n", " print(price)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### List Indexing\n", "\n", "We can access individual items in a list in the following way:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The 1st price: 1.23\n", "The 2nd price: 1.98\n", "The 3rd price: 1.45\n" ] } ], "source": [ "prices = [1.23, 1.98, 1.45, 1.67, 2.40, 1.89]\n", "\n", "print('The 1st price: ', prices[0] )\n", "print('The 2nd price: ', prices[1] )\n", "print('The 3rd price: ', prices[2] )" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The number inside the square brackets is the 'index' of the item. Note that Python uses 'zero indexing', so the first item in a list has an index of 0.\n", "\n", "You can also access items 'from the end' using a negative index. So an index of -1 gives the last item, -2 gives the second last item, and so on." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The last price: 1.89\n", "The 2nd last price: 2.4\n" ] } ], "source": [ "print('The last price: ', prices[-1] )\n", "print('The 2nd last price: ', prices[-2] )" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The index can also be used to overwrite an item in the list:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1.23, 1.98, 1.45, 1.67, 2.4, 1.89]\n", "[2.41, 1.98, 1.45, 1.67, 2.4, 0.99]\n" ] } ], "source": [ "print(prices)\n", "prices[0] = 2.41\n", "prices[-1] = 0.99\n", "print(prices)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can also get a range of items from a list:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1.98, 1.45, 1.67]\n" ] } ], "source": [ "print(prices[1:4] )" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is called a 'slice'. Which indices from the prices list are included in this slice? " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## List Operations\n", "\n", "Lists have some built in functionality that makes them easier to work with.\n", "\n", "### Adding to / Removing from Lists" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['USA', 'UK', 'France', 'Germany', 'Spain']\n" ] } ], "source": [ "countries = ['USA', 'UK', 'France', 'Germany']\n", "\n", "# adding on to the end of a list\n", "countries.append('Spain')\n", "print(countries)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['USA', 'UK', 'Australia', 'France', 'Germany', 'Spain']\n" ] } ], "source": [ "# inserting into a specific index of a list\n", "countries.insert(2, 'Australia')\n", "print(countries)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['USA', 'UK', 'Australia', 'Germany', 'Spain']\n" ] } ], "source": [ "# removing from a specific index\n", "countries.pop(3)\n", "print(countries)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['USA', 'UK', 'Australia', 'Spain']\n" ] } ], "source": [ "# removing a particular value from a list\n", "countries.remove('Germany')\n", "print(countries)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Examining Lists" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n", "False\n" ] } ], "source": [ "primes = [2,3,5,7,11,13]\n", "\n", "# check if a value is in a list\n", "print(3 in primes)\n", "print(4 in primes)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2\n" ] } ], "source": [ "# find the index of a value in a list\n", "print(primes.index(5))" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "minimum: 2\n", "maximum: 13\n", "sum: 41\n" ] } ], "source": [ "# find the minimum, maximum, and sum\n", "print('minimum:', min(primes))\n", "print('maximum:', max(primes))\n", "print('sum:', sum(primes))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Other List Operations\n", "\n", "The addition and multiplication operators also work on lists:" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[18, 22, 21, 32, 12, 11, 16, 18]\n", "[18, 22, 21, 32, 18, 22, 21, 32]\n" ] } ], "source": [ "print(quantities1 + quantities2)\n", "print(quantities1 * 2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise\n", "\n", "Python allows you to have lists containing multiple different types of variable, as in the below example. Try to transform this list into: [1,2,3] using the operations you've learned." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.9" } }, "nbformat": 4, "nbformat_minor": 4 }