Skip to content

Commit

Permalink
add solution notebook function
Browse files Browse the repository at this point in the history
  • Loading branch information
BCampforts committed Jun 10, 2021
1 parent 534a805 commit dc8bb39
Show file tree
Hide file tree
Showing 2 changed files with 1,490 additions and 0 deletions.
268 changes: 268 additions & 0 deletions lessons/python/ESPIN-04- For loops and conditionals-Solution.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,268 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a href=\"https://csdms.colorado.edu/wiki/ESPIn2020\"><img style=\"float: center; width: 75%\" src=\"../../media/ESPIn.png\"></a>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Programming with Python\n",
"## For loops\n",
"### minutes: 30\n",
"---\n",
"> ## Learning Objectives {.objectives}\n",
">\n",
"> * Explain what a for loop does\n",
"> * Correctly write for loops to repeat simple calculations\n",
"> * Trace changes to a loop variable as the loop runs\n",
"> * Trace changes to other variables as they are updated by a for loop\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## For loops\n",
"\n",
"Automating repetitive tasks is best accomplished with a loop. A For Loop repeats a set of actions for every item in a collection (every letter in a word, every number in some range, every name in a list) until it runs out of items:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"e\n",
"s\n",
"p\n",
"i\n",
"n\n"
]
}
],
"source": [
"word = 'espin'\n",
"for char in word:\n",
" print( char)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The general form of a for loop is:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# for item in collection:\n",
"# do things with item"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A for loop starts with the word \"for\", then the variable name that each item in the collection is going to take inside the loop, then the word \"in\", and then the collection or sequence of items to loop through.\n",
"\n",
"In Python, there must be a colon at the end of the line starting the loop. The commands that are run repeatedly inside the loop are indented below that. Unlike many other languages, there is no command to end a loop (e.g. `end for`): the loop ends once the indentation moves back.\n",
"\n",
"### Practice your skills\n",
"Make a for loop to count the letters in the word elephant. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"word ='elephant'\n",
"counter =0\n",
"for char in word:\n",
" print(char)\n",
" counter+=1\n",
" \n",
"print(counter)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"It’s worth tracing the execution of this little program step by step. Since there are eight characters in ‘elephant’, the statement inside the loop will be executed eight times. The first time around, `length` is zero (the value assigned to it on line 1) and `letter` is \"e\". The code adds 1 to the old value of `length`, producing 1, and updates `length` to refer to that new value. The next time the loop starts, `letter` is \"l\" and `length` is 1, so `length` is updated to 2. Once there are no characters left in \"elephant\" for Python to assign to `letter`, the loop finishes and the `print` statement tells us the final value of length.\n",
"\n",
"Note that a loop variable is just a variable that’s being used to record progress in a loop. It still exists after the loop is over (and has the last value it had inside the loop). We can re-use variables previously defined as loop variables, overwriting their value:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"a\n",
"b\n",
"c\n",
"after the loop, letter is c\n"
]
}
],
"source": [
"letter = 'z'\n",
"for letter in 'abc':\n",
" print (letter)\n",
"print ('after the loop, letter is', letter)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Making Choices\n",
"\n",
"When analyzing data, we’ll often want to automatically recognize differences between values and take different actions on the data depending on some conditions. Here, we’ll learn how to write code that runs only when certain conditions are true.\n",
"\n",
"### Conditionals\n",
"We can ask Python to running different commands depending on a condition with an if statement:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"num = 42\n",
"\n",
"if num > 100:\n",
" print ('greater')\n",
"else:\n",
" print ('not greater')\n",
" \n",
"print ('done')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The second line of this code uses the keyword `if` to tell Python that we want to make a choice. If the test that follows the `if` statement is true, the commands in the indented block are executed. If the test is false, the indented block beneath the else is executed instead. Only one or the other is ever executed.\n",
"\n",
"Conditional statements don’t have to include an `else`. If there isn’t one, Python simply does nothing if the test is false:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"num = 42\n",
"print ('before conditional...')\n",
"if num > 100:\n",
" print (num, 'is greater than 100')\n",
"print ('...after conditional')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can also chain several tests together using `elif`, which is short for “else if”. The following Python code uses elif to print the sign of a number. We use a double equals sign `==` to test for equality between two values. The single equal sign is used for assignment:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"num = -3\n",
"\n",
"if num > 0:\n",
" print (num, \"is positive\")\n",
"elif num == 0:\n",
" print (num, \"is zero\")\n",
"else:\n",
" print (num, \"is negative\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can also combine tests using `and` and `or`. `and` is only true if both parts are true:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"if (1 > 0) and (-1 > 0):\n",
" print ('both tests are true')\n",
"else:\n",
" print ('at least one test is false')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"while `or` is true if at least one part is true:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"if (1 > 0) or (-1 > 0):\n",
" print ('at least one test is true')\n",
"else:\n",
" print ('neither test is true')"
]
}
],
"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.8.8"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
Loading

0 comments on commit dc8bb39

Please sign in to comment.