{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Basics\n",
    "\n",
    "**Prerequisites**\n",
    "\n",
    "- [Getting Started]\n",
    "\n",
    "**Outcomes**\n",
    "\n",
    "- Programming concepts  \n",
    "  \n",
    "  - Understand variable assignment  \n",
    "  - Know what a function is and how to figure out what it does  \n",
    "  - Be able to use tab completion  \n",
    "  \n",
    "- Numbers in Python  \n",
    "  \n",
    "  - Understand how Python represents numbers  \n",
    "  - Know the distinction between `int` and `float`  \n",
    "  - Be familiar with various binary operators for numbers  \n",
    "  - Introduction to the `math` library  \n",
    "  \n",
    "- Text (strings) in Python  \n",
    "  \n",
    "  - Understand what a string is and when it is useful  \n",
    "  - Learn some of the methods associated with strings  \n",
    "  - Combining strings and output  \n",
    "  \n",
    "- True and False (booleans) in Python  \n",
    "  \n",
    "  - Understand what a boolean is  \n",
    "  - Become familiar with all binary operators that return booleans  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Outline\n",
    "\n",
    "- [Basics](#Basics)  \n",
    "  - [First Steps](#First-Steps)  \n",
    "  - [Functions](#Functions)  \n",
    "  - [Objects and Types](#Objects-and-Types)  \n",
    "  - [Modules](#Modules)  \n",
    "  - [Good Code Habits](#Good-Code-Habits)  \n",
    "  - [Numbers](#Numbers)  \n",
    "  - [Strings](#Strings)  \n",
    "  - [Booleans](#Booleans)  \n",
    "  - [Exercises](#Exercises)  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## First Steps\n",
    "\n",
    "We are ready to begin writing code!\n",
    "\n",
    "In this section, we will teach you some basic concepts of programming\n",
    "and where to search for help."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Variable Assignment\n",
    "\n",
    "The first thing we will learn is the idea of *variable assignment*.\n",
    "\n",
    "Variable assignment associates a value to a variable.\n",
    "\n",
    "Below, we assign the value “Hello World” to the variable `x`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "x = \"Hello World\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Once we have assigned a value to a variable, Python will remember this\n",
    "variable as long as the *current* session of Python is still running.\n",
    "\n",
    "Notice how writing `x` into the prompt below outputs the value\n",
    "“Hello World”."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "hide-output": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Hello World'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "However, Python returns an error if we ask it about variables that have not yet\n",
    "been created."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "str"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "# uncomment (delete the # and the space) the line below and run\n",
    "# y"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "It is also useful to understand the order in which operations happen.\n",
    "\n",
    "First, the right side of the equal sign is computed.\n",
    "\n",
    "Then, that computed value is stored as the variable to the left of the\n",
    "equal sign.\n",
    "\n",
    "\n",
    "<a id='exercise-0'></a>\n",
    "> See exercise 1\n",
    "\n",
    "Keep in mind that the variable binds a name to something stored in memory.\n",
    "\n",
    "The name can even be bound to a value of a completely different type."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "hide-output": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "something else\n"
     ]
    }
   ],
   "source": [
    "x = 2\n",
    "print(x)\n",
    "x = \"something else\"\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Code Comments\n",
    "\n",
    "Comments are short notes that you leave for yourself and for others who read your\n",
    "code.\n",
    "\n",
    "They should be used to explain what the code does.\n",
    "\n",
    "A comment is made with the `#`. Python ignores everything in a line that follows a `#`.\n",
    "\n",
    "Let’s practice making some comments."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "hide-output": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "i = 1  # Assign the value 1 to variable i\n",
    "j = 2  # Assign the value 2 to variable j\n",
    "\n",
    "# We add i and j below this line\n",
    "i + j"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Functions\n",
    "\n",
    "Functions are processes that take an input (or inputs) and produce an output.\n",
    "\n",
    "If we had a function called `f` that took two arguments `x` and\n",
    "`y`, we would write `f(x, y)` to use the function.\n",
    "\n",
    "For example, the function `print` simply prints whatever it is given.\n",
    "Recall the variable we created called `x`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "hide-output": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "something else\n"
     ]
    }
   ],
   "source": [
    "print(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Getting Help\n",
    "\n",
    "We can figure out what a function does by asking for help.\n",
    "\n",
    "In Jupyter notebooks, this is done by placing a `?` after the function\n",
    "name (without using parenthesis) and evaluating the cell.\n",
    "\n",
    "For example, we can ask for help on the print function by writing\n",
    "`print?`.\n",
    "\n",
    "Depending on how you launched Jupyter, this will either launch\n",
    "\n",
    "- JupyterLab: display the help in text below the cell.  \n",
    "- Classic Jupyter Notebooks: display a new panel at the bottom of your\n",
    "  screen.  You can exit this panel by hitting the escape key or clicking the x at\n",
    "  the top right of the panel.  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "print?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "# print? # remove the comment and <Shift-Enter>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "<a id='exercise-1'></a>\n",
    "> See exercise 2 \n",
    "\n",
    "\n",
    "JupyterLab also has a “Contextual Help” (previously called “Inspector”) window.  To use,\n",
    "\n",
    "- Go to the Commands and choose Contextual Help (or Inspector), or select `<Ctrl-I>` (`<Cmd-I>` for OSX users).  \n",
    "- Drag the new inspector pain to dock in the screen next to your code.  \n",
    "- Then, type `print` or any other function\n",
    "into a cell and see the help.  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "# len? # remove the comment and <Shift-Enter>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "len?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<built-in function len>\n"
     ]
    }
   ],
   "source": [
    "print(len)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "\n",
    "\n",
    "We will learn much more about functions, including how to write our own, in a\n",
    "future lecture."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Objects and Types\n",
    "\n",
    "Everything in Python is an *object*.\n",
    "\n",
    "Objects are “things” that contain 1) data and 2) functions that can operate on\n",
    "the data.\n",
    "\n",
    "Sometimes we refer to the functions inside an object as *methods*.\n",
    "\n",
    "We can investigate what data is inside an object and which methods\n",
    "it supports by typing `.` after that particular variable, then\n",
    "hitting `TAB`.\n",
    "\n",
    "It should then list data and method names to the right of the\n",
    "variable name like this:\n",
    "\n",
    "![https://datascience.quantecon.org/assets/_static/python_fundamentals/introspection.png](https://datascience.quantecon.org/assets/_static/python_fundamentals/introspection.png)  \n",
    "You can scroll through this list by using the up and down arrows.\n",
    "\n",
    "We often refer to this as “tab completion” or “introspection”.\n",
    "\n",
    "Let’s do this together below. Keep going down until you find the method\n",
    "`split`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "# Type a period after `x` and then press TAB.\n",
    "x"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Once you have found the method `split`, you can use the method by adding\n",
    "parenthesis after it.\n",
    "\n",
    "Let’s call the `split` method, which doesn’t have any other required\n",
    "parameters. (Quiz: how would we check that?)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "hide-output": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['something', 'else']"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x.split()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We often want to identify what kind of object some value is–\n",
    "called its “type”.\n",
    "\n",
    "A “type” is an abstraction which defines a set of behavior for any\n",
    "“instance” of that type i.e. `2.0` and `3.0` are instances\n",
    "of `float`, where `float` has a set of particular common behaviors.\n",
    "\n",
    "In particular, the type determines:\n",
    "\n",
    "- the available data for any “instance” of the type (where each\n",
    "  instance may have different values of the data).  \n",
    "- the methods that can be applied on the object and its data.  \n",
    "\n",
    "\n",
    "We can figure this out by using the `type` function.\n",
    "\n",
    "The `type` function takes a single argument and outputs the type of\n",
    "that argument."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "hide-output": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "int"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "hide-output": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "str"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(\"Hello World\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "hide-output": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "list"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type([1, 2, 3])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We will learn more about each of these types (and others!) and how to use them\n",
    "soon, so stay tuned!\n",
    "\n",
    "\n",
    "<a id='modules'></a>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Modules\n",
    "\n",
    "Python takes a modular approach to tools.\n",
    "\n",
    "By this we mean that sets of related tools are bundled together into *packages*.\n",
    "(You may also hear the term modules to describe the same thing.)\n",
    "\n",
    "For example:\n",
    "\n",
    "- `pandas` is a package that implements the tools necessary to do\n",
    "  scalable data analysis.  \n",
    "- `matplotlib` is a package that implements visualization tools.  \n",
    "- `requests` and `urllib` are packages that allow Python to\n",
    "  interface with the internet.  \n",
    "\n",
    "\n",
    "As we move further into the class, being able to\n",
    "access these packages will become very important.\n",
    "\n",
    "We can bring a package’s functionality into our current Python session\n",
    "by writing"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "hide-output": false
   },
   "source": [
    "```python\n",
    "import package\n",
    "```\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Once we have done this, any function or object from that package can\n",
    "be accessed by using `package.name`.\n",
    "\n",
    "Here’s an example."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "hide-output": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'3.9.12 (main, Apr  4 2022, 05:22:27) [MSC v.1916 64 bit (AMD64)]'"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import sys   # for dealing with your computer's system\n",
    "sys.version  # information about the Python version in use"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "<a id='exercise-2'></a>\n",
    "> See exercise 3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Module Aliases\n",
    "\n",
    "Some packages have long names (see `matplotlib`, for example) which\n",
    "makes accessing the package functionality somewhat inconvenient.\n",
    "\n",
    "To ease this burden, Python allows us to give aliases or “nicknames” to packages.\n",
    "\n",
    "For example we can write:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "hide-output": false
   },
   "source": [
    "```python\n",
    "import package as p\n",
    "```\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This statement allows us to access the packages functionality as\n",
    "`p.function_name` rather than `package.function_name`.\n",
    "\n",
    "Some common aliases for packages are\n",
    "\n",
    "- `import pandas as pd`  \n",
    "- `import numpy as np`  \n",
    "- `import matplotlib as mpl`  \n",
    "- `import datetime as dt`  \n",
    "\n",
    "\n",
    "While you *can* choose any name for an alias, we suggest that you stick\n",
    "to the common ones.\n",
    "\n",
    "You will learn what these common ones are over time.\n",
    "\n",
    "\n",
    "<a id='exercise-3'></a>\n",
    "> See exercise 4 "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Good Code Habits\n",
    "\n",
    "A common saying in the software engineering world is:\n",
    "\n",
    "> Always code as if the guy who ends up maintaining your code will be\n",
    "a violent psychopath who knows where you live. Code for readability.\n",
    "\n",
    "\n",
    "This might be a dramatic take, but the most important feature of your code\n",
    "after correctness is readability.\n",
    "\n",
    "We encourage you to do **everything** in your power to make your code as readable as possible.\n",
    "\n",
    "Here are some suggestions for how to do so:\n",
    "\n",
    "- Comment frequently. Leaving short notes not only will help others who\n",
    "  use your code, but will also help you interpret your code\n",
    "  after some time has passed.  \n",
    "- **Anytime** you use a comma, place a space immediately afterwards.  \n",
    "- Whitespace is your friend. Don’t write line after line of code – use\n",
    "  blank lines to break it up.  \n",
    "- Don’t let your lines run too long. Some people reading your code will\n",
    "  be on a laptop, so you want to ensure that they don’t need to scroll horizontally\n",
    "  and right to read your code. We recommend no more than 80 characters per line.  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Numbers\n",
    "\n",
    "Python has two types of numbers.\n",
    "\n",
    "1. Integer (`int`): These can only take the values of the integers\n",
    "  i.e. $ \\{\\dots, -2, -1, 0, 1, 2, \\dots\\} $  \n",
    "1. Floating Point Number (`float`): Think of these as any real number\n",
    "  such as $ 1.0 $, $ 3.1415 $, or $ -100.022358923223 $…  \n",
    "\n",
    "\n",
    "The easiest way to differentiate these types of numbers is to find a decimal place\n",
    "after the number.\n",
    "\n",
    "A float will have a decimal place, but an integer will not.\n",
    "\n",
    "Below, we assign integers to the variables `xi` and `zi` and assign\n",
    "floating point numbers to the variables `xf` and `zf`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "xi = 1\n",
    "xf = 1.0\n",
    "zi = 123\n",
    "zf = 1230.5  # Notice -- There are no commas!\n",
    "zf2 = 1_230.5  # If needed , we use `_` to separate numbers for readability"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1230.5\n"
     ]
    }
   ],
   "source": [
    "print(zf2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "<a id='exercise-4'></a>\n",
    "> See exercise 5 "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Python as a Calculator\n",
    "\n",
    "You can use Python to perform mathematical calculations."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {
    "hide-output": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a + b is 6\n",
      "a - b is 2\n",
      "a * b is 8\n",
      "a / b is 2.0\n",
      "a ** b is 16\n",
      "a ^ b is 6\n"
     ]
    }
   ],
   "source": [
    "a = 4\n",
    "b = 2\n",
    "\n",
    "print(\"a + b is\", a + b)\n",
    "print(\"a - b is\", a - b)\n",
    "print(\"a * b is\", a * b)\n",
    "print(\"a / b is\", a / b) #ให้ผลออกมาเป็นทศนิยม\n",
    "print(\"a ** b is\", a**b) #เลขยกกำลัง\n",
    "print(\"a ^ b is\", a^b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You likely could have guessed all except the last two.\n",
    "\n",
    "**Warning**: Python uses `**`, not `^`, for exponentiation (raising a number\n",
    "to a power)!\n",
    "\n",
    "Notice also that above `+`, `-` and `**` all returned an integer\n",
    "type, but `/` converted the result to a float.\n",
    "\n",
    "When possible, operations between integers return an integer type.\n",
    "\n",
    "All operations involving a float will result in a float."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "hide-output": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a + b is 6.0\n",
      "a - b is 2.0\n",
      "a * b is 8.0\n",
      "a / b is 2.0\n",
      "a ** b is 16.0\n"
     ]
    }
   ],
   "source": [
    "a = 4\n",
    "b = 2.0\n",
    "\n",
    "print(\"a + b is\", a + b)\n",
    "print(\"a - b is\", a - b)\n",
    "print(\"a * b is\", a * b)\n",
    "print(\"a / b is\", a / b)\n",
    "print(\"a ** b is\", a**b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can also chain together operations.\n",
    "\n",
    "When doing this, Python follows the standard [order of operations](https://en.wikipedia.org/wiki/Order_of_operations) — parenthesis, exponents,\n",
    "multiplication and division, followed by addition and subtraction.\n",
    "\n",
    "For example,"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "x = 2.0\n",
    "y = 3.0\n",
    "z1 = x + y * x\n",
    "z2 = (x + y) * x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8.0"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "z1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10.0"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "z2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "What do you think `z1` is?\n",
    "\n",
    "How about `z2`?\n",
    "\n",
    "\n",
    "<a id='exercise-5'></a>\n",
    "> See exercise 6 "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Other Math Functions\n",
    "\n",
    "We often want to use other math functions on our numbers. Let’s try to\n",
    "calculate sin(2.5)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {
    "hide-output": false
   },
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'sin' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Input \u001b[1;32mIn [40]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[43msin\u001b[49m(\u001b[38;5;241m2.5\u001b[39m)\n",
      "\u001b[1;31mNameError\u001b[0m: name 'sin' is not defined"
     ]
    }
   ],
   "source": [
    "sin(2.5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "As seen above, Python complains that `sin` isn’t defined.\n",
    "\n",
    "The problem here is that the `sin` function – as well as many other\n",
    "standard math functions – are contained in the `math` package.\n",
    "\n",
    "We must begin by importing the math package."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "import math"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now, we can use `math.[TAB]` to see what functions are available to us."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "# uncomment, add a period (`.`) and pres TAB\n",
    "# math"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {
    "hide-output": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.5984721441039564"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# found math.sin!\n",
    "math.sin(2.5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "<a id='exercise-6'></a>\n",
    "> See exercise 7 "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Floor/Modulus Division Operators\n",
    "\n",
    "You are less likely to run into the following operators, but understanding\n",
    "that they exist is useful.\n",
    "\n",
    "For two numbers assigned to the variables `x` and `y`,\n",
    "\n",
    "- Floor division: `x // y`  \n",
    "- Modulus division: `x % y`  \n",
    "\n",
    "\n",
    "Remember when you first learned how to do division and you were asked to talk about the quotient\n",
    "and the remainder?\n",
    "\n",
    "That’s what these operators correspond to…\n",
    "\n",
    "Floor division returns the number of times the divisor goes into the dividend (the quotient)\n",
    "and modulus division returns the remainder.\n",
    "\n",
    "An example would be 37 divided by 7:\n",
    "\n",
    "- Floor division would return 5 (7 * 5 = 35)  \n",
    "- Modulus division would return 2 (2 + 35 = 37)  \n",
    "\n",
    "\n",
    "Try it!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {
    "hide-output": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "37 // 7"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {
    "hide-output": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "37 % 7"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Strings\n",
    "\n",
    "Textual information is stored in a data type called a string.\n",
    "\n",
    "To denote that you would like something to be stored as a string, you place it\n",
    "inside of quotation marks.\n",
    "\n",
    "For example,"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "hide-output": false
   },
   "source": [
    "```python\n",
    "\"this is a string\"  # Notice the quotation marks\n",
    "'this is a string'  # Notice the quotation marks\n",
    "this is not a string  # No quotation marks\n",
    "```\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can use either `\"` or `'` to create a string. Just make sure\n",
    "that you start and end the string with the same one!\n",
    "\n",
    "Notice that if we ask Python to tell us the type of a string, it abbreviates\n",
    "its answer to `str`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {
    "hide-output": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "str"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(\"this is a string\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "<a id='exercise-7'></a>\n",
    "> See exercise 8 "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### String Operations\n",
    "\n",
    "Some of the arithmetic operators we saw in the numbers lecture also work\n",
    "on strings:\n",
    "\n",
    "- Put two strings together: `x + y`.  \n",
    "- Repeat the string `x` a total of `n` times: `n * x` (or `x * n`).  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "x = \"Hello\"\n",
    "y = \"World\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {
    "hide-output": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'HelloWorld'"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x + y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {
    "hide-output": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'HelloHelloHello'"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "3 * x"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "What happens if we try `*` with two strings, or `-` or `/`?\n",
    "\n",
    "The best way to find out is to try it!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {
    "hide-output": false
   },
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "can't multiply sequence by non-int of type 'str'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Input \u001b[1;32mIn [49]\u001b[0m, in \u001b[0;36m<cell line: 3>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      1\u001b[0m a \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m1\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m      2\u001b[0m b \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m2\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m----> 3\u001b[0m \u001b[43ma\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43m \u001b[49m\u001b[43mb\u001b[49m\n",
      "\u001b[1;31mTypeError\u001b[0m: can't multiply sequence by non-int of type 'str'"
     ]
    }
   ],
   "source": [
    "a = \"1\"\n",
    "b = \"2\"\n",
    "a * b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {
    "hide-output": false
   },
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "unsupported operand type(s) for -: 'str' and 'str'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Input \u001b[1;32mIn [50]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[43ma\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m-\u001b[39;49m\u001b[43m \u001b[49m\u001b[43mb\u001b[49m\n",
      "\u001b[1;31mTypeError\u001b[0m: unsupported operand type(s) for -: 'str' and 'str'"
     ]
    }
   ],
   "source": [
    "a - b"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "<a id='exercise-8'></a>\n",
    "> See exercise 9 "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### String Methods\n",
    "\n",
    "We can use many *methods* to manipulate strings.\n",
    "\n",
    "We will not be able to cover all of them here, but let’s take a look at\n",
    "some of the most useful ones."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {
    "hide-output": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Hello'"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {
    "hide-output": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'hello'"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x.lower()  # Makes all letters lower case"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {
    "hide-output": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'HELLO'"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x.upper()  # Makes all letters upper case"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {
    "hide-output": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x.count(\"l\")  # Counts number of a particular string"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {
    "hide-output": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x.count(\"ll\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(x) #count chracter"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "<a id='exercise-9'></a>\n",
    "> See exercise 10 \n",
    "\n",
    "\n",
    "\n",
    "<a id='exercise-10'></a>\n",
    "> See exercise 11 "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### String Formatting\n",
    "\n",
    "Sometimes we’d like to reuse some portion of a string repeatedly, but\n",
    "still make some relatively small changes at each usage.\n",
    "\n",
    "We can do this with *string formatting*, which done by using `{}` as a\n",
    "*placeholder* where we’d like to change the string, with a variable name\n",
    "or expression.\n",
    "\n",
    "Let’s look at an example."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {
    "hide-output": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Vietnam had $223.9 billion GDP in 2017\n"
     ]
    }
   ],
   "source": [
    "country = \"Vietnam\"\n",
    "GDP = 223.9\n",
    "year = 2017\n",
    "my_string = f\"{country} had ${GDP} billion GDP in {year}\"\n",
    "print(my_string)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Rather than just substituting a variable name, you can use a calculation\n",
    "or expression."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {
    "hide-output": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5**2 = 25\n"
     ]
    }
   ],
   "source": [
    "print(f\"{5}**2 = {5**2}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Or, using our previous example"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {
    "hide-output": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Vietnam had $223900000.0 GDP in 2017\n"
     ]
    }
   ],
   "source": [
    "my_string = f\"{country} had ${GDP * 1_000_000} GDP in {year}\"\n",
    "print(my_string)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In these cases, the `f` in front of the string causes Python interpolate\n",
    "any valid expression within the `{}` braces.\n",
    "\n",
    "\n",
    "<a id='exercise-11'></a>\n",
    "> See exercise 12 \n",
    "\n",
    "\n",
    "Alternatively, to reuse a formatted string, you can call the `format` method (noting that you do **not** put `f` in front)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {
    "hide-output": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Vietnam had $223.9 billion in 2017'"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gdp_string = \"{country} had ${GDP} billion in {year}\"\n",
    "\n",
    "gdp_string.format(country = \"Vietnam\", GDP = 223.9, year = 2017)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "<a id='exercise-12'></a>\n",
    "> See exercise 13 \n",
    "\n",
    "\n",
    "\n",
    "<a id='exercise-13'></a>\n",
    "> See exercise 14 \n",
    "\n",
    "For more information on what you can do with string formatting (there is *a lot*\n",
    "that can be done…), see the [official Python documentation](https://docs.python.org/3.6/library/string.html) on the subject."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Booleans\n",
    "\n",
    "A boolean is a type that denotes true or false.\n",
    "\n",
    "As you will soon see in the [control flow chapter](https://datascience.quantecon.org/control_flow.html), using\n",
    "boolean values allows you to perform or skip operations depending on whether or\n",
    "not a condition is met.\n",
    "\n",
    "Let’s start by creating some booleans and looking at them."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {
    "hide-output": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "bool"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = True\n",
    "y = False\n",
    "\n",
    "type(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {
    "hide-output": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {
    "hide-output": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Comparison Operators\n",
    "\n",
    "Rather than directly write `True` or `False`, you will usually\n",
    "create booleans by making a comparison.\n",
    "\n",
    "For example, you might want to evaluate whether the price of a particular asset\n",
    "is greater than or less than some price.\n",
    "\n",
    "For two variables `x` and `y`, we can do the following comparisons:\n",
    "\n",
    "- Greater than: `x > y`  \n",
    "- Less than: `x < y`  \n",
    "- Equal to: `==`  \n",
    "- Greater than or equal to: `x >= y`  \n",
    "- Less than or equal to: `x <= y`  \n",
    "\n",
    "\n",
    "We demonstrate these below."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {
    "hide-output": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a > b is True\n",
      "a < b is False\n",
      "a == b is False\n",
      "a >= b is True\n",
      "a <= b is False\n"
     ]
    }
   ],
   "source": [
    "a = 4\n",
    "b = 2\n",
    "\n",
    "print(\"a > b\", \"is\", a > b)\n",
    "print(\"a < b\", \"is\", a < b)\n",
    "print(\"a == b\", \"is\", a == b)\n",
    "print(\"a >= b\", \"is\", a >= b)\n",
    "print(\"a <= b\", \"is\", a <= b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Negation\n",
    "\n",
    "Occasionally, determining whether a statement is\n",
    "“not true” or “not false” is more convenient than simply “true” or “false”.\n",
    "\n",
    "This is known as *negating* a statement.\n",
    "\n",
    "In Python, we can negate a boolean using the word `not`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {
    "hide-output": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "not False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {
    "hide-output": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "not True"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Multiple Comparisons (and/or)\n",
    "\n",
    "Sometimes we need to evaluate multiple comparisons at once.\n",
    "\n",
    "This is done by using the words `and` and `or`.\n",
    "\n",
    "However, these are the “mathematical” *and*s and *or*s – so they\n",
    "don’t carry the same meaning as you’d use them in colloquial English.\n",
    "\n",
    "- `a and b` is true only when **both** `a` and `b` are true.  \n",
    "- `a or b` is true whenever at least one of `a` or `b` is true.  \n",
    "\n",
    "\n",
    "For example\n",
    "\n",
    "- The statement “I will accept the new job if the salary is higher\n",
    "  *and* I receive more vacation days” means that you would only accept\n",
    "  the new job if you both receive a higher salary and are given more\n",
    "  vacation days.  \n",
    "- The statement “I will accept the new job if the salary is higher *or*\n",
    "  I receive more vacation days” means that you would accept the job if\n",
    "  (1) they raised your salary, (2) you are given more vacation days, or\n",
    "  (3) they raise your salary and give you more vacation days.  \n",
    "\n",
    "\n",
    "Let’s see some examples."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {
    "hide-output": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "True and False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {
    "hide-output": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "True and True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {
    "hide-output": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "True or False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {
    "hide-output": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "False or False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {
    "hide-output": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Can chain multiple comparisons together.\n",
    "True and (False or True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### `all` and `any`\n",
    "\n",
    "We have seen how we can use the words `and` and `or` to process two booleans\n",
    "at a time.\n",
    "\n",
    "The functions `all` and `any` allow us to process an unlimited number of\n",
    "booleans at once.\n",
    "\n",
    "`all(bools)` will return `True` if and only if all the booleans in `bools`\n",
    "is `True` and returns `False` otherwise.\n",
    "\n",
    "`any(bools)` returns `True` whenever one or more of `bools` is `True`.\n",
    "\n",
    "The exercise below will give you a chance to practice.\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercises\n",
    "\n",
    "Due date: Aug 12, 2022 before Noon\n",
    "\n",
    "\n",
    "<a id='exerciselist-0'></a>\n",
    "**Exercise 1**\n",
    "\n",
    "What do you think the value of `z` is after running the code below?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 165,
   "metadata": {
    "hide-output": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "z is 7\n"
     ]
    }
   ],
   "source": [
    "#Exercise 1\n",
    "# python\n",
    "z = 3\n",
    "z = z + 4\n",
    "print(\"z is\", z)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "\n",
    "**Exercise 2**\n",
    "\n",
    "Read about out what the `len` function does (by writing len?).\n",
    "\n",
    "What will it produce if we give it the variable `x`?\n",
    "\n",
    "Check whether you were right by running the code `len(x)`.\n",
    "\n",
    "\n",
    "\n",
    "**Exercise 3**\n",
    "\n",
    "We can use our introspection skills to investigate a package's contents.\n",
    "\n",
    "In the cell below, use tab completion to find a function from the `time`\n",
    "module that will display the **local** time.\n",
    "\n",
    "Use `time.FUNC_NAME?` (where `FUNC_NAME` is replaced with the\n",
    "function you found) to see information about that function and\n",
    "then call the function. (Hint: look for something to do with the word\n",
    "`local`)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 163,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Exercise 2\n",
    "len?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<built-in function len>\n"
     ]
    }
   ],
   "source": [
    "print(len)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "11"
      ]
     },
     "execution_count": 81,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = \"Hello World\"\n",
    "len(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 164,
   "metadata": {
    "hide-output": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "time.struct_time(tm_year=2022, tm_mon=8, tm_mday=15, tm_hour=0, tm_min=15, tm_sec=35, tm_wday=0, tm_yday=227, tm_isdst=0)"
      ]
     },
     "execution_count": 164,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#Exercise 3\n",
    "import time\n",
    "# your code here -- notice the comment!\n",
    "time.localtime()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "\n",
    "**Exercise 4**\n",
    "\n",
    "Try running `import time as t` in the cell below, then see if you can\n",
    "call the function you identified above.\n",
    "\n",
    "Does it work?\n",
    "\n",
    "\n",
    "\n",
    "**Exercise 5**\n",
    "\n",
    "Create the following variables:\n",
    "\n",
    "- `D`: A floating point number with the value 10,000  \n",
    "- `r`: A floating point number with value 0.025  \n",
    "- `T`: An integer with value 30  \n",
    "\n",
    "\n",
    "We will use them in a later exercise."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 160,
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "#Exercise 4\n",
    "# your code here!\n",
    "import time as t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 162,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "module"
      ]
     },
     "execution_count": 162,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(t)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 161,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Exercise 5\n",
    "D = 10_000.00\n",
    "r = 0.025\n",
    "T = 30"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "float"
      ]
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(D)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "float"
      ]
     },
     "execution_count": 90,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(r)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "int"
      ]
     },
     "execution_count": 91,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(T)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "\n",
    "**Exercise 6**\n",
    "\n",
    "Remember the variables we created earlier?\n",
    "\n",
    "Let's compute the present discounted value of a payment ($ D $) made\n",
    "in $ T $ years assuming an interest rate of 2.5%. Save this value to\n",
    "a new variable called `PDV` and print your output.\n",
    "\n",
    "Hint: The formula is\n",
    "\n",
    "$$\n",
    "\\text{PDV} = \\frac{D}{(1 + r)^T}\n",
    "$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 159,
   "metadata": {
    "hide-output": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4767.426851809713\n"
     ]
    }
   ],
   "source": [
    "#Exercise 6\n",
    "# your code here\n",
    "PDV = D / (1 + 0.025)**T\n",
    "print(PDV)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "\n",
    "**Exercise 7**\n",
    "\n",
    "Verify the \"trick\" where the percent difference ($ \\frac{x - y}{x} $)\n",
    "between two numbers close to 1 can be well approximated by the difference\n",
    "between the log of the two numbers ($ \\log(x) - \\log(y) $).\n",
    "\n",
    "Use the numbers `x` and `y` below. (Hint: you will want to use the\n",
    "`math.log` function)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 158,
   "metadata": {
    "hide-output": false
   },
   "outputs": [],
   "source": [
    "#Exercise 7\n",
    "x = 1.05\n",
    "y = 1.02"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.04879016416943204"
      ]
     },
     "execution_count": 107,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import math\n",
    "math.log(x) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.01980262729617973"
      ]
     },
     "execution_count": 103,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "math.log(y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.030000000000000027"
      ]
     },
     "execution_count": 104,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x-y"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "\n",
    "**Exercise 8**\n",
    "\n",
    "The code below is invalid Python code"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "hide-output": false
   },
   "source": [
    "```markdown\n",
    "x = 'What's wrong with this string'\n",
    "```\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 157,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "What is wrong with this string\n"
     ]
    }
   ],
   "source": [
    "#Exercise 8\n",
    "x = 'What is wrong with this string'\n",
    "print(x) #the comma on what's just end the string that we want to have"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Can you fix it?\n",
    "\n",
    "*Hint*: Try creating a code cell below and testing things out until you\n",
    "find a solution.\n",
    "\n",
    "\n",
    "**Exercise 9**\n",
    "\n",
    "Using the variables `x` and `y`, how could you create the sentence\n",
    "`Hello World`?\n",
    "\n",
    "Hint: Think about how to represent a space as a string.\n",
    "\n",
    "\n",
    "**Exercise 10**\n",
    "\n",
    "One of our favorite (and most frequently used) string methods is\n",
    "`replace`.\n",
    "\n",
    "It substitutes all occurrences of a particular pattern with a different pattern.\n",
    "\n",
    "For the variable `test` below, use the `replace` method to change the\n",
    "`c` to a `d`.\n",
    "\n",
    "Hint: Type `test.replace?` to get some help for how to use the method\n",
    "replace."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 156,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Hello World'"
      ]
     },
     "execution_count": 156,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#Exercise 9\n",
    "x = \"Hello\"\n",
    "y = \"World\"\n",
    "\n",
    "x + \" \" + y #add space by using \" \" space bar"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 155,
   "metadata": {
    "hide-output": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'abd'"
      ]
     },
     "execution_count": 155,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#Exercise 10\n",
    "test = \"abc\"\n",
    "test.replace(\"c\",\"d\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "\n",
    "**Exercise 11**\n",
    "\n",
    "Suppose you are working with price data and encounter the value\n",
    "`\"\\$6.50\"`.\n",
    "\n",
    "We recognize this as being a number representing the quantity \"six dollars and fifty cents.\"\n",
    "\n",
    "However, Python interprets the value as the string\n",
    "`\"\\$6.50\"`. (Quiz: why is this a problem? Think about the examples above.)\n",
    "\n",
    "In this exercise, your task is to convert the variable `price` below\n",
    "into a number.\n",
    "\n",
    "*Hint*: Once the string is in a suitable format, you can call write\n",
    "`float(clean_price)` to make it a number."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "hide-output": false
   },
   "source": [
    "```python\n",
    "price = \"$6.50\"\n",
    "```\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 132,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "str"
      ]
     },
     "execution_count": 132,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(price)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 154,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Exercise 11\n",
    "price = \"$6.50\" #the problem is about the currency that python assume this to be string"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 135,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "str"
      ]
     },
     "execution_count": 135,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(price)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 130,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "str"
      ]
     },
     "execution_count": 130,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(price)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 148,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'6.50'"
      ]
     },
     "execution_count": 148,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "price.strip(\"$\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 143,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'$6.50'"
      ]
     },
     "execution_count": 143,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "price"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "price."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 151,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6.5"
      ]
     },
     "execution_count": 151,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "float('6.50')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "\n",
    "**Exercise 12**\n",
    "\n",
    "Lookup a country in [World Bank database](https://data.worldbank.org), and\n",
    "format a string showing the growth rate of GDP over the last 2 years.\n",
    "\n",
    "\n",
    "\n",
    "**Exercise 13**\n",
    "\n",
    "Instead of hard-coding the values above, try to use the `country`, `GDP` and\n",
    "`year` variables you previously defined.\n",
    "\n",
    "\n",
    "**Exercise 14**\n",
    "\n",
    "Create a new string and use formatting to produce each of the following\n",
    "statements\n",
    "\n",
    "- \"The 1st quarter revenue was 110M\"  \n",
    "- \"The 2nd quarter revenue was 95M\"  \n",
    "- \"The 3rd quarter revenue was 100M\"  \n",
    "- \"The 4th quarter revenue was 130M\"  \n",
    "\n",
    "\n",
    "\n",
    "\n",
    "**Exercise 15**\n",
    "\n",
    "Without typing the commands, determine whether the following statements are\n",
    "true or false.\n",
    "\n",
    "Once you have evaluated whether the command is `True` or `False`, run the\n",
    "code in Python."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 171,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Thailand had $62,409.71 millions GDP in 2020 which is over the last 2 years\n"
     ]
    }
   ],
   "source": [
    "#Exercise 12\n",
    "country = \"Thailand\"\n",
    "GDP = \"62,409.71\"\n",
    "year = \"2020\"\n",
    "\n",
    "string = \"Thailand had $62,409.71 millions GDP in 2020 which is over the last 2 years\"\n",
    "print(string)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 172,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Thailand had $62,409.71 millions GDP in 2020 which is over the last 2 years\n"
     ]
    }
   ],
   "source": [
    "#Excercise 13\n",
    "my_string = f\"{country} had ${GDP} millions GDP in {year} which is over the last 2 years\"\n",
    "print(my_string)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 177,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'The 1st quarter revenue was 110M,The 2nd quarter revenue was 95M,The 3rd quarter revenue was 100M, and The 4th quarter revenue was 130M'"
      ]
     },
     "execution_count": 177,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#Exercise 14\n",
    "rev_string = \"{q1},{q2},{q3}, and {q4}\"\n",
    "\n",
    "rev_string.format(q1 = \"The 1st quarter revenue was 110M\",\n",
    "                  q2 = \"The 2nd quarter revenue was 95M\", \n",
    "                  q3 = \"The 3rd quarter revenue was 100M\",\n",
    "                  q4 = \"The 4th quarter revenue was 130M\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "hide-output": false
   },
   "source": [
    "x = 2\n",
    "y = 2\n",
    "z = 4\n",
    "\n",
    "#Statement1\n",
    "x > z \n",
    "\n",
    "#Statement2\n",
    "x == y\n",
    "\n",
    "#Statement3\n",
    "(x < y) and (x > y)\n",
    "\n",
    "#Statement4\n",
    "(x < y) or (x > y)\n",
    "\n",
    "#Statement5\n",
    "(x <= y) and (x >= y)\n",
    "\n",
    "#Statement6\n",
    "True and ((x < z) or (x < y))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 188,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Exercise 15\n",
    "\n",
    "#Statement1 --> False\n",
    "#Statement2 --> True\n",
    "#Statement3 --> False\n",
    "#Statement4 --> False\n",
    "#Statement5 --> True\n",
    "#Statement6 --> True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 179,
   "metadata": {
    "hide-output": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "statement1 is False\n",
      "statement2 is True\n",
      "statement3 is False\n",
      "statement4 is False\n",
      "statement5 is True\n",
      "statement6 is True\n"
     ]
    }
   ],
   "source": [
    "# code here!\n",
    "x = 2\n",
    "y = 2\n",
    "z = 4\n",
    "\n",
    "print(\"statement1 is\",x > z)\n",
    "print(\"statement2 is\",x == y)\n",
    "print(\"statement3 is\",(x < y) and (x > y))\n",
    "print(\"statement4 is\",(x < y) or (x > y))\n",
    "print(\"statement5 is\",(x <= y) and (x >= y))\n",
    "print(\"statement6 is\",True and ((x < z) or (x < y)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "\n",
    "**Exercise 16**\n",
    "\n",
    "For each of the code cells below, think carefully about what you expect to\n",
    "be returned *before* evaluating the cell.\n",
    "\n",
    "Then evaluate the cell to check your intuitions.\n",
    "\n",
    "NOTE: For now, do not worry about what the `[` and `]` mean -- they\n",
    "allow us to create lists which we will learn about in an upcoming lecture."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 187,
   "metadata": {
    "hide-output": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 187,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#Exercise 16\n",
    "all([True, True, True]) #result in True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 181,
   "metadata": {
    "hide-output": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 181,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "all([False, True, False]) #result in False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 182,
   "metadata": {
    "hide-output": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 182,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "all([False, False, False]) #result in False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 183,
   "metadata": {
    "hide-output": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 183,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "any([True, True, True]) #result in True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 184,
   "metadata": {
    "hide-output": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 184,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "any([False, True, False]) #result in True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 186,
   "metadata": {
    "hide-output": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 186,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "any([False, False, False]) #result in False"
   ]
  }
 ],
 "metadata": {
  "date": 1596739280.2860346,
  "filename": "basics.rst",
  "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.12"
  },
  "next_doc": {
   "link": "collections",
   "title": "Collections"
  },
  "prev_doc": {
   "link": "index",
   "title": "Python Fundamentals"
  },
  "title": "Basics"
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
