{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "52fcc1c0-b9c1-4b0b-8b9d-86c3d419df6c",
   "metadata": {},
   "source": [
    "## PDSP 2025, Lecture 04, 19 August 2025"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8e31db2b-2f1f-407c-89c5-8cf0d1628db5",
   "metadata": {},
   "source": [
    "### Checking if a number is prime\n",
    "- Checking if `n` is a prime: assume it is, and flag that is not if we find a factor between `2` and `sqrt(n)`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "fb476c24-9826-4b02-b7a5-5a041cd848d2",
   "metadata": {},
   "outputs": [],
   "source": [
    "import math"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "6554115f-174c-4f14-a7de-f54585d9591c",
   "metadata": {},
   "outputs": [],
   "source": [
    "n = 25\n",
    "isprime = True\n",
    "for i in range(2,int(math.sqrt(n))+1):  # int(...) truncates a float to an int\n",
    "    if n % i == 0:\n",
    "        isprime = False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "f2fa791c-a4eb-4a93-a561-a094a8d2d748",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "isprime"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e2acb00f-e770-4bf4-b026-d1302683d227",
   "metadata": {},
   "source": [
    "### Computing primes upto `n`\n",
    "- Instead of checking if `n` is a prime, find all primes upto (and including) `n`\n",
    "- Generate the sequence `2,3,...,n`\n",
    "- For each element in this sequence, check if it is a prime\n",
    "- Accumulate all primes found in a list\n",
    "    - Recall that `l1 + l2` concatenates two lists into a single list\n",
    "- Two *nested* loops, use different variables `j` and `i` to iterate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "c93daab0-2e2c-47f5-af4d-e8526e1fa225",
   "metadata": {},
   "outputs": [],
   "source": [
    "n = 100\n",
    "primelist = []\n",
    "for j in range(2,n+1):\n",
    "    isprime = True\n",
    "    for i in range(2,j):\n",
    "        if j % i == 0:\n",
    "            isprime = False\n",
    "    if isprime:\n",
    "        primelist = primelist + [j]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "7d4b2b1d-2a34-48ea-bc82-ab35456b5751",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[2,\n",
       " 3,\n",
       " 5,\n",
       " 7,\n",
       " 11,\n",
       " 13,\n",
       " 17,\n",
       " 19,\n",
       " 23,\n",
       " 29,\n",
       " 31,\n",
       " 37,\n",
       " 41,\n",
       " 43,\n",
       " 47,\n",
       " 53,\n",
       " 59,\n",
       " 61,\n",
       " 67,\n",
       " 71,\n",
       " 73,\n",
       " 79,\n",
       " 83,\n",
       " 89,\n",
       " 97]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "primelist"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fcc4d665-d729-4448-ba9f-209413e796bd",
   "metadata": {},
   "source": [
    "### Appending a value to a list\n",
    "- Can also use `l.append(v)` to add an element `v` to a list\n",
    "- Note the distinction between `l + [v]` and `l.append(v)`\n",
    "    - In the first case, we have to make `v` into a singleton list `[v]` to use the operator `+`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "6c1e23d4-1b24-4075-8adb-049868e520d0",
   "metadata": {},
   "outputs": [],
   "source": [
    "n = 100\n",
    "primelist = []\n",
    "for j in range(2,n+1):\n",
    "    isprime = True\n",
    "    for i in range(2,j):\n",
    "        if j % i == 0:\n",
    "            isprime = False\n",
    "    if isprime:\n",
    "        primelist.append(j)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "f3abc63c-37cb-4498-8f4d-a16c25d886dc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[2,\n",
       " 3,\n",
       " 5,\n",
       " 7,\n",
       " 11,\n",
       " 13,\n",
       " 17,\n",
       " 19,\n",
       " 23,\n",
       " 29,\n",
       " 31,\n",
       " 37,\n",
       " 41,\n",
       " 43,\n",
       " 47,\n",
       " 53,\n",
       " 59,\n",
       " 61,\n",
       " 67,\n",
       " 71,\n",
       " 73,\n",
       " 79,\n",
       " 83,\n",
       " 89,\n",
       " 97]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "primelist"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bc941c55-3bb1-4f90-9d6e-4da788778c2e",
   "metadata": {},
   "source": [
    "### Functions\n",
    "- Modularise code into functional units\n",
    "- Instead of embedding code to check if `j` is a prime, call a function that returns `True` if `j` is a prime and `False` otherwise\n",
    "- Function definition starts with `def function_name (argument1, argument2, ...):`\n",
    "- When the function completes, it should report an answer -- return a value through `return(v)`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "53d0e488-9f40-4d9a-a27e-8772667ff80a",
   "metadata": {},
   "outputs": [],
   "source": [
    "def isprime(n):\n",
    "    status = True\n",
    "    for i in range(2,n):\n",
    "        if n % i == 0:\n",
    "            status = False\n",
    "    return(status)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "b4ae93dc-eff9-4735-8ef7-712fdf2313a5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(True, False)"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "isprime(17), isprime(25)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7453d99c-8e4f-4f08-a9c3-095393014e94",
   "metadata": {},
   "source": [
    "### Exiting a function in between\n",
    "- If we find a factor, we can declare the number to not be a prime without testing more factors\n",
    "- In the original implementation, we needed to exit the loop\n",
    "- `return()` automatically exits, so we can use this optimisation in the function"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "cda3b471-d7a4-4710-9dcf-ca5ab3f3bd2e",
   "metadata": {},
   "outputs": [],
   "source": [
    "def isprime2(n): # An equivalent defn, terminates with False at first factor\n",
    "    status = True\n",
    "    for i in range(2,n):\n",
    "        if n % i == 0:\n",
    "            status = False\n",
    "            return(status)\n",
    "    return(status)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "42456fdd-0ff7-411c-b979-0576f1b22455",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(True, False)"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "isprime2(47), isprime2(44)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fe7ec67c-ccfb-4bab-ae03-22568ea08452",
   "metadata": {},
   "source": [
    "- In fact, we don't even need the variable `status`\n",
    "- If we find a factor, `return(False)`\n",
    "- If the search for a factor ends without finding one, `return(True)`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "1789e9e2-1153-4f4f-98b2-609971f411f9",
   "metadata": {},
   "outputs": [],
   "source": [
    "def isprime3(n):    # An equivalent defn, without a separate status variable\n",
    "    for i in range(2,n):\n",
    "        if n % i == 0:\n",
    "            return(False)\n",
    "    return(True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "85bfd5c6-5e59-4813-a52b-035018dce2a2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(True, False)"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "isprime3(571), isprime3(573)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e9afff69-2753-4f0b-b213-5980ba090478",
   "metadata": {},
   "source": [
    "### Using functions\n",
    "- We can rewrite our code to search for primes upto `n` to call the function `isprime` for each candidate\n",
    "    - Recall that in our earlier, explicit, code, we had to rename the outer loop variable as `j` to avoid a clash with the loop through potential factors\n",
    "    - If we use a function, the `i` inside the function is different from the `i` outside the function"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "983d652d-024c-4725-a0cf-2bdf403e6ec9",
   "metadata": {},
   "outputs": [],
   "source": [
    "n = 100\n",
    "primelist = []\n",
    "for i in range(2,n+1):\n",
    "    if isprime(i):\n",
    "        primelist.append(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "2bf29cd9-9551-4a20-a123-52537528c43f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[2,\n",
       " 3,\n",
       " 5,\n",
       " 7,\n",
       " 11,\n",
       " 13,\n",
       " 17,\n",
       " 19,\n",
       " 23,\n",
       " 29,\n",
       " 31,\n",
       " 37,\n",
       " 41,\n",
       " 43,\n",
       " 47,\n",
       " 53,\n",
       " 59,\n",
       " 61,\n",
       " 67,\n",
       " 71,\n",
       " 73,\n",
       " 79,\n",
       " 83,\n",
       " 89,\n",
       " 97]"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "primelist"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bf4775ec-3de2-4c84-b485-63d477ff36db",
   "metadata": {},
   "source": [
    "- We can convert this search for primes upto `n` into another function"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "bf22177a-828b-4bba-92a0-6236c54737ff",
   "metadata": {},
   "outputs": [],
   "source": [
    "def primesupto(n):\n",
    "    primelist = []\n",
    "    for i in range(2,n+1):\n",
    "        if isprime(i):\n",
    "            primelist.append(i)\n",
    "    return(primelist)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "fe7d305f-282e-4771-91b5-66427b5dda8d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[2, 3, 5, 7, 11, 13, 17, 19, 23, 29]"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "primesupto(30)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "490cf528-7fc7-4b1e-a865-ff208b496546",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67]"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "primesupto(70)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "a8e88816-1e50-498c-8b54-fba1ce94e016",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[2,\n",
       " 3,\n",
       " 5,\n",
       " 7,\n",
       " 11,\n",
       " 13,\n",
       " 17,\n",
       " 19,\n",
       " 23,\n",
       " 29,\n",
       " 31,\n",
       " 37,\n",
       " 41,\n",
       " 43,\n",
       " 47,\n",
       " 53,\n",
       " 59,\n",
       " 61,\n",
       " 67,\n",
       " 71,\n",
       " 73,\n",
       " 79,\n",
       " 83,\n",
       " 89,\n",
       " 97,\n",
       " 101,\n",
       " 103,\n",
       " 107,\n",
       " 109,\n",
       " 113,\n",
       " 127,\n",
       " 131,\n",
       " 137,\n",
       " 139,\n",
       " 149,\n",
       " 151,\n",
       " 157,\n",
       " 163,\n",
       " 167,\n",
       " 173,\n",
       " 179,\n",
       " 181,\n",
       " 191,\n",
       " 193,\n",
       " 197,\n",
       " 199,\n",
       " 211,\n",
       " 223,\n",
       " 227,\n",
       " 229,\n",
       " 233,\n",
       " 239,\n",
       " 241,\n",
       " 251,\n",
       " 257,\n",
       " 263,\n",
       " 269,\n",
       " 271,\n",
       " 277,\n",
       " 281,\n",
       " 283,\n",
       " 293,\n",
       " 307,\n",
       " 311,\n",
       " 313,\n",
       " 317,\n",
       " 331,\n",
       " 337,\n",
       " 347,\n",
       " 349,\n",
       " 353,\n",
       " 359,\n",
       " 367,\n",
       " 373,\n",
       " 379,\n",
       " 383,\n",
       " 389,\n",
       " 397,\n",
       " 401,\n",
       " 409,\n",
       " 419,\n",
       " 421,\n",
       " 431,\n",
       " 433,\n",
       " 439,\n",
       " 443,\n",
       " 449,\n",
       " 457,\n",
       " 461,\n",
       " 463,\n",
       " 467,\n",
       " 479,\n",
       " 487,\n",
       " 491,\n",
       " 499,\n",
       " 503,\n",
       " 509,\n",
       " 521,\n",
       " 523,\n",
       " 541,\n",
       " 547,\n",
       " 557,\n",
       " 563,\n",
       " 569,\n",
       " 571,\n",
       " 577,\n",
       " 587,\n",
       " 593,\n",
       " 599,\n",
       " 601,\n",
       " 607,\n",
       " 613,\n",
       " 617,\n",
       " 619,\n",
       " 631,\n",
       " 641,\n",
       " 643,\n",
       " 647,\n",
       " 653,\n",
       " 659,\n",
       " 661,\n",
       " 673,\n",
       " 677,\n",
       " 683,\n",
       " 691,\n",
       " 701,\n",
       " 709,\n",
       " 719,\n",
       " 727,\n",
       " 733,\n",
       " 739,\n",
       " 743,\n",
       " 751,\n",
       " 757,\n",
       " 761,\n",
       " 769,\n",
       " 773,\n",
       " 787,\n",
       " 797,\n",
       " 809,\n",
       " 811,\n",
       " 821,\n",
       " 823,\n",
       " 827,\n",
       " 829,\n",
       " 839,\n",
       " 853,\n",
       " 857,\n",
       " 859,\n",
       " 863,\n",
       " 877,\n",
       " 881,\n",
       " 883,\n",
       " 887,\n",
       " 907,\n",
       " 911,\n",
       " 919,\n",
       " 929,\n",
       " 937,\n",
       " 941,\n",
       " 947,\n",
       " 953,\n",
       " 967,\n",
       " 971,\n",
       " 977,\n",
       " 983,\n",
       " 991,\n",
       " 997]"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "primesupto(1000)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f32582e3-937d-4591-afe2-b5cbebf3333e",
   "metadata": {},
   "source": [
    "### Functions and modularity\n",
    "- Functions modularise code\n",
    "- Each function has an *interface contract* -- if the input $x$ is valid, the output is $f(x)$\n",
    "- Can change the implementation of the function so long as the interface contract is upheld\n",
    "    - Any one of our three implmentations of `isprime` can be used\n",
    "    - For instance, can use a naive implementation as a *prototype* and later replace by a more refined, optimised implementation"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "317ed237-2d5c-4dc6-928b-568775df2a24",
   "metadata": {},
   "source": [
    "### First $n$ primes\n",
    "What if we want a list of the first $n$ primes?\n",
    "- Generate numbers 2,3,... and check if each one is a prime\n",
    "- Stop when we have generated $n$ primes\n",
    "\n",
    "We don't know the upper bound of the list 2,3,...\n",
    "- Can't use `range()`\n",
    "\n",
    "Instead, a new kind of loop\n",
    "- \"Manually\" generate the sequence\n",
    "- Stop when we reach the terminating condition\n",
    "\n",
    "```\n",
    "while (condition):\n",
    "  statement 1\n",
    "  ...\n",
    "  statement k\n",
    "```\n",
    "\n",
    "- If `condition` evaluates to `True` the block of k statements is executed\n",
    "- After this, the condition is checked again and the same process is repeated\n",
    "- Compare to `if` where the condition is evaluated once\n",
    "\n",
    "```\n",
    "if (condition):\n",
    "  statement 1\n",
    "  ...\n",
    "  statement k\n",
    "```\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "29f1c291-6b2e-4187-af2f-6b40742d422b",
   "metadata": {},
   "outputs": [],
   "source": [
    "def nprimes(n):\n",
    "    primelist = []\n",
    "    i = 2\n",
    "    while (len(primelist) < n):\n",
    "        if (isprime(i)):\n",
    "            primelist.append(i)\n",
    "        i = i+1\n",
    "    return(primelist)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "06fea4bc-3d2d-4d90-b8a2-4cde7f9c55df",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71]"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nprimes(20)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6c5f56cd-273a-4400-a10e-b30ab5bd3e73",
   "metadata": {},
   "source": [
    "### Infinite loops\n",
    "\n",
    "- Need to ensure that the statements make *progress* towards falsifying the condition\n",
    "- If the condition remains `True` forever, the loop never terminates\n",
    "- For instance, suppose there were only finitely many primes, say $M$. For any $n > M$, the length of `primelist` would saturate at $M$ so the condition `len(primelist) < n` would never become `False`"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e5c6e08c-8715-42a7-9715-e3f347359f09",
   "metadata": {},
   "source": [
    "### Looping --- `for` and `while`\n",
    "\n",
    "- `while` is more general than `for`\n",
    "- Can implement\n",
    "  ```\n",
    "\n",
    "  for x in l:\n",
    "    ...\n",
    "\n",
    "\n",
    "  ```\n",
    "  using `while` by explicitly going through `l` from first to last position\n",
    "```\n",
    "pos = 0\n",
    "while (pos < len(l)):\n",
    "  ...\n",
    "  pos = pos + 1\n",
    "```\n",
    "- Note that we have to move the position \"manually\" to ensure that we make progress towards termination\n",
    "- However, using `for` is preferred if it is clearly an iteration over a fixed sequence\n",
    "    - The intent is capture much more clearly\n",
    "    - In the `while` form it is slightly obfuscated"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6a6023cb-3133-4aab-9f61-b163497bee5b",
   "metadata": {},
   "source": [
    "### Boolean datatypes\n",
    "- Usually an outcome of comparisons: `==`, `!=`, `<`, `<=`, `>`, `>=`\n",
    "- Useful shortcut\n",
    "    - Any \"empty\" value is interpreted as `False`\n",
    "    - So `0`, `[]`, `\"\"` (empty string) are all `False`\n",
    "    - Any other value is interpreted as `True`\n",
    "- Avoid comparisons such as `if x == 0` or `if l != []`\n",
    "    - Write `if not(x)`, `if l` instead"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "0f0a7e2b-29d6-4dfc-b20f-4302f425445c",
   "metadata": {},
   "outputs": [],
   "source": [
    "l = [1,2,3]\n",
    "if l:\n",
    "    x = True\n",
    "else:\n",
    "    x = False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "757e7331-9412-4be9-8f5b-46414f4a2ab4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "cd43451f-973b-4aaf-9d17-419c3becd917",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = 0\n",
    "if not(m):\n",
    "    y = True\n",
    "else:\n",
    "    y = False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "6b057be1-1144-4674-84d3-224a8f04b889",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "52bf74e0-5ea1-4542-937a-31884788fc74",
   "metadata": {},
   "source": [
    "- Note that Python does not insist on brackets around the condition in `if` and `while`\n",
    "    - Can write `if (cond):` or `if cond:`, `while (cond):` or `while cond:`"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "60928e4d-e32d-4135-97f3-e6c8b92ed08a",
   "metadata": {},
   "source": [
    "### Variables, values and types\n",
    "- Variables (names) have no intrinsic types\n",
    "- Values have types\n",
    "    - A variable inherits the type of the value it currently holds\n",
    "- The type of value a variable holds can vary over time\n",
    "    - But not a good idea to use the same name for different types of values in the same piece of code\n",
    "    - Reduces readability, maintainability\n",
    "- The `type()` function returns the type of a variable that is currently assigned a value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "52367c6d-832d-4457-89d1-4322e363d878",
   "metadata": {},
   "outputs": [],
   "source": [
    "x = True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "69ec90e7-4bcf-408c-9e3f-bfa5cfffd0e2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "bool"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "d53ebb50-f105-4920-b5d9-04214f24c69e",
   "metadata": {},
   "outputs": [],
   "source": [
    "x = 5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "70ca1fa5-4a0f-4772-82de-2e21d808337c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "int"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a38bb0d5-017e-4cce-a7fe-75da82a15f61",
   "metadata": {},
   "source": [
    "- The function `del()` unassigns a value from a name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "a26c969e-016b-47ef-9b1d-83c57f3b3dbc",
   "metadata": {},
   "outputs": [],
   "source": [
    "del(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "f7593366-d9c8-4abd-bc34-d4779b4ee062",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'x' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[31m---------------------------------------------------------------------------\u001b[39m",
      "\u001b[31mNameError\u001b[39m                                 Traceback (most recent call last)",
      "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[31]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m \u001b[38;5;28mtype\u001b[39m(\u001b[43mx\u001b[49m)\n",
      "\u001b[31mNameError\u001b[39m: name 'x' is not defined"
     ]
    }
   ],
   "source": [
    "type(x)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
