{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

Practical 2: Foundations (Part 1)

\n", "

Getting to grips with the 'Basics'

\n", "
\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "| Complete | Part 1: Foundations | Part 2: Data | Part 3: Analysis | |\n", "| :------- | :------------------ | :----------- | :--------------- | --: |\n", "| 10% | ▓▓▓░░░░░ | ░░░░░░ | ░░░░░░ | 2/10\n", "\n", "This notebook is focussed on ensuring that you're able to run Jupyter notebooks locally (i.e. on youu own computer) in Docker and are comfortable with the basics of Python: if you've already done Code Camp then this will be a refresher and you'll have plenty of time to get to grips with Git and GitHub, which often presents significant practical challenges. You *should* find this notebook quite straightforward, but take any challenges as a sign that you need to keep practicing since subsequent weeks will build on these foundational concepts." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", " ⚠ Remember: save your work regularly, or prepare to be disappointed. This will be my only reminder about this!\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Task 1: Setting Up\n", "\n", "I'm going to assume that you've got Docker installed and have managed to 'pull' the `jreades/sds:2023` or `jreades/sds:2023-silicon` image at this point. If not, this is your **most urgent** priority. Please make one of us aware of your situation and we'll do our best to get you going.\n", "\n", "### 1.1 Is the Container Running?\n", "\n", "Docker will happily keep a container running in the background even if you close every open window. So how do you know if the `sds2023` container is already running? There are two ways:\n", "\n", "1. Open the Docker Dashboard/Desktop from the menu and make sure that you select the `Containers` tab on the left (it may be hidden by a message from Docker about upcoming conferences). You _should_ see something like this if the container is _running_ and _available_:\n", "\n", "![sds2023 container running](./img/Running_Container.png)\n", "\n", "2. From the Terminal/Power Shell you should be able to run: `docker ps`. This will give you output something like this:\n", "\n", "![sds2023 container running from Terminal](./img/Running_Container_2.png)\n", "\n", "If the `sds2023` *container* is not running then you'll need to run the startup command (`docker run...`) covered in the [last practical session](Practical-01-Getting_Started.ipynb)\n", "\n", "### 1.2 Connect to Jupyter Lab\n", "\n", "Once you know the container is running you can connected to Jupyter Lab on [localhost:8888](http://localhost:8888/lab/tree/work/) and should see something like this:\n", "\n", "\"Screenshot\n", "\n", "You're connected and can go to Task 2. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 1.2 Getting the Notebook\n", "\n", "#### 1.2.1 Downloading from GitHub\n", "\n", "On the [Week 2 of FSDS page](https://jreades.github.io/fsds/sessions/week2.html#practical) you'll see that there is a 'preview' link and a a 'download' link in the [Practical section](https://jreades.github.io/fsds/sessions/week2.html#practical). If you click the preview link you will be taken to the GitHub page for the notebook where it has been 'rendered' as a web page by GitHub. But to make the notebook useable on *your* computer, you need to download the RAW notebook content.\n", "\n", "So now:\n", "\n", "1. Click on the `Download the Notebook` link.\n", "2. When you see a page of raw code, select `File` then `Save Page As...`.\n", "3. Save the file somewhere easy to find (e.g. Downloads or Desktop). \n", "4. Notice that *most* browsers will save the file as `Practical-02-Foundations_1.ipynb.txt`; they have *added* `.txt` to the file name without asking. You will need to correct this by *either*: 1) changing the name in the command line using `mv .ipynb.txt .ipynb` (you can even do this in Jupyter Lab after doing Task **1.2.2**); or 2) renaming the file using the Finder (Mac) or Windows Explorer.\n", "\n", "#### 1.2.2 Making the Notebook Available to Jupyter Lab\n", "\n", "You now need to place the notebook where Jupyter Lab can access it. There is only _one_ place that you can do this, and that is where you ran the `docker run ...` command which **mounted** the current working directory to the `work` directory. Remember that the startup command is something like:\n", "\n", "```shell\n", "docker run ... -v \"$(pwd):/home/jovyan/work\" ...\n", "```\n", "\n", "The `-v` (short for *volume*) tells Docker what part of _your_ computer (`$(pwd)`) to connect to container (`/home/jovyan/work`). `pwd` is short-hand for 'working directory' and is the location where you ran the Docker startup command! So we're talking about the location on _your_ computer when you access the `work` folder from within Docker/Jupyter Lab:\n", "\n", "- On a Mac it will most likely be your `$HOME` directory (e.g. `/Users/your_username/`).\n", "- On a Windows machine it _may_ be your `$HOME` directory but we can't promise. See the steps above for trying to ensure this is where you end up.\n", "\n", "Perhaps a video will help clarify?\n", "\n", "
\n", " \n", " \"Docker\n", " \n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 1.2.3 Staring the Notebook\n", "\n", "From _within Jupyter Lab_ you should now be able to start the notebook:\n", "\n", "1. Make sure Jupyter Lab is showing (📁`/work/`) in the left-hand menu.\n", "2. Navigate to the location that you saved `Practical-02-Foundations_1.ipynb`.\n", "3. Double-click the file and you should see the notebook appear on the right-hand side.\n", "\n", "Now you can run code directly in your browser, so let's try it!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Task 2: Hello World!\n", "\n", "Nearly every introduction to programming course starts with the famous 'Hello World!', so why do anything different? Try clicking on the right-triangle (▶) in the tab area above (between the clipboard 📋 for copying and the ■ for stopping running code):" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello World!\n" ] } ], "source": [ "print('Hello World!')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Hopefully, the following has appeared directly below the code:\n", "```bash\n", "Hello World!\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", " 💡 Tip: You can always click the ▶ icon above, but it will be much faster to get into the habit of type Ctrl+Enter instead when you have placed your cursor in a code cell. This is also how to turn a Markdown cell back into display text.\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" } } }, "source": [ "## Task 3: Python Variables\n", "\n", "
\n", " 🔗 Connections: This is a short recap of materials covered in this week's video on Python: the Basics as well as Code Camp's The Basics.\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 3.1 Variables Names\n", "\n", "
\n", " ⚠ Note: Pay attention to the colour of the code, while it might be subtle (a single character in a different colour), it is giving you clues as to where problems might be found because it means the Python 'interpreter' is reading what you wrote differently from how you probably meant it...\n", "
\n", " \n", "_Some_ of the lines of code below are valid Python, but others _may_ have a problem that will cause Python to generate an error. Each error can be fixed by changing _one_ character. See if you can work out which ones you need to fix **before running the code**:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Pi = 3.14159 # Valid Python\n", "pi = 3.14159 # ??\n", "3pi = 3.14159*3 # ??\n", "pi_2 = 3.14159**2 # ??\n", "pi^2 = 3.14159**2 # ??\n", "my radius = 5 # ??\n", "My_Radius = 5 # ??\n", "class = 5 # ??" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 3.2 Variable Types\n", "\n", "**Before running the code** below, work out what the output will be for _each_ of the `print` commands below when the code is executed. Write them as comments on the same line as the code (after the `#`, see example)." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x = '2'\n", "y = z = 2\n", "\n", "print(y * z) # 4\n", "print(x * y) # ??\n", "print(x + x) # ??\n", "print((y+z)**z) # ??\n", "print(y/(y+z)) # ??\n", "\n", "print( type(x * y) ) # ??\n", "print( type(y * z) ) # ??\n", "print( type((y+z)**z) ) # ??\n", "print( type(y/(y+z)) ) # ??" ] }, { "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 Assignment\n", "\n", "**Before running the code**, work out what the values of `x`, `y` and `z` will be after every line of code in the block has been executed." ] }, { "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 = 12\n", "y = 10\n", "\n", "z = x + y # ??\n", "x = x + y # ??\n", "y = z + y # ??\n", "\n", "# print(x)\n", "# ??\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" } } }, "source": [ "Once you have worked out what you think `x`, `y` and `z` are, add `print(...)` statements to the code above to check your answers!\n", "\n", "Make sure you understand the results you find. **Ask someone if you need help to understand.**" ] }, { "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.4 Operators & Precedence\n", "\n", "**Before running the code**, work out what the values of `x`, `y` and `z` will be after every line of code in the block has been executed. Feel free to use a calculator.\n", "\n", "
\n", " 💡 Tip: This question is about what operations (i.e. multiplication, division, powers, etc.) are done first based on the type of operation and the presence of parentheses... it's the same as it would be for a maths problem!
" ] }, { "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 + (2 * 3) / 4\n", "y = (1 + 2) * (3 / 4)\n", "z = 1 + 2 * (3 / 4)\n", "\n", "print(x) # x is \n", "print(y) # y is \n", "print(z) # z is " ] }, { "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": [ "Once you have calculated what you think `x`, `y` and `z` are, run the code to check." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 3.5 Test Your Operator Knowledge\n", "\n", "Now let's look at some of the stranger operators. Many of these can be very useful in more complex code but can seem a little pointless now.\n", "\n", "**Work out what operator should replace the `??`** in each of the lines of code below to produce the output I've shown in the comments. I've mixed in ones you have seen above with ones that we've not seen before." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x = 10\n", "y = 3\n", "\n", "print( x ?? y ) # 1\n", "print( x ?? y ) # 13\n", "print( x ?? y ) # False\n", "print( x ?? y ) # 1000\n", "print( x ?? y ) # 7\n", "print( x ?? y ) # 3" ] }, { "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.6 Applying What We've Learned\n", "\n", "Now we are going to take what we've learned and apply it in a more 'abstract' way: how do we translate some well-known mathematical formulae _into code_? In particular, I'm interested in the formula for the volume of a sphere (and this gives me a chance to show that Notebooks can show formulae as well!):\n", "\n", "$$\n", "V = \\frac{4}{3}\\pi r^{3}\n", "$$\n", "\n", "#### 3.6.1. Calculate the Volume of a Sphere\n", "\n", "So, given a sphere with a **diameter** of 12cm, calculate its volume:\n", "\n", "
\n", " 💡 Tip: I would strongly advise you to Google: `python constant pi` and look for code that will save you having to write down the value of $\\pi$.\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" } } }, "outputs": [], "source": [ "??\n", "\n", "print(v)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "I get an answer of 904.779cm$^3$." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 3.6.2. Calculate the Radius of a Sphere\n", "\n", "Now, given a sphere of volume 14,137cm$^3$ calculate its radius as a **whole number**. The formula for this can be worked out as:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\\begin{align*}\n", "\\frac{3}{4}V &= \\pi r^{3} \\\\\n", "\\frac{3}{4}\\frac{V}{\\pi} &= r^{3} \\\\\n", "(\\frac{3}{4}\\frac{V}{\\pi})^{1/3} &= r\n", "\\end{align*}" ] }, { "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": [ "If you can't remember how to rearrange formulae this would be a good skill to refresh!\n", "\n", "
\n", " 💡 Tip: There are two ways to get a \"whole number\" from a float. One way is to change the variable's type, the other is to make use of Google to find out if there are ways of rounding to the nearest integer?\n", "
" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "I get an answer of either 14 or 15... can you work out why?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Task 4: Python Conditions\n", "\n", "
\n", " 🔗 Connections: This is a short recap of material covered in Code Camp's Truth & Conditions and, to some extent, the Iteration lecture.
" ] }, { "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 Working with Conditions\n", "\n", "Use **if, elif, and else** so that you get the following output:\n", "\n", "1. When `hours` is 10 or more, then the code prints `At least 10 hours worked!`\n", "2. When `hours` is exactly 2, then the code prints `Exactly 2 hours worked.` \n", "3. When `hours` is 9 or less *but not 2*, then the code prints `Less than 10 hours worked!`\n", "\n", "
⚠ Hint: You will also need to think about the order in which these conditions are tested.
" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "hours=0\n", "\n", "if hours ??:\n", " print(\" \")\n", "elif hours ??:\n", " print(\" \")\n", "else:\n", " print(\" \")" ] }, { "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 Flow Control\n", "\n", "Using the flow chart shown in the image below as a model, write the code to make this condition work. You will need to complete the code such that it produces the following:\n", "1. When `a = 2` and `b = 2` four lines of output will be written\n", "2. When `a = 1` and `b = 2` one line of output will be written\n", "\n", "" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = 1\n", "b = 1\n", "\n", "# \n", "??" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Task 5: Python Logic\n", "\n", "
\n", " 🔗 Connections: This is a short recap of Code Camp's Boolean Logic session and the Python: the Basics lecture.
" ] }, { "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": [ "### 5.1 It's All Quite Logical...\n", "\n", "Read the code in the 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=??\n", "\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": [ "**Before** adding a value for `x` and running the code above, answer the following questions in the Markdown cell below:\n", "\n", "1. What names are name(s) are printed when `x = 5`? \n", "2. What value(s) can `x` be when the names `Joe` and `Aled` are printed? \n", "3. What name(s) are printed when `x = -1`?\n", "4. Is 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" } } }, "source": [ "
\n", " 💡 Hint: Double-click here to start editing this cell using Markdown.
\n", "\n", "#### 5.1.1 Answers\n", "\n", "1. ??\n", "2. ??\n", "3. ??\n", "4. ??\n", "\n", "When you're finished, run the code above and test your answer!" ] }, { "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": [ "### 5.2 Logic (Cont'd)\n", "\n", "Study the flow chart below.\n", "\n", "\n", "\n", "
\n", " ⚠ Hint: This will require you to combine logic with one of the operators that we saw earlier. Also note the new iterator that we've got here: range(<start>, <stop>) to create a range of numbers between two other numbers.
\n", "\n", "In the cell below, use the for loop _already set up_ to as a starting point for implementing the flow chart shown above for values of `x` between 0 and 9.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for x in range(0,9):\n", " #... do something...\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" } } }, "source": [ "### 5.3 'Nested' Conditionals\n", "\n", "Conditional statements can be nested within one another. That is, Python evaluates the first, or 'outer', condition and can then evaluate secondary, or 'inner', conditions. 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": [ "x = 5\n", "y = 4\n", "\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(\"\")" ] }, { "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’ (outer or inner condition) 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 `` to a string that explains the condition of `x` and `y`\n", "2. For `x = 2` and `y = 3`, type below what line(s) will be output:" ] }, { "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": [ "**Answer** _Edit this cell!_\n", "\n", "1. ??\n", "2. ??" ] }, { "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": [ "Great! You should now have a pretty good understanding of how conditional and logical operators work. This understanding will be handy in future as we work through other computational concepts." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Task 6: Python Lists\n", "\n", "
\n", " 🔗 Connections: This is a short recap of material covered in the Lists lecture and Code Camp's Lists session.
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 6.1 Who's in the List?\n", "\n", "Here we are looking to interact with lists in a straightforward way that will help you to understand accessing them using indexes and slices, and searching." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cities = ['New York', 'London', 'Beijing', 'Tokyo', 'Delhi']\n", "\n", "# Print out London from cities:\n", "print( ?? )\n", "\n", "# Print out Tokyo using *negative* indexing:\n", "print( ?? )\n", "\n", "# Print out Beijing *and* Tokyo using a list slice\n", "print( ?? )\n", "\n", "# Print out London to Delhi using a slice\n", "print( ?? ) # You could also do cities[1:5] but this way is neater\n", "\n", "# Combine positive and negative indexing to print out London, Beijing and Tokyo using a list slice\n", "print( ?? )\n", "\n", "# Print out the position of New York in the list by searching for it (i.e. you can't just type 0)\n", "print( ?? )" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 6.2 Manipulating Lists\n", "\n", "Let's break a few things...\n", "\n", "#### 6.2.1 Create an IndexError" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Cause an 'IndexError: list index out of range' errr\n", "??" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 6.2.2 Create a ValueError" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Cause a ValueError using the city of Toronto\n", "??" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 6.2.3 Sort the List\n", "\n", "Sort the list *in place* in reverse alphabetical order (i.e. z...a) and then print the sorted list" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "??\n", "print(cities)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The output from this should be: `['Tokyo', 'New York', 'London', 'Delhi', 'Beijing']`" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 6.3 Adding/Removing Values\n", "\n", "#### 6.3.1 Inserting into a List\n", "\n", "Add the city of Toronto to the list _after_ New York in the *sorted* list." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Just in case you make a mistake...\n", "cities = ['Tokyo', 'New York', 'London', 'Delhi', 'Beijing']\n", "\n", "??\n", "print(cities)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The output should be: `['Tokyo', 'New York', 'Toronto', 'London', 'Delhi', 'Beijing']`\n", "\n", "#### 6.3.2 Removing from a List\n", "\n", "Now *Pop* New York from the list *without* specifying its index (i.e. the number `1` should *not* appear in your code). Print out the value that you popped and the print out the cities list to check you've done the right thing..." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "??\n", "print(p)\n", "print(cities)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The output should be:\n", "```python\n", "New York\n", "['Tokyo', 'Toronto', 'London', 'Delhi', 'Beijing']\n", "```\n", "\n", "#### 6.3.3 Checking Lists\n", "\n", "Finally, how can you check if the city of Moscow is in the list and let the user know if it is or is not?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "??" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### You're Done!\n", "\n", "This is quite a lot to get through. If you've managed it in under 2 hours then *congratulations*! Either you must have paid a lot of attention when doing Code Camp, or you might want to check in with us as to whether you should really be doing this module...\n", "\n", "#### No Wait, One More Thing...\n", "\n", "You might want to refer to both [The Command Line](https://jreades.github.io/fsds/sessions/week2.html#lectures) and [Getting Stuck into Git](https://jreades.github.io/fsds/sessions/week2.html#lectures) lectures as well as to [The Terminal](https://jreades.github.io/code-camp/lessons/The_Terminal.html) Code Camp session.\n", "\n", "You now want to push your completed notebook to your GitHub reposistory. Using the Terminal or Git Bash you need to:\n", "1. Navigate (if you've not got a shell already open there) to `$HOME/Documents/CASA/modules/`.\n", "2. Check the status of your notebooks using `git status` (you should see that `Practical-02-Foundations_1.ipynb has been modified`).\n", "3. Add this changed notebook to git using `git add Practical-02-Foundations_1.ipynb`\n", "4. Commit this changed notebook with a message using `git commit -m \"\"`\n", "5. Push this change to GitHub using: `git push`\n", "\n", "You should now be able to visit your repository on [GitHub](https://github.com/) and see that your changes are now stored there as well!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
⚠ Note: If you are using Docker then you can also save your work as a PDF using: File > Export Notebook As... (this does not work for notebooks with lots of complex formatting).
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "## Credits!\n", "\n", "#### Contributors:\n", "The following individuals have contributed to these teaching materials: James Millington (james.millington@kcl.ac.uk)\n", "\n", "#### License\n", "These teaching materials are licensed under a mix of the MIT and CC-BY licenses...\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\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.11.5" } }, "nbformat": 4, "nbformat_minor": 4 }