{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "12b02b53",
   "metadata": {},
   "source": [
    "<h2>Conditionals - Control Flow</h2>\n",
    "<hr>\n",
    "<h3>Basic operators</h3>\n",
    "<ul>\n",
    "<li>> and < symbols are probably quite familiar to you.\n",
    "<li>>= denotes “greater than or equal to.”\n",
    "<li><= denotes “less than or equal to.”\n",
    "<li>== denotes “equals, though do notice the double equal sign! A single equal sign would assign a value. Double equal signs are used to compare variables.\n",
    "<li>!= denotes “not equal to.\n",
    "</ul>\n",
    "<br>\n",
    "Note that conditional statements compare a left-hand term to a right-hand term.\n",
    "<hr>\n",
    "<h3>Basic control flow</h3>\n",
    "<p>Lets try som basic operators in a flow where we compare integers</p>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "c913485b",
   "metadata": {},
   "outputs": [],
   "source": [
    "#Getting 2 integers and compare them\n",
    "x = int(input(\"What's x? \"))\n",
    "y = int(input(\"What's y? \"))\n",
    "\n",
    "if x < y:\n",
    "    print(\"x is less than y\")\n",
    "elif x > y:\n",
    "    print(\"x is greater than y\")\n",
    "else:\n",
    "    print(\"x is equal to y\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1fa8e0d6",
   "metadata": {},
   "source": [
    "<p>This is the basic a <code>if, elif, else</code> block. Of course one could use three if-clauses... but that would be just stupid, and inefficient</p>\n",
    "<h3>Using <code>and, or</code></h3>\n",
    "<p>We can combine conditions with som sweet old logic</p>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "12162899",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Nice temp!\n"
     ]
    }
   ],
   "source": [
    "#Checking the temeperature using and\n",
    "temp = int(input(\"What's the temperature? \"))\n",
    "if temp<30 and temp>20:\n",
    "    print(\"Nice temp!\")\n",
    "elif temp<20:\n",
    "    print(\"A bit chilly\")\n",
    "else:\n",
    "    print(\"To hot!!!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "07b46975",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Nice temp\n"
     ]
    }
   ],
   "source": [
    "#Checking the temperature using or, stupid, but possible\n",
    "temp = int(input(\"What's the temperature? \"))\n",
    "if temp<20 or temp>30:\n",
    "    if temp<20:\n",
    "        print(\"A bit chilly\")\n",
    "    else:\n",
    "        print(\"To hot!!!\")\n",
    "else:\n",
    "    print(\"Nice temp\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5cb130fc",
   "metadata": {},
   "source": [
    "<h3>Using <code>match</code> to be more efficient</h3>\n",
    "<p>When we have MANY cases we shouldn't use <code>if, elif, else</code></p>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "63db02ef",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Ahhh, nice!\n"
     ]
    }
   ],
   "source": [
    "#Checking the temeperature using match\n",
    "temp = int(input(\"What's the temperature? \"))\n",
    "match temp:\n",
    "    case t if t<0:\n",
    "        print(\"Freezing\")\n",
    "    case t if t<10:\n",
    "        print(\"Cold and Damp\")\n",
    "    case t if t<20:\n",
    "        print(\"Ok, but chilly\")\n",
    "    case t if t<30:\n",
    "        print(\"Ahhh, nice!\")\n",
    "    case _:\n",
    "        print(\"To hot!!!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "297b1036",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (487562835.py, line 4)",
     "output_type": "error",
     "traceback": [
      "\u001b[0;36m  Cell \u001b[0;32mIn[7], line 4\u001b[0;36m\u001b[0m\n\u001b[0;31m    case \"Harry\" or \"Hermione\" or \"Ron\":\u001b[0m\n\u001b[0m                 ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "#CS50 example for checking names also using or to get more compact code\n",
    "name = input(\"What's your name? \")\n",
    "match name: \n",
    "    case \"Harry\" | \"Hermione\" | \"Ron\":\n",
    "        print(\"Gryffindor\")\n",
    "    case \"Draco\":\n",
    "        print(\"Slytherin\")\n",
    "    case _:\n",
    "        print(\"Who?\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6d7be4c4",
   "metadata": {},
   "source": [
    "<h3>What about <code>float</code>?</h3>\n",
    "<p>When it comes to equalities, this can be quite tricky</p>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "78ddaa07",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n"
     ]
    }
   ],
   "source": [
    "#Simple compararision\n",
    "a = 0.1\n",
    "b = 0.2\n",
    "c = 0.3\n",
    "print(a+b==c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "a8173100",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "#Compararision using math.close\n",
    "import math\n",
    "a = 0.1\n",
    "b = 0.2\n",
    "c = 0.3\n",
    "print(math.isclose((a+b),c))\n",
    "print(math.isclose((a+b),c,rel_tol=1e-20))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "56794cf4",
   "metadata": {},
   "source": [
    "<h3>True and False in a odd way...</h3>\n",
    "<p>Some people might not know, but in Python, anything evaluating to 0 is <code>False</code>, everything else is <code>True</code></p>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "76ef14d7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Odd\n"
     ]
    }
   ],
   "source": [
    "#Really compact code\n",
    "def odd(x):\n",
    "    return x%2\n",
    "def main():\n",
    "    n = int(input(\"What's n? \"))\n",
    "    if odd(n):\n",
    "        print(\"Odd\")\n",
    "    else:\n",
    "        print(\"Even\")\n",
    "main()"
   ]
  }
 ],
 "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.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
