{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "tags": []
   },
   "source": [
    "(pythonbasics)=\n",
    "# Python Basics\n",
    "\n",
    "**Last Revised September 26, 2022**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "(pythoninteractively)=\n",
    "## Using Python interactively\n",
    "\n",
    "For some tasks, Python can be used interactively, much like a scientific calculator (and later, like a graphing scientific calculator)\n",
    "and that is how we will work for this section.\n",
    "\n",
    "There are multiple ways of doing this, and I invite you to experiment with them all at some stage:\n",
    "- With the \"bare\" IPython console; this is available in many ways, including from the Anaconda Navigator by opening *Qt Console*\n",
    "\n",
    "- With Spyder, which can be accessed from within the Anaconda Navigator by opening *Spyder*; within that, there is an interactive Python console frame at bottom-right.\n",
    "\n",
    "- In Jupyter notebooks (like this one), which can be accessed from within the Anaconda Navigator by opening *JupyterLab*."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "I suggest learning one tool at a time, starting with this Jupyter notebook;\n",
    "however if you wish to also one of both of the alternatives above, go ahead."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Running some or all cells in a notebook\n",
    "\n",
    "A Jupyter notebook consists of a sequence of cells, and there are two types that we care about:\n",
    "- **Code** cells, which contain Python code\n",
    "- **Markdown** cells (like all the ones so far), which contain text and mathematics, using the\n",
    "[Markdown](https://www.markdownguide.org/) markup notation for text combined with LaTeX markup notation for mathematical formulas.\n",
    "\n",
    "Running a Code cell executes its Python code;\n",
    "running a Markdown cell formats its content for nicer output.\n",
    "\n",
    "There are several ways to run a cell:\n",
    "- The \"Play\" (triangle icon) button above\n",
    "- Typing \"Shift-Return\" (or \"Shift-Enter\")\n",
    "\n",
    "Both of these run the current cell and then move down to the next cell as \"current\".\n",
    "\n",
    "Also, the menu \"Run\" above has various options — hopefully self-explanatory, at least after some experimentation.\n",
    "\n",
    "To get a Markdown cell back from nicely formatted \"display mode\" back to \"edit mode\", double click in it."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "It will be convenient to see the value of an expression by simply making it the last line of a cell."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "2 + 2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To see several values from a cell, put them on that last line, separated by commas;\n",
    "the results will appear also separated by commas, and in parentheses:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(4, 5)"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "2 + 2, 6 - 1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "(As we will see in the next section, these are `tuples`; a very convenient way of grouping information.)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Numbers and basic arithmetic\n",
    "\n",
    "Python, like most programming languages distinguishes integers from floating point (\"real\") numbers;\n",
    "even \"1.0\" is different from '1', the decimal point showing that the former is a floating point number.\n",
    "\n",
    "One difference is that there is a maximum possible floating point number of about $10^{300}$,\n",
    "whereas Python integers can be arbitrarily large — see below with exponentiation."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Addition and subtraction with \"+\" and \"-\" are obvious, as seen above."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(4, 4)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "2 + 2, 6 - 2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "However, there are a few points to note with multiplication, exponentiation, and division."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Multiplication and exponentiation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Firstly, the usual multiplication sign not in the standard character setor on standard keyboards, so an asterisk \"\\*\" is used instead:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "2 * 3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Likewise exponentiation needs a special \"keyboard-friendly\" notation, and it is a dobuble asterisk \"\\*\\*\" (not \"^\"):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "2**3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Numbers with exponents can be expressed using this exponential notation, but note how Python outputs them:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5.3e+21"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "5.3*10**21"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "7e-08"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "7*10**-8"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This \"e\" notation is the standard way to describe numbers with exponents, and you can input them that way too."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "When printing numbers, Python decides if the number is big enough or small enough to be worth printing with an exponent:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2e-10, 3e-05, 0.0003, 50000.0, 10000000000, 6000000000000000.0, 6e+16)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "2e-10, 3E-5, 3e-4, 5e+4, 10000000000, 6e15, 6e16"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Aside:** note that either \"e\" or \"E\" can be used in input of exponents.\n",
    "However in most contexts, Python is case sensitive; we will see an example soon."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Division"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Division is normaly denoted by \"/\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2.0"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "6/3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.6666666666666667"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "5/3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "but there are a few things to note with integers.\n",
    "\n",
    "Firstly as you see above, diving two integers always given a floating point number result, not an integer: note the decimal point in \"2.0\".\n",
    "\n",
    "To do integer division, use \"//\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "6//3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "5//3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "and to get the remainder, use \"%\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "6%3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "5%3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "(complexnumbers)=\n",
    "### Complex numbers\n",
    "\n",
    "Python uses `j` for the square root of -1 rather than `i`, and complex numbers are writen as `a + bj` or just  `bj` for purely imaginary numbers. (Here 'a' and 'b mut be literal numbers, nt mnakem of variables.)\n",
    "\n",
    "The coeffcient `b` in the imaginary part is always needed, even if it is 1."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((2+2j), 8j, 1j, (-1+0j))"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "2 + 2j, (2+2j)**2, 1j, 1j**2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "but"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'j' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Input \u001b[0;32mIn [16]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mj\u001b[49m\n",
      "\u001b[0;31mNameError\u001b[0m: name 'j' is not defined"
     ]
    }
   ],
   "source": [
    "j"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "(boolean)=\n",
    "## Boolean values (True-False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "These are `True` and `False`, capitalized.\n",
    "\n",
    "Many other options are allowed, like using `0` for `False` and `1` for `True`, but for the sake of redability, I discourage that.\n",
    "\n",
    "The basic logical operations are done wth the usual English words: \"and\", \"or\" and \"not\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(True, False, False)"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "True and True, True and False, False and False "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(True, True, False)"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "True or True, True or False, False or False "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Both \"and\" and \"or\" are use lazy or \"short-circuiting\" evaluation:\n",
    "if the T/F at left determines the overall truth value answer (it is `False` with `and`, `True` with `or`) then the term at right is not evaluated.\n",
    "\n",
    "For example, the following avoids division by zero (the syntax will be explained later, but hopefuly it is mostly clear.)\n",
    "\n",
    "    if q != 0 and -1 < p/q < 1:\n",
    "        print(f\"{p}/{q} is a proper fraction\")\n",
    "    else:\n",
    "        print(f\"{p}/{q} is not a proper fraction\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "(comparisons)=\n",
    "## Comparisons\n",
    "\n",
    "Comparisons of numbers and tests for equality exist,\n",
    "with two catches:\n",
    "- There are no keyboard symbols for $\\leq$, $\\geq$ or $\\neq$, so `<=`, `>=` and `!=` are used\n",
    "- Equality is indicated with a *double* equals sign `==` because a single equals sign has another job,\n",
    "as seen below under {ref}`namingdisplaying`.\n",
    "\n",
    "So in all:\n",
    "\n",
    "    < <= == != >= >\n",
    "\n",
    "One convenient difference from most programming languages is that comparisons can be chained, as seen in the example above:\n",
    "\n",
    "    -1 < p/q < 1\n",
    "\n",
    "is equivalant to\n",
    "\n",
    "    -1 < p/q and p/q < 1\n",
    "\n",
    "but is both more readable and more efficient, because the middle term is only evaluated once.\n",
    "\n",
    "Like `and` and `or`, this is short-circuiting."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Chaining can even be done in cases where usual mathematical style forbids, with reversing of the direction of the inequalities:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "2 < 4 > 3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "(charactersstrings)=\n",
    "## Character strings"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Chunks of text can be described by surrounding them either with double quotes (\"real quotation marks\") or so-called 'single quotes', (which are actualy apostrophes)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"I recommend using 'double quote' characters, except perhaps when quote marks must appear within a string, like here.\""
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"I recommend using 'double quote' characters, except perhaps when quote marks must appear within a string, like here.\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "However, using apostrophes (single right quotes) is also allowed by Python, perhaps because they are slightly easier to type."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "(concatenationduplication)=\n",
    "### String concatenation and duplication\n",
    "\n",
    "To concatenate two strings, \"add\" them with `+`.\n",
    "\n",
    "Also, consistent with that, making multiple copies of a string is done by \"mutiplication\", with `*`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello world.\n",
      "Hello Hello Hello \n"
     ]
    }
   ],
   "source": [
    "greeting = \"Hello\"\n",
    "audience = \"world\"\n",
    "sentence = greeting + \" \" + audience + \".\"\n",
    "print(sentence)\n",
    "print(3*(greeting+' '))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "(namingdisplaying)=\n",
    "## Naming quantities, and displaying information with `print`\n",
    "\n",
    "It often helps to give names to quantities, which is done with assignment statements.\n",
    "For example, to solve for $x$ in the very simple equation $ax + b = 0$ for given values of $a$ and $b$, let us name all three quantities:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 3\n",
    "b = 7\n",
    "x = -b/a"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "(Note: this is why testing for equality uses `==` instead of `=`.)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Running the above cell just computes the three values with no output.\n",
    "To see the values, a more flexible alternative to the \"last line of a cell\" method is the function `print`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3 7 -2.3333333333333335\n"
     ]
    }
   ],
   "source": [
    "print(a, b, x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "That output does not explain things very clearly;\n",
    "we can add explanatory text to the printed results in several ways.\n",
    "Most basically:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "For a = 3 and b = 7 the solution of ax + b = 0 is -2.3333333333333335\n"
     ]
    }
   ],
   "source": [
    "print(\"For a =\", a, \"and b =\", b, \"the solution of ax + b = 0 is\", x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The above prints six items — three text strings (each quoted), three numbers — with the input items separated by commas.\n",
    "\n",
    "There is an alternative notation for such output, called \"f-strings\", which use braces to specify that the value of a variable be inserted into a string of text to display:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "For a = 3 and b = 7 the solution of ax + b = 0 is -2.3333333333333335\n"
     ]
    }
   ],
   "source": [
    "print(f\"For a = {a} and b = {b} the solution of ax + b = 0 is {x}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In this example each pair of braces inserts the value of a variable;\n",
    "one can instead put an expression in braces to have it evaluated and that value displayed.\n",
    "So in fact we could skip the variable $x$ entirely:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "For a = 3 and b = 7 the solution of ax + b = 0 is -2.3333333333333335\n"
     ]
    }
   ],
   "source": [
    "print(f\"For a = {a} and b = {b} the solution of ax + b = 0 is {-b/a}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can also display the equation more directly:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The solution of 3 x + 7 = 0 is -2.3333333333333335\n"
     ]
    }
   ],
   "source": [
    "print(f\"The solution of {a} x + {b} = 0 is {-b/a}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "A final shortcut with `print`: if an expression in braces ends with `=`, both the expression and its value are displayed:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "For a=3 and b=7 the solution of ax + b = 0 is -b/a=-2.3333333333333335\n"
     ]
    }
   ],
   "source": [
    "print(f\"For {a=} and {b=} the solution of ax + b = 0 is {-b/a=}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "(module-math)=\n",
    "## Some mathematical functions and constants: module <code>math</code>\n",
    "\n",
    "Python includes many **modules** and **packages** that add useful defintions of functions, constants and such.\n",
    "For us, the most fundamental is `math`, which is a standard part of Python.\n",
    "\n",
    "**Aside:** we will soon learn about another package `numpy` and then mostly use that instead of `math`.\n",
    "I mention `math` for now because it is a core part of Python whereas `numpy` is a separate \"add-on\",\n",
    "and you should be aware of `math` if only because many references on doing mathematical stuff with Python will refer to it.\n",
    "\n",
    "We can access specific items with a `from` ... `import` command; to start with, two variables containing famous values:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "from math import pi, e"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "pi is approximately 3.141592653589793\n"
     ]
    }
   ],
   "source": [
    "print(f'pi is approximately {pi}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "e is approximately 2.718281828459045\n"
     ]
    }
   ],
   "source": [
    "print(f'e is approximately {e}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Each is accurate to about 16 significant digits; that is the precision of the 64-bit number system standard in most modern computers."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Names (like \"e\" and \"pi\") are case-sensitive\n",
    "\n",
    "We can now return to the comment above about case sensitivity.\n",
    "Compare the results of the following two input lines:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "e = 2.718281828459045\n"
     ]
    }
   ],
   "source": [
    "print(f'e = {e}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'E' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Input \u001b[0;32mIn [33]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mE = \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mE\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m'\u001b[39m)\n",
      "\u001b[0;31mNameError\u001b[0m: name 'E' is not defined"
     ]
    }
   ],
   "source": [
    "print(f'E = {E}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Some <code>math</code> functions\n",
    "Module <code>math</code> also provides a lot of familiar mathematical functions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "from math import sin, cos, cosh"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "which can be used like this:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.7071067811865475"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sin(pi/4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-1.0"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cos(pi) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.0"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cosh(0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Notes\n",
    "- **All** Python functions need parentheses around their arguments; no lazy shortcuts with trig. functions and such.\n",
    "- Trig. functions use radians, not degrees."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Importing all of a module\n",
    "\n",
    "With the command"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [],
   "source": [
    "import math"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "we get access to many functions such as `tan`, but need to address them by fully qualified name, like `math.tan`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tan(pi/4) = 0.9999999999999999\n"
     ]
    }
   ],
   "source": [
    "print(f\"tan(pi/4) = {math.tan(pi/4)}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If we had not already imported `pi` above, its full name `math.pi` would also be needed, as in:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tan(pi/4) = 0.9999999999999999\n"
     ]
    }
   ],
   "source": [
    "print(f\"tan(pi/4) = {math.tan(math.pi/4)}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Aside:** The imperfection of computer arithmetic becomes clear:\n",
    "the exact value is 1 of course.\n",
    "However, the precision of about 16 decimal places is far greater than for any experimental measurement (so far),\n",
    "so this is usually not a problem in practice."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Wild imports: avoid, usually\n",
    "\n",
    "There is another way of importing all the contents of a module so that they are available on a \"first name basis\", called a **wild import**; it is like the single-item imports above with `from math import ...` but no with an asterisk (often called the *wild-card character* in this context) as the name of the items to import:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [],
   "source": [
    "from math import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2.302585092994046"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "log(10)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Wild imports can be convenient when working interactively (using Python like a scientific calculator) through saving a bit of typing, but I strongly recommend using the previous more specific approaches when creating files and programs.\n",
    "\n",
    "One reason is that it is then unambiguous where a given item came from, even if multiple `import` commands are used.\n",
    "For example, we will see later than there are both `math.cos` and `numpy.cos`, and they behave differently in some situations.\n",
    "\n",
    "Another reason for explicit imports is for internal efficiency in storage and execution time; wild imports can potentially load thousands of items from a large module even if only a few are used."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Logarithmic functions\n",
    "\n",
    "We just saw that there is a function `log` in module `math`, but which base does it use?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2.302585092994046"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "log(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.0"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "log(e)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Evaluating these two expressions reveals that \"log()\" is the *natural logarithm*, base $e$;  \n",
    "what mathematicians usually call \"$\\ln$\".\n",
    "\n",
    "For the base ten version \"$\\log_{10}$\" (sometimes just called \"$\\log$\") use `log10`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2.0"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "log10(100)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Powers and roots\n",
    "A square root can of course be computed using the 1/2 power, as with"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4.0"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "16**0.5"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "but this function is important enough to have a named form provided by module <code>math</code>:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.4142135623730951"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sqrt(2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Notes on organization and presentation of course work\n",
    "\n",
    "If you are doing the exercises in these notes for a course,\n",
    "record both your input and the resulting output, in a document that you will submit for feedback and then grading, suc as a moified copy of this notebook.\n",
    "\n",
    "I also encourage you to make notes of other things that you learn, beyond what must be submitted — they will help later in the course.\n",
    "\n",
    "Also, every document that you submit, hand-written or electronic, should start with:\n",
    "- A *title*,\n",
    "- *your name* (possibly also with your email address), and\n",
    "- the *date*, of the **current version**.\n",
    "\n",
    "(See the {ref}`Title Cell <pythonbasics>` above for example.)"
   ]
  }
 ],
 "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.9.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
