{ "cells": [ { "cell_type": "markdown", "id": "c7bfb035-a828-4a11-a343-506ba39a3aab", "metadata": {}, "source": [ "###
San Jose State University
Department of Applied Data Science

**DATA 200
Computational Programming for Data Analytics**

Spring 2024
Instructor: Ron Mak
" ] }, { "cell_type": "markdown", "id": "2b2c3cc2-5bbb-410e-9d4d-dec940bacc62", "metadata": {}, "source": [ "# Named Tuples" ] }, { "cell_type": "markdown", "id": "85fdfc41-7b80-4ac0-ac2f-119ca217feb8", "metadata": {}, "source": [ "#### Recall that a tuple is similar to a list, except that it is immutable. The `collections` module has a `namedtuple()` function that returns a tuple subclass. Each object instantiated from this subclass has named attributes." ] }, { "cell_type": "code", "execution_count": 8, "id": "3c3be33e-e72f-4c40-a190-113f52e9875e", "metadata": {}, "outputs": [], "source": [ "from collections import namedtuple" ] }, { "cell_type": "markdown", "id": "82849b94-4031-40d8-9fb9-4fa6fedd1d9e", "metadata": {}, "source": [ "#### Create a named tuple class `Employeee` whose objects have attributes named `id`, `last`, `first`, and `salary`." ] }, { "cell_type": "code", "execution_count": 9, "id": "1cf8d699-0541-43cc-955a-c44d16524646", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "__main__.Employee" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Employee = namedtuple('Employee', ['id', 'last', 'first', 'salary'])\n", "Employee" ] }, { "cell_type": "code", "execution_count": 10, "id": "ddf1726e-f305-4697-9822-aa9b5e27dfe5", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "('id', 'last', 'first', 'salary')" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Employee._fields" ] }, { "cell_type": "markdown", "id": "9ad6cf19-03be-48f3-a7ee-7412fa88f688", "metadata": {}, "source": [ "## Create some Employee objects." ] }, { "cell_type": "code", "execution_count": 11, "id": "f0c841ef-22ec-41c3-a4e4-5d2421aa72f4", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "bob = Employee(id=12345, last='Smith', first='Robert', salary=100000.0)\n", "sal = Employee(id=98765, last='Brown', first='Sally', salary=125000.0)\n" ] } ], "source": [ "bob = Employee(id=12345, last='Smith', first='Robert', salary=100_000.00)\n", "sal = Employee(id=98765, last='Brown', first='Sally', salary=125_000.00)\n", "\n", "print(f'{bob = }')\n", "print(f'{sal = }')" ] }, { "cell_type": "code", "execution_count": 12, "id": "8277b948-f4a1-4b03-a783-a7dcd2464d8f", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "bob.id = 12345\n", "sal.salary = 125,000.00\n" ] } ], "source": [ "print(f'{bob.id = }')\n", "print(f'{sal.salary = :,.2f}')" ] }, { "cell_type": "markdown", "id": "300eeeb0-a412-4a1d-9973-6ac588563ef7", "metadata": {}, "source": [ "## A named tuple has a built-in `_make()` class method." ] }, { "cell_type": "code", "execution_count": 13, "id": "70334083-ffc7-4c52-9187-d57332cd54c7", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "sid = Employee(id=10293, last='Jones', first='Sidney', salary=85000.0)\n" ] } ], "source": [ "data = [10293, 'Jones', 'Sidney', 85_000.00]\n", "sid = Employee._make(data)\n", "\n", "print(f'{sid = }')" ] }, { "cell_type": "markdown", "id": "bbffb558-4026-40bf-bce6-de489815f2c2", "metadata": {}, "source": [ "#### Tuples are immutable!" ] }, { "cell_type": "code", "execution_count": 14, "id": "9ede7c85-5327-4cac-b767-095dcfc1ac9e", "metadata": {}, "outputs": [ { "ename": "AttributeError", "evalue": "can't set attribute", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[14], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m sid\u001b[38;5;241m.\u001b[39msalary \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m90_000\u001b[39m\n", "\u001b[0;31mAttributeError\u001b[0m: can't set attribute" ] } ], "source": [ "sid.salary = 90_000" ] }, { "cell_type": "code", "execution_count": null, "id": "138212aa-8121-4792-bfae-7852ba4c2850", "metadata": {}, "outputs": [], "source": [] } ], "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.11.5" } }, "nbformat": 4, "nbformat_minor": 5 }