{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Tuples\n", "- containers used for grouping data values surrounded with parenthesis\n", "- two major operations done with tuples are:\n", " 1. packing (creating tuples)\n", " 2. unpacking (storing data into individual variables)" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "year_born = (\"Paris Hilton\", 1981) # tuple packing" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "('Paris Hilton', 1981)\n" ] } ], "source": [ "print(year_born)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "star = \"Paris\", 'J', 'Hilton', 1981, 32, 1.2 # tuple packing" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "('Paris', 'J', 'Hilton', 1981, 32, 1.2)" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "star" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tuple" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(star)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "# tuple assignment\n", "fname, mi, lname, year, age, income = star # tuple unpacking; # variables must match # values" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Paris'" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fname" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Hilton'" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "lname" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1.2\n" ] } ], "source": [ "print(income)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "# swap values of two variables\n", "a = 100\n", "b = 200\n", "a, b = b, a" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "200 100\n" ] } ], "source": [ "print(a, b)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Tuples can be returned from a function\n", "## Tuples are immutable" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [], "source": [ "def maxAndMin(a, b, c, d, e):\n", " myMax = a #max(a, b, c, d, e)\n", " if myMax < b:\n", " myMax = b\n", " if myMax < c:\n", " myMax = c\n", " if myMax < d:\n", " myMax = d\n", " if myMax < e:\n", " myMax = e\n", " values = [a, b, c, d, e]\n", " myMin = min(values)\n", " return [myMax, myMin]\n", " " ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "max = 100 min= 5\n" ] } ], "source": [ "ab = maxAndMin(10, 20, 5, 100, 34)\n", "print('max =', ab[0], 'min=', ab[1])" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n" ] }, { "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\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0ma\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m3\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0ma\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m100\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": [ "a = (1, 2, 3)\n", "print(a[0])\n", "a[0] = 100" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n" ] } ], "source": [ "print(a[0])" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on built-in function min in module builtins:\n", "\n", "min(...)\n", " min(iterable, *[, default=obj, key=func]) -> value\n", " min(arg1, arg2, *args, *[, key=func]) -> value\n", " \n", " With a single iterable argument, return its smallest item. The\n", " default keyword-only argument specifies an object to return if\n", " the provided iterable is empty.\n", " With two or more arguments, return the smallest argument.\n", "\n" ] } ], "source": [ "help(min)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.7.2" } }, "nbformat": 4, "nbformat_minor": 2 }