{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "view-in-github", "colab_type": "text" }, "source": [ "\"Open" ] }, { "cell_type": "markdown", "metadata": { "id": "01q4IzhPYhOl" }, "source": [ "# Funciones. Pasar un iterable como argumento" ], "id": "01q4IzhPYhOl" }, { "cell_type": "code", "execution_count": 1, "metadata": { "id": "Fl1Rb5HVYhOq", "outputId": "f0f98336-fc7f-48b0-e403-877070ac0c58", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Titanio\n", "Platino\n", "Oro\n" ] } ], "source": [ "def listado(metales): # La función recibe una lista\n", " for metal in metales:\n", " print(metal)\n", "\n", "mi_lista=[\"Titanio\", \"Platino\", \"Oro\"]\n", "listado(mi_lista)" ], "id": "Fl1Rb5HVYhOq" }, { "cell_type": "code", "execution_count": 2, "metadata": { "id": "-rGAnnAPYhOw", "outputId": "41d68e7f-fa27-4879-e253-bfa732a2bf12", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n" ] } ], "source": [ "def enumera(iterable): # La función recibe un rango\n", " for i in iterable:\n", " print(i)\n", "\n", "enumera(range(5))" ], "id": "-rGAnnAPYhOw" }, { "cell_type": "code", "execution_count": 3, "metadata": { "id": "jEpwRiFfYhO0", "outputId": "37d5b16b-c93c-4192-f042-0dcfc84c21c3", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "invierno\n", "verano\n", "otoño\n", "primavera\n" ] } ], "source": [ "def muestra(conjunto): # La función recibe un conjunto\n", " for elemento in conjunto:\n", " print(elemento)\n", "\n", "estaciones=set(('primavera', 'verano', 'otoño', 'invierno')) # Creamos un conjunto con set()\n", "muestra(estaciones) # No guarda ningún orden" ], "id": "jEpwRiFfYhO0" }, { "cell_type": "code", "execution_count": 4, "metadata": { "id": "b6-nzxXZYhO3", "outputId": "b195ad65-b311-42bc-9452-0e195cb1daae", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Hello World" ] } ], "source": [ "def recorrer(texto): # La función recibe un string\n", " for caracter in texto:\n", " print(caracter, end='')\n", "\n", "recorrer(\"Hello World\")" ], "id": "b6-nzxXZYhO3" }, { "cell_type": "code", "execution_count": 5, "metadata": { "id": "jan3W1i4YhO5", "outputId": "e3c73954-391a-455c-96d4-bcd5b1463714", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Hola Mundo" ] } ], "source": [ "def recorrer2(texto):\n", " for i in range(len(texto)): # r ecorremos las posiciones del string hasta su longitud\n", " print(texto[i], end='')\n", "\n", "recorrer2(\"Hola Mundo\")" ], "id": "jan3W1i4YhO5" }, { "cell_type": "code", "execution_count": 6, "metadata": { "id": "P09ehiaiYhO7", "outputId": "ed775e8d-2fc0-4c97-bf6a-1b396360c201", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Buenos días" ] } ], "source": [ "def recorrer3(texto):\n", " for i in range(len(texto)):\n", " print(texto[i:i+1], end='') # slicing. Método de corte\n", "\n", "recorrer3(\"Buenos días\")" ], "id": "P09ehiaiYhO7" } ], "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.8.5" }, "colab": { "name": "0610_iterable_como_argumento.ipynb", "provenance": [], "include_colab_link": true } }, "nbformat": 4, "nbformat_minor": 5 }