{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from __future__ import print_function" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Tuplas" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Las tuplas se crean dinamicamente poniendo valores cualesquiera entre parentesis separados por comas" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1, 2, 3.4)" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = (1,2,3.4)\n", "a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Tenemos acceso por posicion" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[0]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Podemos extraer los elementos con asignacion multiple" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 2 3.4\n" ] } ], "source": [ "b, c, d = a\n", "\n", "print(b, c, d)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Acceder a un subconjunto de los elementos de la tupla" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1, 2)" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[0:2]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Acceder de manera relativa al último elemento" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[-2]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Imbricar la tuplas" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(2, 3)\n", "3\n" ] } ], "source": [ "a = (1, (2, 3))\n", "\n", "print(a[1])\n", "print(a[1][1])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Las tuplas son objetos inmutables, lo siguiente da error" ] }, { "cell_type": "code", "execution_count": 8, "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;36m3\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] = 3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Tenemos operaciones interesantes sobre tuplas como pertenencia de elementos (in)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = (1, 2, 3, 4 ,5)\n", "1 in a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Concatenacion de tuplas" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1, 2, 3, 4, 5, 1, 2, 3, 4, 5)" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b = a + a\n", "b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Replicación" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1, 2, 3, 4, 5, 1, 2, 3, 4, 5)" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a * 2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Posicion de un elemento en la tupla (la primera aparicion)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b.index(5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Cuantas veces aparece un elemento en una tupla" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b.count(1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Longitud" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "10" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(b)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Para mas operaciones consultar la documentación de python" ] } ], "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.6.6" }, "latex_envs": { "LaTeX_envs_menu_present": true, "autoclose": false, "autocomplete": true, "bibliofile": "biblio.bib", "cite_by": "apalike", "current_citInitial": 1, "eqLabelWithNumbers": true, "eqNumInitial": 1, "hotkeys": { "equation": "Ctrl-E", "itemize": "Ctrl-I" }, "labels_anchors": false, "latex_user_defs": false, "report_style_numbering": false, "user_envs_cfg": false }, "toc": { "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "toc_cell": false, "toc_position": {}, "toc_section_display": "block", "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 1 }