{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Module 2\n", "\n", "## Video 8: Tuples and Dictionaries\n", "**Python for the Energy Industry**\n", "\n", "This lesson covers a couple of other ways of storing data in Python that you will come across: tuples and dictionaries.\n", "\n", "## Tuples\n", "\n", "Tuples function very similarly to lists, with one key difference: once a tuple is created, it cannot be modified. Tuples are created using round, rather than square brackets." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(3, 5, 7, 10)\n" ] } ], "source": [ "counts = (3,5,7,10)\n", "print(counts)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "First item: 3\n", "Second item: 5\n" ] } ], "source": [ "# Indexing works the same for tuples as lists\n", "print('First item:', counts[0])\n", "print('Second item:', counts[1])" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# Trying to modify the tuple gives an error\n", "\n", "# uncomment the following line:\n", "# counts[0] = 4" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Dictionaries\n", "\n", "Dictionaries store pairs of variables: the 'key' and the 'value'. The key can be used instead of an index to access the value. Here's an example dictionary for storing the cubic capacity of different vessels:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "124518\n" ] } ], "source": [ "capacities = {'ABIOLA': 47261, 'ABLIANI': 124518, 'ACACIA': 14570}\n", "\n", "print(capacities['ABLIANI'])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It is straightforward to add new items to a dictionary:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'ABIOLA': 47261, 'ABLIANI': 124518, 'ACACIA': 14570, 'A STAR': 333924}\n" ] } ], "source": [ "capacities['A STAR'] = 333924\n", "\n", "print(capacities)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Using a loop to iterate over a dictionary will iterate over the keys:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ABIOLA capacity = 47261\n", "ABLIANI capacity = 124518\n", "ACACIA capacity = 14570\n", "A STAR capacity = 333924\n" ] } ], "source": [ "for vessel in capacities:\n", " print(vessel, 'capacity =', capacities[vessel])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The keys and values can also be accessed separately from a dictionary:" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "keys - ['ABIOLA', 'ABLIANI', 'ACACIA', 'A STAR']\n", "values - [47261, 124518, 14570, 333924]\n" ] } ], "source": [ "print('keys -', list(capacities.keys()))\n", "\n", "print('values -', list(capacities.values()))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*Note: what's up with the pairs of brackets in keys() and values()? These are functions that act on the dictionary like append, pop etc. but which take no arguments. We will explore functions more in the next lesson.*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise\n", "\n", "Use a `for` loop to find which vessel in `capacities` has the largest capacity." ] }, { "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 }