{ "cells": [ { "cell_type": "markdown", "id": "4f3df9a8-7646-4644-a79b-4380876ed3fc", "metadata": { "tags": [] }, "source": [ "Operadores --- 10 min\n", "===\n", "\n", "* 10 min | Última modificación: Octubre 4, 2021" ] }, { "cell_type": "markdown", "id": "c325f5eb-7564-43a8-9766-b97bf9e11195", "metadata": {}, "source": [ "Operadores aritméticos\n", "---\n" ] }, { "cell_type": "markdown", "id": "167666b9-3c69-4d49-9038-0439c04e67ad", "metadata": {}, "source": [ "\n", "```\n", "Operador Nombre\n", "-------------------------------------------------------------------------------\n", "+ Suma\n", "- Resta\n", "* Multiplicación\n", "/ Division\n", "// Parte entera de la división\n", "% Residuo de la división\n", "** Potenciación\n", "````\n" ] }, { "cell_type": "code", "execution_count": 1, "id": "e7f51fb0-721b-45af-a1e3-be4fe81315fe", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1.0" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "9 / 3 ** 2" ] }, { "cell_type": "code", "execution_count": 2, "id": "bf36d6c4-9380-4021-83a5-22f06656be6e", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "5 // 3" ] }, { "cell_type": "code", "execution_count": 3, "id": "98b7ded9-62a9-45c4-84ae-86834bce2a7c", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "5 % 3" ] }, { "cell_type": "markdown", "id": "c8fc52c7-adbf-4adf-bcf9-a1647f0126ac", "metadata": {}, "source": [ "Operadores de asignación\n", "----" ] }, { "cell_type": "markdown", "id": "db4d9064-ffef-4812-bbe3-438370c41fb8", "metadata": {}, "source": [ "```\n", "Operador Ejemplo Equivalente a\n", "----------------------------------------\n", "+= x += 2 x = x + 2\n", "-= x -= 2 x = x - 2\n", "*= x *= 3 x = x * 3\n", "/= x /= 2 x = x / 2\n", "%= x %= 2 x = x % 2\n", "//= x //= 2 x = x // 2\n", "**= x **= 2 x = x ** 2\n", "```" ] }, { "cell_type": "code", "execution_count": 4, "id": "b1d29514-e90f-4634-8b76-65ba6e78f49d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1.0" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#\n", "# El operador es equivalente a x = x / (2 + 2)\n", "x = 4\n", "x /= 2 + 2\n", "x" ] }, { "cell_type": "markdown", "id": "57da0195-4a13-44b1-b36c-571a334b5d19", "metadata": { "tags": [] }, "source": [ "## Operadores lógicos" ] }, { "cell_type": "markdown", "id": "ab557898-31cf-472e-8f1c-870fb309c4dd", "metadata": {}, "source": [ "```\n", "Operador Nombre\n", "-------------------------------------------------------------------------------\n", "> Mayor que\n", ">= Mayor o igual que\n", "< Menor que\n", "<= Menor o igual que\n", "== Igual\n", "!= Diferente\n", "```" ] }, { "cell_type": "code", "execution_count": 5, "id": "515b7c21-6fa3-467c-8256-48162d88ff10", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 > 2" ] }, { "cell_type": "code", "execution_count": 6, "id": "e1734e3c-8d92-449d-af20-33a96faf3714", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#\n", "# float == int\n", "#\n", "1.0 == 1" ] }, { "cell_type": "code", "execution_count": 7, "id": "fdb50639-bef9-4d53-a247-7ee6350ce489", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#\n", "# El mismo tipo, no el mismo valor\n", "#\n", "type(1.0) == type(1)" ] }, { "cell_type": "code", "execution_count": 8, "id": "bcc30384-a437-49e3-bc4b-a41d3562a194", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "True == 1" ] }, { "cell_type": "code", "execution_count": 9, "id": "90c7ff29-50ec-49fb-aa46-75ce9f9015e8", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"Hola\" == \"hola\"" ] }, { "cell_type": "markdown", "id": "ff6d0f3c-eb49-41a5-9ea2-56b17d6b05d8", "metadata": { "tags": [] }, "source": [ "Conectores lógicos\n", "---" ] }, { "cell_type": "markdown", "id": "b0049870-2573-49f8-b5e0-e58c44dcd70a", "metadata": {}, "source": [ "```\n", "Operador Nombre\n", "-------------------------------------------------------------------------------\n", "and AND lógico\n", "or OR lógico\n", "not Negación\n", "```" ] }, { "cell_type": "markdown", "id": "ca2ecdf9-9d69-487f-b87a-3bfd76eef7bf", "metadata": {}, "source": [ "Tablas de Verdad\n", "\n", " x y (x OR y) (x AND y)\n", " ----------------------------------\n", " True True True True\n", " True False True False\n", " False True True False\n", " False False False False" ] }, { "cell_type": "code", "execution_count": 10, "id": "3c0a490e-eb0b-4fcb-9b4b-c011a625f34f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "7 > 8 and \"hola\" == \"hola\"" ] }, { "cell_type": "code", "execution_count": 11, "id": "cda2eb84-2d94-44f3-8c38-5e56365de16f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "True and True and 1 > 0" ] }, { "cell_type": "code", "execution_count": 12, "id": "dc897790-f4c3-422e-bb31-bbd105f8c6f0", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(2 > 1) and (3 < 2) or (3 > 2)" ] }, { "cell_type": "code", "execution_count": 13, "id": "a4ab77f9-a03f-44a5-b008-295f05adc2b2", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "not 1 > 2" ] }, { "cell_type": "code", "execution_count": 14, "id": "3b374384-d324-485e-bf60-2c8c320a31c4", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "not True" ] }, { "cell_type": "code", "execution_count": 15, "id": "fa8370fb-8df2-4818-901b-500897049de9", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "not (1 < 2 or 5 >= 5 or True)" ] }, { "cell_type": "code", "execution_count": 23, "id": "a77d9708-a088-453c-b482-3c5e85c7e637", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#\n", "# Shortcircuit\n", "#\n", "# Si es un AND, el cómputo se detiene\n", "# al encontrar el primer valor False.\n", "#\n", "# Si es un OR, el computo se detiene\n", "# al encontrar el primer valor True.\n", "#\n", "False and True and True and True" ] }, { "cell_type": "markdown", "id": "e7bb194d-79f3-49d8-8e1d-9ed7fc55bc85", "metadata": {}, "source": [ "Operadores de identidad\n", "---" ] }, { "cell_type": "markdown", "id": "3a49370e-e90a-49f2-b645-c80fc475cbbb", "metadata": {}, "source": [ "```\n", "Operador Descripción\n", "-------------------------------------------------------------------------------\n", "is Retorna True cuando los operados son el mismo objeto\n", "is not Retorna True cuando los operados NO son el mismo objeto\n", "```\n" ] }, { "cell_type": "code", "execution_count": 17, "id": "6962aacb-43da-42d6-9801-c17cffbd4084", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 is None" ] }, { "cell_type": "code", "execution_count": 18, "id": "73905b9e-930e-46c1-bfc1-808a7b7f5c0b", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "True is None" ] }, { "cell_type": "code", "execution_count": 19, "id": "a020edd0-3d81-4278-80bf-5865fae98b74", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "False is None" ] }, { "cell_type": "code", "execution_count": 20, "id": "6837d782-6835-4452-bf37-9fd7ea202563", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "None is None" ] }, { "cell_type": "markdown", "id": "2336e8b2-bf74-4ea3-9158-37184b9fce51", "metadata": {}, "source": [ "Operadores de pertenencia\n", "---" ] }, { "cell_type": "markdown", "id": "9abf4e43-888a-4c62-887e-6f1bf21a1e4c", "metadata": {}, "source": [ "```\n", "Operador Descripción\n", "-------------------------------------------------------------------------------\n", "in Retorna verdadero cuando el 1er operando está presente en el 2do \n", " operando\n", "not in Retorna verdadero cuando el 1er operando NO está presente en el 2do \n", " operando\n", "```" ] }, { "cell_type": "code", "execution_count": 21, "id": "67f023ce-b56f-42c5-a347-8fab23e2d418", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"mundo\" in \"Hola mundo cruel!\"" ] }, { "cell_type": "code", "execution_count": 22, "id": "e78e603f-dd65-4363-837f-40cfa17f074b", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"Mundo\" in \"Hola mundo cruel!\"" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3.10.7 ('.venv': venv)", "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.10.7" }, "vscode": { "interpreter": { "hash": "99683f87c1354598af39114791cc78b29c5617e49c4bfa4c5fb7492534eeeb87" } } }, "nbformat": 4, "nbformat_minor": 5 }