{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Prerequisites \n",
    "\n",
    "- Types\n",
    "- Collections\n",
    "- Flow Control\n",
    "\n",
    "# And and or operations"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In the previous tutorial, flow controls were introduced which allow the manipulation/determination of data based on certain characteristics. This took the form `If A is true then complete action B`. Often there are more than one criteria that one wished to evaluate against, for example in the cases of `If A and B are true then complete action C` or `If A or B are true then complete action C`. Alternatively we coult want a criteria such as `If A is False then complete action B`. \n",
    "\n",
    "Python can enact all these processes using `and`, `or`, and `not` statements. These statements collectively are called Boolean logic statements, named after the mathematician George Boole who created this field of mathematics. These statements are the building blocks of computers and Boolean logic is fundamentally important in all computation. \n",
    "\n",
    "In this tutorial these logic statements will be used in combination with flow controls to manipulate some data from the first twenty elements of the periodic table. In order to get started, we shall first import the data that we require and explain how the data is stored. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-04-07T06:26:21.648942Z",
     "start_time": "2020-04-07T06:26:20.830411Z"
    }
   },
   "outputs": [],
   "source": [
    "from periodic_table_20 import periodic_table_20"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The periodic_table_20 variable is a dictionary that contains dictionaries of properties of the the elements. The keys of the main dictionary are the chemical symbols of the first twenty elements. For example, to observe information about hydrogen we use"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-04-07T06:26:21.662849Z",
     "start_time": "2020-04-07T06:26:21.651173Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'mass': 1.008,\n",
       " 'group': 1,\n",
       " 'period': 1,\n",
       " 'block': 's',\n",
       " 'melting_point': 14.01,\n",
       " 'boiling_point': 20.28}"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "periodic_table_20[\"H\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The information stored about each element is its: relative atomic mass, group, period, block, melting point, and boiling point. The keys of the elemental dictionary are `mass`, `group`, `period`, `block`, `melting_point`, and `boiling_point`. In order to obtain the block that hydrogen is in we can use"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-04-07T06:26:21.672119Z",
     "start_time": "2020-04-07T06:26:21.665734Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'s'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "periodic_table_20[\"H\"][\"block\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This indicates that, as we know, hydrogen lies in the s block of the periodic table. \n",
    "\n",
    "If a periodic table is not to hand it may be difficult to remember the group and period of an element. However, Boolean statements can be used to determine the location of an element if a guess is given. To check whether phosphorus is in period 2 and group 15, the following python code can be used"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-04-07T06:26:21.684153Z",
     "start_time": "2020-04-07T06:26:21.676098Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Phosphorus is not in period 2 and group 15\n"
     ]
    }
   ],
   "source": [
    "if periodic_table_20[\"P\"][\"period\"] == 2 and periodic_table_20[\"P\"][\"group\"] == 15:\n",
    "    print(\"Correct, phosphorus is in period 2 and group 15!\")\n",
    "else:\n",
    "    print(\"Phosphorus is not in period 2 and group 15\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This is incorrect, as one would expect. Phosphorus is in period 3. Whilst phosphorus is in group 15, it is not in period 2 so the code \"flows to\" the else statement. What if we use the following code? "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-04-07T06:26:21.693439Z",
     "start_time": "2020-04-07T06:26:21.687168Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Correct, phosphorus is in period 3 and group 15!\n"
     ]
    }
   ],
   "source": [
    "if periodic_table_20[\"P\"][\"period\"] == 3 and periodic_table_20[\"P\"][\"group\"] == 15:\n",
    "    print(\"Correct, phosphorus is in period 3 and group 15!\")\n",
    "else:\n",
    "    print(\"Phosphorus is not in period 3 and group 15\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now we have provided the correct information for phosphorus the code is correct. But what will happen if we replace the `and` statement for an `or` statement? We will keep the incorrect guess of period 2 though. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-04-07T06:26:21.705176Z",
     "start_time": "2020-04-07T06:26:21.698359Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Correct, phosphorus is in period 2 or group 15!\n"
     ]
    }
   ],
   "source": [
    "if periodic_table_20[\"P\"][\"period\"] == 2 or periodic_table_20[\"P\"][\"group\"] == 15:\n",
    "    print(\"Correct, phosphorus is in period 2 or group 15!\")\n",
    "else:\n",
    "    print(\"Phosphorus is not in period 2 or group 15\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This states that we are correct as whilst phosphorus is not in period 2, it is in group 15. Therefore one of the two statements is true, and so the code \"flow through\" the if statement instead of the else statement. To better understand the process of what python is doing, the concept of truth tables is introduced. Below is a truth table for the `and` and `or` statements. \n",
    "\n",
    "| A        | B        | AND  |OR|\n",
    "| ------------- |:-------------:| -----:|----:|\n",
    "| True      | True | True | True|\n",
    "| True      | False |  False  |   True |\n",
    "| False | True      |    False | True | \n",
    "|False | False | False | False |\n",
    "\n",
    "The truth tables give the output of the `and` and `or` statements when two statements, `A` and `B`, are evaluated. For example, taking the first line, if both statement A and statement B are true then `A and B` and `A or B` will both give true. \n",
    "\n",
    "The outputs shown in the above diagrams are the same as those in the logic table. We can use the logic illustrated above to show what is happening in the examples above. Step by step the first example's logic follows:\n",
    "\n",
    "1) `periodic_table_20[\"P\"][\"period\"] == 2`, is evaluated which is `False` as phosphorus is in period 3.\n",
    "\n",
    "2) `periodic_table_20[\"P\"][\"group\"] == 15` is evaluated which is `True` as phosphorus is in group 15.\n",
    "\n",
    "3) `False and True` is evaluated which overall gives `False`. \n",
    "\n",
    "4) `if False` is evaluated and the line `\"Phosphorus is not in period 2 and group 15\"` is printed through the else statement. \n",
    "\n",
    "While in the third example the following logic occurs:\n",
    "\n",
    "1) `periodic_table_20[\"P\"][\"period\"] == 2`, is evaluated which is `False` as phosphorus is in period 3.\n",
    "\n",
    "2) `periodic_table_20[\"P\"][\"group\"] == 15` is evaluated which is `True` as phosphorus is in group 15.\n",
    "\n",
    "3) `False or True` is evaluated which overall gives `True`. \n",
    "\n",
    "4) `if True` is evaluated and the line `\"Correct, phosphorus is in period 2 or group 15!\"` is printed. \n",
    "\n",
    "Other statements can be evaluated. For example, if we wanted to determine whether lithium is liquid at 700 K we could use the following code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-04-07T06:26:21.714180Z",
     "start_time": "2020-04-07T06:26:21.707338Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Lithium is liquid at 700 K\n"
     ]
    }
   ],
   "source": [
    "if periodic_table_20[\"Li\"][\"melting_point\"] < 700 and periodic_table_20[\"Li\"][\"boiling_point\"] > 700:\n",
    "    print(\"Lithium is liquid at 700 K\")\n",
    "else:\n",
    "    print(\"Lithium is not liquid at 700 K.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This indicatrs that the melting point of lithium is below 700 K and its boiling point is greater than 700 K. Therefore lithium is liquid at 700 K.\n",
    "\n",
    "**Exercise:** Write a similar code to determine whether sodium is a liquid at 100 $^{\\circ}$C. Remember the melting and boiling points are recorded in K. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-04-07T06:26:21.723058Z",
     "start_time": "2020-04-07T06:26:21.719819Z"
    }
   },
   "outputs": [],
   "source": [
    "### Write your code here"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Chemical molecules can also behave as logic gates. For example the molecule below was determined to be useful in detecting cancers by Ozlam and Akkaya$^{1}$. The molecule facilitates the transfer of singlet oxygen to triplet oxygen in the presence of both high concentrations of sodium ions and protons when irradiated with 660 nm radiation. \n",
    "\n",
    "<img src=\"../images/AND_logic_gate.png\" alt=\"drawing\" width=\"200\"/>\n",
    "\n",
    "The concentration of sodium ions and protons are high in the presence of cancers. The transfer of singlet oxygen to triplet oxygen does not occur with the aid of this molecule unless both sodium ions **and** proton concentrations are high. This makes the \"truth table\" as follows:\n",
    "\n",
    "| Sodium ion concentration        | Proton concentration | Oxygen state  |\n",
    "| ------------- |:-------------:| -----:|\n",
    "| High      | High | Triplet | \n",
    "| High      | Low |  Singlet  |\n",
    "| Low | High      |    Singlet |\n",
    "|Low | Low | Singlet |\n",
    "\n",
    "This is analagous to the AND truth table as the production of triplet oxygen requires a high concentration of oxygen **and** a high concentration of sodium. Therefore the molecule above can aid in the detection of cancers using Boolean logic. There are other molecules that behave as logic gates for a multitude of other uses.\n",
    "\n",
    "$^{1}$ S. Ozlam and E. U. Akkaya, J. Am. Chem. Soc. 2009, 131, 1, 48-49."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Not statements\n",
    "\n",
    "The third aspect of Boolean logic is the `not` statement. The `not` statement simply reverses a Boolean statement so a `True` becomes `False` and a `False` becomes `True`. The periodic_table_20 variable only contains information on the first twenty elements of the periodic table so the elements must be in either the s or p block. To determine whether silicon is in the s or the p block, two methods can be used:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-04-07T06:26:21.735489Z",
     "start_time": "2020-04-07T06:26:21.726467Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Silicon is in the p block\n"
     ]
    }
   ],
   "source": [
    "if periodic_table_20['Si']['block'] == 's':\n",
    "    print(\"Silicon is in the s block.\")\n",
    "else:\n",
    "    print(\"Silicon is in the p block\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Alternatively, one could use a `not` statement such that:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-04-07T06:26:21.744830Z",
     "start_time": "2020-04-07T06:26:21.738449Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Silicon is in the p block.\n"
     ]
    }
   ],
   "source": [
    "if not(periodic_table_20['Si']['block'] == 's'):\n",
    "    print(\"Silicon is in the p block.\")\n",
    "else:\n",
    "    print(\"Silicon is in the s block\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Both are valid ways of determining whether silicon is in the s or p block. What is happening in the second method?\n",
    "\n",
    "1) `periodic_table_20['Si']['block'] == 's'` is evaluated to `False` as silicon is not in the s block.\n",
    "\n",
    "2) `not(False)` is evaluated to `True`.\n",
    "\n",
    "3) `if True:` is evaluated and \"Silicon is in the p block.\" is printed. \n",
    "\n",
    "How does the process change if sodium is evaluated?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-04-07T06:26:21.753251Z",
     "start_time": "2020-04-07T06:26:21.747970Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Sodium is in the s block\n"
     ]
    }
   ],
   "source": [
    "if not(periodic_table_20['Na']['block'] == 's'):\n",
    "    print(\"Sodium is in the p block.\")\n",
    "else:\n",
    "    print(\"Sodium is in the s block\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "When sodium is evaluated the logic is:\n",
    "\n",
    "Both are valid ways of determining whether silicon is in the s or p block. What is happening in the second method?\n",
    "\n",
    "1) `periodic_table_20['Na']['block'] == 's'` is evaluated to `True` as sodium is in the s block.\n",
    "\n",
    "2) `not(True)` is evaluated to `False`.\n",
    "\n",
    "3) `if False:` is evaluated so the else statement is used and \"Sodium is in the s block.\" is printed. \n",
    "\n",
    "**Exercise:** Using a not statement determine whether calcium is in group 2. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-04-07T06:26:21.758704Z",
     "start_time": "2020-04-07T06:26:21.755802Z"
    }
   },
   "outputs": [],
   "source": [
    "## Write your code here"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In the next tutorial loops will be introduced. The use of loops in combination with Boolean logic statements and flow controls can create efficient methods of analysing data. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
