{ "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) >
"
]
},
{
"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": [
"
"
]
}
],
"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
}