{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Prerequisites \n",
    "\n",
    "- Types\n",
    "\n",
    "# Collections\n",
    "\n",
    "Previously, we were introduced to individual types that exist in Python. \n",
    "Python types go beyond individual items, including objects that can hold **collections** of variables. \n",
    "This section will introduce three different collection types:\n",
    "- Lists\n",
    "- Tuples\n",
    "- Dictionaries\n",
    "\n",
    "As is the case with individual types, different collection types can be acted on in different ways. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<hr>\n",
    "\n",
    "## Lists\n",
    "\n",
    "The first type of collection we will look at is a **list**. \n",
    "Let us look first at how to create a list."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "noble_gases = ['helium', 'Neon', 'Argon', 'Krypton', 'Xenon', 'Radon']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "list"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(noble_gases)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['helium', 'Neon', 'Argon', 'Krypton', 'Xenon', 'Radon']\n"
     ]
    }
   ],
   "source": [
    "print(noble_gases)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can use the **index notation** to identify particular elements in a list. \n",
    "However, it is important to note that this notation counts from $0$, so the first element of the list is found with, "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "helium\n"
     ]
    }
   ],
   "source": [
    "print(noble_gases[0])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The index notation is shown for a list of the first 7 elements in the figure below. \n",
    "\n",
    "<img src='./../images/list_indexing.png' width='60%'>\n",
    "\n",
    "We can also change particular values, as lists are **mutable**."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "noble_gases[0] = 'Helium'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The final element in the tuple therefore has the index of $n-1$, where $n$ is the tuple length. \n",
    "The lengths of a collection can be found with the `len()` function."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(noble_gases)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Radon\n"
     ]
    }
   ],
   "source": [
    "print(noble_gases[len(noble_gases)-1])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You may notice that Oganesson is missing from the list, it is possible to concatenate it."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "noble_gases = noble_gases + ['Oganesson']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Helium', 'Neon', 'Argon', 'Krypton', 'Xenon', 'Radon', 'Oganesson']\n"
     ]
    }
   ],
   "source": [
    "print(noble_gases)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In addition to standard indexing, it is also possible to perform **negative** indexing with lists and tuples, where $-1$ is the final element of the list with the counting up moving backwords through the list. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Oganesson\n"
     ]
    }
   ],
   "source": [
    "print(noble_gases[-1])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can also get **slices** of a list, this is achieved using the `:` notation."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Neon', 'Argon']\n"
     ]
    }
   ],
   "source": [
    "print(noble_gases[1:3])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This notation can be a bit confusing, as this notation is inclusive then exclusive. \n",
    "The slice of the list includes the element with the index that preceeds the colon and exclusive of that after the colon. \n",
    "In the example above, we can see that is **includes** the index $1$ item (`'Neon'`) but **excludes** the index $3$ element (`'Krypton'`).\n",
    "\n",
    "This is shown for the `elements` list in the figure below. \n",
    "\n",
    "<img src='./../images/list_slicing.png' width='60%'>\n",
    "\n",
    "It is also possible to use list slicing to **skip** elements of the list, where the number after the second colon is the step size. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Neon', 'Krypton']\n"
     ]
    }
   ],
   "source": [
    "print(noble_gases[1:5:2])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Above, the second to the sixth elements are printed skipping each second (so those with indices $2$ and $4$)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "As well as list concatentation (the adding together of lists above), it is possible to `append` single elements to the end of a list. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Fluorine', 'Chlorine', 'Bromine', 'Iodine']\n"
     ]
    }
   ],
   "source": [
    "halogens = ['Fluorine', 'Chlorine', 'Bromine', 'Iodine']\n",
    "print(halogens)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Fluorine', 'Chlorine', 'Bromine', 'Iodine', 'Astanine']\n"
     ]
    }
   ],
   "source": [
    "halogens.append('Astanine')\n",
    "print(halogens)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Fluorine', 'Chlorine', 'Bromine', 'Iodine', 'Astanine', 'Tennessine']\n"
     ]
    }
   ],
   "source": [
    "halogens.append('Tennessine')\n",
    "print(halogens)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Finally, we note that a list does **not** have to be of a consistent `type`. \n",
    "So it is possble for a list to be made up of disparate forms of information."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "chlorine = ['Cl', 17, 35.45]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Cl', 17, 35.45]\n"
     ]
    }
   ],
   "source": [
    "print(chlorine)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Where the list `chlorine` contains information about the chemical symbol, atomic number, and the average mass number."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<hr> \n",
    "\n",
    "## Tuples\n",
    "\n",
    "\n",
    "\n",
    "The **immutable** cousin to a list is a **tuple**, a tuple acts in many ways the same as a tuple (such as indexing). \n",
    "However, the elements of a tuple cannot change once the object is defined. \n",
    "Let's look at how a tuple is defined."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "noble_gases = ('helium', 'neon', 'argon', 'krypton', 'xenon', 'radon')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Above is a tuple of noble gases, where the tuple is defined using round brackets. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tuple"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(noble_gases)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Like with lists, we can use the **index notation** to identify particular elements in the tuple. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "helium\n"
     ]
    }
   ],
   "source": [
    "print(noble_gases[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "radon\n"
     ]
    }
   ],
   "source": [
    "print(noble_gases[-1])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Above it was mentioned that a tuple is an immutable type, that once created it's objects cannot be altered. \n",
    "We can see this in action if we try and change the value of one of the elements in the tuple. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "'tuple' object does not support item assignment",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-22-22efed8af598>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mnoble_gases\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m'Helium'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m: 'tuple' object does not support item assignment"
     ]
    }
   ],
   "source": [
    "noble_gases[0] = 'Helium'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "However, it is still possible to concatenate tuples, like lists, but the item that is being concatentated **must** be a tuple, hence the brackets and trailing comma."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('helium', 'neon', 'argon', 'krypton', 'xenon', 'radon', 'oganesson')"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "noble_gases + ('oganesson',)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The immutability of a tuple does not mean that the whole variable cannot be overwritten. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "noble_gases = 'inert'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "inert\n"
     ]
    }
   ],
   "source": [
    "print(noble_gases)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Additionally, like lists, tuple items can also consist of data of different types."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<hr>\n",
    "\n",
    "## Dictionaries\n",
    "\n",
    "The final type of collection we will discuss here is the dictionary (`dict`). \n",
    "This collection type consists of `keys` and `values`.\n",
    "These `keys` and `values` are analogous to the words and definitions of a traditional dictionary.\n",
    "Consider if we rewrite the `chlorine` variable as a dictionary. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "chlorine = {'chemical symbol': 'Cl', 'atomic number': 17, 'average mass number': 35.45}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'chemical symbol': 'Cl', 'atomic number': 17, 'average mass number': 35.45}\n"
     ]
    }
   ],
   "source": [
    "print(chlorine)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Here, we are able to use the `keys` of the dictionaries as labels for each datum. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dict_keys(['chemical symbol', 'atomic number', 'average mass number'])\n"
     ]
    }
   ],
   "source": [
    "print(chlorine.keys())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "It is possible to access a single one of the elements of a dictionary, by passing the relevant `key` in lieu of an index. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "17"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "chlorine['atomic number']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "However, traditional indexing does **not** work for dictionaries, as the index is not a `key`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "1",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-30-a0685985ddc1>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mchlorine\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mKeyError\u001b[0m: 1"
     ]
    }
   ],
   "source": [
    "chlorine[1]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can add elements to a dictionary rather simply."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "chlorine['number of isotopes'] = 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'chemical symbol': 'Cl', 'atomic number': 17, 'average mass number': 35.45, 'number of isotopes': 2}\n"
     ]
    }
   ],
   "source": [
    "print(chlorine)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Finally, the items in a dictionary need not be single variables, like a float or an int. \n",
    "They can also be a `list` or `tuple`. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "chlorine['mass numbers'] = (35, 37)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'chemical symbol': 'Cl', 'atomic number': 17, 'average mass number': 35.45, 'number of isotopes': 2, 'mass numbers': (35, 37)}\n"
     ]
    }
   ],
   "source": [
    "print(chlorine)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Exercise\n",
    "\n",
    "Add the necessary information to the dictionary below such that it is a dictionary where the `keys` are the names of the first-row of transition metals, and the values are `int` of the number of valence electrons.\n",
    "Test your dictionary by printing some of the values. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [],
   "source": [
    "d_block = {}"
   ]
  },
  {
   "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.8.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
