├── Introduccion_Python.ipynb └── README.md /Introduccion_Python.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "colab_type": "text", 7 | "id": "T0hkIiNQAY92" 8 | }, 9 | "source": [ 10 | "# Introducción a Python 🐍 💻\n", 11 | "Temas a Tratar:\n", 12 | "\n", 13 | "✅ Variables y tipos de datos\n", 14 | "\n", 15 | "✅ Operadores basicos\n", 16 | "\n", 17 | "✅ Condicionales\n", 18 | "\n", 19 | "✅ Ciclos o Bucles\n", 20 | "\n", 21 | "✅ Listas y diccionarios\n", 22 | "\n", 23 | "✅ Funciones\n", 24 | "\n", 25 | "✅ Clases y objetos" 26 | ] 27 | }, 28 | { 29 | "cell_type": "markdown", 30 | "metadata": {}, 31 | "source": [ 32 | "# Hola mundo 😊" 33 | ] 34 | }, 35 | { 36 | "cell_type": "code", 37 | "execution_count": 1, 38 | "metadata": {}, 39 | "outputs": [ 40 | { 41 | "name": "stdout", 42 | "output_type": "stream", 43 | "text": [ 44 | "Hola mundo!\n" 45 | ] 46 | } 47 | ], 48 | "source": [ 49 | "print(\"Hola mundo!\")" 50 | ] 51 | }, 52 | { 53 | "cell_type": "markdown", 54 | "metadata": { 55 | "colab_type": "text", 56 | "id": "bfCE_EoZXXwU" 57 | }, 58 | "source": [ 59 | "# Variables y tipos de datos\n", 60 | "Las variables son elementos en los cuales guardamos información. Por ejemplo:" 61 | ] 62 | }, 63 | { 64 | "cell_type": "code", 65 | "execution_count": 6, 66 | "metadata": { 67 | "colab": {}, 68 | "colab_type": "code", 69 | "id": "dGSLVRKjZPNR" 70 | }, 71 | "outputs": [], 72 | "source": [ 73 | "mi_nombre = 'Ana'" 74 | ] 75 | }, 76 | { 77 | "cell_type": "code", 78 | "execution_count": 7, 79 | "metadata": { 80 | "colab": {}, 81 | "colab_type": "code", 82 | "id": "e-3QCNS1ZRY9" 83 | }, 84 | "outputs": [], 85 | "source": [ 86 | "saludo = 'Hola!'" 87 | ] 88 | }, 89 | { 90 | "cell_type": "markdown", 91 | "metadata": { 92 | "colab_type": "text", 93 | "id": "IbNjcYdwZY4C" 94 | }, 95 | "source": [ 96 | "Para mostrar el valor que se encuentra dentro de las variables podemos llamar la funcion **print()**" 97 | ] 98 | }, 99 | { 100 | "cell_type": "code", 101 | "execution_count": 8, 102 | "metadata": {}, 103 | "outputs": [ 104 | { 105 | "name": "stdout", 106 | "output_type": "stream", 107 | "text": [ 108 | "Ana\n" 109 | ] 110 | } 111 | ], 112 | "source": [ 113 | "print(mi_nombre)" 114 | ] 115 | }, 116 | { 117 | "cell_type": "code", 118 | "execution_count": null, 119 | "metadata": { 120 | "colab": { 121 | "base_uri": "https://localhost:8080/", 122 | "height": 35 123 | }, 124 | "colab_type": "code", 125 | "id": "DFNUHgWFZgCl", 126 | "outputId": "9ab74cab-9766-4f9c-ddd8-83b35622d0e1" 127 | }, 128 | "outputs": [ 129 | { 130 | "name": "stdout", 131 | "output_type": "stream", 132 | "text": [ 133 | "Hola!\n" 134 | ] 135 | } 136 | ], 137 | "source": [ 138 | "print(saludo)" 139 | ] 140 | }, 141 | { 142 | "cell_type": "markdown", 143 | "metadata": { 144 | "colab_type": "text", 145 | "id": "P8jzRtGDZjoA" 146 | }, 147 | "source": [ 148 | "En Python se manejan diferentes tipos de datos, entre ellos los siguientes:\n", 149 | "\n", 150 | "* **Int:** Representación de los números enteros.\n", 151 | "\n" 152 | ] 153 | }, 154 | { 155 | "cell_type": "code", 156 | "execution_count": 9, 157 | "metadata": { 158 | "colab": { 159 | "base_uri": "https://localhost:8080/", 160 | "height": 35 161 | }, 162 | "colab_type": "code", 163 | "id": "Ce_k0As8ZhJH", 164 | "outputId": "697193c1-2aed-4209-da95-471bbff39349" 165 | }, 166 | "outputs": [ 167 | { 168 | "name": "stdout", 169 | "output_type": "stream", 170 | "text": [ 171 | "9 \n" 172 | ] 173 | } 174 | ], 175 | "source": [ 176 | "entero = 9\n", 177 | "print(entero, type(entero))" 178 | ] 179 | }, 180 | { 181 | "cell_type": "markdown", 182 | "metadata": { 183 | "colab_type": "text", 184 | "id": "HUBA5hcqaA5V" 185 | }, 186 | "source": [ 187 | "\n", 188 | "* **Float:** Representación de los números decimales.\n" 189 | ] 190 | }, 191 | { 192 | "cell_type": "code", 193 | "execution_count": 10, 194 | "metadata": { 195 | "colab": { 196 | "base_uri": "https://localhost:8080/", 197 | "height": 35 198 | }, 199 | "colab_type": "code", 200 | "id": "x7ad8m5XZ9-_", 201 | "outputId": "d6dc510c-9547-4872-8b7d-08e89a23f48a" 202 | }, 203 | "outputs": [ 204 | { 205 | "name": "stdout", 206 | "output_type": "stream", 207 | "text": [ 208 | "1.5 \n" 209 | ] 210 | } 211 | ], 212 | "source": [ 213 | "flotante = 1.5\n", 214 | "print(flotante, type(flotante))" 215 | ] 216 | }, 217 | { 218 | "cell_type": "markdown", 219 | "metadata": { 220 | "colab_type": "text", 221 | "id": "MySWRJh4aS_z" 222 | }, 223 | "source": [ 224 | "* **String**: Representación de las cadenas.\n" 225 | ] 226 | }, 227 | { 228 | "cell_type": "code", 229 | "execution_count": 11, 230 | "metadata": { 231 | "colab": { 232 | "base_uri": "https://localhost:8080/", 233 | "height": 54 234 | }, 235 | "colab_type": "code", 236 | "id": "4TSrdstHabRZ", 237 | "outputId": "29fa45a5-a60c-4e02-90db-d35b5513a41d" 238 | }, 239 | "outputs": [ 240 | { 241 | "name": "stdout", 242 | "output_type": "stream", 243 | "text": [ 244 | "Hola Mundo! \n", 245 | "Ejemplo Cadena \n" 246 | ] 247 | } 248 | ], 249 | "source": [ 250 | "cadena = 'Hola Mundo!'\n", 251 | "cadena1 = \"Ejemplo Cadena\"\n", 252 | "print(cadena, type(cadena))\n", 253 | "print(cadena1, type(cadena1))" 254 | ] 255 | }, 256 | { 257 | "cell_type": "markdown", 258 | "metadata": { 259 | "colab_type": "text", 260 | "id": "GTbzaKEGbF0P" 261 | }, 262 | "source": [ 263 | "* **Bool:** Representación de valores booleanos (True, False). " 264 | ] 265 | }, 266 | { 267 | "cell_type": "code", 268 | "execution_count": 12, 269 | "metadata": { 270 | "colab": { 271 | "base_uri": "https://localhost:8080/", 272 | "height": 35 273 | }, 274 | "colab_type": "code", 275 | "id": "bFZ-h_wZao97", 276 | "outputId": "149db580-fa24-49ae-ed04-c9f74dd7f769", 277 | "scrolled": true 278 | }, 279 | "outputs": [ 280 | { 281 | "name": "stdout", 282 | "output_type": "stream", 283 | "text": [ 284 | "True \n" 285 | ] 286 | } 287 | ], 288 | "source": [ 289 | "booleano = True\n", 290 | "print(booleano, type(booleano))" 291 | ] 292 | }, 293 | { 294 | "cell_type": "markdown", 295 | "metadata": { 296 | "colab_type": "text", 297 | "id": "dsvWa0K4b3xb" 298 | }, 299 | "source": [ 300 | "# Operadores Básicos\n", 301 | "Un operador es un símbolo del lenguaje de programación, el cual es capaz de realizar operaciones con los valores. Python tiene operaciones básicas como:" 302 | ] 303 | }, 304 | { 305 | "cell_type": "code", 306 | "execution_count": null, 307 | "metadata": { 308 | "colab": { 309 | "base_uri": "https://localhost:8080/", 310 | "height": 35 311 | }, 312 | "colab_type": "code", 313 | "id": "9PxwBSkpbXds", 314 | "outputId": "9b1c4dd6-3cff-4b87-e373-dd45ca32c8f7" 315 | }, 316 | "outputs": [ 317 | { 318 | "name": "stdout", 319 | "output_type": "stream", 320 | "text": [ 321 | "5\n" 322 | ] 323 | } 324 | ], 325 | "source": [ 326 | "# Suma\n", 327 | "a = 2 + 3\n", 328 | "print(a)" 329 | ] 330 | }, 331 | { 332 | "cell_type": "code", 333 | "execution_count": null, 334 | "metadata": { 335 | "colab": { 336 | "base_uri": "https://localhost:8080/", 337 | "height": 35 338 | }, 339 | "colab_type": "code", 340 | "id": "iKFGMzFlckIy", 341 | "outputId": "4ce3633a-d0c1-41e6-e65f-22dee8ea347c" 342 | }, 343 | "outputs": [ 344 | { 345 | "name": "stdout", 346 | "output_type": "stream", 347 | "text": [ 348 | "2\n" 349 | ] 350 | } 351 | ], 352 | "source": [ 353 | "# Resta\n", 354 | "b = 5 - 3\n", 355 | "print(b)" 356 | ] 357 | }, 358 | { 359 | "cell_type": "code", 360 | "execution_count": null, 361 | "metadata": { 362 | "colab": { 363 | "base_uri": "https://localhost:8080/", 364 | "height": 35 365 | }, 366 | "colab_type": "code", 367 | "id": "uQNmrkbCcu0I", 368 | "outputId": "1d5dd6cb-8891-4a3f-bb29-6c43d53a21d6" 369 | }, 370 | "outputs": [ 371 | { 372 | "name": "stdout", 373 | "output_type": "stream", 374 | "text": [ 375 | "18\n" 376 | ] 377 | } 378 | ], 379 | "source": [ 380 | "# Multiplicación\n", 381 | "c = 6 * 3\n", 382 | "print(c)" 383 | ] 384 | }, 385 | { 386 | "cell_type": "code", 387 | "execution_count": null, 388 | "metadata": { 389 | "colab": { 390 | "base_uri": "https://localhost:8080/", 391 | "height": 35 392 | }, 393 | "colab_type": "code", 394 | "id": "N3e-agPuc0nu", 395 | "outputId": "4db7d47f-4917-4023-e895-1e4c578d1cdb" 396 | }, 397 | "outputs": [ 398 | { 399 | "name": "stdout", 400 | "output_type": "stream", 401 | "text": [ 402 | "3.0\n" 403 | ] 404 | } 405 | ], 406 | "source": [ 407 | "# División\n", 408 | "d = 12 / 4\n", 409 | "print(d)" 410 | ] 411 | }, 412 | { 413 | "cell_type": "code", 414 | "execution_count": null, 415 | "metadata": { 416 | "colab": { 417 | "base_uri": "https://localhost:8080/", 418 | "height": 35 419 | }, 420 | "colab_type": "code", 421 | "id": "w6ED_kJDc6Yd", 422 | "outputId": "79b722d9-8dcf-4b22-b112-3dd04e85a255" 423 | }, 424 | "outputs": [ 425 | { 426 | "name": "stdout", 427 | "output_type": "stream", 428 | "text": [ 429 | "3\n" 430 | ] 431 | } 432 | ], 433 | "source": [ 434 | "# División entera\n", 435 | "e = 12 // 4\n", 436 | "print(e)" 437 | ] 438 | }, 439 | { 440 | "cell_type": "code", 441 | "execution_count": null, 442 | "metadata": { 443 | "colab": { 444 | "base_uri": "https://localhost:8080/", 445 | "height": 35 446 | }, 447 | "colab_type": "code", 448 | "id": "Wq6Y7P2NdA5B", 449 | "outputId": "4bc3ccf7-8596-44fb-f84a-b6df6fd88981" 450 | }, 451 | "outputs": [ 452 | { 453 | "name": "stdout", 454 | "output_type": "stream", 455 | "text": [ 456 | "8\n" 457 | ] 458 | } 459 | ], 460 | "source": [ 461 | "# Potencia\n", 462 | "f = 2 ** 3\n", 463 | "print(f)" 464 | ] 465 | }, 466 | { 467 | "cell_type": "code", 468 | "execution_count": null, 469 | "metadata": { 470 | "colab": { 471 | "base_uri": "https://localhost:8080/", 472 | "height": 35 473 | }, 474 | "colab_type": "code", 475 | "id": "2UTGPou3dK_I", 476 | "outputId": "70bb6028-62c1-4b1b-981e-5cfba0237bdc" 477 | }, 478 | "outputs": [ 479 | { 480 | "name": "stdout", 481 | "output_type": "stream", 482 | "text": [ 483 | "1\n" 484 | ] 485 | } 486 | ], 487 | "source": [ 488 | "# Módulo (Lo que queda de la división entera)\n", 489 | "g = 10 % 3\n", 490 | "print(g)" 491 | ] 492 | }, 493 | { 494 | "cell_type": "markdown", 495 | "metadata": { 496 | "colab_type": "text", 497 | "id": "PU2vuJqUeDm2" 498 | }, 499 | "source": [ 500 | "**Operadores Abreviados**: Cuando se quiere utilizar una misma variable al lado derecho e izquierdo de un operador, podemos reducir el codigo de la siguiente manera:" 501 | ] 502 | }, 503 | { 504 | "cell_type": "code", 505 | "execution_count": null, 506 | "metadata": { 507 | "colab": { 508 | "base_uri": "https://localhost:8080/", 509 | "height": 35 510 | }, 511 | "colab_type": "code", 512 | "id": "nnHxPZtWdQI1", 513 | "outputId": "ec363a63-afc4-418d-c6ad-1d4f42e8fcf9" 514 | }, 515 | "outputs": [ 516 | { 517 | "name": "stdout", 518 | "output_type": "stream", 519 | "text": [ 520 | "6\n" 521 | ] 522 | } 523 | ], 524 | "source": [ 525 | "a = 5\n", 526 | "a = a + 1\n", 527 | "print(a)" 528 | ] 529 | }, 530 | { 531 | "cell_type": "code", 532 | "execution_count": null, 533 | "metadata": { 534 | "colab": { 535 | "base_uri": "https://localhost:8080/", 536 | "height": 35 537 | }, 538 | "colab_type": "code", 539 | "id": "WQkPQbAdeaQU", 540 | "outputId": "dd8a937d-6e58-4dd5-aff5-586d735d687c" 541 | }, 542 | "outputs": [ 543 | { 544 | "name": "stdout", 545 | "output_type": "stream", 546 | "text": [ 547 | "6\n" 548 | ] 549 | } 550 | ], 551 | "source": [ 552 | "# De una manera mas corta\n", 553 | "a = 5\n", 554 | "a += 1\n", 555 | "print(a)" 556 | ] 557 | }, 558 | { 559 | "cell_type": "markdown", 560 | "metadata": { 561 | "colab_type": "text", 562 | "id": "BkTpz3MCgRXE" 563 | }, 564 | "source": [ 565 | "# Operadores Relacionales\n", 566 | "* **Igualdad:** Compara si dos elementos son iguales ( == ).\n", 567 | "* **Desigualdad:** Comprar si un valor es diferente a otro (!= ).\n", 568 | "* **Mayor que:** Compara si un valor es mayor que otro ( > ).\n", 569 | "* **Menor que:** Compara si un valor es menor que otro ( < ).\n", 570 | "* **Mayor o igual que:** Compara si un valor es mayor o igual a otro ( >= ).\n", 571 | "* **Menor o igual que:** Comprara si un valor es menor o igual que otro ( <= ).\n", 572 | "\n", 573 | "**Ejemplos:**\n" 574 | ] 575 | }, 576 | { 577 | "cell_type": "code", 578 | "execution_count": null, 579 | "metadata": { 580 | "colab": { 581 | "base_uri": "https://localhost:8080/", 582 | "height": 35 583 | }, 584 | "colab_type": "code", 585 | "id": "jRNSKV26f4Ep", 586 | "outputId": "7b91b1eb-ac48-4382-9b27-2b6bbe57ccee" 587 | }, 588 | "outputs": [ 589 | { 590 | "data": { 591 | "text/plain": [ 592 | "False" 593 | ] 594 | }, 595 | "execution_count": 20, 596 | "metadata": { 597 | "tags": [] 598 | }, 599 | "output_type": "execute_result" 600 | } 601 | ], 602 | "source": [ 603 | "2 == 3" 604 | ] 605 | }, 606 | { 607 | "cell_type": "code", 608 | "execution_count": null, 609 | "metadata": { 610 | "colab": { 611 | "base_uri": "https://localhost:8080/", 612 | "height": 35 613 | }, 614 | "colab_type": "code", 615 | "id": "gBAsuALOBqVC", 616 | "outputId": "17f6b4e3-9a7f-4cbf-d06e-a4f2b5112ee9" 617 | }, 618 | "outputs": [ 619 | { 620 | "data": { 621 | "text/plain": [ 622 | "True" 623 | ] 624 | }, 625 | "execution_count": 21, 626 | "metadata": { 627 | "tags": [] 628 | }, 629 | "output_type": "execute_result" 630 | } 631 | ], 632 | "source": [ 633 | "2 == 2" 634 | ] 635 | }, 636 | { 637 | "cell_type": "code", 638 | "execution_count": null, 639 | "metadata": { 640 | "colab": { 641 | "base_uri": "https://localhost:8080/", 642 | "height": 35 643 | }, 644 | "colab_type": "code", 645 | "id": "49H13CGGBufe", 646 | "outputId": "0b218e5b-c130-400b-9fea-ec984a53425b" 647 | }, 648 | "outputs": [ 649 | { 650 | "data": { 651 | "text/plain": [ 652 | "True" 653 | ] 654 | }, 655 | "execution_count": 22, 656 | "metadata": { 657 | "tags": [] 658 | }, 659 | "output_type": "execute_result" 660 | } 661 | ], 662 | "source": [ 663 | "5 >= 4" 664 | ] 665 | }, 666 | { 667 | "cell_type": "code", 668 | "execution_count": 17, 669 | "metadata": {}, 670 | "outputs": [ 671 | { 672 | "data": { 673 | "text/plain": [ 674 | "False" 675 | ] 676 | }, 677 | "execution_count": 17, 678 | "metadata": {}, 679 | "output_type": "execute_result" 680 | } 681 | ], 682 | "source": [ 683 | "5 < 4" 684 | ] 685 | }, 686 | { 687 | "cell_type": "code", 688 | "execution_count": 16, 689 | "metadata": {}, 690 | "outputs": [ 691 | { 692 | "data": { 693 | "text/plain": [ 694 | "False" 695 | ] 696 | }, 697 | "execution_count": 16, 698 | "metadata": {}, 699 | "output_type": "execute_result" 700 | } 701 | ], 702 | "source": [ 703 | "4 != 4" 704 | ] 705 | }, 706 | { 707 | "cell_type": "code", 708 | "execution_count": 15, 709 | "metadata": { 710 | "colab": { 711 | "base_uri": "https://localhost:8080/", 712 | "height": 35 713 | }, 714 | "colab_type": "code", 715 | "id": "o3o3MEh7Bwph", 716 | "outputId": "29bae3bd-94f2-4fef-acf5-25a94895565e" 717 | }, 718 | "outputs": [ 719 | { 720 | "data": { 721 | "text/plain": [ 722 | "False" 723 | ] 724 | }, 725 | "execution_count": 15, 726 | "metadata": {}, 727 | "output_type": "execute_result" 728 | } 729 | ], 730 | "source": [ 731 | "4 != '4'" 732 | ] 733 | }, 734 | { 735 | "cell_type": "markdown", 736 | "metadata": {}, 737 | "source": [ 738 | "# Operadores Lógicos\n", 739 | "\n", 740 | "✅ **and:** Todas las comparaciones son verdaderas\n", 741 | "✅ **or:** Por lo menos una comparación es verdadera.\n", 742 | "✅ **not:** Intercambia el valor." 743 | ] 744 | }, 745 | { 746 | "cell_type": "code", 747 | "execution_count": 18, 748 | "metadata": {}, 749 | "outputs": [ 750 | { 751 | "data": { 752 | "text/plain": [ 753 | "True" 754 | ] 755 | }, 756 | "execution_count": 18, 757 | "metadata": {}, 758 | "output_type": "execute_result" 759 | } 760 | ], 761 | "source": [ 762 | "5 > 4 and 3 > 1" 763 | ] 764 | }, 765 | { 766 | "cell_type": "code", 767 | "execution_count": 19, 768 | "metadata": {}, 769 | "outputs": [ 770 | { 771 | "data": { 772 | "text/plain": [ 773 | "False" 774 | ] 775 | }, 776 | "execution_count": 19, 777 | "metadata": {}, 778 | "output_type": "execute_result" 779 | } 780 | ], 781 | "source": [ 782 | "5 > 4 and 3 < 1" 783 | ] 784 | }, 785 | { 786 | "cell_type": "code", 787 | "execution_count": 20, 788 | "metadata": {}, 789 | "outputs": [ 790 | { 791 | "data": { 792 | "text/plain": [ 793 | "True" 794 | ] 795 | }, 796 | "execution_count": 20, 797 | "metadata": {}, 798 | "output_type": "execute_result" 799 | } 800 | ], 801 | "source": [ 802 | "5 > 4 or 3 < 1" 803 | ] 804 | }, 805 | { 806 | "cell_type": "code", 807 | "execution_count": 21, 808 | "metadata": {}, 809 | "outputs": [ 810 | { 811 | "data": { 812 | "text/plain": [ 813 | "False" 814 | ] 815 | }, 816 | "execution_count": 21, 817 | "metadata": {}, 818 | "output_type": "execute_result" 819 | } 820 | ], 821 | "source": [ 822 | "5 < 4 or 3 < 1" 823 | ] 824 | }, 825 | { 826 | "cell_type": "code", 827 | "execution_count": 22, 828 | "metadata": {}, 829 | "outputs": [ 830 | { 831 | "data": { 832 | "text/plain": [ 833 | "False" 834 | ] 835 | }, 836 | "execution_count": 22, 837 | "metadata": {}, 838 | "output_type": "execute_result" 839 | } 840 | ], 841 | "source": [ 842 | "not True" 843 | ] 844 | }, 845 | { 846 | "cell_type": "code", 847 | "execution_count": 23, 848 | "metadata": {}, 849 | "outputs": [ 850 | { 851 | "data": { 852 | "text/plain": [ 853 | "True" 854 | ] 855 | }, 856 | "execution_count": 23, 857 | "metadata": {}, 858 | "output_type": "execute_result" 859 | } 860 | ], 861 | "source": [ 862 | "not False" 863 | ] 864 | }, 865 | { 866 | "cell_type": "code", 867 | "execution_count": 24, 868 | "metadata": {}, 869 | "outputs": [ 870 | { 871 | "data": { 872 | "text/plain": [ 873 | "True" 874 | ] 875 | }, 876 | "execution_count": 24, 877 | "metadata": {}, 878 | "output_type": "execute_result" 879 | } 880 | ], 881 | "source": [ 882 | "not 4 > 5" 883 | ] 884 | }, 885 | { 886 | "cell_type": "markdown", 887 | "metadata": {}, 888 | "source": [ 889 | "# ⌨️ Entrada de datos por teclado\n", 890 | "Obtener los valores a partir de lo que ingresa el usuario" 891 | ] 892 | }, 893 | { 894 | "cell_type": "code", 895 | "execution_count": 25, 896 | "metadata": {}, 897 | "outputs": [ 898 | { 899 | "name": "stdout", 900 | "output_type": "stream", 901 | "text": [ 902 | "Cuál es tu nombre?\n", 903 | "Ana\n", 904 | "Mi nombre es: Ana\n" 905 | ] 906 | } 907 | ], 908 | "source": [ 909 | "print(\"Cuál es tu nombre?\")\n", 910 | "nombre = input()\n", 911 | "print(\"Mi nombre es: \", nombre)" 912 | ] 913 | }, 914 | { 915 | "cell_type": "code", 916 | "execution_count": 26, 917 | "metadata": {}, 918 | "outputs": [ 919 | { 920 | "name": "stdout", 921 | "output_type": "stream", 922 | "text": [ 923 | "Cuál es tu edad?\n", 924 | "23\n", 925 | "\n", 926 | "Mi edad es: 23\n" 927 | ] 928 | } 929 | ], 930 | "source": [ 931 | "print(\"Cuál es tu edad?\")\n", 932 | "edad = input()\n", 933 | "print(type(edad))\n", 934 | "print(\"Mi edad es: \", edad)" 935 | ] 936 | }, 937 | { 938 | "cell_type": "code", 939 | "execution_count": 27, 940 | "metadata": {}, 941 | "outputs": [ 942 | { 943 | "name": "stdout", 944 | "output_type": "stream", 945 | "text": [ 946 | "Cuál es tu edad?\n", 947 | "23\n", 948 | "\n", 949 | "Mi edad es: 23\n" 950 | ] 951 | } 952 | ], 953 | "source": [ 954 | "print(\"Cuál es tu edad?\")\n", 955 | "edad = int(input())\n", 956 | "print(type(edad))\n", 957 | "print(\"Mi edad es: \", edad)" 958 | ] 959 | }, 960 | { 961 | "cell_type": "code", 962 | "execution_count": 28, 963 | "metadata": {}, 964 | "outputs": [ 965 | { 966 | "name": "stdout", 967 | "output_type": "stream", 968 | "text": [ 969 | "Cuál es tu edad?23\n", 970 | "Mi edad es: 23\n" 971 | ] 972 | } 973 | ], 974 | "source": [ 975 | "edad = int(input(\"Cuál es tu edad?\"))\n", 976 | "print(\"Mi edad es: \", edad)" 977 | ] 978 | }, 979 | { 980 | "cell_type": "code", 981 | "execution_count": 29, 982 | "metadata": {}, 983 | "outputs": [ 984 | { 985 | "name": "stdout", 986 | "output_type": "stream", 987 | "text": [ 988 | "Cuál es tu edad? \n", 989 | "23\n", 990 | "Mi edad es: 23\n" 991 | ] 992 | } 993 | ], 994 | "source": [ 995 | "edad = int(input(\"Cuál es tu edad?\\n\"))\n", 996 | "print(\"Mi edad es: \", edad)" 997 | ] 998 | }, 999 | { 1000 | "cell_type": "markdown", 1001 | "metadata": { 1002 | "colab_type": "text", 1003 | "id": "12H1dMc9CTxJ" 1004 | }, 1005 | "source": [ 1006 | "# Condicionales\n", 1007 | "Los condicionales nos permiten validar si una sentencia es verdadera o no, y deacuerdo al resultado realizar mas instrucciones. Tienen la siguiente estructura:\n", 1008 | "\n", 1009 | "```\n", 1010 | "if sentencia:\n", 1011 | " # instrucciones a realizar si la sentencia es verdadera\n", 1012 | "else:\n", 1013 | " # instrucciones a realizar si la sentencia es falsa\n", 1014 | "```\n", 1015 | "\n", 1016 | "**Ejemplos:**" 1017 | ] 1018 | }, 1019 | { 1020 | "cell_type": "code", 1021 | "execution_count": 31, 1022 | "metadata": { 1023 | "colab": { 1024 | "base_uri": "https://localhost:8080/", 1025 | "height": 54 1026 | }, 1027 | "colab_type": "code", 1028 | "id": "bwL4Tv-zB0QJ", 1029 | "outputId": "4f084a61-e318-408a-dbc1-457b90b33b52" 1030 | }, 1031 | "outputs": [ 1032 | { 1033 | "name": "stdout", 1034 | "output_type": "stream", 1035 | "text": [ 1036 | "Cuál es tu edad?\n", 1037 | "18\n", 1038 | "Eres mayor de edad\n" 1039 | ] 1040 | } 1041 | ], 1042 | "source": [ 1043 | "edad = int(input(\"Cuál es tu edad?\\n\"))\n", 1044 | "if edad >= 18:\n", 1045 | " print('Eres mayor de edad')\n", 1046 | "else:\n", 1047 | " print('Eres menor de edad')" 1048 | ] 1049 | }, 1050 | { 1051 | "cell_type": "markdown", 1052 | "metadata": { 1053 | "colab_type": "text", 1054 | "id": "XAhRN6IxEFPL" 1055 | }, 1056 | "source": [ 1057 | "Tambien se pueden enlazar diferentes sentencias por medio de los operadores lógicos." 1058 | ] 1059 | }, 1060 | { 1061 | "cell_type": "code", 1062 | "execution_count": 34, 1063 | "metadata": { 1064 | "colab": { 1065 | "base_uri": "https://localhost:8080/", 1066 | "height": 54 1067 | }, 1068 | "colab_type": "code", 1069 | "id": "2svFobTTDmPe", 1070 | "outputId": "25563fe1-a223-44dd-996c-185e617acf54" 1071 | }, 1072 | "outputs": [ 1073 | { 1074 | "name": "stdout", 1075 | "output_type": "stream", 1076 | "text": [ 1077 | "Cuál es tu apellido?\n", 1078 | "GOMEZ\n", 1079 | "Hola prim@!\n" 1080 | ] 1081 | } 1082 | ], 1083 | "source": [ 1084 | "apellido = input(\"Cuál es tu apellido?\\n\")\n", 1085 | "if apellido.lower() == 'gomez' or apellido.lower() == 'trejos':\n", 1086 | " print('Hola prim@!')\n", 1087 | "else:\n", 1088 | " print('Hola!')" 1089 | ] 1090 | }, 1091 | { 1092 | "cell_type": "code", 1093 | "execution_count": 40, 1094 | "metadata": { 1095 | "scrolled": true 1096 | }, 1097 | "outputs": [ 1098 | { 1099 | "name": "stdout", 1100 | "output_type": "stream", 1101 | "text": [ 1102 | "Cuál es tu edad?\n", 1103 | "12\n", 1104 | "Eres un niño\n" 1105 | ] 1106 | } 1107 | ], 1108 | "source": [ 1109 | "edad = int(input(\"Cuál es tu edad?\\n\"))\n", 1110 | "\n", 1111 | "if edad >= 18:\n", 1112 | " print(\"Eres mayor de edad\")\n", 1113 | "elif edad >= 13 and edad < 18:\n", 1114 | " print(\"Eres un adolescente\")\n", 1115 | "else:\n", 1116 | " print(\"Eres un niño\")" 1117 | ] 1118 | }, 1119 | { 1120 | "cell_type": "markdown", 1121 | "metadata": { 1122 | "colab_type": "text", 1123 | "id": "Oo643n-2FoXG" 1124 | }, 1125 | "source": [ 1126 | "# Ciclos o bucles\n", 1127 | "Un ciclo es una instrucción que se ejecuta tantas veces como se cumpla una condición:\n", 1128 | "## While\n", 1129 | "El ciclo while tiene la siguiente estructura:\n", 1130 | "```\n", 1131 | "while expresión_condicional:\n", 1132 | " instrucción\n", 1133 | "```" 1134 | ] 1135 | }, 1136 | { 1137 | "cell_type": "code", 1138 | "execution_count": null, 1139 | "metadata": { 1140 | "colab": { 1141 | "base_uri": "https://localhost:8080/", 1142 | "height": 72 1143 | }, 1144 | "colab_type": "code", 1145 | "id": "Sv5GJ1_oE2CV", 1146 | "outputId": "8aee3dae-8edb-46ba-dcc0-54b22625dcc0" 1147 | }, 1148 | "outputs": [ 1149 | { 1150 | "name": "stdout", 1151 | "output_type": "stream", 1152 | "text": [ 1153 | "3\n", 1154 | "2\n", 1155 | "1\n" 1156 | ] 1157 | } 1158 | ], 1159 | "source": [ 1160 | "a = 3\n", 1161 | "while a > 0:\n", 1162 | " print(a)\n", 1163 | " a -= 1" 1164 | ] 1165 | }, 1166 | { 1167 | "cell_type": "markdown", 1168 | "metadata": { 1169 | "colab_type": "text", 1170 | "id": "Emry1hMIHedB" 1171 | }, 1172 | "source": [ 1173 | "## For\n", 1174 | "El ciclo for tiene la siguiente estructura:\n", 1175 | "```\n", 1176 | "for expresión_condicional:\n", 1177 | " instrucción\n", 1178 | "```" 1179 | ] 1180 | }, 1181 | { 1182 | "cell_type": "code", 1183 | "execution_count": null, 1184 | "metadata": { 1185 | "colab": { 1186 | "base_uri": "https://localhost:8080/", 1187 | "height": 201 1188 | }, 1189 | "colab_type": "code", 1190 | "id": "nlhf_CwuGjVI", 1191 | "outputId": "da051457-0a0e-43bd-f28e-d3a7649b5dea" 1192 | }, 1193 | "outputs": [ 1194 | { 1195 | "name": "stdout", 1196 | "output_type": "stream", 1197 | "text": [ 1198 | "0\n", 1199 | "1\n", 1200 | "2\n", 1201 | "3\n", 1202 | "4\n", 1203 | "5\n", 1204 | "6\n", 1205 | "7\n", 1206 | "8\n", 1207 | "9\n" 1208 | ] 1209 | } 1210 | ], 1211 | "source": [ 1212 | "for i in range(10):\n", 1213 | " print(i)" 1214 | ] 1215 | }, 1216 | { 1217 | "cell_type": "markdown", 1218 | "metadata": { 1219 | "colab_type": "text", 1220 | "id": "Aky7RMGoJYT0" 1221 | }, 1222 | "source": [ 1223 | "## Las declaraciones break y continue\n", 1224 | "\n", 1225 | "**Break:** Sale del ciclo inmediatamente, e incondicionalmente termina la operación del ciclo.\n", 1226 | "\n", 1227 | "**Continue:** Pasa a la siguiente iteración del ciclo.\n" 1228 | ] 1229 | }, 1230 | { 1231 | "cell_type": "code", 1232 | "execution_count": null, 1233 | "metadata": { 1234 | "colab": { 1235 | "base_uri": "https://localhost:8080/", 1236 | "height": 219 1237 | }, 1238 | "colab_type": "code", 1239 | "id": "fiPfzxuKH8wC", 1240 | "outputId": "f2c76e34-b563-4bcf-be01-0fa4fae3bdd0" 1241 | }, 1242 | "outputs": [ 1243 | { 1244 | "name": "stdout", 1245 | "output_type": "stream", 1246 | "text": [ 1247 | "La instrucción de ruptura:\n", 1248 | "Dentro del ciclo. 0\n", 1249 | "Dentro del ciclo. 1\n", 1250 | "Fuera del ciclo.\n", 1251 | "\n", 1252 | "La instrucción continue:\n", 1253 | "Dentro del ciclo. 0\n", 1254 | "Dentro del ciclo. 1\n", 1255 | "Dentro del ciclo. 3\n", 1256 | "Dentro del ciclo. 4\n", 1257 | "Fuera del ciclo.\n" 1258 | ] 1259 | } 1260 | ], 1261 | "source": [ 1262 | "#Ejemplo break\n", 1263 | "print(\"La instrucción de ruptura:\")\n", 1264 | "for i in range(5):\n", 1265 | " if i == 2:\n", 1266 | " break\n", 1267 | " print(\"Dentro del ciclo.\", i)\n", 1268 | "print(\"Fuera del ciclo.\")\n", 1269 | "\n", 1270 | "# Ejemplo continue\n", 1271 | "\n", 1272 | "print(\"\\nLa instrucción continue:\")\n", 1273 | "for i in range(5):\n", 1274 | " if i == 2:\n", 1275 | " continue\n", 1276 | " print(\"Dentro del ciclo.\", i)\n", 1277 | "print(\"Fuera del ciclo.\")" 1278 | ] 1279 | }, 1280 | { 1281 | "cell_type": "markdown", 1282 | "metadata": { 1283 | "colab_type": "text", 1284 | "id": "UIpN8wB4L7Si" 1285 | }, 1286 | "source": [ 1287 | "# Listas\n", 1288 | "Las listas son muy parecidas a lo que se llama en otros lenguajes arreglos o arrays. Pueden contener cualquier tipo de variable y se pueden iterar de una manera muy simple:" 1289 | ] 1290 | }, 1291 | { 1292 | "cell_type": "code", 1293 | "execution_count": null, 1294 | "metadata": { 1295 | "colab": { 1296 | "base_uri": "https://localhost:8080/", 1297 | "height": 35 1298 | }, 1299 | "colab_type": "code", 1300 | "id": "qVKd4mc7Kj7a", 1301 | "outputId": "7bc793dd-c294-4f7b-d7ec-defcf6f4c9d3" 1302 | }, 1303 | "outputs": [ 1304 | { 1305 | "name": "stdout", 1306 | "output_type": "stream", 1307 | "text": [ 1308 | "['a', 'b', 'c']\n" 1309 | ] 1310 | } 1311 | ], 1312 | "source": [ 1313 | "mi_lista = []\n", 1314 | "mi_lista.append('a')\n", 1315 | "mi_lista.append('b')\n", 1316 | "mi_lista.append('c')\n", 1317 | "print(mi_lista)" 1318 | ] 1319 | }, 1320 | { 1321 | "cell_type": "code", 1322 | "execution_count": null, 1323 | "metadata": { 1324 | "colab": { 1325 | "base_uri": "https://localhost:8080/", 1326 | "height": 35 1327 | }, 1328 | "colab_type": "code", 1329 | "id": "XlFICzxsMY4H", 1330 | "outputId": "c226b807-2093-4299-9384-0f07f8874410" 1331 | }, 1332 | "outputs": [ 1333 | { 1334 | "name": "stdout", 1335 | "output_type": "stream", 1336 | "text": [ 1337 | "a\n" 1338 | ] 1339 | } 1340 | ], 1341 | "source": [ 1342 | "print(mi_lista[0])" 1343 | ] 1344 | }, 1345 | { 1346 | "cell_type": "markdown", 1347 | "metadata": { 1348 | "colab_type": "text", 1349 | "id": "0RB43t-AM2-P" 1350 | }, 1351 | "source": [ 1352 | "Hay varias funciones que tienen las listas por defecto como por ejemplo:\n", 1353 | "* **len()**: Nos muestra el tamaño de la lista\n", 1354 | "* **sort()**: Ordena una lista" 1355 | ] 1356 | }, 1357 | { 1358 | "cell_type": "code", 1359 | "execution_count": null, 1360 | "metadata": { 1361 | "colab": { 1362 | "base_uri": "https://localhost:8080/", 1363 | "height": 35 1364 | }, 1365 | "colab_type": "code", 1366 | "id": "BQGJetFjMaPc", 1367 | "outputId": "c2047899-cada-4c99-b15f-826e0b0ca819" 1368 | }, 1369 | "outputs": [ 1370 | { 1371 | "name": "stdout", 1372 | "output_type": "stream", 1373 | "text": [ 1374 | "4\n" 1375 | ] 1376 | } 1377 | ], 1378 | "source": [ 1379 | "a = [1, 2, 3, 4]\n", 1380 | "print(len(a))" 1381 | ] 1382 | }, 1383 | { 1384 | "cell_type": "code", 1385 | "execution_count": null, 1386 | "metadata": { 1387 | "colab": { 1388 | "base_uri": "https://localhost:8080/", 1389 | "height": 90 1390 | }, 1391 | "colab_type": "code", 1392 | "id": "Br0NfjCaNMGS", 1393 | "outputId": "e9184758-bb8e-4929-96c9-64e81ec2a4a5" 1394 | }, 1395 | "outputs": [ 1396 | { 1397 | "name": "stdout", 1398 | "output_type": "stream", 1399 | "text": [ 1400 | "Elemento: 1\n", 1401 | "Elemento: 2\n", 1402 | "Elemento: 3\n", 1403 | "Elemento: 4\n" 1404 | ] 1405 | } 1406 | ], 1407 | "source": [ 1408 | "for i in a:\n", 1409 | " print(f'Elemento: {i}')" 1410 | ] 1411 | }, 1412 | { 1413 | "cell_type": "code", 1414 | "execution_count": null, 1415 | "metadata": { 1416 | "colab": { 1417 | "base_uri": "https://localhost:8080/", 1418 | "height": 35 1419 | }, 1420 | "colab_type": "code", 1421 | "id": "vxc_VzbkNSkG", 1422 | "outputId": "f6138af2-be1c-4ce9-be5f-2ed4890bd097" 1423 | }, 1424 | "outputs": [ 1425 | { 1426 | "name": "stdout", 1427 | "output_type": "stream", 1428 | "text": [ 1429 | "[1, 2, 3, 4, 5]\n" 1430 | ] 1431 | } 1432 | ], 1433 | "source": [ 1434 | "b = [2, 5, 4, 1, 3]\n", 1435 | "b.sort()\n", 1436 | "print(b)" 1437 | ] 1438 | }, 1439 | { 1440 | "cell_type": "markdown", 1441 | "metadata": { 1442 | "colab_type": "text", 1443 | "id": "GuQgrKFsNoHf" 1444 | }, 1445 | "source": [ 1446 | "Para eliminar un elemento de una lista usamos la instruccion del:\n" 1447 | ] 1448 | }, 1449 | { 1450 | "cell_type": "code", 1451 | "execution_count": null, 1452 | "metadata": { 1453 | "colab": { 1454 | "base_uri": "https://localhost:8080/", 1455 | "height": 35 1456 | }, 1457 | "colab_type": "code", 1458 | "id": "N0tk4-g2NXhl", 1459 | "outputId": "4dd49cf5-8bae-4dcc-d335-f17630d622a8" 1460 | }, 1461 | "outputs": [ 1462 | { 1463 | "name": "stdout", 1464 | "output_type": "stream", 1465 | "text": [ 1466 | "[1, 2, 3, 4]\n" 1467 | ] 1468 | } 1469 | ], 1470 | "source": [ 1471 | "del b[4]\n", 1472 | "print(b)" 1473 | ] 1474 | }, 1475 | { 1476 | "cell_type": "markdown", 1477 | "metadata": { 1478 | "colab_type": "text", 1479 | "id": "wCb_o6JcOZ7u" 1480 | }, 1481 | "source": [ 1482 | "# Diccionarios\n", 1483 | "Los diccionarios son tipos de datos parecidos a las listas pero en vez de indices tienen atributos de llave y valor, de la siguiente manera:" 1484 | ] 1485 | }, 1486 | { 1487 | "cell_type": "code", 1488 | "execution_count": null, 1489 | "metadata": { 1490 | "colab": { 1491 | "base_uri": "https://localhost:8080/", 1492 | "height": 35 1493 | }, 1494 | "colab_type": "code", 1495 | "id": "fp9W3YclNX6B", 1496 | "outputId": "a15e956e-4225-4439-d828-391385028d94" 1497 | }, 1498 | "outputs": [ 1499 | { 1500 | "name": "stdout", 1501 | "output_type": "stream", 1502 | "text": [ 1503 | "{'nombre': 'Carolina', 'apellido': 'Gomez'}\n" 1504 | ] 1505 | } 1506 | ], 1507 | "source": [ 1508 | "a = {'nombre': 'Carolina', 'apellido': 'Gomez'}\n", 1509 | "print(a)" 1510 | ] 1511 | }, 1512 | { 1513 | "cell_type": "code", 1514 | "execution_count": null, 1515 | "metadata": { 1516 | "colab": { 1517 | "base_uri": "https://localhost:8080/", 1518 | "height": 35 1519 | }, 1520 | "colab_type": "code", 1521 | "id": "_huqi5oMOnhm", 1522 | "outputId": "65db45f3-c798-4f0f-8d76-1afa4280766b" 1523 | }, 1524 | "outputs": [ 1525 | { 1526 | "name": "stdout", 1527 | "output_type": "stream", 1528 | "text": [ 1529 | "Carolina\n" 1530 | ] 1531 | } 1532 | ], 1533 | "source": [ 1534 | "print(a['nombre'])" 1535 | ] 1536 | }, 1537 | { 1538 | "cell_type": "code", 1539 | "execution_count": null, 1540 | "metadata": { 1541 | "colab": { 1542 | "base_uri": "https://localhost:8080/", 1543 | "height": 35 1544 | }, 1545 | "colab_type": "code", 1546 | "id": "-TJ3LFrgOrjw", 1547 | "outputId": "c2f5a91a-1a39-461d-80af-7bd3a4be8f8b" 1548 | }, 1549 | "outputs": [ 1550 | { 1551 | "name": "stdout", 1552 | "output_type": "stream", 1553 | "text": [ 1554 | "{'nombre': 'Carolina', 'apellido': 'Gomez', 'edad': 24}\n" 1555 | ] 1556 | } 1557 | ], 1558 | "source": [ 1559 | "# Para agregar un nuevo valor\n", 1560 | "a['edad'] = 24\n", 1561 | "print(a)" 1562 | ] 1563 | }, 1564 | { 1565 | "cell_type": "code", 1566 | "execution_count": null, 1567 | "metadata": { 1568 | "colab": { 1569 | "base_uri": "https://localhost:8080/", 1570 | "height": 72 1571 | }, 1572 | "colab_type": "code", 1573 | "id": "wik_l_qYO0IY", 1574 | "outputId": "fe0db02f-0fd4-4f0d-a829-aa3b1736340e" 1575 | }, 1576 | "outputs": [ 1577 | { 1578 | "name": "stdout", 1579 | "output_type": "stream", 1580 | "text": [ 1581 | "nombre: Carolina\n", 1582 | "apellido: Gomez\n", 1583 | "edad: 24\n" 1584 | ] 1585 | } 1586 | ], 1587 | "source": [ 1588 | "# Iterando sobre diccionarios\n", 1589 | "for llave, valor in a.items():\n", 1590 | " print(f'{llave}: {valor}')" 1591 | ] 1592 | }, 1593 | { 1594 | "cell_type": "markdown", 1595 | "metadata": { 1596 | "colab_type": "text", 1597 | "id": "Ivz9qQAlRMot" 1598 | }, 1599 | "source": [ 1600 | "# Funciones\n", 1601 | "cuando estamos escribiendo instrucciones en muchas ocaciones estas se repiten varias veces, por lo que las funciones nos ayudan a dividir nuestro código en porciones que nos ayudan a ordenar nuestro código y hacerlo mas entendible, ademas de que nos evita repetir porciones de código. Las funciones tienen la siguiente estructura:\n", 1602 | "```\n", 1603 | "def nombre_funcion(parametros):\n", 1604 | " instrucciones\n", 1605 | "```\n", 1606 | "\n", 1607 | "**Ejemplo:**" 1608 | ] 1609 | }, 1610 | { 1611 | "cell_type": "code", 1612 | "execution_count": null, 1613 | "metadata": { 1614 | "colab": {}, 1615 | "colab_type": "code", 1616 | "id": "1Xzayu-CRAz_" 1617 | }, 1618 | "outputs": [], 1619 | "source": [ 1620 | "def suma(a, b):\n", 1621 | " return a + b" 1622 | ] 1623 | }, 1624 | { 1625 | "cell_type": "code", 1626 | "execution_count": null, 1627 | "metadata": { 1628 | "colab": { 1629 | "base_uri": "https://localhost:8080/", 1630 | "height": 35 1631 | }, 1632 | "colab_type": "code", 1633 | "id": "cv8U-e68TrXy", 1634 | "outputId": "b290e687-debe-41b7-cf21-c6b269233484" 1635 | }, 1636 | "outputs": [ 1637 | { 1638 | "data": { 1639 | "text/plain": [ 1640 | "8" 1641 | ] 1642 | }, 1643 | "execution_count": 40, 1644 | "metadata": { 1645 | "tags": [] 1646 | }, 1647 | "output_type": "execute_result" 1648 | } 1649 | ], 1650 | "source": [ 1651 | "suma(5, 3)" 1652 | ] 1653 | }, 1654 | { 1655 | "cell_type": "code", 1656 | "execution_count": null, 1657 | "metadata": { 1658 | "colab": {}, 1659 | "colab_type": "code", 1660 | "id": "c3KRTwSjTtTS" 1661 | }, 1662 | "outputs": [], 1663 | "source": [ 1664 | "# Sumar los valores de una lista\n", 1665 | "def suma_lista(lista):\n", 1666 | " suma_total = 0\n", 1667 | " for elem in lista:\n", 1668 | " suma_total += elem\n", 1669 | " return suma_total" 1670 | ] 1671 | }, 1672 | { 1673 | "cell_type": "code", 1674 | "execution_count": null, 1675 | "metadata": { 1676 | "colab": { 1677 | "base_uri": "https://localhost:8080/", 1678 | "height": 35 1679 | }, 1680 | "colab_type": "code", 1681 | "id": "PdJ0iBTXUTW-", 1682 | "outputId": "f704cf60-6dba-430d-a420-03132ee7ab4a" 1683 | }, 1684 | "outputs": [ 1685 | { 1686 | "data": { 1687 | "text/plain": [ 1688 | "15" 1689 | ] 1690 | }, 1691 | "execution_count": 42, 1692 | "metadata": { 1693 | "tags": [] 1694 | }, 1695 | "output_type": "execute_result" 1696 | } 1697 | ], 1698 | "source": [ 1699 | "lista = [1, 2, 3, 4, 5]\n", 1700 | "suma_lista(lista)" 1701 | ] 1702 | }, 1703 | { 1704 | "cell_type": "markdown", 1705 | "metadata": { 1706 | "colab_type": "text", 1707 | "id": "JkSXYvrxdzOL" 1708 | }, 1709 | "source": [ 1710 | "# Clases y Objetos\n", 1711 | "Un objeto es un encapsulamiento de variables y funciones. Los objetos obtienen sus variables y funciones por medio de clases. Las clases son basicamente el esqueleto de los objetos. Tienen la siguiente estructura:\n", 1712 | "\n", 1713 | "```\n", 1714 | "class NombreClase:\n", 1715 | " variables\n", 1716 | "\n", 1717 | " def nombre_funciones(self):\n", 1718 | " instrucciones\n", 1719 | "```\n", 1720 | "\n", 1721 | "**self** es un parámetro especial que siempre se utilizará como una referencia al objeto mismo. Debe ser siempre el primer parámetro formal; no obstante, nunca se le dará un valor de parámetro real en la invocación.\n", 1722 | "\n", 1723 | "**Ejemplo:** Clase de Fracciones" 1724 | ] 1725 | }, 1726 | { 1727 | "cell_type": "code", 1728 | "execution_count": null, 1729 | "metadata": { 1730 | "colab": {}, 1731 | "colab_type": "code", 1732 | "id": "uDbwo641Ua6x" 1733 | }, 1734 | "outputs": [], 1735 | "source": [ 1736 | "class Fraccion():\n", 1737 | " def __init__(self, numerador, denominador):\n", 1738 | " self.numerador = numerador\n", 1739 | " self.denominador = denominador\n", 1740 | "\n", 1741 | " def imprimir(self):\n", 1742 | " print(f'{self.numerador} / {self.denominador}')\n", 1743 | "\n", 1744 | " def suma(self, fraccion):\n", 1745 | " numerador = (self.numerador * fraccion.denominador) + (self.denominador * fraccion.numerador)\n", 1746 | " denominador = self.denominador * fraccion.denominador\n", 1747 | " return Fraccion(numerador, denominador)\n", 1748 | "\n", 1749 | " def resta(self, fraccion):\n", 1750 | " numerador = (self.numerador * fraccion.denominador) - (self.denominador * fraccion.numerador)\n", 1751 | " denominador = self.denominador * fraccion.denominador\n", 1752 | " return Fraccion(numerador, denominador)\n", 1753 | "\n", 1754 | " def multiplicacion(self, fraccion):\n", 1755 | " numerador = self.numerador * fraccion.numerador\n", 1756 | " denominador = self.denominador * fraccion.denominador\n", 1757 | " return Fraccion(numerador, denominador)\n", 1758 | " \n", 1759 | " def division(self, fraccion):\n", 1760 | " numerador = self.numerador * fraccion.denominador\n", 1761 | " denominador = self.denominador * fraccion.numerador\n", 1762 | " return Fraccion(numerador, denominador)" 1763 | ] 1764 | }, 1765 | { 1766 | "cell_type": "code", 1767 | "execution_count": null, 1768 | "metadata": { 1769 | "colab": { 1770 | "base_uri": "https://localhost:8080/", 1771 | "height": 35 1772 | }, 1773 | "colab_type": "code", 1774 | "id": "nxMuTe9rjbDF", 1775 | "outputId": "0f6435c8-4ee9-476b-b85f-4a16c63230bf" 1776 | }, 1777 | "outputs": [ 1778 | { 1779 | "name": "stdout", 1780 | "output_type": "stream", 1781 | "text": [ 1782 | "1 / 2\n" 1783 | ] 1784 | } 1785 | ], 1786 | "source": [ 1787 | "fraccion1 = Fraccion(1, 2)\n", 1788 | "fraccion1.imprimir()" 1789 | ] 1790 | }, 1791 | { 1792 | "cell_type": "code", 1793 | "execution_count": null, 1794 | "metadata": { 1795 | "colab": { 1796 | "base_uri": "https://localhost:8080/", 1797 | "height": 35 1798 | }, 1799 | "colab_type": "code", 1800 | "id": "Y7Z88Okkji4y", 1801 | "outputId": "8ab0d75d-daf3-4141-a09a-b5d108b9fb3f" 1802 | }, 1803 | "outputs": [ 1804 | { 1805 | "name": "stdout", 1806 | "output_type": "stream", 1807 | "text": [ 1808 | "2 / 3\n" 1809 | ] 1810 | } 1811 | ], 1812 | "source": [ 1813 | "fraccion2 = Fraccion(2, 3)\n", 1814 | "fraccion2.imprimir()" 1815 | ] 1816 | }, 1817 | { 1818 | "cell_type": "code", 1819 | "execution_count": null, 1820 | "metadata": { 1821 | "colab": { 1822 | "base_uri": "https://localhost:8080/", 1823 | "height": 35 1824 | }, 1825 | "colab_type": "code", 1826 | "id": "9A-c54qQjo_W", 1827 | "outputId": "5cfb527f-ec6e-4679-f07f-7e9b2291ce0e" 1828 | }, 1829 | "outputs": [ 1830 | { 1831 | "data": { 1832 | "text/plain": [ 1833 | "<__main__.Fraccion at 0x7f7d7bdc12e8>" 1834 | ] 1835 | }, 1836 | "execution_count": 46, 1837 | "metadata": { 1838 | "tags": [] 1839 | }, 1840 | "output_type": "execute_result" 1841 | } 1842 | ], 1843 | "source": [ 1844 | "fraccion3 = fraccion1.suma(fraccion2)\n", 1845 | "fraccion3" 1846 | ] 1847 | }, 1848 | { 1849 | "cell_type": "code", 1850 | "execution_count": null, 1851 | "metadata": { 1852 | "colab": { 1853 | "base_uri": "https://localhost:8080/", 1854 | "height": 35 1855 | }, 1856 | "colab_type": "code", 1857 | "id": "UczaM-l3jt80", 1858 | "outputId": "4acde0a5-4146-4280-cf09-291c7d8431e4" 1859 | }, 1860 | "outputs": [ 1861 | { 1862 | "name": "stdout", 1863 | "output_type": "stream", 1864 | "text": [ 1865 | "7 / 6\n" 1866 | ] 1867 | } 1868 | ], 1869 | "source": [ 1870 | "fraccion3.imprimir()" 1871 | ] 1872 | }, 1873 | { 1874 | "cell_type": "code", 1875 | "execution_count": null, 1876 | "metadata": { 1877 | "colab": { 1878 | "base_uri": "https://localhost:8080/", 1879 | "height": 35 1880 | }, 1881 | "colab_type": "code", 1882 | "id": "3AzLZM6wj59b", 1883 | "outputId": "9d1985b7-5eea-4250-ecf4-86c829ff75f3" 1884 | }, 1885 | "outputs": [ 1886 | { 1887 | "name": "stdout", 1888 | "output_type": "stream", 1889 | "text": [ 1890 | "9 / 18\n" 1891 | ] 1892 | } 1893 | ], 1894 | "source": [ 1895 | "fraccion4 = fraccion3.resta(fraccion2)\n", 1896 | "fraccion4.imprimir()" 1897 | ] 1898 | }, 1899 | { 1900 | "cell_type": "code", 1901 | "execution_count": null, 1902 | "metadata": { 1903 | "colab": { 1904 | "base_uri": "https://localhost:8080/", 1905 | "height": 35 1906 | }, 1907 | "colab_type": "code", 1908 | "id": "ROk8ZlNZj1OX", 1909 | "outputId": "32e12448-51ba-4f20-c418-c72323c78da2" 1910 | }, 1911 | "outputs": [ 1912 | { 1913 | "name": "stdout", 1914 | "output_type": "stream", 1915 | "text": [ 1916 | "4 / 3\n" 1917 | ] 1918 | } 1919 | ], 1920 | "source": [ 1921 | "fraccion5 = fraccion2.division(fraccion1)\n", 1922 | "fraccion5.imprimir()" 1923 | ] 1924 | }, 1925 | { 1926 | "cell_type": "markdown", 1927 | "metadata": { 1928 | "colab_type": "text", 1929 | "id": "kKQ5atN6kRqf" 1930 | }, 1931 | "source": [ 1932 | "# Bibliografía\n", 1933 | "\n", 1934 | "\n", 1935 | "* [Interactive Python Tutorial](https://www.learnpython.org/en/Welcome)\n", 1936 | "* [PCAP: Programming Essentials in Python](https://www.netacad.com/es/courses/programming/pcap-programming-essentials-python)\n", 1937 | "* [Programación orientada a objetos en Python](https://runestone.academy/runestone/static/pythoned/Introduction/ProgramacionOrientadaAObjetosEnPythonDefinicionDeClases.html)\n", 1938 | "\n", 1939 | "\n", 1940 | "\n" 1941 | ] 1942 | } 1943 | ], 1944 | "metadata": { 1945 | "colab": { 1946 | "name": "TallerPython.ipynb", 1947 | "provenance": [] 1948 | }, 1949 | "kernelspec": { 1950 | "display_name": "Python 3", 1951 | "language": "python", 1952 | "name": "python3" 1953 | }, 1954 | "language_info": { 1955 | "codemirror_mode": { 1956 | "name": "ipython", 1957 | "version": 3 1958 | }, 1959 | "file_extension": ".py", 1960 | "mimetype": "text/x-python", 1961 | "name": "python", 1962 | "nbconvert_exporter": "python", 1963 | "pygments_lexer": "ipython3", 1964 | "version": "3.7.8" 1965 | } 1966 | }, 1967 | "nbformat": 4, 1968 | "nbformat_minor": 1 1969 | } 1970 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Introduccion a Python 2 | 3 | Para realizar el taller se pueden seguir las siguientes opciones: 4 | 5 | 1. [Instalar Python](https://www.python.org/downloads/) 6 | 7 | 2. [Instalar Anaconda y ejecutar el notebook localmente](https://docs.conda.io/projects/conda/en/latest/user-guide/install/) 8 | ``` 9 | conda create --name intro-python 10 | conda activate intro-python 11 | conda install jupyter 12 | jupyter notebook 13 | ``` 14 | 15 | 3. Trabajar en la version online del notebook. Para ver el notebook click en el siguiente enlace: 16 | 17 | [![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/carogomezt/IntroduccionPython/master?filepath=Introduccion_Python.ipynb) 18 | 19 | ## Diapositivas: 20 | [Primeros pasos con Python :snake:](https://docs.google.com/presentation/d/12PAPQ-e4BFVX4fYQ1TFxBebXgemq54J18_wHw-4g_TA/edit?usp=sharing) 21 | --------------------------------------------------------------------------------