{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "view-in-github", "colab_type": "text" }, "source": [ "\"Open" ] }, { "cell_type": "markdown", "metadata": { "id": "pwoqJ7HIZN9w" }, "source": [ "# Funciones. Ejemplos" ], "id": "pwoqJ7HIZN9w" }, { "cell_type": "code", "execution_count": 7, "metadata": { "id": "n3sp3k9DZN9y", "outputId": "2458b63e-df27-4e25-d5fa-817b0b0582ad", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "El número 21 es impar.\n" ] } ], "source": [ "def par_impar(numero):\n", " if numero%2 == 0:\n", " resultado = 'par'\n", " else:\n", " resultado = 'impar'\n", " print(\"El número {} es {}.\".format(numero, resultado))\n", "\n", "par_impar(21)" ], "id": "n3sp3k9DZN9y" }, { "cell_type": "code", "execution_count": 8, "metadata": { "id": "KsNtiSqyZN91", "outputId": "b6e0726a-e37c-4b92-fb6b-c1b9ebbefa5f", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Introduce un texto para invertir: sogima\n", "El reverso es: amigos\n" ] } ], "source": [ "def reverso(cadena):\n", " return cadena[::-1]\n", "\n", "texto = input(\"Introduce un texto para invertir: \")\n", "print('El reverso es: ', reverso(texto))" ], "id": "KsNtiSqyZN91" }, { "cell_type": "code", "execution_count": 9, "metadata": { "id": "aB0LUGR7ZN91", "outputId": "9918a3b2-557f-427e-ed87-2f0e890ba280", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "El área de un triángulo de base 10 y altura 4 es 20.0.\n" ] } ], "source": [ "def area_tri(base, altura):\n", " return base*altura/2\n", "\n", "print(\"El área de un triángulo de base {} y altura {} es {}.\".format(10, 4, area_tri(10, 4)))" ], "id": "aB0LUGR7ZN91" }, { "cell_type": "code", "execution_count": 10, "metadata": { "id": "qCH4s3wkZN93", "outputId": "9e469da5-68a3-4418-a569-04f73d18bd03", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Indique el radio del círculo o pulsa Enter: \n" ] } ], "source": [ "import math\n", "def area_circulo(radio):\n", " radio = float(radio)\n", " return math.pi*radio**2\n", "while True:\n", " r = input(\"Indique el radio del círculo o pulsa Enter: \")\n", " if r == \"\":\n", " break\n", " print(\"El área de un círculo de radio {} es {}.\".format(r, round(area_circulo(r), 1)))" ], "id": "qCH4s3wkZN93" }, { "cell_type": "markdown", "metadata": { "id": "XG_PihHSZN94" }, "source": [ "## El número e \n", "[Exponential function](https://en.wikipedia.org/wiki/Exponential_function) \n", "$$\n", "e = \\lim_{n\\to\\infty} \\biggl( 1 + \\frac{1}{n} \\biggr)^n\n", "$$\n", "\n", "Otra forma de trabajar con la exponencial. \n", "\n", "$$\n", "e^x = \\sum_{n = 0}^\\infty \\frac{x^n}{n!}\n", "$$ \n", "Vamos a crear una función que calcula esta última expresión y la compara con la función ```math.exp(1)```." ], "id": "XG_PihHSZN94" }, { "cell_type": "code", "execution_count": 11, "metadata": { "id": "EwPY9xsbZN95", "outputId": "a6c4e233-3d54-4491-f25b-252cf4017501", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "2.7182818284590455\n", "2.718281828459045\n" ] } ], "source": [ "import math\n", "#from math import factorial, exp\n", "\n", "def exponencial(x):\n", " total = 0\n", " for n in range(0, 1000):\n", " total += x**n/math.factorial(n)\n", " return total\n", "\n", "x = 1\n", "print(exponencial(x))\n", "print(math.exp(x))" ], "id": "EwPY9xsbZN95" }, { "cell_type": "markdown", "metadata": { "id": "86qqQ-aCZN95" }, "source": [ "## Calculadora" ], "id": "86qqQ-aCZN95" }, { "cell_type": "code", "execution_count": 12, "metadata": { "id": "tSSFbH9FZN96", "outputId": "45a0fcf1-d351-41c7-c682-23430ea3cccb", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ " Menú: \n", "1. Sumar\n", "2. Restar\n", "3. Multiplicar\n", "4. Dividir\n", "0. Finalizar\n", "1\n", "Introduzca el primer número: 2\n", "Introduzca el segundo número: 3\n", "2.0 + 3.0 = 5.0\n", " Menú: \n", "1. Sumar\n", "2. Restar\n", "3. Multiplicar\n", "4. Dividir\n", "0. Finalizar\n", "0\n", "Programa finalizado\n" ] } ], "source": [ "def sumar(num1, num2): return num1 + num2\n", "def restar(num1, num2): return num1 - num2\n", "def multiplicar(num1, num2): return num1 * num2\n", "def dividir(num1, num2): return num1 / num2\n", "\n", "while True:\n", " opcion = input(''' Menú: \\n1. Sumar\\n2. Restar\\n3. Multiplicar\\n4. Dividir\\n0. Finalizar\\n''')\n", " if opcion in ('1', '2', '3', '4'):\n", " num1 = float(input(\"Introduzca el primer número: \"))\n", " num2 = float(input(\"Introduzca el segundo número: \"))\n", " if opcion == '1': print(num1, '+', num2, '=', sumar(num1, num2))\n", " elif opcion == '2': print(num1, '-', num2, '=', restar(num1, num2))\n", " elif opcion == '3': print(num1, '*', num2, '=', multiplicar(num1, num2))\n", " elif opcion == '4': print(num1, '/', num2, '=', dividir(num1, num2))\n", " else: print(\"Programa finalizado\"); break" ], "id": "tSSFbH9FZN96" } ], "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": "0620_funciones_ejemplos.ipynb", "provenance": [], "include_colab_link": true } }, "nbformat": 4, "nbformat_minor": 5 }