{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "view-in-github", "colab_type": "text" }, "source": [ "\"Open" ] }, { "cell_type": "markdown", "id": "b7786f7a", "metadata": { "id": "b7786f7a" }, "source": [ "# Booleanos: True, False\n" ] }, { "cell_type": "code", "execution_count": 1, "id": "df8694c5", "metadata": { "id": "df8694c5", "outputId": "ee90cd19-a5bd-492a-c071-564b4b6ef612", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "True" ] }, "metadata": {}, "execution_count": 1 } ], "source": [ "5 == 5 # True Operador de comparación: 'Igual a'" ] }, { "cell_type": "code", "execution_count": 2, "id": "2528485c", "metadata": { "id": "2528485c", "outputId": "9a44b00d-17ca-416c-e6fe-39203ff66a91", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "False" ] }, "metadata": {}, "execution_count": 2 } ], "source": [ "5 >= 7 # False Mayor o igual que" ] }, { "cell_type": "code", "execution_count": 3, "id": "57fc1da5", "metadata": { "id": "57fc1da5", "outputId": "4821faaf-260a-4ba1-d1a9-5634e844ff77", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "False" ] }, "metadata": {}, "execution_count": 3 } ], "source": [ "9 == '9' # False" ] }, { "cell_type": "code", "execution_count": 4, "id": "db50c39e", "metadata": { "id": "db50c39e", "outputId": "402f0d75-b83a-4389-aafd-eb0edff65fe1", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "True" ] }, "metadata": {}, "execution_count": 4 } ], "source": [ "'Roma' > 'Bogotá' # True" ] }, { "cell_type": "code", "execution_count": 5, "id": "232a68ba", "metadata": { "id": "232a68ba", "outputId": "26857489-85e4-4a27-b778-10cd4610d050", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "False" ] }, "metadata": {}, "execution_count": 5 } ], "source": [ "4 != 4 # False Operador de comparación: Distinto o 'no es igual a'" ] }, { "cell_type": "markdown", "id": "64cab790", "metadata": { "id": "64cab790" }, "source": [ "## Comparación de Booleanos" ] }, { "cell_type": "code", "execution_count": 6, "id": "f68c8a65", "metadata": { "id": "f68c8a65", "outputId": "1b8e2dbe-e65a-4042-92ac-bff3f4747bd9", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "True" ] }, "metadata": {}, "execution_count": 6 } ], "source": [ "True == True # True" ] }, { "cell_type": "code", "execution_count": 7, "id": "05a3defa", "metadata": { "id": "05a3defa", "outputId": "72a4ddc7-a207-4faf-a910-64a32850e5b4", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "False" ] }, "metadata": {}, "execution_count": 7 } ], "source": [ "True == False # False" ] }, { "cell_type": "code", "execution_count": 8, "id": "399a072f", "metadata": { "id": "399a072f", "outputId": "a44e3544-2f0c-48fd-8073-861a89c4b881", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "True" ] }, "metadata": {}, "execution_count": 8 } ], "source": [ "True != False # True" ] }, { "cell_type": "markdown", "id": "f53d11a1", "metadata": { "id": "f53d11a1" }, "source": [ "### Verdadero es 1 y Falso es 0" ] }, { "cell_type": "code", "execution_count": 9, "id": "c971002f", "metadata": { "id": "c971002f", "outputId": "2ea9cd0a-d63f-4a0b-c6ca-14127b04c3d9", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "True" ] }, "metadata": {}, "execution_count": 9 } ], "source": [ "True == 1 # True" ] }, { "cell_type": "code", "execution_count": 10, "id": "1b8e0cdc", "metadata": { "id": "1b8e0cdc", "outputId": "b91d6393-4101-41a0-9a4a-e9fa1fdaee47", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "True" ] }, "metadata": {}, "execution_count": 10 } ], "source": [ "False == 0 # True" ] }, { "cell_type": "code", "execution_count": 11, "id": "afb4fe60", "metadata": { "id": "afb4fe60", "outputId": "5200dd4e-9f83-48a8-84d2-906c29122571", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "3" ] }, "metadata": {}, "execution_count": 11 } ], "source": [ "True *3 # 3" ] }, { "cell_type": "code", "execution_count": 12, "id": "bd7872b7", "metadata": { "id": "bd7872b7", "outputId": "0af699bd-c4c8-454b-afe6-ad29e9f89beb", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "-1" ] }, "metadata": {}, "execution_count": 12 } ], "source": [ "2*(-True)+False+True # -1" ] }, { "cell_type": "code", "execution_count": 13, "id": "e44b9664", "metadata": { "id": "e44b9664", "outputId": "52153524-2d09-4006-91ff-a88d3e8c59fb", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "True" ] }, "metadata": {}, "execution_count": 13 } ], "source": [ "True > False # True" ] }, { "cell_type": "code", "execution_count": 14, "id": "62e8618c", "metadata": { "id": "62e8618c", "outputId": "635c33be-c3ca-450e-edb9-6188ba432c80", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "False" ] }, "metadata": {}, "execution_count": 14 } ], "source": [ "False > True # False" ] }, { "cell_type": "code", "execution_count": 15, "id": "ef4904f4", "metadata": { "id": "ef4904f4", "outputId": "dcd3ebb8-d4e9-4bba-eb1e-2bb4c54e57ec", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "0" ] }, "metadata": {}, "execution_count": 15 } ], "source": [ "True * False # 0" ] }, { "cell_type": "markdown", "id": "3a67c098", "metadata": { "id": "3a67c098" }, "source": [ "## Comparando varios simultáneamente" ] }, { "cell_type": "code", "execution_count": 16, "id": "95560b07", "metadata": { "id": "95560b07", "outputId": "d374ec11-4b26-41f6-d262-951b00dd5a20", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "True" ] }, "metadata": {}, "execution_count": 16 } ], "source": [ "1 < 2 < 3 # True" ] }, { "cell_type": "markdown", "id": "2312cf05", "metadata": { "id": "2312cf05" }, "source": [ "## Operando con variables" ] }, { "cell_type": "code", "execution_count": 17, "id": "a3520208", "metadata": { "id": "a3520208", "outputId": "7dbb297a-1851-47c8-e274-8de7d4dfa036", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "True" ] }, "metadata": {}, "execution_count": 17 } ], "source": [ "a = 5\n", "b = 10\n", "b == 2*a # True No confundir == (comparación) con = (asignación)" ] }, { "cell_type": "markdown", "id": "dcb0f085", "metadata": { "id": "dcb0f085" }, "source": [ "### Trabajando con strings" ] }, { "cell_type": "code", "execution_count": 18, "id": "6e7c3801", "metadata": { "id": "6e7c3801", "outputId": "6d4cac7d-9bd8-46bb-80ae-4f18c0ae127f", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "True" ] }, "metadata": {}, "execution_count": 18 } ], "source": [ "x = \"Python\"\n", "x[-1] == 'n' # True" ] }, { "cell_type": "markdown", "id": "380c7963", "metadata": { "id": "380c7963" }, "source": [ "## type()" ] }, { "cell_type": "code", "execution_count": 19, "id": "e7f6ece4", "metadata": { "id": "e7f6ece4", "outputId": "f7b1f478-283d-4bc7-82ce-394285a3af8e", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "bool" ] }, "metadata": {}, "execution_count": 19 } ], "source": [ "type(False)" ] }, { "cell_type": "code", "execution_count": 20, "id": "49d448d5", "metadata": { "id": "49d448d5", "outputId": "ef6137fc-546a-45f3-c9d7-6603a175042e", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "int" ] }, "metadata": {}, "execution_count": 20 } ], "source": [ "type(5)" ] }, { "cell_type": "code", "execution_count": 21, "id": "78106204", "metadata": { "id": "78106204", "outputId": "ba908cef-c30c-4a50-b604-cde962e0c1fc", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "str" ] }, "metadata": {}, "execution_count": 21 } ], "source": [ "type(\"Hola\")" ] }, { "cell_type": "markdown", "id": "18bd64db", "metadata": { "id": "18bd64db" }, "source": [ "## not" ] }, { "cell_type": "code", "execution_count": 22, "id": "82da5b0c", "metadata": { "id": "82da5b0c", "outputId": "68e821ad-0204-4eca-8ea6-80e705222f66", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "False" ] }, "metadata": {}, "execution_count": 22 } ], "source": [ "not True # False" ] }, { "cell_type": "code", "execution_count": 23, "id": "c88d5fe1", "metadata": { "id": "c88d5fe1", "outputId": "f3fe8f14-519c-4001-fc43-34ccb5f8e0cd", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "True" ] }, "metadata": {}, "execution_count": 23 } ], "source": [ "not False # True" ] }, { "cell_type": "markdown", "id": "b16df3de", "metadata": { "id": "b16df3de" }, "source": [ "## Distinto ```!=```" ] }, { "cell_type": "code", "execution_count": 24, "id": "6e1042bc", "metadata": { "id": "6e1042bc", "outputId": "9f67cf98-a3ff-468a-adc2-c2c70a235baa", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "False" ] }, "metadata": {}, "execution_count": 24 } ], "source": [ "4 != 4 # False" ] }, { "cell_type": "code", "execution_count": 25, "id": "f43b201e", "metadata": { "id": "f43b201e", "outputId": "e83c08d1-b83b-48d9-fd36-489f6e7e2b25", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "True" ] }, "metadata": {}, "execution_count": 25 } ], "source": [ "4 != 5 # True" ] }, { "cell_type": "markdown", "id": "bbdaaf93", "metadata": { "id": "bbdaaf93" }, "source": [ "## Operador ```is``` y operador ```is not```\n", "Permiten comparar **objetos idénticos**." ] }, { "cell_type": "code", "execution_count": 26, "id": "ef86a604", "metadata": { "id": "ef86a604", "outputId": "872f1a19-ff8d-4fd7-be09-83cc578d94aa", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "True" ] }, "metadata": {}, "execution_count": 26 } ], "source": [ "x = 5\n", "y = 5\n", "x is y # True" ] }, { "cell_type": "markdown", "id": "cf79c500", "metadata": { "id": "cf79c500" }, "source": [ "Dos listas son idénticas cuando se trata del mismo objeto, cuando ocupan el mismo lugar en memoria." ] }, { "cell_type": "code", "execution_count": 27, "id": "e820b6c0", "metadata": { "id": "e820b6c0", "outputId": "83f4c1bf-9887-4818-8bbc-0309677ffc89", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "False" ] }, "metadata": {}, "execution_count": 27 } ], "source": [ "p = [1, 2, 3]\n", "q = [1, 2, 3]\n", "p is q # False ¡Sorpresa!" ] }, { "cell_type": "code", "execution_count": 28, "id": "d6c03c21", "metadata": { "id": "d6c03c21", "outputId": "5bb4740e-8595-458b-cdc2-e4a3a68169dc", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "True" ] }, "metadata": {}, "execution_count": 28 } ], "source": [ "p is not q # True" ] }, { "cell_type": "code", "execution_count": 29, "id": "a207ec59", "metadata": { "id": "a207ec59", "outputId": "2f4eaa2f-878a-4c20-9634-41b5bea7c7d0", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "True" ] }, "metadata": {}, "execution_count": 29 } ], "source": [ "p == q # True" ] }, { "cell_type": "markdown", "id": "5a745ab4", "metadata": { "id": "5a745ab4" }, "source": [ "### Comprobación con el método ```id()```\n", "El método id() devuelve un identificador del **objeto**. \n", "Veamos que ambos identificadores difieren." ] }, { "cell_type": "code", "execution_count": 30, "id": "59833715", "metadata": { "id": "59833715", "outputId": "d85204c4-d984-414b-8202-472a0212a564", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "135237686210560" ] }, "metadata": {}, "execution_count": 30 } ], "source": [ "id(p) # identificador del objeto" ] }, { "cell_type": "code", "execution_count": 31, "id": "f61f6ed2", "metadata": { "id": "f61f6ed2", "outputId": "8e3eb758-ffdb-44d1-b8ca-e1283fe75853", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "135237686092224" ] }, "metadata": {}, "execution_count": 31 } ], "source": [ "id(q) # un identificador distinto" ] }, { "cell_type": "markdown", "id": "233a517f", "metadata": { "id": "233a517f" }, "source": [ "### Idéntica lista" ] }, { "cell_type": "code", "execution_count": 32, "id": "8b34e1bf", "metadata": { "id": "8b34e1bf", "outputId": "8933b115-0acb-4ed6-d7d2-91c8382625ad", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "[1, 2, 3]" ] }, "metadata": {}, "execution_count": 32 } ], "source": [ "p = [1, 2, 3]\n", "q = p # q es una lista idéntica a p\n", "q" ] }, { "cell_type": "code", "execution_count": 33, "id": "48f91131", "metadata": { "id": "48f91131", "outputId": "18f1251e-6bcd-47b8-a0e2-7d6106fdf13c", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "True" ] }, "metadata": {}, "execution_count": 33 } ], "source": [ "q is p # ambas listas ocupan el mismo lugar de memoria" ] }, { "cell_type": "code", "execution_count": 34, "id": "bcd834b9", "metadata": { "id": "bcd834b9", "outputId": "51d8b954-89d7-4063-a975-f7f2ea181acf", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "135237686336640" ] }, "metadata": {}, "execution_count": 34 } ], "source": [ "id(p) # el identificador de la lista p" ] }, { "cell_type": "code", "execution_count": 35, "id": "68b2bfae", "metadata": { "id": "68b2bfae", "outputId": "0af6d3f4-e7d2-4e18-962f-1ffefe425ec1", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "135237686336640" ] }, "metadata": {}, "execution_count": 35 } ], "source": [ "id(q) # es el mismo que el identificado de la lista q" ] } ], "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": "0120_booleanos.ipynb", "provenance": [], "include_colab_link": true } }, "nbformat": 4, "nbformat_minor": 5 }