{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true
   },
   "source": [
    "# Reminder: Do not rename file (or allow your computer to do that)\n",
    "\n",
    "Please overwrite the original exercise release file(s) when uploading your assignment. I still had to fix some of your submissions.\n",
    "\n",
    "# Update: Trouble with previous assignment due to `_` breaking.\n",
    "\n",
    "In the previous assignment, some of you had trouble because the assignment expected `_` to be the output of the previous cell, but the Jupyter output was increasing by \"2\" as described [here](https://stackoverflow.com/q/78765478). The solution to this problem [is to disable the Anaconda Assistant](https://stackoverflow.com/a/78766025/1633026), namely:\n",
    "\n",
    "Steps to fix:\n",
    "\n",
    "1. Disable Anaconda Assistant\n",
    "1. Run `jupyter labextension disable @anaconda/assistant`\n",
    "1. Restart the Jupyter notebook\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# `()` and `[]`\n",
    "\n",
    "Remember that Python is very, very precise about how it understands your code.\n",
    "\n",
    "`()` have multiple meanings in python:\n",
    "\n",
    "- function call: `my_function(arg1)`\n",
    "- defining a function: `def my_function(arg1):`\n",
    "- specifying the order of operations: `4*(3+2)`\n",
    "- creating a tuple: `(1,2,3,4)` or `(1,)` or `()` (Note that `(1)` will not create a tuple. Also: you can create a tuple without parentheses...)\n",
    "\n",
    "`[]` similarly has a few meanings, based on context\n",
    "\n",
    "- creating a list: `[1,2,3,4]` or `[1,]` or `[1]` or `[]`\n",
    "- getting an item: `x = my_list[index]`\n",
    "  - getting a slice: `x = my_list[start_index:end_index:increment]`\n",
    "- setting an item: `my_list[index] = x`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Basic plotting, revisited"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Below, we will use matplotlib, so we need to import it here.\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "x=[1,2,3,4,5,6,7,8,9,1]\n",
    "y=[0,4,0,3,3,0,3,4,5,2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<matplotlib.lines.Line2D at 0x10b899cd0>]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 36216 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.plot(x,y,'--')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<matplotlib.lines.Line2D at 0x10ba02490>]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 39348 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "x=[1,2,3,4,5,6,7,8,9,10]\n",
    "y1=[0,4,0,3,3,0,3,4,5,2]\n",
    "y2=[3,2,4,4,2,4,4,2,4,2]\n",
    "plt.plot(x,y1)\n",
    "plt.plot(x,y2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# String formatting"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Old-style string formatting with `%`\n",
    "\n",
    "When the operator `%` is used on a string, the string is used as a *format string* for old-style formatting.\n",
    "\n",
    "In these old-style format strings, `%d` means to print an integer, `%s` means to print a string."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'The numbers are 5, 10, 20'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"The numbers are %d, %d, %d\" % (5,10,20)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'The numbers are 5, 10, 20'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tmp = (5,10,20)\n",
    "\"The numbers are %d, %d, %d\" % tmp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "not all arguments converted during string formatting",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[7], line 2\u001b[0m\n\u001b[1;32m      1\u001b[0m tmp \u001b[38;5;241m=\u001b[39m (\u001b[38;5;241m5\u001b[39m,\u001b[38;5;241m10\u001b[39m,\u001b[38;5;241m20\u001b[39m,\u001b[38;5;241m100\u001b[39m)\n\u001b[0;32m----> 2\u001b[0m \u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mThe numbers are \u001b[39;49m\u001b[38;5;132;43;01m%d\u001b[39;49;00m\u001b[38;5;124;43m, \u001b[39;49m\u001b[38;5;132;43;01m%d\u001b[39;49;00m\u001b[38;5;124;43m, \u001b[39;49m\u001b[38;5;132;43;01m%d\u001b[39;49;00m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;241;43m%\u001b[39;49m\u001b[43m \u001b[49m\u001b[43mtmp\u001b[49m\n",
      "\u001b[0;31mTypeError\u001b[0m: not all arguments converted during string formatting"
     ]
    }
   ],
   "source": [
    "tmp = (5,10,20,100)\n",
    "\"The numbers are %d, %d, %d\" % tmp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tmp = (5,10)\n",
    "\"The numbers are %d, %d, %d\" % tmp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "my_string = \"The numbers are %d, %d, %d\"\n",
    "my_string"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "my_string%(7, 14, 21)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tuple1 = (100, 200, 300)\n",
    "my_string % tuple1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"The numbers are %d, %d, %d\"%(5,10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"The numbers are %d, %d, %d\"%(5,10,20,40)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"Hello %s\"%(\"world\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"Hello %s\"%1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"Hello %d, %d\"%(1, 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"Hello %d, %d\"%(1, 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"Hello %d\"%1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"Hello %d\"%'hello'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"Hello %s\"%'hello'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## New-style (since Python 2.7) formatting with `.format()`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"The numbers are {}, {}, {}\".format(5,10,20)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"Hello {}\".format(\"world\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Even newer (since Python 3.6) style formatting with f-strings."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "name=\"Andrew\"\n",
    "my_string = f\"Hello, my name is {name}.\"\n",
    "print(my_string)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "f\"Hello, my name is {name}.\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "f\"Hello, my name is {name}. {akjfhasd}\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "f\"Hello, my name is {name}. {{akjfhasd}}\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"Hello, my name is {}.\".format(name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "my_template = \"hello {}\"\n",
    "print('my template is:', my_template)\n",
    "my_template.format(name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "my_template = \"hello {}\"\n",
    "my_template.format(name)\n",
    "print('my template is', my_template)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Control flow with `while`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = True\n",
    "y = 0\n",
    "while x:\n",
    "    print(y)\n",
    "    y = y + 100\n",
    "    if y >= 1000:\n",
    "        x = False\n",
    "print('done')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "y = 0\n",
    "while True:\n",
    "    print(y)\n",
    "    y = y + 100\n",
    "    if y >= 1000:\n",
    "        break\n",
    "print('done')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "y = 0\n",
    "while y < 1000:\n",
    "    print(y)\n",
    "    y = y + 100\n",
    "print('done')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## `break` and `continue`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "y = 0\n",
    "while True:\n",
    "    print(y)\n",
    "    y = y + 100\n",
    "    if y >= 1000:\n",
    "        break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "y = 0\n",
    "while True:\n",
    "    print(y)    \n",
    "    y = y + 100\n",
    "    if y >= 1000:\n",
    "        break\n",
    "    if y < 400:\n",
    "        continue    \n",
    "    y = y + 10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "y"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## The `+=` (and `-=`, `*=`, .. ) operator."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "y += 20"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "y = y +20"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "y -= 20"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "y"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Running Python from the terminal\n",
    "\n",
    "We want to be able to run Python from the terminal. This is a low-level approach but will let us Python on all kinds of computers in all kinds of ways, not just interactively inside a Jupyter notebook.\n",
    "\n",
    "First, we need to be able to edit Python scripts. There are lots of programs that let us do that. I recommend [Visual Studio Code](https://code.visualstudio.com). To keep things simple, you can also use Jupyter Lab. In that case, you can click on the \"+\" in the tab list to open a Launcher tab, scroll down and create a \"Python File\". This will open an empty `untitled.py` file in the Jupyter lab editor. These `.py` files are *plain text* files which the `.py` extension tells the computer that this file should be Python source code. Let's create a file called `hello.py` which has the following contents.\n",
    "\n",
    "```python\n",
    "print(\"hello world\")\n",
    "```\n",
    "\n",
    "Now, let's run it in the terminal. Again, this can be done in multiple ways. One way is to use Jupyter Lab by clicking on the \"+\" in the tab list again. This type create a \"Terminal\". Another way is to use Anaconda Navigator as shown in this movie:\n",
    "\n",
    "<video controls src=\"CLI-python.mp4\" />"
   ]
  }
 ],
 "metadata": {
  "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.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
