{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "# Python Conditionals"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "* Keywords\n",
    "    * ```if```\n",
    "    * ```elif```\n",
    "    * ```else```\n",
    "* Conditionals\n",
    "    * ==, !=, <, <=, >, >=, ```and, or, not```\n",
    "    * Keywords: ```True``` and ```False``` \n",
    "        \n",
    "**Examples**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = 55\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "if x < 10:                  # note the \":\"\n",
    "    print(\"x < 10\")\n",
    "    \n",
    "# do more code here"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x >= 3\n"
     ]
    }
   ],
   "source": [
    "if x < 3 :\n",
    "    print(\"x < 3\")\n",
    "else :                      # note the \"else\"\n",
    "    print(\"x >= 3\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "if x < 3 :\n",
    "    print(\"x < 3\")\n",
    "elif x < 4 :                # note the \"elif\"\n",
    "    print(\"x < 4\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "stuff\n",
      "stuff\n"
     ]
    }
   ],
   "source": [
    "x = 1\n",
    "if x < 3:\n",
    "    print(\"stuff\")\n",
    "if x < 5:\n",
    "    print(\"stuff\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x < 3\n"
     ]
    }
   ],
   "source": [
    "if x < 3 :\n",
    "    print(\"x < 3\")\n",
    "elif x < 4 :                \n",
    "    print(\"x < 4\")\n",
    "else:                       # else to cap it off \n",
    "    print(\"x >= 4\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x<3\n"
     ]
    }
   ],
   "source": [
    "if x < 3 :                                        \n",
    "    print(\"x<3\")\n",
    "elif x < 5 :               # multiple elif\n",
    "    print(\"x<5\")\n",
    "elif x > 5 :\n",
    "    print(\"x>5\")\n",
    "else :\n",
    "    print(\"something else\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "made it\n",
      "Doing some more stuff in here\n",
      "Can have multiple lines. \n",
      "Blank lines don't matter.\n",
      "Indent all lines inside the block at the same level\n"
     ]
    }
   ],
   "source": [
    "x = 5.5\n",
    "\n",
    "if (x > 3 and x < 6) or x <= 1 :\n",
    "    \n",
    "    print(\"made it\")\n",
    "    print(\"Doing some more stuff in here\")\n",
    "    \n",
    "    print(\"Can have multiple lines. \")\n",
    "    print(\"Blank lines don't matter.\")        # note the use of the single and double quotes\n",
    "    print(\"Indent all lines inside the block at the same level\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "**Can use variables or expressions in ```if``` statements**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "All is well with T, nice work.\n"
     ]
    }
   ],
   "source": [
    "Tmin = 298.15\n",
    "T    = 500\n",
    "\n",
    "if T < Tmin :\n",
    "    print(\"Warning, T<Tmin\")\n",
    "    \n",
    "if T > Tmin*10 :\n",
    "    print(\"Warning, T>10*Tmin\")\n",
    "    \n",
    "if Tmin <= T and T <= 10*Tmin:\n",
    "    print(\"All is well with T, nice work.\")\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Scope: variables declared outside the if statement can be seen inside.**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "T = 500\n"
     ]
    }
   ],
   "source": [
    "Tmin = 298.15\n",
    "T    = 500\n",
    "\n",
    "if T> Tmin:\n",
    "    print(\"T =\", T)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "**Here is a *condensed* ```if``` statement:**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x= 5.5 , T= 500.0\n"
     ]
    }
   ],
   "source": [
    "T = 298.15 if x<5.0 else 500.0              # single line if statement\n",
    "\n",
    "print(\"x=\", x, \", T=\", T)# note: sep='' changes the separation character"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "**Example**\n",
    "\n",
    "Take the the square root, and make the argument positive if its negative:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1.8369701987210297e-16+3j)\n",
      "y =  3.0\n"
     ]
    }
   ],
   "source": [
    "x = -9\n",
    "print(x**0.5)\n",
    "\n",
    "y = (x if x>=0 else -x)**0.5\n",
    "print(\"y = \", y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-9\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "print(x)\n",
    "torf = x>5.5\n",
    "if torf:\n",
    "    print(\"stuff\")\n",
    "print(torf)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "bool"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(torf)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "int"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "float"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = 3.3\n",
    "type(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "def sqrt(x):\n",
    "    if x >= 0:\n",
    "        print(x**0.5)\n",
    "        return True\n",
    "    else:\n",
    "        return False\n",
    "    \n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = 55\n",
    "\n",
    "if x==0:\n",
    "    a/(x+1E-16)\n",
    "else:\n",
    "    a/x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "float"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = 5.\n",
    "type(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "bool"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = True\n",
    "type(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "celltoolbar": "Slideshow",
  "kernelspec": {
   "display_name": "base",
   "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.14.6"
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
