{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Tuple and namedtuple\n",
    "\n",
    "\n",
    "### Tuple\n",
    "\n",
    "1. Tuples are immutable data structure. They are homogeneous/heterogeneous.\n",
    "2. Order have meaning.\n",
    "3. Tuple are faster than list."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```{note}\n",
    "\n",
    "What defines tuple is not parenthesis but a comma.\n",
    "```\n",
    "eg:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tuple, int)"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tup = 1,\n",
    "num = (1)\n",
    "type(tup), type(num)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "(namedtuple)=\n",
    "## Namedtuple\n",
    "\n",
    "- It is just a fancy way to instanciating a tuple object so that we can access the element using dot notation like in python classes.\n",
    "- It is class generator.\n",
    "- Inherits the tuple class."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Point2d(x=10, y=20)"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from collections import namedtuple\n",
    "\n",
    "x = ['x','y'] # or ('x','y) or \"x y\" or \"x,y\"\n",
    "Point2d =namedtuple('Point2d',x)\n",
    "\n",
    "coord = Point2d(10,20)\n",
    "\n",
    "coord"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Accessing Data in named tuple"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10,20\n"
     ]
    }
   ],
   "source": [
    "x,y = coord\n",
    "\n",
    "# or\n",
    "x = coord[0]\n",
    "y = coord[1]\n",
    "\n",
    "# or using field name\n",
    "x = coord.x\n",
    "y = coord.y\n",
    "\n",
    "print(x,y,sep=\",\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "isinstance(coord,tuple)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('name', 'email', 'address', 'contact', 'religion', 'education')"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Person = namedtuple(\"Person\",\"name email address contact religion education\")\n",
    "Person._fields"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "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.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
