{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "view-in-github", "colab_type": "text" }, "source": [ "\"Open" ] }, { "cell_type": "markdown", "metadata": { "id": "wwbIjbttDKfO" }, "source": [ "# Introducción a las funciones [2]" ], "id": "wwbIjbttDKfO" }, { "cell_type": "markdown", "metadata": { "id": "uKZ4MT6iDKfQ" }, "source": [ "## El operador asterisco\n", "Cuando tenemos un número indeterminado de argumentos" ], "id": "uKZ4MT6iDKfQ" }, { "cell_type": "code", "execution_count": 1, "metadata": { "id": "QepenHRLDKfR", "outputId": "f214f377-db4c-41dc-8b0e-7c73d0693cb6", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "El mayor en la fiesta tenía 30 años\n" ] } ], "source": [ "def mimax(*edades): #cuando no se sabe el número de argumentos se pone asterisco\n", " print(f\"El mayor en la fiesta tenía {max(edades)} años\")\n", "\n", "mimax(20, 30, 18, 24, 26)" ], "id": "QepenHRLDKfR" }, { "cell_type": "markdown", "metadata": { "id": "WGv4Xx5ADKfS" }, "source": [ "## Valor por defecto de un argumento" ], "id": "WGv4Xx5ADKfS" }, { "cell_type": "code", "execution_count": 2, "metadata": { "id": "FphL_DaqDKfT", "outputId": "095786fc-791e-458c-c371-550c35c484c3", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "De postre mejor una fruta, por ejemplo manzana\n", "De postre mejor una fruta, por ejemplo naranja\n" ] } ], "source": [ "def postre(fruta = \"naranja\"):\n", " return \"De postre mejor una fruta, por ejemplo \" + fruta\n", "\n", "print(postre('manzana'))\n", "print(postre()) #si no se especifica un argumento se toma el valor por defecto" ], "id": "FphL_DaqDKfT" }, { "cell_type": "markdown", "metadata": { "id": "DPzCvtbmDKfT" }, "source": [ "## Función con dos return\n", "Cuando una función no lleva return el compilador añade al final un ```return True```. \n", "\n", "Si en una función hay dos returns solo se ejecuta el primero ya que finaliza la función." ], "id": "DPzCvtbmDKfT" }, { "cell_type": "code", "execution_count": 3, "metadata": { "id": "IL2DoO7-DKfU", "outputId": "f14b28dc-2065-4354-f1d9-dd8530b5ae64", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Este verano viajaré hacia el Norte\n" ] } ], "source": [ "import random\n", "\n", "def destino(numero):\n", " if numero == 1:\n", " return 'Norte'\n", " if numero == 2:\n", " return 'Sur'\n", "\n", "aleatorio = random.randint(1,2)\n", "punto_cardinal = destino(aleatorio)\n", "print(\"Este verano viajaré hacia el\", punto_cardinal)" ], "id": "IL2DoO7-DKfU" }, { "cell_type": "markdown", "metadata": { "id": "vqqeuuwUDKfU" }, "source": [ "## Nº argumentos = Nº parámetros\n", "Cuando esto no sucede se produce un error." ], "id": "vqqeuuwUDKfU" }, { "cell_type": "code", "execution_count": 4, "metadata": { "id": "caZ2ylbTDKfV" }, "outputs": [], "source": [ "def suma(a,b):\n", " return a+b\n", "\n", "#suma(10) # error" ], "id": "caZ2ylbTDKfV" }, { "cell_type": "markdown", "metadata": { "id": "BZTtHveTDKfV" }, "source": [ "Una posible solución es establecer unos valores por defecto de tipo ```None``` y luego con un ```if``` controlar el error." ], "id": "BZTtHveTDKfV" }, { "cell_type": "code", "execution_count": 5, "metadata": { "id": "bxqDOf01DKfV", "outputId": "1e78f954-a721-49e4-d101-e1e7f9e87bd8", "colab": { "base_uri": "https://localhost:8080/", "height": 35 } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'Error: se han de pasar dos argumentos'" ], "application/vnd.google.colaboratory.intrinsic+json": { "type": "string" } }, "metadata": {}, "execution_count": 5 } ], "source": [ "def suma(a=None,b=None):\n", " if a==None or b==None: return \"Error: se han de pasar dos argumentos\"\n", " else: return a+b # sin el else también funciona\n", "\n", "suma()" ], "id": "bxqDOf01DKfV" }, { "cell_type": "code", "execution_count": 6, "metadata": { "id": "ykm6lLNkDKfW", "outputId": "f7eaae85-c364-48c1-ae52-9872cfc89740", "colab": { "base_uri": "https://localhost:8080/", "height": 35 } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'Error: se han de pasar dos argumentos'" ], "application/vnd.google.colaboratory.intrinsic+json": { "type": "string" } }, "metadata": {}, "execution_count": 6 } ], "source": [ "suma(10)" ], "id": "ykm6lLNkDKfW" }, { "cell_type": "code", "execution_count": 7, "metadata": { "id": "1ZsBFt7oDKfW", "outputId": "5729799c-a278-412b-9907-30cccef867c3", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "15" ] }, "metadata": {}, "execution_count": 7 } ], "source": [ "suma(10,5)" ], "id": "1ZsBFt7oDKfW" } ], "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": "0540_funciones2.ipynb", "provenance": [], "include_colab_link": true } }, "nbformat": 4, "nbformat_minor": 5 }