{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Tuples\n", "\"Open\n", "\n", "- http://openbookproject.net/thinkcs/python/english3e/tuples.html\n", "- containers used for grouping data values surrounded with parenthesis\n", "- data values in tuples are called elements/items/members\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": 11, "metadata": {}, "outputs": [], "source": [ "star = \"Paris\", 'J', 'Hilton', 1981, 32, 1.2 # tuple packing" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "('Paris', 'J', 'Hilton', 1981, 32, 1.2)" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "star" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tuple" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(star)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "# tuple assignment\n", "fname, mi, lname, year, age, income = star # tuple unpacking; \n", "# no. of variables must match no. tuple 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": [ "## Member access\n", "- each member of tuple can be accessed using [ index ] operator\n", "- index is 0 based or starts from 0" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "name = ('John', 'A.', 'Smith')" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "John A. Smith\n" ] } ], "source": [ "print(name[0], name[1], name[2])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## len built-in function\n", "- gives the length (no. of elements) of tuple" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(name)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Tuple membership\n", "- **in** and **not in** boolean operators let's you check for membership" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'John' in name" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'B.' in name" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'Jake' not in name" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Tuples can be returned from a function" ] }, { "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": "markdown", "metadata": {}, "source": [ "## Tuples are immutable\n", "- can't change tuple in-place or update its elements similar to string" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n" ] } ], "source": [ "a = (1, 2, 3)\n", "print(a[0])" ] }, { "cell_type": "code", "execution_count": 6, "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\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\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[0] = 100" ] } ], "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 }