{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "92df5e2a-3851-4239-a718-9dd04fc703f4",
   "metadata": {},
   "source": [
    "# Lesson 04 in-class demo\n",
    "---\n",
    "## 1. Data types\n",
    "\n",
    "### 1.1. Strings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "d9e3f0b1-a0e8-4d77-a6d1-bf390d3c04c3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello world!\n"
     ]
    }
   ],
   "source": [
    "my_string = 'Hello world!'\n",
    "print(my_string)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "ed172346-fad0-4b1e-8761-20a62152fdc6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "str"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(my_string)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "85bf5214-f311-4d2b-83ea-fcbf16405c08",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "12"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(my_string)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b9fbf7a9-8e96-47da-8814-4b2ed99aa311",
   "metadata": {},
   "source": [
    "### 1.2. Numbers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "c84ba0be-1245-4271-91ed-2e261f53042e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "int"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "my_int = 32\n",
    "type(my_int)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "9e952272-a7a3-4cb7-aaa2-2d8410dc3159",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "float"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "my_float = 32.0\n",
    "type(my_float)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b5d3a80f-541d-4556-bf47-112530f7c005",
   "metadata": {},
   "source": [
    "### 1.3. Lists"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "b08c3ae1-feed-48e5-bfca-c18f1f2535f1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "list"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "my_list = ['zero', 'one', 'two', 'three']\n",
    "type(my_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "678bdaa3-3fff-4719-abd4-55d017b25836",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(my_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "2c6c66e9-a56d-4be8-97a3-4bd6ec4a404e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['zero', 'one', 'two', 'three']\n"
     ]
    }
   ],
   "source": [
    "print(my_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "20834947-d7b0-4b91-8bdb-3b41a0787d41",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'zero'"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "my_list[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "017c4aa3-d50b-40ae-b98c-2ccae688df7a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['zero', 'one', 'two', 'three', 'four']\n"
     ]
    }
   ],
   "source": [
    "my_list.append('four')\n",
    "print(my_list)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d92fa95b-b487-4a20-a6a3-205c8e3e2398",
   "metadata": {},
   "source": [
    "### 1.4. Dictionaries"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "25e338e2-7b10-46b2-b162-33518ef3bfb4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': 'George', 'location': 'New York', 'status': 'Caffinated'}\n"
     ]
    }
   ],
   "source": [
    "my_dict = {\n",
    "    'name': 'George',\n",
    "    'location': 'New York',\n",
    "    'status': 'Caffinated'\n",
    "}\n",
    "\n",
    "print(my_dict)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "38f23c3a-1a23-4f0c-9748-ffb6caf490aa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'George'"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "my_dict['name']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "e06f9146-196e-4173-86db-34406ebd3534",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_keys(['name', 'location', 'status'])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "my_dict.keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "e0482b69-a85a-4ef7-9aa7-8f438f1241a2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_values(['George', 'New York', 'Caffinated'])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "my_dict.values()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3f231ec6-d9f3-47c5-9c04-160bb8235f5e",
   "metadata": {},
   "source": [
    "### 1.5. Type conversion "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "e944ef36-c272-49ca-9642-7376da166a9e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "str"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "string_num = '32'\n",
    "type(string_num)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "30ffe723-cc8b-465c-8770-b4a5e0e8505a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "int"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "int_num = int(string_num)\n",
    "type(int_num)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "50616d2d-dad7-4a29-825d-460e1e1229cf",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "32\n"
     ]
    }
   ],
   "source": [
    "print(int_num)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "844f89f3-c3bc-4a43-85ac-3b804f5cfef9",
   "metadata": {},
   "source": [
    "---\n",
    "## 2. Operators\n",
    "\n",
    "### 2.1. Arithmetic"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "cb968c9e-fd2c-4eec-a75b-daedbeba4b12",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "4 + 4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "d68c16f6-8a0c-46d5-9642-d566e4796f2c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = 4\n",
    "y = 4\n",
    "x + y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "73729970-a778-48b7-91e9-100fbae525d9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "5 % 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "19bba535-f8b2-4a7c-a550-992edfa5b19c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2.5"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "5 / 2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "49a7eae0-20c9-460c-8142-abc3fb628def",
   "metadata": {},
   "source": [
    "### 2.2. Comparison"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "438a186b-6da5-4b5b-8f63-f83e57c56e8d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = 20\n",
    "y = 20\n",
    "y == x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "32abbabf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x != y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "2af0e865-43ad-4cd3-8fe9-81ee14bfbbf9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = '20'\n",
    "z = 20\n",
    "x == z"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "c755d3fb-00d8-4a2c-a54c-9599cfcb0cdc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "<>:2: SyntaxWarning: \"is\" with 'int' literal. Did you mean \"==\"?\n",
      "<>:2: SyntaxWarning: \"is\" with 'int' literal. Did you mean \"==\"?\n",
      "/tmp/ipykernel_7905/1637039292.py:2: SyntaxWarning: \"is\" with 'int' literal. Did you mean \"==\"?\n",
      "  print(a is 1)\n"
     ]
    }
   ],
   "source": [
    "a = 1\n",
    "print(a is 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "9b31fcfc-71ce-40b9-9c6a-9a1e95e5cf6d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "source": [
    "print(a == 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "e139fa56",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n"
     ]
    }
   ],
   "source": [
    "print(a is True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "5bc00b21",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "source": [
    "print(a == True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "fa7d1280-79ab-4ac4-989b-164c13bb026f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "<>:2: SyntaxWarning: \"is\" with 'int' literal. Did you mean \"==\"?\n",
      "<>:2: SyntaxWarning: \"is\" with 'int' literal. Did you mean \"==\"?\n",
      "/tmp/ipykernel_7905/3193089720.py:2: SyntaxWarning: \"is\" with 'int' literal. Did you mean \"==\"?\n",
      "  print(b is 1000)\n"
     ]
    }
   ],
   "source": [
    "b = 1000\n",
    "print(b is 1000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "97fb53b3-e386-4fef-a613-2416b970e03d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "source": [
    "print(b == 1000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "8e26f40c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n"
     ]
    }
   ],
   "source": [
    "print(b is True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "9e9d5bbb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n"
     ]
    }
   ],
   "source": [
    "print(b == True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "65742483-b9aa-4841-a2c6-173cc951c568",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n"
     ]
    }
   ],
   "source": [
    "c = 1\n",
    "print(not c)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "31cdd211",
   "metadata": {},
   "source": [
    "### 2.3. Membership"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "cf9959c0-6b6e-4dda-ae69-32a7005429b1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "my_list = ['zero', 'one', 'two', 'three']\n",
    "\n",
    "'zero' in my_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "992dcd4c-313c-4a47-9b67-12a0d8b59446",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'ten' not in my_list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ed38066e-c998-4cc8-b98f-e15c2e61cf03",
   "metadata": {},
   "source": [
    "---\n",
    "## 3. Working with files\n",
    "\n",
    "### 3.1. Reading from a file"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d24fb4e4-51c3-4c76-b25f-dacc01e69bbb",
   "metadata": {},
   "outputs": [],
   "source": [
    "'''\n",
    "Note: The following code requires a file named 'test.txt' in a folder \n",
    "named 'data' in the current working directory.\n",
    "'''\n",
    "\n",
    "file = open('data/test.txt', 'r')\n",
    "contents = file.read()\n",
    "print(contents)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "19cb3a90-6860-4f22-ab86-98a959c33393",
   "metadata": {},
   "outputs": [],
   "source": [
    "file.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b229e149",
   "metadata": {},
   "source": [
    "### 3.2. Writing to a file"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3fdd5c45-76ac-4eb7-ac88-b9e1b99ac00d",
   "metadata": {},
   "outputs": [],
   "source": [
    "file = open('data/test_output.txt', 'w')\n",
    "new_contents = 'This is some new data'\n",
    "file.write(new_contents)\n",
    "file.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aa46790d-93ac-4003-b74a-01d470589dfd",
   "metadata": {},
   "source": [
    "---\n",
    "## 4. Error handling"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9b82ad97-519f-4adf-88f4-d08767cf94a2",
   "metadata": {},
   "outputs": [],
   "source": [
    "try:\n",
    "    file = open('data/test_b.txt', 'r')\n",
    "    contents = file.read()\n",
    "    print(contents)\n",
    "\n",
    "except:\n",
    "    print(f'File not found')\n",
    "    \n",
    "finally:\n",
    "    file.close()    "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e10c2f19",
   "metadata": {},
   "source": [
    "---\n",
    "## 5. Extra\n",
    "\n",
    "### 5.1. Formatting output"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "64f23431-1187-47d7-a9e3-3ef6df6958cf",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The new data: 5\n"
     ]
    }
   ],
   "source": [
    "print(f'The new data: {3 + 2}')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3c225f3e",
   "metadata": {},
   "source": [
    "### 5.2. Markdown formatting"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "803c8c96",
   "metadata": {},
   "source": [
    "**Bold**\n",
    "\n",
    "*italics*\n",
    "\n",
    "~~strike-through~~\n",
    "\n",
    "Unordered list:\n",
    "- one\n",
    "- two\n",
    "- three\n",
    "\n",
    "Ordered list:\n",
    "1. one\n",
    "2. two\n",
    "3. three\n",
    "\n",
    "Table:\n",
    "\n",
    "| Simple | markdown | table |\n",
    "|--------|----------|-------|\n",
    "| Row    | Number   | 1     |\n",
    "| Row    | Number   | 2     |\n",
    "| Row    | Number   | 3     |\n",
    "\n",
    "\n"
   ]
  }
 ],
 "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.12.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
