Sesion1 Ipynb
AI-enhanced title
"nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "name": "SesionPython1.ipynb", "provenance": [], "collapsed_sections": [] }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" } }, "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "id": "9FGMZD7WyboH" }, "outputs": [], "source": [ "#Python" ] }, { "cell_type": "markdown", "source": [ "#Python\n", "\n", "## Variables de Python y Booleanos\n", "\n", "Se define como las magniudes que tienden a sufrir modificaciones o cambiosen un determinado dominio\n", "\n", "Las variables son tipos de datos o estructuras de datos que se utilizanpara almacenar cierta información." ], "metadata": { "id": "MsLBAPP5yn1C" } }, { "cell_type": "code", "source": [ "a = 2\n", "print(a)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "65AKqlKmzPyT", "outputId": "19b16214-1803-43d1-ae16-dbf2db8a4896" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "2\n" ] } ] }, { "cell_type": "code", "source": [ "b = 4\n", "a = b\n", "print(a)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "LmuJx3f4zfNT", "outputId": "66b5aef7-a43e-4b94-8159-c73c75e2729c" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "4\n" ] } ] }, { "cell_type": "markdown", "source": [ "**¿Comó nombramos variables?**\n", "* Las variables 'distinguen entre mayúsculas y minúsculas'; \n", "* Los números no pueden preceder a los caracteres; \n", "* Los símbolos, como @, no se pueden utilizar solos, solo el guión bajo'_' es un nombre de variable válido." ], "metadata": { "id": "KI2ikssMzrVK" } }, { "cell_type": "code", "source": [ "Variable = 2\n", "Variable" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "v2Q7V7OI0Bgr", "outputId": "9bc22719-8e17-454e-8c65-0538a938ae32" }, "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "2" ] }, "metadata": {}, "execution_count": 6 } ] }, { "cell_type": "code", "source": [ "variable" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 165 }, "id": "hE-ZSZ950XAK", "outputId": "b162d6db-a6bf-47c4-acf1-fe71d6d7f321" }, "execution_count": null, "outputs": [ { "output_type": "error", "ename": "NameError", "evalue": "ignored", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\u001b[0mTraceback (most recent call last)", "\u001b[0;32m<ipython-input-7-1748287bc46a>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mvariable\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mNameError\u001b[0m: name 'variable' is not defined" ] } ] }, { "cell_type": "code", "source": [ "12AAA = 3" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 130 }, "id": "CQdRJpBp0ZGB", "outputId": "875c6cc5-9523-4542-c283-6cc02669c24e" }, "execution_count": null, "outputs": [ { "output_type": "error", "ename": "SyntaxError", "evalue": "ignored", "traceback": [ "\u001b[0;36m File \u001b[0;32m\"<ipython-input-8-66f3ea7fb05f>\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m 12AAA = 3\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0minvalid syntax\n" ] } ] }, { "cell_type": "code", "source": [ "_ejemplo = 3\n", "_ejemplo" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "qHM_JbpF0cYJ", "outputId": "37458415-17e8-4b25-8961-582fd9b0fd26" }, "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "3" ] }, "metadata": {}, "execution_count": 9 } ] }, { "cell_type": "markdown", "source": [ "La función print(), que es la función predeterminada de Python paraimprimir resultados. Si usamos la función print(), podemos hacerlo en la mismacelda:" ], "metadata": { "id": "i0suDzSk1OGD" } }, { "cell_type": "code", "source": [ "print(_ejemplo)\n", "print(\"Hola Mundo\")\n", "print(_ejemplo, Variable)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "4ZisdX8OzmB7", "outputId": "16dd4c30-893e-480d-ca11-8a6c420824c6" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "3\n", "Hola Mundo\n", "3 2\n" ] } ] }, { "cell_type": "markdown", "source": [ "## Operadores Python\n", "\n", "La suma, resta, multiplicación y división se realizan con los signos delteclado correspondientes a los operadores." ], "metadata": { "id": "YqjQz73V180J" } }, { "cell_type": "code", "source": [ "20/5" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "DkMjeir6166C", "outputId": "0e564394-4b0e-4e5e-f2ca-82a21db10179" }, "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "4.0" ] }, "metadata": {}, "execution_count": 14 } ]},{ "cell_type": "code", "source": [ "2**3" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "5MB8gMON2QZ3", "outputId": "c18543a6-360e-45e1-e397-8dca1e10dbf5" }, "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "8" ] }, "metadata": {}, "execution_count": 15 } ]},{ "cell_type": "code", "source": [ "5==4" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "8AZRmTGw2Rz-", "outputId": "0069b121-da86-41c4-e360-da5673546a6c" }, "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "False" ] }, "metadata": {}, "execution_count": 16 } ]},{ "cell_type": "markdown", "source": [ "Los operadores lógicos son and – or – not, tratan con valores booleanos yel trato es que las dos declaraciones son verdaderas o no." ], "metadata": { "id": "wQXSEbat24wE" } }, { "cell_type": "code", "source": [ "True and False" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "BFAhAf5j28ao", "outputId": "e08c22bf-1f08-4637-ea4c-d3856dc3e31e" }, "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "False" ] }, "metadata": {}, "execution_count": 18 } ] }, { "cell_type": "code", "source": [ "(3==4) or (4 == 4)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "KTkwHvjQ3AmU", "outputId": "c68b9782-9689-48c3-8da2-f5d583c63e75" }, "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "True" ] }, "metadata": {}, "execution_count": 19 } ]},{ "cell_type": "markdown", "source": [ "##Cadenas de Texto\n", "Una cadena en Python puede ser una combinación de caracteres y espacios" ], "metadata": { "id": "7z68vjQk3T4X" }},{ "cell_type": "code", "source": [ "nombre = \"Remberto Quispe\"\n", "print(nombre)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "HMxbHl1P3r5G", "outputId": "8f078966-605f-448f-f457-377369bdfe5c" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Remberto Quispe\n" ] } ]},{ "cell_type": "code", "source": [ "print(nombre[3:9])" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "VSGwQ_6q3yAb", "outputId": "070b2a37-5df0-4472-f6d0-3b3f5ca88038" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "berto \n" ] } ]},{ "cell_type": "code", "source": [ "print(nombre[-6:])" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "wqmT7gzu35D6", "outputId": "699d90f0-7dac-4a05-bc0b-ee36c5a43764" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Quispe\n" ] } ]},{ "cell_type": "code", "source": [ "primer, apellido = nombre.split(\" \")\n", "print(primer, apellido)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "xakLfU464NKL", "outputId": "7472eca8-8816-4c41-cdb7-2a39c21b5bda" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Remberto Quispe\n" ] } ]},{ "cell_type": "code", "source": [ "print(\"Primer Nombre: \"+ primer + \" Apellido: \" + apellido)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "GQi3gYcB4ZXh", "outputId": "91c048f7-9e1e-4e5b-e1aa-7ee0c2d126fa" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Primer Nombre: Remberto Apellido: Quispe\n" ] } ]},{ "cell_type": "code", "source": [ "edades = \"21, 22, 23, 24, 43, 34, 32\"\n", "edades_ = edades.split(\", \")\n", "print(edades_)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "zHEfkQdd4lF5", "outputId": "12dfb9cc-74fb-4dc7-c50e-4b402267959a" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "['21', '22', '23', '24', '43', '34', '32']\n" ] } ]},{ "cell_type": "code", "source": [ "edades_int = (map(int, edades_))\n", "print(edades_int)\n", "print(type(edades_int))" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "qgI7IhgQ4irC", "outputId": "48d76511-2e46-4de1-af7c-5360e834349f" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "<map object at 0x7f72940d7150>\n", "<class 'map'>\n" ] } ] }, { "cell_type": "code", "source": [ "edades_int = list(map(int, edades_))\n", "print(edades_int)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "9RCB3Ih96Zw5", "outputId": "a4ab7dbf-8643-4f74-abba-7ccef2827bad" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "[21, 22, 23, 24, 43, 34, 32]\n" ] } ] }, { "cell_type": "markdown", "source": [ "##Declaraciones Condicionales de Python\n", "Un programa se ejecuta en una secuencia particular, desde la primera líneade código hasta el final, pero a veces tenemos que decidir qué secuencia debeseguir, dependiendo de ciertas reglas. La ramificación nos permite ejecutardiferentes declaraciones para diferentes entradas. Es útil pensar en unadeclaración **'if'** como una habitación cerrada: si la declaración es *Verdadera(True)*, podemos ingresar a la habitación y el programa ejecutará algunas tareaspredefinidas, pero si la declaración es *Falsa (False)*, el programa ignorará latarea.\n", ". " ], "metadata": { "id": "TpeqAz5f7AZ5" } }, { "cell_type": "code", "source": [ "edad = 19\n", "if(edad > 18):\n", " print(\"Puedes entrar\")\n", "print(\"continue\")" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "gEqrmd8R7wJD", "outputId": "ca131481-67d3-4f85-e0c8-c905136036d3" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Puedes entrar\n", "continue\n" ] } ]},{ "cell_type": "code", "source": [ "if(edad > 18):\n", " print(\"puedes entrar\")\n", "else:\n", " print(\"no puedes entrar\")\n", " " ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "NiNo1Nrc8FJ6", "outputId": "d7909116-5798-40c2-bb95-7625802d8fcf" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "puedes entrar\n" ] } ]},{ "cell_type": "code", "source": [ "if(edad > 18):\n", " print(\"puedes entrar y tomar un licor\")\n", "elif(edad == 18):\n", " print(\"puedes entrar y tomar cerveza\")\n", "else:\n", " print(\"no puedes entrar\")" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "-Cj2zDLj8QXB", "outputId": "192e61dd-798b-4361-a83c-c106dbf416fb" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "puedes entrar y tomar un licor\n" ] } ] } ]}