{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "*This notebook contains material for CBE 20258 Numerical and Statistical Analysis taught at the University of Notre Dame. (c) Professors Alexander Dowling, Ryan McClarren, and Yamil Colón. This collection of notebooks [cbe-xx258](https://ndcbe.github.io/cbe-xx258) is available [on Github](https://github.com/ndcbe/cbe-xx258).*\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "< [1.4 Functions, Scoping, and Other Fun Stuff](https://ndcbe.github.io/cbe-xx258/01.04-Functions-scoping.html) | [Contents](toc.html) | [1.6 Linear Algebra with Numpy and Scipy](https://ndcbe.github.io/cbe-xx258/01.06-NumPy.html) >

\"Open

\"Download\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# IMPORT DATA FILES USED BY THIS NOTEBOOK\n", "import os, requests\n", "\n", "file_links = [(\"data/ChemicalSymbols.csv\", \"https://ndcbe.github.io/cbe-xx258/data/ChemicalSymbols.csv\")]\n", "\n", "# This cell has been added by nbpages. Run this cell to download data files required for this notebook.\n", "\n", "for filepath, fileurl in file_links:\n", " stem, filename = os.path.split(filepath)\n", " if stem:\n", " if not os.path.exists(stem):\n", " os.mkdir(stem)\n", " if not os.path.isfile(filepath):\n", " with open(filepath, 'wb') as f:\n", " response = requests.get(fileurl)\n", " f.write(response.content)\n" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "rG3kq9V5BPoN", "nbpages": { "level": 1, "link": "[1.5 List, Dictionaries, and Enumeration](https://ndcbe.github.io/cbe-xx258/01.05-Lists-Dictionaries-Enumerate.html#1.5-List,-Dictionaries,-and-Enumeration)", "section": "1.5 List, Dictionaries, and Enumeration" } }, "source": [ "# 1.5 List, Dictionaries, and Enumeration" ] }, { "cell_type": "markdown", "metadata": { "nbpages": { "level": 1, "link": "[1.5 List, Dictionaries, and Enumeration](https://ndcbe.github.io/cbe-xx258/01.05-Lists-Dictionaries-Enumerate.html#1.5-List,-Dictionaries,-and-Enumeration)", "section": "1.5 List, Dictionaries, and Enumeration" } }, "source": [ "**Reference**: Chapters 2 and 5 of *Computational Nuclear Engineering and Radiological Science Using Python*, R. McClarren (2018) " ] }, { "cell_type": "markdown", "metadata": { "nbpages": { "level": 2, "link": "[1.5.1 Learning Objectives](https://ndcbe.github.io/cbe-xx258/01.05-Lists-Dictionaries-Enumerate.html#1.5.1-Learning-Objectives)", "section": "1.5.1 Learning Objectives" } }, "source": [ "## 1.5.1 Learning Objectives\n", "\n", "After studying this notebook, completing the activities, and asking questions in class, you should be able to:\n" ] }, { "cell_type": "markdown", "metadata": { "nbpages": { "level": 2, "link": "[1.5.2 Lists](https://ndcbe.github.io/cbe-xx258/01.05-Lists-Dictionaries-Enumerate.html#1.5.2-Lists)", "section": "1.5.2 Lists" } }, "source": [ "## 1.5.2 Lists" ] }, { "cell_type": "markdown", "metadata": { "nbpages": { "level": 2, "link": "[1.5.2 Lists](https://ndcbe.github.io/cbe-xx258/01.05-Lists-Dictionaries-Enumerate.html#1.5.2-Lists)", "section": "1.5.2 Lists" } }, "source": [ "You can also do other fun stuff with for loops. For instance, you can have the control variable take on non-numeric things:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "nbpages": { "level": 2, "link": "[1.5.2 Lists](https://ndcbe.github.io/cbe-xx258/01.05-Lists-Dictionaries-Enumerate.html#1.5.2-Lists)", "section": "1.5.2 Lists" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "It is Monday and I will wear a fedora\n", "It is Tuesday and I will wear a trilby\n", "It is Wednesday and I will wear a porkpie\n", "It is Thursday and I will wear a tam o'shanter\n", "It is Friday and I will wear a Phrygian cap\n", "It is Saturday and I will wear a Beefeaters' hat\n", "It is Sunday and I will wear a sombrero\n" ] } ], "source": [ "#silly hat code\n", "hats = [\"fedora\",\"trilby\",\"porkpie\",\"tam o'shanter\",\n", " \"Phrygian cap\",\"Beefeaters' hat\",\"sombrero\"]\n", "days = [\"Monday\",\"Tuesday\",\"Wednesday\",\"Thursday\",\n", " \"Friday\",\"Saturday\",\"Sunday\"]\n", "count = 0\n", "for today in hats:\n", " print(\"It is\",days[count],\"and I will wear a\",today)\n", " count += 1" ] }, { "cell_type": "markdown", "metadata": { "nbpages": { "level": 2, "link": "[1.5.2 Lists](https://ndcbe.github.io/cbe-xx258/01.05-Lists-Dictionaries-Enumerate.html#1.5.2-Lists)", "section": "1.5.2 Lists" } }, "source": [ "Notice what else we did here: we defined a list called `days` that contained strings for the names of the days of the week. Inside our `for` loop we had a numeric variable that kept track of what number the day of the week was (0 for Monday in this case). Then when we access days[count] it returns the string in position count.\n", "\n", "We can go one step beyond and plan our wardrobe a month in advance using random numbers. The code below generates a random integer between 0 and 6:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "nbpages": { "level": 2, "link": "[1.5.2 Lists](https://ndcbe.github.io/cbe-xx258/01.05-Lists-Dictionaries-Enumerate.html#1.5.2-Lists)", "section": "1.5.2 Lists" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n" ] } ], "source": [ "import random\n", "my_random_number = round(random.uniform(0,6))\n", "print(my_random_number)" ] }, { "cell_type": "markdown", "metadata": { "nbpages": { "level": 2, "link": "[1.5.2 Lists](https://ndcbe.github.io/cbe-xx258/01.05-Lists-Dictionaries-Enumerate.html#1.5.2-Lists)", "section": "1.5.2 Lists" } }, "source": [ "

\n", "Home Activity: Modify the code below to loop over 30 days. For each day, randomly select which hat to wear.\n", "
" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "nbpages": { "level": 2, "link": "[1.5.2 Lists](https://ndcbe.github.io/cbe-xx258/01.05-Lists-Dictionaries-Enumerate.html#1.5.2-Lists)", "section": "1.5.2 Lists" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "It is Monday and I will wear a tam o'shanter\n", "It is Tuesday and I will wear a Phrygian cap\n", "It is Wednesday and I will wear a Beefeaters' hat\n", "It is Thursday and I will wear a Beefeaters' hat\n", "It is Friday and I will wear a Phrygian cap\n", "It is Saturday and I will wear a porkpie\n", "It is Sunday and I will wear a trilby\n", "It is Monday and I will wear a trilby\n", "It is Tuesday and I will wear a fedora\n", "It is Wednesday and I will wear a sombrero\n", "It is Thursday and I will wear a tam o'shanter\n", "It is Friday and I will wear a sombrero\n", "It is Saturday and I will wear a trilby\n", "It is Sunday and I will wear a porkpie\n", "It is Monday and I will wear a Phrygian cap\n", "It is Tuesday and I will wear a tam o'shanter\n", "It is Wednesday and I will wear a Phrygian cap\n", "It is Thursday and I will wear a Beefeaters' hat\n", "It is Friday and I will wear a Phrygian cap\n", "It is Saturday and I will wear a trilby\n", "It is Sunday and I will wear a Phrygian cap\n", "It is Monday and I will wear a Beefeaters' hat\n", "It is Tuesday and I will wear a trilby\n", "It is Wednesday and I will wear a Beefeaters' hat\n", "It is Thursday and I will wear a Phrygian cap\n", "It is Friday and I will wear a Phrygian cap\n", "It is Saturday and I will wear a Beefeaters' hat\n", "It is Sunday and I will wear a tam o'shanter\n", "It is Monday and I will wear a Beefeaters' hat\n", "It is Tuesday and I will wear a trilby\n" ] } ], "source": [ "#silly hat code\n", "import random\n", "hats = [\"fedora\",\"trilby\",\"porkpie\",\"tam o'shanter\",\n", " \"Phrygian cap\",\"Beefeaters' hat\",\"sombrero\"]\n", "days = [\"Monday\",\"Tuesday\",\"Wednesday\",\"Thursday\",\n", " \"Friday\",\"Saturday\",\"Sunday\"]\n", "\n", "# loop over 30 days\n", "# YOUR SOLUTION HERE" ] }, { "cell_type": "markdown", "metadata": { "nbpages": { "level": 2, "link": "[1.5.2 Lists](https://ndcbe.github.io/cbe-xx258/01.05-Lists-Dictionaries-Enumerate.html#1.5.2-Lists)", "section": "1.5.2 Lists" } }, "source": [ "**Lists** are like vectors in MATLAB, except, as you can see above, we can store more than just numbers in them. We will talk about using a package `numpy` to represent vectors and matrices next class session." ] }, { "cell_type": "markdown", "metadata": { "nbpages": { "level": 2, "link": "[1.5.2 Lists](https://ndcbe.github.io/cbe-xx258/01.05-Lists-Dictionaries-Enumerate.html#1.5.2-Lists)", "section": "1.5.2 Lists" } }, "source": [ "
\n", "Home Activity: Fix the three examples below to count from 1 to 10.\n", "
" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "nbpages": { "level": 2, "link": "[1.5.2 Lists](https://ndcbe.github.io/cbe-xx258/01.05-Lists-Dictionaries-Enumerate.html#1.5.2-Lists)", "section": "1.5.2 Lists" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "3\n", "4\n", "5\n", "6\n", "7\n", "8\n" ] } ], "source": [ "## Attempt 1\n", "N = [1, 2, 3, 4, 5, 6, 7, 11]\n", "for i in range(len(N)):\n", " print(i+1)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "nbpages": { "level": 2, "link": "[1.5.2 Lists](https://ndcbe.github.io/cbe-xx258/01.05-Lists-Dictionaries-Enumerate.html#1.5.2-Lists)", "section": "1.5.2 Lists" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "3\n", "4\n", "5\n", "6\n", "7\n", "8\n", "9\n", "10\n" ] } ], "source": [ "# YOUR SOLUTION HERE" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "nbpages": { "level": 2, "link": "[1.5.2 Lists](https://ndcbe.github.io/cbe-xx258/01.05-Lists-Dictionaries-Enumerate.html#1.5.2-Lists)", "section": "1.5.2 Lists" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "3\n", "4\n", "5\n", "6\n", "7\n", "11\n" ] } ], "source": [ "## Attempt 2\n", "N = [1, 2, 3, 4, 5, 6, 7, 11]\n", "for i in range(len(N)):\n", " print(N[i])" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "nbpages": { "level": 2, "link": "[1.5.2 Lists](https://ndcbe.github.io/cbe-xx258/01.05-Lists-Dictionaries-Enumerate.html#1.5.2-Lists)", "section": "1.5.2 Lists" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "3\n", "4\n", "5\n", "6\n", "7\n", "8\n", "9\n", "10\n" ] } ], "source": [ "# YOUR SOLUTION HERE" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "nbpages": { "level": 2, "link": "[1.5.2 Lists](https://ndcbe.github.io/cbe-xx258/01.05-Lists-Dictionaries-Enumerate.html#1.5.2-Lists)", "section": "1.5.2 Lists" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n", "5\n", "6\n", "7\n", "8\n", "9\n" ] } ], "source": [ "## Attempt 3\n", "for i in range(0,10):\n", " print(i)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "nbpages": { "level": 2, "link": "[1.5.2 Lists](https://ndcbe.github.io/cbe-xx258/01.05-Lists-Dictionaries-Enumerate.html#1.5.2-Lists)", "section": "1.5.2 Lists" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "3\n", "4\n", "5\n", "6\n", "7\n", "8\n", "9\n", "10\n" ] } ], "source": [ "# YOUR SOLUTION HERE" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "DDov8J6SBPoQ", "nbpages": { "level": 2, "link": "[1.5.3 Dictionaries](https://ndcbe.github.io/cbe-xx258/01.05-Lists-Dictionaries-Enumerate.html#1.5.3-Dictionaries)", "section": "1.5.3 Dictionaries" } }, "source": [ "## 1.5.3 Dictionaries" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "i1H8kfkjBPoQ", "nbpages": { "level": 2, "link": "[1.5.3 Dictionaries](https://ndcbe.github.io/cbe-xx258/01.05-Lists-Dictionaries-Enumerate.html#1.5.3-Dictionaries)", "section": "1.5.3 Dictionaries" } }, "source": [ "So far, we have used lists to store a sequence of numbers or more generally Python objects. Lists (and NumPy arrays - stay tuned) access elements by position and have an inherent ordering.\n", "\n", "A dictionary is a set of **key** : **value** pairs. You can think of a dictionary as a fancy list that instead of accessing via the position, you access using a key." ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 68 }, "colab_type": "code", "executionInfo": { "elapsed": 687, "status": "ok", "timestamp": 1548774349667, "user": { "displayName": "Alexander Dowling", "photoUrl": "https://lh3.googleusercontent.com/-LChdQ2m5OQE/AAAAAAAAAAI/AAAAAAAAAA0/JeXJe4vQJ7M/s64/photo.jpg", "userId": "00988067626794866502" }, "user_tz": 300 }, "id": "FETcpggzBPoS", "nbpages": { "level": 2, "link": "[1.5.3 Dictionaries](https://ndcbe.github.io/cbe-xx258/01.05-Lists-Dictionaries-Enumerate.html#1.5.3-Dictionaries)", "section": "1.5.3 Dictionaries" }, "outputId": "3166ab17-8199-4bbf-bd07-fc1e57b607e1" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Key M gives Monday\n", "Key R gives Thursday\n" ] } ], "source": [ "#simple dictionary\n", "days_of_week = {\"M\":\"Monday\", \"T\":\"Tuesday\",\n", " \"W\":\"Wednesday\", \"R\":\"Thursday\",\n", " \"F\":\"Friday\", \"S\":\"Saturday\",\n", " \"U\":\"Sunday\"}\n", "\n", "print(\"Key M gives\", days_of_week[\"M\"])\n", "print(\"Key R gives\", days_of_week[\"R\"])" ] }, { "cell_type": "markdown", "metadata": { "nbpages": { "level": 2, "link": "[1.5.3 Dictionaries](https://ndcbe.github.io/cbe-xx258/01.05-Lists-Dictionaries-Enumerate.html#1.5.3-Dictionaries)", "section": "1.5.3 Dictionaries" } }, "source": [ "
\n", "Home Activity: Store the value for key F in the variable answer.\n", "
" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "nbpages": { "level": 2, "link": "[1.5.3 Dictionaries](https://ndcbe.github.io/cbe-xx258/01.05-Lists-Dictionaries-Enumerate.html#1.5.3-Dictionaries)", "section": "1.5.3 Dictionaries" } }, "outputs": [], "source": [ "# YOUR SOLUTION HERE" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "nbgrader": { "grade": true, "grade_id": "dictionary", "locked": true, "points": "0.3", "solution": false }, "nbpages": { "level": 2, "link": "[1.5.3 Dictionaries](https://ndcbe.github.io/cbe-xx258/01.05-Lists-Dictionaries-Enumerate.html#1.5.3-Dictionaries)", "section": "1.5.3 Dictionaries" } }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "nbpages": { "level": 2, "link": "[1.5.3 Dictionaries](https://ndcbe.github.io/cbe-xx258/01.05-Lists-Dictionaries-Enumerate.html#1.5.3-Dictionaries)", "section": "1.5.3 Dictionaries" } }, "source": [ "Calling `days_of_week.keys()` returns a special list of the keys of the dictionary." ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "nbpages": { "level": 2, "link": "[1.5.3 Dictionaries](https://ndcbe.github.io/cbe-xx258/01.05-Lists-Dictionaries-Enumerate.html#1.5.3-Dictionaries)", "section": "1.5.3 Dictionaries" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "dict_keys(['M', 'T', 'W', 'R', 'F', 'S', 'U'])\n" ] } ], "source": [ "print(days_of_week.keys())" ] }, { "cell_type": "markdown", "metadata": { "nbpages": { "level": 2, "link": "[1.5.3 Dictionaries](https://ndcbe.github.io/cbe-xx258/01.05-Lists-Dictionaries-Enumerate.html#1.5.3-Dictionaries)", "section": "1.5.3 Dictionaries" } }, "source": [ "We can also check if `G` is a valid key for the dictionary." ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "colab_type": "code", "executionInfo": { "elapsed": 213, "status": "ok", "timestamp": 1548774404839, "user": { "displayName": "Alexander Dowling", "photoUrl": "https://lh3.googleusercontent.com/-LChdQ2m5OQE/AAAAAAAAAAI/AAAAAAAAAA0/JeXJe4vQJ7M/s64/photo.jpg", "userId": "00988067626794866502" }, "user_tz": 300 }, "id": "L3XwquXiJSoR", "nbpages": { "level": 2, "link": "[1.5.3 Dictionaries](https://ndcbe.github.io/cbe-xx258/01.05-Lists-Dictionaries-Enumerate.html#1.5.3-Dictionaries)", "section": "1.5.3 Dictionaries" }, "outputId": "a21361f0-ee5f-493e-864a-4dcdb09626d5" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False\n" ] } ], "source": [ "print(\"G\" in days_of_week.keys()) #is G a key in days_of_week?" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "96n_rpOhBPoX", "nbpages": { "level": 2, "link": "[1.5.3 Dictionaries](https://ndcbe.github.io/cbe-xx258/01.05-Lists-Dictionaries-Enumerate.html#1.5.3-Dictionaries)", "section": "1.5.3 Dictionaries" } }, "source": [ "**Main idea:** With dictionaries, we access elements using the **key**. In contrast, we access elements of strings, lists, and Numpy arrays using the position." ] }, { "cell_type": "markdown", "metadata": { "nbpages": { "level": 3, "link": "[1.5.3.1 Example: Shopping List](https://ndcbe.github.io/cbe-xx258/01.05-Lists-Dictionaries-Enumerate.html#1.5.3.1-Example:-Shopping-List)", "section": "1.5.3.1 Example: Shopping List" } }, "source": [ "### 1.5.3.1 Example: Shopping List" ] }, { "cell_type": "markdown", "metadata": { "nbpages": { "level": 3, "link": "[1.5.3.1 Example: Shopping List](https://ndcbe.github.io/cbe-xx258/01.05-Lists-Dictionaries-Enumerate.html#1.5.3.1-Example:-Shopping-List)", "section": "1.5.3.1 Example: Shopping List" } }, "source": [ "
\n", "Home Activity: Create a dictionary to store the following shopping list. The keys should be the item names and the elements should be the quantity of the items.\n", "
" ] }, { "cell_type": "markdown", "metadata": { "nbpages": { "level": 3, "link": "[1.5.3.1 Example: Shopping List](https://ndcbe.github.io/cbe-xx258/01.05-Lists-Dictionaries-Enumerate.html#1.5.3.1-Example:-Shopping-List)", "section": "1.5.3.1 Example: Shopping List" } }, "source": [ "* 1.5 bannas (pounds)\n", "* 3 cans of soup\n", "* 6 apples\n", "* 2.3 pork tenderloin (pounds)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "nbpages": { "level": 3, "link": "[1.5.3.1 Example: Shopping List](https://ndcbe.github.io/cbe-xx258/01.05-Lists-Dictionaries-Enumerate.html#1.5.3.1-Example:-Shopping-List)", "section": "1.5.3.1 Example: Shopping List" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'bananas (pounds)': 1.5, 'cans of soup': 3, 'apples': 6, 'pork tenderloin (pounds)': 2.3}\n" ] } ], "source": [ "# YOUR SOLUTION HERE" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "nbgrader": { "grade": true, "grade_id": "4a-i", "locked": true, "points": "0.4", "solution": false }, "nbpages": { "level": 3, "link": "[1.5.3.1 Example: Shopping List](https://ndcbe.github.io/cbe-xx258/01.05-Lists-Dictionaries-Enumerate.html#1.5.3.1-Example:-Shopping-List)", "section": "1.5.3.1 Example: Shopping List" } }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "JGhJe03MBPoY", "nbpages": { "level": 3, "link": "[1.5.3.2 Another Example: Chemical Symbols and Element Names](https://ndcbe.github.io/cbe-xx258/01.05-Lists-Dictionaries-Enumerate.html#1.5.3.2-Another-Example:-Chemical-Symbols-and-Element-Names)", "section": "1.5.3.2 Another Example: Chemical Symbols and Element Names" } }, "source": [ "### 1.5.3.2 Another Example: Chemical Symbols and Element Names" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "S_bNJ3DhDRlB", "nbpages": { "level": 3, "link": "[1.5.3.2 Another Example: Chemical Symbols and Element Names](https://ndcbe.github.io/cbe-xx258/01.05-Lists-Dictionaries-Enumerate.html#1.5.3.2-Another-Example:-Chemical-Symbols-and-Element-Names)", "section": "1.5.3.2 Another Example: Chemical Symbols and Element Names" } }, "source": [ "Below is code that parses ``ChemicalSymbols.csv`` and stores the data in dictionary." ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 51 }, "colab_type": "code", "executionInfo": { "elapsed": 47193, "status": "ok", "timestamp": 1548774660782, "user": { "displayName": "Alexander Dowling", "photoUrl": "https://lh3.googleusercontent.com/-LChdQ2m5OQE/AAAAAAAAAAI/AAAAAAAAAA0/JeXJe4vQJ7M/s64/photo.jpg", "userId": "00988067626794866502" }, "user_tz": 300 }, "id": "KQ5sxgePBPoZ", "nbpages": { "level": 3, "link": "[1.5.3.2 Another Example: Chemical Symbols and Element Names](https://ndcbe.github.io/cbe-xx258/01.05-Lists-Dictionaries-Enumerate.html#1.5.3.2-Another-Example:-Chemical-Symbols-and-Element-Names)", "section": "1.5.3.2 Another Example: Chemical Symbols and Element Names" }, "outputId": "2d3703c4-c899-45c2-887d-98aea092eea2" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Enter a valid chemical symbol: Ar\n", "Ar is Argon\n" ] } ], "source": [ "import csv\n", "element_dict = {} #create a blank dictionary\n", "\n", "# this block will only execute if the file opens\n", "with open('./data/ChemicalSymbols.csv') as csvfile: \n", " chemreader = csv.reader(csvfile)\n", " for row in chemreader: # have for loop that loops over each line\n", " element_dict[row[0]] = row[1] # add a key:value pair\n", "\n", "# Ask user to enter a chemical symbol \n", "key = input(\"Enter a valid chemical symbol: \")\n", "if key in element_dict:\n", " print(key,\"is\",element_dict[key])\n", "else:\n", " print(\"Not a valid element\")" ] }, { "cell_type": "markdown", "metadata": { "nbpages": { "level": 3, "link": "[1.5.3.2 Another Example: Chemical Symbols and Element Names](https://ndcbe.github.io/cbe-xx258/01.05-Lists-Dictionaries-Enumerate.html#1.5.3.2-Another-Example:-Chemical-Symbols-and-Element-Names)", "section": "1.5.3.2 Another Example: Chemical Symbols and Element Names" } }, "source": [ "
\n", "Home Activity: Run the code above twice. Try first with a valid chemical symbol. Then try with a chemical symbol not on the periodic table of elements.\n", "
" ] }, { "cell_type": "markdown", "metadata": { "nbpages": { "level": 3, "link": "[1.5.3.2 Another Example: Chemical Symbols and Element Names](https://ndcbe.github.io/cbe-xx258/01.05-Lists-Dictionaries-Enumerate.html#1.5.3.2-Another-Example:-Chemical-Symbols-and-Element-Names)", "section": "1.5.3.2 Another Example: Chemical Symbols and Element Names" } }, "source": [ "We can also print out the dictionary." ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 2040 }, "colab_type": "code", "executionInfo": { "elapsed": 216, "status": "ok", "timestamp": 1548774693412, "user": { "displayName": "Alexander Dowling", "photoUrl": "https://lh3.googleusercontent.com/-LChdQ2m5OQE/AAAAAAAAAAI/AAAAAAAAAA0/JeXJe4vQJ7M/s64/photo.jpg", "userId": "00988067626794866502" }, "user_tz": 300 }, "id": "xxlUI8vgKhBW", "nbpages": { "level": 3, "link": "[1.5.3.2 Another Example: Chemical Symbols and Element Names](https://ndcbe.github.io/cbe-xx258/01.05-Lists-Dictionaries-Enumerate.html#1.5.3.2-Another-Example:-Chemical-Symbols-and-Element-Names)", "section": "1.5.3.2 Another Example: Chemical Symbols and Element Names" }, "outputId": "c295ca3d-a1d5-416e-e065-8a68385e130d" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'Ac': 'Actinium', 'Ag': 'Silver', 'Al': 'Aluminium (Aluminum)', 'Am': 'Americium', 'Ar': 'Argon', 'As': 'Arsenic', 'At': 'Astatine', 'Au': 'Gold', 'B': 'Boron', 'Ba': 'Barium', 'Be': 'Beryllium', 'Bh': 'Bohrium', 'Bi': 'Bismuth', 'Bk': 'Berkelium', 'Br': 'Bromine', 'C': 'Carbon', 'Ca': 'Calcium', 'Cd': 'Cadmium', 'Ce': 'Cerium', 'Cf': 'Californium', 'Cl': 'Chlorine', 'Cm': 'Curium', 'Cn': 'Copernicium', 'Co': 'Cobalt', 'Cr': 'Chromium', 'Cs': 'Caesium (Cesium)', 'Cu': 'Copper', 'Db': 'Dubnium', 'Ds': 'Darmstadtium', 'Dy': 'Dysprosium', 'Er': 'Erbium', 'Es': 'Einsteinium', 'Eu': 'Europium', 'F': 'Fluorine', 'Fe': 'Iron', 'Fl': 'Flerovium', 'Fm': 'Fermium', 'Fr': 'Francium', 'Ga': 'Gallium', 'Gd': 'Gadolinium', 'Ge': 'Germanium', 'H': 'Hydrogen', 'He': 'Helium', 'Hf': 'Hafnium', 'Hg': 'Mercury', 'Ho': 'Holmium', 'Hs': 'Hassium', 'I': 'Iodine', 'In': 'Indium', 'Ir': 'Iridium', 'K': 'Potassium', 'Kr': 'Krypton', 'La': 'Lanthanum', 'Li': 'Lithium', 'Lr': 'Lawrencium', 'Lu': 'Lutetium', 'Lv': 'Livermorium', 'Md': 'Mendelevium', 'Mg': 'Magnesium', 'Mn': 'Manganese', 'Mo': 'Molybdenum', 'Mt': 'Meitnerium', 'N': 'Nitrogen', 'Na': 'Sodium', 'Nb': 'Niobium', 'Nd': 'Neodymium', 'Ne': 'Neon', 'Ni': 'Nickel', 'No': 'Nobelium', 'Np': 'Neptunium', 'O': 'Oxygen', 'Os': 'Osmium', 'P': 'Phosphorus', 'Pa': 'Protactinium', 'Pb': 'Lead', 'Pd': 'Palladium', 'Pm': 'Promethium', 'Po': 'Polonium', 'Pr': 'Praseodymium', 'Pt': 'Platinum', 'Pu': 'Plutonium', 'Ra': 'Radium', 'Rb': 'Rubidium', 'Re': 'Rhenium', 'Rf': 'Rutherfordium', 'Rg': 'Roentgenium', 'Rh': 'Rhodium', 'Rn': 'Radon', 'Ru': 'Ruthenium', 'S': 'Sulfur (Sulphur)', 'Sb': 'Antimony', 'Sc': 'Scandium', 'Se': 'Selenium', 'Sg': 'Seaborgium', 'Si': 'Silicon', 'Sm': 'Samarium', 'Sn': 'Tin', 'Sr': 'Strontium', 'Ta': 'Tantalum', 'Tb': 'Terbium', 'Tc': 'Technetium', 'Te': 'Tellurium', 'Th': 'Thorium', 'Ti': 'Titanium', 'Tl': 'Thallium', 'Tm': 'Thulium', 'U': 'Uranium', 'Uuo': 'Ununoctium', 'Uup': 'Ununpentium', 'Uus': 'Ununseptium', 'Uut': 'Ununtrium', 'V': 'Vanadium', 'W': 'Tungsten', 'Xe': 'Xenon', 'Y': 'Yttrium', 'Yb': 'Ytterbium', 'Zn': 'Zinc', 'Zr': 'Zirconium'}\n" ] } ], "source": [ "print(element_dict)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "nbpages": { "level": 3, "link": "[1.5.3.2 Another Example: Chemical Symbols and Element Names](https://ndcbe.github.io/cbe-xx258/01.05-Lists-Dictionaries-Enumerate.html#1.5.3.2-Another-Example:-Chemical-Symbols-and-Element-Names)", "section": "1.5.3.2 Another Example: Chemical Symbols and Element Names" } }, "outputs": [ { "data": { "text/plain": [ "{'Ac': 'Actinium',\n", " 'Ag': 'Silver',\n", " 'Al': 'Aluminium (Aluminum)',\n", " 'Am': 'Americium',\n", " 'Ar': 'Argon',\n", " 'As': 'Arsenic',\n", " 'At': 'Astatine',\n", " 'Au': 'Gold',\n", " 'B': 'Boron',\n", " 'Ba': 'Barium',\n", " 'Be': 'Beryllium',\n", " 'Bh': 'Bohrium',\n", " 'Bi': 'Bismuth',\n", " 'Bk': 'Berkelium',\n", " 'Br': 'Bromine',\n", " 'C': 'Carbon',\n", " 'Ca': 'Calcium',\n", " 'Cd': 'Cadmium',\n", " 'Ce': 'Cerium',\n", " 'Cf': 'Californium',\n", " 'Cl': 'Chlorine',\n", " 'Cm': 'Curium',\n", " 'Cn': 'Copernicium',\n", " 'Co': 'Cobalt',\n", " 'Cr': 'Chromium',\n", " 'Cs': 'Caesium (Cesium)',\n", " 'Cu': 'Copper',\n", " 'Db': 'Dubnium',\n", " 'Ds': 'Darmstadtium',\n", " 'Dy': 'Dysprosium',\n", " 'Er': 'Erbium',\n", " 'Es': 'Einsteinium',\n", " 'Eu': 'Europium',\n", " 'F': 'Fluorine',\n", " 'Fe': 'Iron',\n", " 'Fl': 'Flerovium',\n", " 'Fm': 'Fermium',\n", " 'Fr': 'Francium',\n", " 'Ga': 'Gallium',\n", " 'Gd': 'Gadolinium',\n", " 'Ge': 'Germanium',\n", " 'H': 'Hydrogen',\n", " 'He': 'Helium',\n", " 'Hf': 'Hafnium',\n", " 'Hg': 'Mercury',\n", " 'Ho': 'Holmium',\n", " 'Hs': 'Hassium',\n", " 'I': 'Iodine',\n", " 'In': 'Indium',\n", " 'Ir': 'Iridium',\n", " 'K': 'Potassium',\n", " 'Kr': 'Krypton',\n", " 'La': 'Lanthanum',\n", " 'Li': 'Lithium',\n", " 'Lr': 'Lawrencium',\n", " 'Lu': 'Lutetium',\n", " 'Lv': 'Livermorium',\n", " 'Md': 'Mendelevium',\n", " 'Mg': 'Magnesium',\n", " 'Mn': 'Manganese',\n", " 'Mo': 'Molybdenum',\n", " 'Mt': 'Meitnerium',\n", " 'N': 'Nitrogen',\n", " 'Na': 'Sodium',\n", " 'Nb': 'Niobium',\n", " 'Nd': 'Neodymium',\n", " 'Ne': 'Neon',\n", " 'Ni': 'Nickel',\n", " 'No': 'Nobelium',\n", " 'Np': 'Neptunium',\n", " 'O': 'Oxygen',\n", " 'Os': 'Osmium',\n", " 'P': 'Phosphorus',\n", " 'Pa': 'Protactinium',\n", " 'Pb': 'Lead',\n", " 'Pd': 'Palladium',\n", " 'Pm': 'Promethium',\n", " 'Po': 'Polonium',\n", " 'Pr': 'Praseodymium',\n", " 'Pt': 'Platinum',\n", " 'Pu': 'Plutonium',\n", " 'Ra': 'Radium',\n", " 'Rb': 'Rubidium',\n", " 'Re': 'Rhenium',\n", " 'Rf': 'Rutherfordium',\n", " 'Rg': 'Roentgenium',\n", " 'Rh': 'Rhodium',\n", " 'Rn': 'Radon',\n", " 'Ru': 'Ruthenium',\n", " 'S': 'Sulfur (Sulphur)',\n", " 'Sb': 'Antimony',\n", " 'Sc': 'Scandium',\n", " 'Se': 'Selenium',\n", " 'Sg': 'Seaborgium',\n", " 'Si': 'Silicon',\n", " 'Sm': 'Samarium',\n", " 'Sn': 'Tin',\n", " 'Sr': 'Strontium',\n", " 'Ta': 'Tantalum',\n", " 'Tb': 'Terbium',\n", " 'Tc': 'Technetium',\n", " 'Te': 'Tellurium',\n", " 'Th': 'Thorium',\n", " 'Ti': 'Titanium',\n", " 'Tl': 'Thallium',\n", " 'Tm': 'Thulium',\n", " 'U': 'Uranium',\n", " 'Uuo': 'Ununoctium',\n", " 'Uup': 'Ununpentium',\n", " 'Uus': 'Ununseptium',\n", " 'Uut': 'Ununtrium',\n", " 'V': 'Vanadium',\n", " 'W': 'Tungsten',\n", " 'Xe': 'Xenon',\n", " 'Y': 'Yttrium',\n", " 'Yb': 'Ytterbium',\n", " 'Zn': 'Zinc',\n", " 'Zr': 'Zirconium'}" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "element_dict" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "EjPprXYjBPog", "nbpages": { "level": 3, "link": "[1.5.3.3 Dictionary of Dictionaries](https://ndcbe.github.io/cbe-xx258/01.05-Lists-Dictionaries-Enumerate.html#1.5.3.3-Dictionary-of-Dictionaries)", "section": "1.5.3.3 Dictionary of Dictionaries" } }, "source": [ "### 1.5.3.3 Dictionary of Dictionaries" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "-JU8YeC6BPog", "nbpages": { "level": 3, "link": "[1.5.3.3 Dictionary of Dictionaries](https://ndcbe.github.io/cbe-xx258/01.05-Lists-Dictionaries-Enumerate.html#1.5.3.3-Dictionary-of-Dictionaries)", "section": "1.5.3.3 Dictionary of Dictionaries" } }, "source": [ "We can make dictionaries even more powerful if we make a dictionary of dictionaries.\n", "\n", "**Top Level**. Day of the week.\n", "* Key: symbol \"M\", \"T\", etc.\n", "* Value: Another dictionary!\n", "\n", "**Second Level**. Data for each day.\n", "* Keys: \"name\", \"weekday\", \"weekend\"\n", "* Values: string, boolean (True or False), boolean" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 171 }, "colab_type": "code", "executionInfo": { "elapsed": 247, "status": "ok", "timestamp": 1548774990616, "user": { "displayName": "Alexander Dowling", "photoUrl": "https://lh3.googleusercontent.com/-LChdQ2m5OQE/AAAAAAAAAAI/AAAAAAAAAA0/JeXJe4vQJ7M/s64/photo.jpg", "userId": "00988067626794866502" }, "user_tz": 300 }, "id": "z5AF0lF3BPoh", "nbpages": { "level": 3, "link": "[1.5.3.3 Dictionary of Dictionaries](https://ndcbe.github.io/cbe-xx258/01.05-Lists-Dictionaries-Enumerate.html#1.5.3.3-Dictionary-of-Dictionaries)", "section": "1.5.3.3 Dictionary of Dictionaries" }, "outputId": "8fa1fa66-62a7-4769-e455-b58f93e23ef4" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The days that are weekdays are\n", "Monday is a weekday.\n", "Tuesday is a weekday.\n", "Wednesday is a weekday.\n", "Thursday is a weekday.\n", "Friday is a weekday.\n", "The days that are weekend days are\n", "Saturday is a weekend, whoop.\n", "Sunday is a weekend, whoop.\n" ] } ], "source": [ "#simple dictionary of dictionaries\n", "days_of_week = {\"M\":{\"name\":\"Monday\",\"weekday\":True,\"weekend\":False},\n", " \"T\":{\"name\":\"Tuesday\",\"weekday\":True,\"weekend\":False},\n", " \"W\":{\"name\":\"Wednesday\",\"weekday\":True,\"weekend\":False},\n", " \"R\":{\"name\":\"Thursday\",\"weekday\":True,\"weekend\":False},\n", " \"F\":{\"name\":\"Friday\",\"weekday\":True,\"weekend\":False},\n", " \"S\":{\"name\":\"Saturday\",\"weekday\":False,\"weekend\":True},\n", " \"U\":{\"name\":\"Sunday\",\"weekday\":False,\"weekend\":True}}\n", "\n", "print(\"The days that are weekdays are\")\n", "for day in days_of_week: #for loop over dictionary, loops over keys\n", " if days_of_week[day][\"weekday\"] == True:\n", " print(days_of_week[day][\"name\"],\"is a weekday.\")\n", "\n", "print(\"The days that are weekend days are\") \n", "for day in days_of_week: #for loop over dictionary, loops over keys\n", " if days_of_week[day][\"weekend\"] == True:\n", " print(days_of_week[day][\"name\"],\"is a weekend, whoop.\")" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "3Nyl33fDBPoj", "nbpages": { "level": 3, "link": "[1.5.3.3 Dictionary of Dictionaries](https://ndcbe.github.io/cbe-xx258/01.05-Lists-Dictionaries-Enumerate.html#1.5.3.3-Dictionary-of-Dictionaries)", "section": "1.5.3.3 Dictionary of Dictionaries" } }, "source": [ "Notice that when you loop over a dictionary in a for loop, the loop variable will get each of the keys of the dictionary." ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "JKLdQ1x6BPom", "nbpages": { "level": 3, "link": "[1.5.3.3 Dictionary of Dictionaries](https://ndcbe.github.io/cbe-xx258/01.05-Lists-Dictionaries-Enumerate.html#1.5.3.3-Dictionary-of-Dictionaries)", "section": "1.5.3.3 Dictionary of Dictionaries" } }, "source": [ "The dictionary has been modified to include the day name in Spanish." ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "colab": {}, "colab_type": "code", "id": "KAbje-BHBPom", "nbpages": { "level": 3, "link": "[1.5.3.3 Dictionary of Dictionaries](https://ndcbe.github.io/cbe-xx258/01.05-Lists-Dictionaries-Enumerate.html#1.5.3.3-Dictionary-of-Dictionaries)", "section": "1.5.3.3 Dictionary of Dictionaries" } }, "outputs": [], "source": [ "#more complicated dictionary of dictionaries\n", "days_of_week2 = {\"M\":{\"name\":\"Monday\",\"weekday\":True,\"Spanish\":\"Lunes\"},\n", " \"T\":{\"name\":\"Tuesday\",\"weekday\":True,\"Spanish\":\"Martes\"},\n", " \"W\":{\"name\":\"Wednesday\",\"weekday\":True,\"Spanish\":\"Miércoles\"},\n", " \"R\":{\"name\":\"Thursday\",\"weekday\":True,\"Spanish\":\"Jueves\"},\n", " \"F\":{\"name\":\"Friday\",\"weekday\":True,\"Spanish\":\"Viernes\"},\n", " \"S\":{\"name\":\"Saturday\",\"weekday\":False,\"Spanish\":\"Sábado\"},\n", " \"U\":{\"name\":\"Sunday\",\"weekday\":False,\"Spanish\":\"Domingo\"}}" ] }, { "cell_type": "markdown", "metadata": { "nbpages": { "level": 3, "link": "[1.5.3.3 Dictionary of Dictionaries](https://ndcbe.github.io/cbe-xx258/01.05-Lists-Dictionaries-Enumerate.html#1.5.3.3-Dictionary-of-Dictionaries)", "section": "1.5.3.3 Dictionary of Dictionaries" } }, "source": [ "
\n", "Class Activity: With a partner, write pseudocode to loop over the days of the week. Print off the day name in Spanish and whether it is a weekend or weekday.\n", "
" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "nbpages": { "level": 3, "link": "[1.5.3.3 Dictionary of Dictionaries](https://ndcbe.github.io/cbe-xx258/01.05-Lists-Dictionaries-Enumerate.html#1.5.3.3-Dictionary-of-Dictionaries)", "section": "1.5.3.3 Dictionary of Dictionaries" } }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "QIereMdFBPor", "nbpages": { "level": 3, "link": "[1.5.3.3 Dictionary of Dictionaries](https://ndcbe.github.io/cbe-xx258/01.05-Lists-Dictionaries-Enumerate.html#1.5.3.3-Dictionary-of-Dictionaries)", "section": "1.5.3.3 Dictionary of Dictionaries" } }, "source": [ "
\n", "Class Activity: Implement your pseudocode in Python.\n", "
" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "nbpages": { "level": 3, "link": "[1.5.3.3 Dictionary of Dictionaries](https://ndcbe.github.io/cbe-xx258/01.05-Lists-Dictionaries-Enumerate.html#1.5.3.3-Dictionary-of-Dictionaries)", "section": "1.5.3.3 Dictionary of Dictionaries" } }, "outputs": [], "source": [ "# YOUR SOLUTION HERE" ] }, { "cell_type": "markdown", "metadata": { "nbpages": { "level": 2, "link": "[1.5.4 Enumerate](https://ndcbe.github.io/cbe-xx258/01.05-Lists-Dictionaries-Enumerate.html#1.5.4-Enumerate)", "section": "1.5.4 Enumerate" } }, "source": [ "## 1.5.4 Enumerate" ] }, { "cell_type": "markdown", "metadata": { "nbpages": { "level": 3, "link": "[1.5.4.1 Lists](https://ndcbe.github.io/cbe-xx258/01.05-Lists-Dictionaries-Enumerate.html#1.5.4.1-Lists)", "section": "1.5.4.1 Lists" } }, "source": [ "### 1.5.4.1 Lists" ] }, { "cell_type": "markdown", "metadata": { "nbpages": { "level": 3, "link": "[1.5.4.1 Lists](https://ndcbe.github.io/cbe-xx258/01.05-Lists-Dictionaries-Enumerate.html#1.5.4.1-Lists)", "section": "1.5.4.1 Lists" } }, "source": [ "Let's see some syntax for growing lists." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "nbpages": { "level": 3, "link": "[1.5.4.1 Lists](https://ndcbe.github.io/cbe-xx258/01.05-Lists-Dictionaries-Enumerate.html#1.5.4.1-Lists)", "section": "1.5.4.1 Lists" } }, "outputs": [], "source": [ "# create a list with nothing\n", "\n", "students = []\n", "\n", "# add a student\n", "students.append(\"Joe Smith\")\n", "\n", "# add another student\n", "students.append(\"Jane Doe\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "nbpages": { "level": 3, "link": "[1.5.4.1 Lists](https://ndcbe.github.io/cbe-xx258/01.05-Lists-Dictionaries-Enumerate.html#1.5.4.1-Lists)", "section": "1.5.4.1 Lists" } }, "outputs": [], "source": [ "print(students)" ] }, { "cell_type": "markdown", "metadata": { "nbpages": { "level": 3, "link": "[1.5.4.1 Lists](https://ndcbe.github.io/cbe-xx258/01.05-Lists-Dictionaries-Enumerate.html#1.5.4.1-Lists)", "section": "1.5.4.1 Lists" } }, "source": [ "How to loop for the list? Let's review." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "nbpages": { "level": 3, "link": "[1.5.4.1 Lists](https://ndcbe.github.io/cbe-xx258/01.05-Lists-Dictionaries-Enumerate.html#1.5.4.1-Lists)", "section": "1.5.4.1 Lists" } }, "outputs": [], "source": [ "# Approach 1\n", "for s in students:\n", " print(s)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "nbpages": { "level": 3, "link": "[1.5.4.1 Lists](https://ndcbe.github.io/cbe-xx258/01.05-Lists-Dictionaries-Enumerate.html#1.5.4.1-Lists)", "section": "1.5.4.1 Lists" } }, "outputs": [], "source": [ "# Approach 2\n", "for i in range(len(students)):\n", " print(i,\": \",students[i])" ] }, { "cell_type": "markdown", "metadata": { "nbpages": { "level": 3, "link": "[1.5.4.1 Lists](https://ndcbe.github.io/cbe-xx258/01.05-Lists-Dictionaries-Enumerate.html#1.5.4.1-Lists)", "section": "1.5.4.1 Lists" } }, "source": [ "Can simply the syntax? It is really convienent to access the index with `i` and the student with `s`." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "nbpages": { "level": 3, "link": "[1.5.4.1 Lists](https://ndcbe.github.io/cbe-xx258/01.05-Lists-Dictionaries-Enumerate.html#1.5.4.1-Lists)", "section": "1.5.4.1 Lists" } }, "outputs": [], "source": [ "for i, s in enumerate(students):\n", " print(i,\": \",s)" ] }, { "cell_type": "markdown", "metadata": { "nbpages": { "level": 3, "link": "[1.5.4.2 Dictionaries](https://ndcbe.github.io/cbe-xx258/01.05-Lists-Dictionaries-Enumerate.html#1.5.4.2-Dictionaries)", "section": "1.5.4.2 Dictionaries" } }, "source": [ "### 1.5.4.2 Dictionaries" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "nbpages": { "level": 3, "link": "[1.5.4.2 Dictionaries](https://ndcbe.github.io/cbe-xx258/01.05-Lists-Dictionaries-Enumerate.html#1.5.4.2-Dictionaries)", "section": "1.5.4.2 Dictionaries" } }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "< [1.4 Functions, Scoping, and Other Fun Stuff](https://ndcbe.github.io/cbe-xx258/01.04-Functions-scoping.html) | [Contents](toc.html) | [1.6 Linear Algebra with Numpy and Scipy](https://ndcbe.github.io/cbe-xx258/01.06-NumPy.html) >

\"Open

\"Download\"" ] } ], "metadata": { "colab": { "collapsed_sections": [ "EjPprXYjBPog", "JKLdQ1x6BPom" ], "name": "L5-Dictionaries-and-Functions-as-Arguments.ipynb", "provenance": [], "version": "0.3.2" }, "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.3" } }, "nbformat": 4, "nbformat_minor": 2 }