{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "view-in-github", "colab_type": "text" }, "source": [ "\"Open" ] }, { "cell_type": "markdown", "id": "314204d8", "metadata": { "id": "314204d8" }, "source": [ "# Operadores de asignación \n", "\n", "Los operadores de asignación se utilizan para asignar valores a las variables. \n", "\n", "![Operadores de asignación](https://github.com/financieras/pyCourse/blob/main/jupyter/img/operadores_asignacion.png?raw=1)" ] }, { "cell_type": "code", "execution_count": 1, "id": "24bc1a02", "metadata": { "id": "24bc1a02", "outputId": "fd69c045-79e1-4f61-e207-35387ca206c0", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "5" ] }, "metadata": {}, "execution_count": 1 } ], "source": [ "x = 5 # es necesario inicializar la variable\n", "x" ] }, { "cell_type": "code", "execution_count": 2, "id": "0dbc3daf", "metadata": { "id": "0dbc3daf", "outputId": "cb3f3974-1d1c-4831-f589-7ea8f4192467", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "10" ] }, "metadata": {}, "execution_count": 2 } ], "source": [ "# suma en asignación\n", "x += 5 # equivale a x = x + 5\n", "x # cada vez que ejecutemos este código el valor de x se incrementará en 5" ] }, { "cell_type": "code", "execution_count": 3, "id": "d7b7a2e9", "metadata": { "id": "d7b7a2e9", "outputId": "519132c0-9c2d-4508-eaa0-48ec96a96076", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "5" ] }, "metadata": {}, "execution_count": 3 } ], "source": [ "# resta en asignación\n", "x -= 5 # equivale a x = x - 5\n", "x" ] }, { "cell_type": "code", "execution_count": 4, "id": "3eeda87d", "metadata": { "id": "3eeda87d", "outputId": "e09c202d-78d7-4cd1-ab2d-2cec9bfcc84c", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "25" ] }, "metadata": {}, "execution_count": 4 } ], "source": [ "# producto en asignación\n", "x *= 5 # equivale a x = x * 5\n", "x" ] }, { "cell_type": "code", "execution_count": 5, "id": "3b9e1ae0", "metadata": { "id": "3b9e1ae0", "outputId": "84d56d3c-d493-4d27-d6ae-d2ecaa95eef0", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "5.0" ] }, "metadata": {}, "execution_count": 5 } ], "source": [ "# división en asignación\n", "x /= 5 # equivale a x = x / 5\n", "x # el resultado siempre será un float porque es una división" ] }, { "cell_type": "code", "execution_count": 6, "id": "5335036f", "metadata": { "id": "5335036f", "outputId": "085df7e7-3640-47eb-8241-ffbc70661e96", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "125.0" ] }, "metadata": {}, "execution_count": 6 } ], "source": [ "# potencias en asignación\n", "x **= 3 # equivale a x = x ** 3\n", "x" ] }, { "cell_type": "code", "execution_count": 7, "id": "abef53a3", "metadata": { "id": "abef53a3", "outputId": "5c9162f0-bf84-4f19-aeee-ddb38d52838c", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "127.0" ] }, "metadata": {}, "execution_count": 7 } ], "source": [ "x += 2\n", "x" ] }, { "cell_type": "code", "execution_count": 8, "id": "02a013ec", "metadata": { "id": "02a013ec", "outputId": "2d8397ed-4fbc-4692-bdfd-2b71fd35d07c", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "2.0" ] }, "metadata": {}, "execution_count": 8 } ], "source": [ "# módulo en asignación\n", "x %= 5 # equivale a x = x % 5\n", "x" ] }, { "cell_type": "code", "execution_count": 9, "id": "444ddc33", "metadata": { "id": "444ddc33", "outputId": "a99bafd2-2b10-4130-cf06-b37040a760c1", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "32.0" ] }, "metadata": {}, "execution_count": 9 } ], "source": [ "x += 30\n", "x" ] }, { "cell_type": "code", "execution_count": 10, "id": "ca6a5a40", "metadata": { "id": "ca6a5a40", "outputId": "1484f9d4-5bf0-4e5f-a657-ccd34e6b9464", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "6.0" ] }, "metadata": {}, "execution_count": 10 } ], "source": [ "# división entera en asignación\n", "x //= 5 # equivale a x = x // 5\n", "x" ] }, { "cell_type": "markdown", "id": "d6615f7a", "metadata": { "id": "d6615f7a" }, "source": [ "### Ejercicio\n", "Suma los números pares entre 0 y 100." ] }, { "cell_type": "markdown", "id": "e295e28e", "metadata": { "id": "e295e28e" }, "source": [ "#### Solución 1 con un bucle ```for```" ] }, { "cell_type": "code", "execution_count": 11, "id": "64230a28", "metadata": { "id": "64230a28", "outputId": "31a8e618-1187-4347-ce68-7a8f5e905561", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "2550\n" ] } ], "source": [ "total = 0\n", "for i in range(0,101,2):\n", " total += i\n", "\n", "print(total)" ] }, { "cell_type": "markdown", "id": "d2c35fd5", "metadata": { "id": "d2c35fd5" }, "source": [ "#### Solución 2" ] }, { "cell_type": "code", "execution_count": 12, "id": "33f826ce", "metadata": { "id": "33f826ce", "outputId": "d5c8ab2c-cf60-4b54-c092-dd339e6caf4e", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "2550" ] }, "metadata": {}, "execution_count": 12 } ], "source": [ "sum(list(range(0,101,2)))" ] }, { "cell_type": "markdown", "id": "861badf4", "metadata": { "id": "861badf4" }, "source": [ "### Ejercicio\n", "Solicite al usuario cuántos números desea introducir, luego pídale los números y finalmente muestre la media de esos valores." ] }, { "cell_type": "code", "execution_count": 13, "id": "2cc105e4", "metadata": { "id": "2cc105e4", "outputId": "52299ed0-c810-4a9f-8d25-7285e5638717", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "¿Cuántos números desea? 4\n", "Indique el número 1: 5\n", "Indique el número 2: 10\n", "Indique el número 3: 7\n", "Indique el número 4: 9\n", "La media es 7.75\n" ] } ], "source": [ "n = int(input('¿Cuántos números desea? '))\n", "suma = 0\n", "for i in range(n):\n", " suma += float(input(f'Indique el número {i+1}: '))\n", "print('La media es',suma/n)" ] } ], "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.8" }, "colab": { "name": "0370_operadores_de_asignacion.ipynb", "provenance": [], "include_colab_link": true } }, "nbformat": 4, "nbformat_minor": 5 }