{ "cells": [ { "cell_type": "markdown", "metadata": { "geopyter": { "Contributors": "James Millington (james.millington@kcl.ac.uk)", "git": { "active_branch": "master", "author.name": "Jon Reades", "authored_date": "2017-08-17 19:06:58", "committed_date": "2017-08-17 19:06:58", "committer.name": "Jon Reades", "sha": "5e3b396ae18a982d693c4bfd86c721c7e1e21051" } } }, "source": [ "
"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"solution2": "hidden",
"solution2_first": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"solution2": "hidden"
},
"outputs": [],
"source": [
"a = 1\n",
"b = 2\n",
"\n",
"x = a\n",
"if x == b:\n",
" print(\"inside 1\")\n",
" print(\"inside 2\")\n",
" print(\"inside 3\")\n",
"print(\"after condition\")"
]
},
{
"cell_type": "markdown",
"metadata": {
"geopyter": {
"Contributors": "James Millington (james.millington@kcl.ac.uk)",
"git": {
"active_branch": "master",
"author.name": "Jon Reades",
"authored_date": "2017-08-17 19:06:58",
"committed_date": "2017-08-17 19:06:58",
"committer.name": "Jon Reades",
"sha": "5e3b396ae18a982d693c4bfd86c721c7e1e21051"
}
}
},
"source": [
"#### 3.1 It's All Quite Logical\n",
"\n",
"Read the code in the code cell below. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"geopyter": {
"Contributors": "James Millington (james.millington@kcl.ac.uk)",
"git": {
"active_branch": "master",
"author.name": "Jon Reades",
"authored_date": "2017-08-17 19:06:58",
"committed_date": "2017-08-17 19:06:58",
"committer.name": "Jon Reades",
"sha": "5e3b396ae18a982d693c4bfd86c721c7e1e21051"
}
}
},
"outputs": [],
"source": [
"x = 1\n",
"if x > 0 and x < 5:\n",
" print(\"Joe\")\n",
" \n",
"if x > 0 or x < 5:\n",
" print(\"Aled\")\n",
" \n",
"if not(x > 0):\n",
" print(\"Sarah\")"
]
},
{
"cell_type": "markdown",
"metadata": {
"geopyter": {
"Contributors": "James Millington (james.millington@kcl.ac.uk)",
"git": {
"active_branch": "master",
"author.name": "Jon Reades",
"authored_date": "2017-08-17 19:06:58",
"committed_date": "2017-08-17 19:06:58",
"committer.name": "Jon Reades",
"sha": "5e3b396ae18a982d693c4bfd86c721c7e1e21051"
}
}
},
"source": [
"Assume that the code executes for a given value of `x` and answer the following questions. Only test the code **AFTER** you have worked out the answers and typed them below.\n",
"\n",
"1.\tWhat names are name(s) are printed when `x = 5`? \n",
"2.\tWhat value(s) can `x` be when the names `Joe` and `Aled` are printed? \n",
"3.\tWhat name(s) are printed when `x = -1`?\n",
"4.\tIs there any value for which all three names will be printed?\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"geopyter": {
"Contributors": "James Millington (james.millington@kcl.ac.uk)",
"git": {
"active_branch": "master",
"author.name": "Jon Reades",
"authored_date": "2017-08-17 19:06:58",
"committed_date": "2017-08-17 19:06:58",
"committer.name": "Jon Reades",
"sha": "5e3b396ae18a982d693c4bfd86c721c7e1e21051"
}
},
"solution2": "hidden",
"solution2_first": true
},
"source": [
"**Answers** _Edit this cell!_\n",
"\n",
"To edit this cell just double click on it (right _here_ ). When you're finished, click the _run cell_ button in the toolbar just below the menu near the top of the window.\n",
"\n",
"1.\n",
"\n",
"2.\n",
"\n",
"3.\n",
"\n",
"4."
]
},
{
"cell_type": "markdown",
"metadata": {
"solution2": "hidden"
},
"source": [
"1. Aled\n",
"\n",
"2. 1, 2, 3, 4\n",
"\n",
"3. Aled, Sarah\n",
"\n",
"4. No"
]
},
{
"cell_type": "markdown",
"metadata": {
"geopyter": {
"Contributors": "James Millington (james.millington@kcl.ac.uk)",
"git": {
"active_branch": "master",
"author.name": "Jon Reades",
"authored_date": "2017-08-17 19:06:58",
"committed_date": "2017-08-17 19:06:58",
"committer.name": "Jon Reades",
"sha": "5e3b396ae18a982d693c4bfd86c721c7e1e21051"
}
}
},
"source": [
"#### 3.2 Logic (Cont'd)\n",
"\n",
"Study the flow chart below.\n",
"\n",
"
\n",
"\n",
"In the cell below, use the for loop already set up to execute the alternative execution shown by the flow chart for values of `x` between 0 and 9. Check your code works. Also check you understand why the Modulo `%` allows us to find odd vs even numbers. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"solution2": "hidden",
"solution2_first": true
},
"outputs": [],
"source": [
"for x in range(0,9):\n",
" # ... do something ..."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"solution2": "hidden"
},
"outputs": [],
"source": [
"for x in range(0,9):\n",
" print(x)\n",
" if(x%2 == 0):\n",
" print(\"x is even\")\n",
" else:\n",
" print(\"x is odd\")\n",
" print(x)"
]
},
{
"cell_type": "markdown",
"metadata": {
"geopyter": {
"Contributors": "James Millington (james.millington@kcl.ac.uk)",
"git": {
"active_branch": "master",
"author.name": "Jon Reades",
"authored_date": "2017-08-17 19:06:58",
"committed_date": "2017-08-17 19:06:58",
"committer.name": "Jon Reades",
"sha": "5e3b396ae18a982d693c4bfd86c721c7e1e21051"
}
}
},
"source": [
"#### 3.3 Nested Conditions\n",
"\n",
"Conditional statements can be nested within one another. That is, once one conditional statement has been evaluated others may subsequently be evaluated (or not) depending on the result of the initial conditional statement. The code below shows an example of this. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"geopyter": {
"Contributors": "James Millington (james.millington@kcl.ac.uk)",
"git": {
"active_branch": "master",
"author.name": "Jon Reades",
"authored_date": "2017-08-17 19:06:58",
"committed_date": "2017-08-17 19:06:58",
"committer.name": "Jon Reades",
"sha": "5e3b396ae18a982d693c4bfd86c721c7e1e21051"
}
}
},
"outputs": [],
"source": [
"if x != y: #line 1\n",
" print(\"x is not equal to y\")\n",
" \n",
" if(x > y): #line 4\n",
" print(\"x is greater than y\")\n",
" \n",
" else: #line 7\n",
" print(\"x is less than y\")\n",
"\n",
"else:\n",
" print(\"insert conclusion here\")"
]
},
{
"cell_type": "markdown",
"metadata": {
"geopyter": {
"Contributors": "James Millington (james.millington@kcl.ac.uk)",
"git": {
"active_branch": "master",
"author.name": "Jon Reades",
"authored_date": "2017-08-17 19:06:58",
"committed_date": "2017-08-17 19:06:58",
"committer.name": "Jon Reades",
"sha": "5e3b396ae18a982d693c4bfd86c721c7e1e21051"
}
}
},
"source": [
"Note how the indentation makes it easier to work out which ‘level’ the code is operating on. In the code above, lines 4 and 7 are at the same indentation meaning that both will be skipped if the initial condition (on line 1) is `False`. \n",
"\n",
"To check you understand how the code above works:\n",
"1. Change `insert conclusion here` in the code above to a string that helps to explain the condition of `x` and `y`\n",
"2. **Before running the code** for `x = 2` and `y = 3`, type below what line(s) will be output "
]
},
{
"cell_type": "markdown",
"metadata": {
"solution2": "hidden",
"solution2_first": true
},
"source": [
"Output for `x = 2` and `y = 3` is:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"solution2": "hidden"
},
"outputs": [],
"source": [
"x = 2\n",
"y = 3\n",
"if x != y: #line 1\n",
" print(\"x is not equal to y\")\n",
" \n",
" if(x > y): #line 4\n",
" print(\"x is greater than y\")\n",
" \n",
" else: #line 7\n",
" print(\"x is less than y\")\n",
"\n",
"else:\n",
" print(\"x is equal to y\")"
]
},
{
"cell_type": "markdown",
"metadata": {
"geopyter": {
"Contributors": "James Millington (james.millington@kcl.ac.uk)",
"git": {
"active_branch": "master",
"author.name": "Jon Reades",
"authored_date": "2017-08-17 19:06:58",
"committed_date": "2017-08-17 19:06:58",
"committer.name": "Jon Reades",
"sha": "5e3b396ae18a982d693c4bfd86c721c7e1e21051"
}
}
},
"source": [
"#### 4.1. Alterative Execution\n",
"In the code cell below, enter code that reports whether person A is older than person B or not. Whole numbers should be used as ages in years and these ages should be specified on the first two lines of code. Either “Person A is older than Person B” or “Person A is not older than Person B” should be reported to the user."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"solution2": "hidden",
"solution2_first": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"solution2": "hidden"
},
"outputs": [],
"source": [
"ageA = 20\n",
"ageB = 19\n",
"\n",
"if ageA > ageB:\n",
" print(\"Person A is older than Person B\")\n",
"else:\n",
" print(\"Person A is not older than Person B\")"
]
},
{
"cell_type": "markdown",
"metadata": {
"geopyter": {
"Contributors": "James Millington (james.millington@kcl.ac.uk)",
"git": {
"active_branch": "master",
"author.name": "Jon Reades",
"authored_date": "2017-08-17 19:06:58",
"committed_date": "2017-08-17 19:06:58",
"committer.name": "Jon Reades",
"sha": "5e3b396ae18a982d693c4bfd86c721c7e1e21051"
}
}
},
"source": [
"#### 4.2. Conditional Execution\n",
"Copy your final code from the previous exercise into the code block below. Now build on this code to check if Person A is older than Person B, but also whether they are the same age. One of following three responses should be reported to the user:\n",
"1.\t“Person A is older than Person B”\n",
"2.\t“Person B is older than Person A”\n",
"3.\t“Person A and Person B are the same age”"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"solution2": "hidden",
"solution2_first": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"solution2": "hidden"
},
"outputs": [],
"source": [
"ageA = 20\n",
"ageB = 20\n",
"\n",
"if ageA > ageB:\n",
" print(\"Person A is older than Person B\")\n",
"elif ageA < ageB:\n",
" print(\"Person B is older than Person A\")\n",
"else:\n",
" print(\"Person A and Person B are the same age\")"
]
},
{
"cell_type": "markdown",
"metadata": {
"geopyter": {
"Contributors": "James Millington (james.millington@kcl.ac.uk)",
"git": {
"active_branch": "master",
"author.name": "Jon Reades",
"authored_date": "2017-08-17 19:06:58",
"committed_date": "2017-08-17 19:06:58",
"committer.name": "Jon Reades",
"sha": "5e3b396ae18a982d693c4bfd86c721c7e1e21051"
}
}
},
"source": [
"#### 4.3. Nested Execution\n",
"Copy your final code from the previous exercise into the code block below. Build on this code with a nested execution structure to report if Person A is much younger, a little younger, the same age, a little older or much older than Person B. Write your code with a nested structure so that first it evaluates whether one person is older than the other, and then, if so, whether there is a large difference in age or a small difference in age. You should create a variable to specify what the number of years is that indicates a ‘large difference’ in age. One of the following five responses should be reported to the user:\n",
"1.\t“Person A is much younger than Person B” [if age difference > ‘large difference’]\n",
"2.\t“Person A is a little younger than Person B” [if age difference <= ‘large difference’]\n",
"3.\t“Person A is the same age as Person B”\n",
"4.\t“Person A is a little older than Person B” [if age difference <= ‘large difference’]\n",
"5.\t“Person A is a much older than Person B” [if age difference > ‘large difference’]\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"geopyter": {
"Contributors": "James Millington (james.millington@kcl.ac.uk)",
"git": {
"active_branch": "master",
"author.name": "Jon Reades",
"authored_date": "2017-08-17 19:06:58",
"committed_date": "2017-08-17 19:06:58",
"committer.name": "Jon Reades",
"sha": "5e3b396ae18a982d693c4bfd86c721c7e1e21051"
}
},
"solution2": "hidden",
"solution2_first": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"solution2": "hidden"
},
"outputs": [],
"source": [
"ageA = 25\n",
"ageB = 30\n",
"largeD = 20\n",
"\n",
"diff = ageA - ageB\n",
"\n",
"if diff <= (largeD * -1):\n",
" print(\"Person A is much younger than Person B\")\n",
"elif diff < 0 and diff > (largeD * -1):\n",
" print(\"Person A is a little younger than Person B\")\n",
"elif diff == 0:\n",
" print(\"Person A is the same age as Person B\")\n",
"elif diff >= (largeD * 1):\n",
" print(\"Person A is much older than Person B\")\n",
"elif diff > 0 and diff < (largeD * 1): \n",
" print(\"Person A is a little older than Person B\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 4.4a Bonus! Managing Errors\n",
"\n",
"Python provides tools to 'handle exceptions' (i.e. manage errors). [For example](read more at https://docs.python.org/3/tutorial/errors.html#handling-exceptions) you can use combinations of `try`, `except`, `else` and `finally` keywords. \n",
"\n",
"Read the link above (and maybe Google yourself for some other examples) and use the information gained to handle an exception in which the user an age as a `string` rather than a integer (e.g. print \"Invalid Input\" if `diff` cannot be calculated):"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"solution2": "hidden",
"solution2_first": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"solution2": "hidden"
},
"outputs": [],
"source": [
"ageA = \"five\"\n",
"ageB = 30\n",
"largeD = 20\n",
"\n",
"try:\n",
" diff = ageA - ageB\n",
"except:\n",
" print(\"Invalid Input\")\n",
"else: \n",
" if diff <= (largeD * -1):\n",
" print(\"Person A is much younger than Person B\")\n",
" elif diff < 0 and diff > (largeD * -1):\n",
" print(\"Person A is a little younger than Person B\")\n",
" elif diff == 0:\n",
" print(\"Person A is the same age as Person B\")\n",
" elif diff >= (largeD * 1):\n",
" print(\"Person A is much older than Person B\")\n",
" elif diff > 0 and diff < (largeD * 1): \n",
" print(\"Person A is a little older than Person B\")\n",
" \n",
"#read more at https://docs.python.org/3/tutorial/errors.html#handling-exceptions"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 4.4b Bonus! Managing Errors\n",
"\n",
"Another way to trap errors is by 'raising exceptions'. [This requires](https://docs.python.org/3/tutorial/errors.html#raising-exceptions) using the `raise` keyword. \n",
"\n",
"Read the link above (and maybe Google yourself for some other examples) and use the information gained to raise an exception if the user provide a negative age (outputing \"Invalid Age\" in the error mmessage):"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"solution2": "hidden",
"solution2_first": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"solution2": "hidden"
},
"outputs": [],
"source": [
"ageA = -1\n",
"ageB = 30\n",
"largeD = 20\n",
"\n",
"diff = ageA - ageB\n",
"\n",
"if ageA < 0 or ageB < 0:\n",
" raise Exception(\"Invalid Age\") \n",
"else: \n",
" if diff <= (largeD * -1):\n",
" print(\"Person A is much younger than Person B\")\n",
" elif diff < 0 and diff > (largeD * -1):\n",
" print(\"Person A is a little younger than Person B\")\n",
" elif diff == 0:\n",
" print(\"Person A is the same age as Person B\")\n",
" elif diff >= (largeD * 1):\n",
" print(\"Person A is much older than Person B\")\n",
" elif diff > 0 and diff < (largeD * 1): \n",
" print(\"Person A is a little older than Person B\")\n",
" \n",
"#read more at https://docs.python.org/3/tutorial/errors.html#raising-exceptions"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Credits!\n",
"\n",
"#### Contributors:\n",
"The following individuals have contributed to these teaching materials: \n",
"- [James Millington](https://github.com/jamesdamillington)\n",
"- [Jon Reades](https://github.com/jreades)\n",
"- [Michele Ferretti](https://github.com/miccferr)\n",
"- [Zahratu Shabrina](https://github.com/zarashabrina)\n",
"\n",
"#### License\n",
"The content and structure of this teaching project itself is licensed under the [Creative Commons Attribution-NonCommercial-ShareAlike 4.0 license](https://creativecommons.org/licenses/by-nc-sa/4.0/), and the contributing source code is licensed under [The MIT License](https://opensource.org/licenses/mit-license.php).\n",
"\n",
"#### Acknowledgements:\n",
"Supported by the [Royal Geographical Society](https://www.rgs.org/HomePage.htm) (with the Institute of British Geographers) with a Ray Y Gildea Jr Award.\n",
"\n",
"#### Potential Dependencies:\n",
"This notebook may depend on the following libraries: None"
]
}
],
"metadata": {
"anaconda-cloud": {},
"geopyter": {
"Contributors": [
"James Millington (james.millington@kcl.ac.uk)"
],
"git": {
"active_branch": "master",
"author.name": "Jon Reades",
"authored_date": "2017-08-17 19:06:58",
"committed_date": "2017-08-17 19:06:58",
"committer.name": "Jon Reades",
"sha": "5e3b396ae18a982d693c4bfd86c721c7e1e21051"
},
"libs": {}
},
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.10.12"
}
},
"nbformat": 4,
"nbformat_minor": 4
}