{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "ac897494",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Tuples\n",
    "#### CS 65: Introduction to Computer Science I"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "278533ed",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Tuple\n",
    "\n",
    "It's an ordered collection like a list, but it uses parentheses ( ) instead of square brackets [ ]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "181ce6d3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Eric\n"
     ]
    }
   ],
   "source": [
    "my_tuple = (\"Eric\",100,3.14)\n",
    "print(my_tuple[0])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "74deccef",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "Except, tuples are _immutable_.\n",
    "* You can't change an item in a tuple\n",
    "* There are no methods like `append()`, `insert()`, `pop()`, or `remove()`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "8b22528b",
   "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-6-0d96e7d7f79c>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mmy_tuple\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m200\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": [
    "my_tuple[1] = 200"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f224b2c0",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Purpose and benefits of tuples\n",
    "\n",
    "Tuples are typically used when you have a grouping of related data that doesn't need to change; whereas, lists are typically used when you have a collection of a bunch of the same thing.\n",
    "\n",
    "Tuples are also \n",
    "* Faster than lists\n",
    "* Safer for things that shouldn't be changed\n",
    "* Expected for working some modules and libraries (including working with images!)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ef6fddb3",
   "metadata": {},
   "outputs": [],
   "source": [
    "student_info = (\"Stu Dent\",100123456,\"Computer Science\")\n",
    "\n",
    "student_list = [(\"Stu Dent\",100123456,\"Computer Science\"), (\"Elena Schmidt\",100123457,\"Math\"), \n",
    "                (\"Krystal Harmon\",100123458,\"Computer Science\"), (\"Marcos Hopkins\",100123459,\"Biology\"), \n",
    "                (\"Terry Richardson\",100123460,\"Political Science\")]                                                    "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b3a7f89f",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "Tuples can also be used to return more than one thing from a function."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "43777b00",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(8, 2944702.3)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import csv\n",
    "\n",
    "def parks_num_size(state):\n",
    "    \n",
    "    with open(\"nationalparks.csv\") as npfile:\n",
    "        parks_2d_list = list(csv.reader(npfile))\n",
    "    \n",
    "    num_parks_in_state = 0\n",
    "    size_parks_in_state = 0\n",
    "    \n",
    "    for park in parks_2d_list:\n",
    "        \n",
    "        if park[1] == state:\n",
    "            num_parks_in_state += 1\n",
    "            size_parks_in_state += float(park[3])\n",
    "    \n",
    "    return (num_parks_in_state, size_parks_in_state)\n",
    "\n",
    "parks_num_size(\"California\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "13071f52",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Single-item tuples\n",
    "\n",
    "You can have tuples with one thing in them, but you need to include a comma to tell Python it's a tuple and not just a parenthesized value."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "0873a6ff",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "100 <class 'int'>\n"
     ]
    }
   ],
   "source": [
    "my_val = (100) \n",
    "print(my_val, type(my_val) )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "e4f08702",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(100,) <class 'tuple'>\n"
     ]
    }
   ],
   "source": [
    "my_monuple = (100,)\n",
    "print(my_monuple, type(my_monuple))"
   ]
  }
 ],
 "metadata": {
  "celltoolbar": "Slideshow",
  "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.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
