{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "view-in-github", "colab_type": "text" }, "source": [ "\"Open" ] }, { "cell_type": "markdown", "id": "f8434e2b", "metadata": { "id": "f8434e2b" }, "source": [ "# Escribiendo líneas de código" ] }, { "cell_type": "code", "execution_count": 1, "id": "e907aa45", "metadata": { "id": "e907aa45", "outputId": "6c219485-302b-4771-f8d2-30deaf6d2b02", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Hola\n" ] } ], "source": [ "print(\"Hola\") # imprimir en una línea" ] }, { "cell_type": "markdown", "id": "1c107459", "metadata": { "id": "1c107459" }, "source": [ "## Imprimir en varias líneas \n" ] }, { "cell_type": "code", "execution_count": 2, "id": "cc0643a3", "metadata": { "id": "cc0643a3", "outputId": "e1536284-7fc9-4fff-d948-0e00739ae28d", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Esto es\n", "una impresión\n", "de un párrafo\n", "en varias líneas\n" ] } ], "source": [ "print(\"\"\"Esto es\n", "una impresión\n", "de un párrafo\n", "en varias líneas\"\"\")" ] }, { "cell_type": "code", "execution_count": 3, "id": "58f26846", "metadata": { "id": "58f26846", "outputId": "d4623fa6-9ae2-493f-e3f0-4bf42f5a9ea4", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Elija la opción deseada:\n", " 1 Escribir\n", " 2 Editar\n", " 3 Grabar\n", " 4 Borrar\n" ] } ], "source": [ "print('''Elija la opción deseada:\n", " 1 Escribir\n", " 2 Editar\n", " 3 Grabar\n", " 4 Borrar''')" ] }, { "cell_type": "markdown", "id": "0b44c481", "metadata": { "id": "0b44c481" }, "source": [ "### ```\\n``` significa new line" ] }, { "cell_type": "code", "execution_count": 4, "id": "29352df9", "metadata": { "id": "29352df9", "outputId": "9b959fc5-f5a5-457d-a56a-42e8cdcc3ab6", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Seleccione su destino \n", "Playa \n", "Montaña\n" ] } ], "source": [ "print(\"Seleccione su destino \\nPlaya \\nMontaña\") # con \\n hacemos un cambio de línea" ] }, { "cell_type": "markdown", "id": "8af16443", "metadata": { "id": "8af16443" }, "source": [ "### ```\\t``` significa tabulador" ] }, { "cell_type": "code", "execution_count": 5, "id": "28441c30", "metadata": { "id": "28441c30", "outputId": "3dfae26a-29e6-44e3-bbef-155077630a3c", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Seleccione su destino \n", "\tPlaya \n", "\tMontaña\n" ] } ], "source": [ "print(\"Seleccione su destino \\n\\tPlaya \\n\\tMontaña\") # con \\t introducimos un tabulador" ] }, { "cell_type": "markdown", "id": "c84b6e09", "metadata": { "id": "c84b6e09" }, "source": [ "### ```r``` significa raw\n", "Permite escribir el contenido de una cadena en bruto (en crudo)." ] }, { "cell_type": "code", "execution_count": 6, "id": "21c1f3f0", "metadata": { "id": "21c1f3f0", "outputId": "7528d91b-4282-4ffe-b680-4df7573a0d3a", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Seleccione su destino \\n\\tPlaya \\n\\tMontaña\n" ] } ], "source": [ "print(r\"Seleccione su destino \\n\\tPlaya \\n\\tMontaña\") # r Imprime en bruto (raw)" ] }, { "cell_type": "markdown", "source": [ "#### Usando caracteres de escape\n", "Las secuencias de escape permiten incluir caracteres especiales en las cadenas. Lo conseguiremos añadiendo una barra invertida `\\` antes del carácter que deseemos escapar." ], "metadata": { "id": "6oVdySV5UYKC" }, "id": "6oVdySV5UYKC" }, { "cell_type": "markdown", "source": [ "##### sin raw, sin caracter de escape" ], "metadata": { "id": "Y0awSshaWpxc" }, "id": "Y0awSshaWpxc" }, { "cell_type": "code", "source": [ "print(\"Si usamos \\n creamos una nueva línea, quivale a un 'retorno de carro'.\") # no ha funcionado, no se ve el \\n" ], "metadata": { "id": "or1dCPTFUh_7", "outputId": "47a67669-243d-449d-9958-cc2801c337c0", "colab": { "base_uri": "https://localhost:8080/" } }, "id": "or1dCPTFUh_7", "execution_count": 7, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Si usamos \n", " creamos una nueva línea, quivale a un 'retorno de carro'.\n" ] } ] }, { "cell_type": "markdown", "source": [ "##### con raw" ], "metadata": { "id": "genEx6ivWyaY" }, "id": "genEx6ivWyaY" }, { "cell_type": "code", "source": [ "print(r\"Si usamos \\n creamos una nueva línea, quivale a un 'retorno de carro'.\") # ahora si ha funcionado, si se ve el \\n" ], "metadata": { "id": "TSdJRLIcVZ2_", "outputId": "10e8126a-ec6f-4810-e57d-854d9a18e9b0", "colab": { "base_uri": "https://localhost:8080/" } }, "id": "TSdJRLIcVZ2_", "execution_count": 8, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Si usamos \\n creamos una nueva línea, quivale a un 'retorno de carro'.\n" ] } ] }, { "cell_type": "markdown", "source": [ "##### con caracter de escape" ], "metadata": { "id": "i_VaPhmJXBlt" }, "id": "i_VaPhmJXBlt" }, { "cell_type": "code", "source": [ "print(\"Si usamos \\\\n creamos una nueva línea, quivale a un 'retorno de carro'.\") # también funciona con un caracter de escape \\\\n" ], "metadata": { "id": "Bnn4jOvEVloA", "outputId": "af6e1676-1cdd-4c60-822d-4b8e5b8c86fe", "colab": { "base_uri": "https://localhost:8080/" } }, "id": "Bnn4jOvEVloA", "execution_count": 9, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Si usamos \\n creamos una nueva línea, quivale a un 'retorno de carro'.\n" ] } ] }, { "cell_type": "markdown", "id": "0aef9439", "metadata": { "id": "0aef9439" }, "source": [ "## Una única línea de código en varias líneas de texto" ] }, { "cell_type": "markdown", "id": "619db544", "metadata": { "id": "619db544" }, "source": [ "Veamos un ejemplo que luego vamos a transformar." ] }, { "cell_type": "code", "execution_count": 10, "id": "69c75219", "metadata": { "id": "69c75219", "outputId": "ef5bfabc-c5bb-489a-f5bc-16f25ab6d8d7", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Los valores son correctos.\n" ] } ], "source": [ "x = 1\n", "y = 2\n", "z = 3\n", "if x==1 and y==2 and z==3:\n", " print(\"Los valores son correctos.\")" ] }, { "cell_type": "markdown", "id": "d2e8d49f", "metadata": { "id": "d2e8d49f" }, "source": [ "El caso anterior se puede escribir de otra forma.\n", "\n", "* ```;``` Con el punto y coma podemos escribir varias líneas de código en una sola línea de texto.\n", "* ```\\``` Con la barra invertida podemos separar una línea de código en varias líneas de texto." ] }, { "cell_type": "code", "execution_count": 11, "id": "eda3e051", "metadata": { "id": "eda3e051", "outputId": "647c2dc3-ce41-447d-f842-9700ee30d28b", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Los valores son correctos.\n" ] } ], "source": [ "x=1; y=2; z=3\n", "if x==1 and \\\n", " y==2 and \\\n", " z==3:\n", " print(\"Los valores son correctos.\")" ] }, { "cell_type": "markdown", "id": "a9c355ce", "metadata": { "id": "a9c355ce" }, "source": [ "## Escribiendo en raw" ] }, { "cell_type": "markdown", "id": "24d8b71d", "metadata": { "id": "24d8b71d" }, "source": [ "### Ejemplo 1" ] }, { "cell_type": "code", "execution_count": 12, "id": "cb178046", "metadata": { "id": "cb178046", "outputId": "313509ab-a4fc-4fb6-bbfc-bdcc0d079f9c", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "C:\\directorio\n", "ombre\n" ] } ], "source": [ "print(\"C:\\directorio\\nombre\") # se imprime incorrectamente en dos líneas" ] }, { "cell_type": "markdown", "id": "d1bb35e0", "metadata": { "id": "d1bb35e0" }, "source": [ "#### Solución 1" ] }, { "cell_type": "code", "execution_count": 13, "id": "25b9c375", "metadata": { "id": "25b9c375", "outputId": "c23b773b-35af-48cc-a65b-d9dc5a069023", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "C:\\directorio\\nombre\n" ] } ], "source": [ "print(r\"C:\\directorio\\nombre\") # r indica que se imprimirá en raw (en crudo)" ] }, { "cell_type": "markdown", "id": "d121577f", "metadata": { "id": "d121577f" }, "source": [ "#### Solución 2" ] }, { "cell_type": "code", "execution_count": 14, "id": "6d3b286b", "metadata": { "id": "6d3b286b", "outputId": "effe127a-8293-4e63-c961-49bdf7d76039", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "C:\\directorio\\nombre\n" ] } ], "source": [ "print(\"C:\\\\directorio\\\\nombre\") # utilizando caracteres de escape" ] }, { "cell_type": "markdown", "id": "e9d700b2", "metadata": { "id": "e9d700b2" }, "source": [ "### Ejemplo 2" ] }, { "cell_type": "code", "execution_count": 15, "id": "9f3729e5", "metadata": { "id": "9f3729e5", "outputId": "48185aa5-a6f7-4286-8b29-b7fe08977af1", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "a\n", "♞\n", "Ⅷ\n", "∑\n", "⏳\n" ] } ], "source": [ "# imprimiendo caracteres Unicode. Se pone \\u y luego el código del carácter Unicode\n", "print(\"\\u0061\") # a\n", "print(\"\\u265E\") # ♞\n", "print(\"\\u2167\") # Ⅷ\n", "print(\"\\u2211\") # ∑\n", "print(\"\\u23F3\") # ⏳" ] }, { "cell_type": "code", "execution_count": 16, "id": "cd162d4b", "metadata": { "id": "cd162d4b" }, "outputs": [], "source": [ "#print(\"C:\\user\\eloisa\") # Se produce un error ya que después de \\u Python espera encontrar un carácter Unicode" ] }, { "cell_type": "markdown", "id": "e382a1b5", "metadata": { "id": "e382a1b5" }, "source": [ "#### Solución 1: con raw" ] }, { "cell_type": "code", "execution_count": 17, "id": "ef9b0cc1", "metadata": { "id": "ef9b0cc1", "outputId": "b960e346-badb-426f-bb4e-dd5fd7878b5f", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "C:\\user\\eloisa\n" ] } ], "source": [ "print(r\"C:\\user\\eloisa\") # solución 1 con r de raw" ] }, { "cell_type": "markdown", "id": "add14f0a", "metadata": { "id": "add14f0a" }, "source": [ "#### Solución 2: con caracter de escape" ] }, { "cell_type": "code", "execution_count": 18, "id": "8ecc7e6b", "metadata": { "id": "8ecc7e6b", "outputId": "76aedb1d-221c-4e2d-a93e-d644f99cd53e", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "C:\\user\\eloisa\n" ] } ], "source": [ "print(\"C:\\\\user\\\\eloisa\") # solución 2 con caracteres de escape" ] }, { "cell_type": "markdown", "id": "364fe9c3", "metadata": { "id": "364fe9c3" }, "source": [ "## Variables con caracteres de impresión" ] }, { "cell_type": "code", "execution_count": 19, "id": "d0875f67", "metadata": { "id": "d0875f67", "outputId": "0b9a3fda-f6ed-47cd-ef61-43f29925ba1d", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Primero \tiremos a la playa \n", "Segundo \taprenderemos a nadar\n" ] } ], "source": [ "frase = \"Primero \\tiremos a la playa \\nSegundo \\taprenderemos a nadar\"\n", "print(frase)" ] }, { "cell_type": "markdown", "id": "14ea54a6", "metadata": { "id": "14ea54a6" }, "source": [ "## Imprimiendo línea vacía\n", "Usando print()" ] }, { "cell_type": "code", "execution_count": 20, "id": "430a93e6", "metadata": { "id": "430a93e6", "outputId": "1845f25d-930a-4916-f916-334465573de7", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Playa\n", "Montaña\n", "\n", "Elija sus vacaciones favoritas\n" ] } ], "source": [ "print('Playa')\n", "print('Montaña')\n", "print()\n", "print('Elija sus vacaciones favoritas')" ] }, { "cell_type": "code", "execution_count": 21, "id": "b92e09e4", "metadata": { "id": "b92e09e4", "outputId": "1250e2fa-91ae-4c34-a570-65f25d867d29", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Playa\n", "Montaña\n", "\n", "Elija sus vacaciones favoritas\n" ] } ], "source": [ "print('Playa')\n", "print('Montaña')\n", "print('')\n", "print('Elija sus vacaciones favoritas')" ] }, { "cell_type": "code", "execution_count": 22, "id": "4e101455", "metadata": { "id": "4e101455", "outputId": "8889e33f-49a3-4b6d-ba41-f650a8bb0144", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Playa\n", "Montaña\n", "\n", "Elija sus vacaciones favoritas\n" ] } ], "source": [ "print('Playa')\n", "print('Montaña\\n')\n", "print('Elija sus vacaciones favoritas')" ] }, { "cell_type": "markdown", "id": "07757f42", "metadata": { "id": "07757f42" }, "source": [ "## La coma como separador\n", "En un print podemos separar con coma que es un separador que imprime un espacio. \n", "Este separador se puede cambiar." ] }, { "cell_type": "code", "execution_count": 23, "id": "b582d57c", "metadata": { "id": "b582d57c", "outputId": "4290b2bd-c9c1-44c3-bfac-8f2440eb8d8c", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Madrid París Roma\n" ] } ], "source": [ "m = \"Madrid\"\n", "p = \"París\"\n", "r = \"Roma\"\n", "print(m, p, r) # al imprimir se separan las ciudades con un espacio" ] }, { "cell_type": "code", "execution_count": 24, "id": "6280dc51", "metadata": { "id": "6280dc51", "outputId": "ba21f1c7-f054-4960-96e3-89d20855736c", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Madrid;París;Roma\n" ] } ], "source": [ "print(m, p, r, sep=\";\") # cambiamos el separador a un punto y coma" ] }, { "cell_type": "code", "execution_count": 25, "id": "6eb74123", "metadata": { "id": "6eb74123", "outputId": "491dbfb3-54fd-430c-c739-38bb03afe302", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Madrid\n", "París\n", "Roma\n" ] } ], "source": [ "print(m, p, r, sep=\"\\n\") # cambiamos el separador a un retorno de carro" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.11" }, "colab": { "name": "0080_imprimir.ipynb", "provenance": [], "include_colab_link": true } }, "nbformat": 4, "nbformat_minor": 5 }