{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "view-in-github", "colab_type": "text" }, "source": [ "\"Open" ] }, { "cell_type": "markdown", "id": "c733515f", "metadata": { "id": "c733515f" }, "source": [ "# Bucles ```for``` anidados" ] }, { "cell_type": "markdown", "id": "3b50923b", "metadata": { "id": "3b50923b" }, "source": [ "Un bucle anidado es un bucle dentro de un bucle. \n", "El \"bucle interno\" se ejecutará una vez por cada iteración del \"bucle externo\"." ] }, { "cell_type": "code", "execution_count": 6, "id": "476a1e71", "metadata": { "id": "476a1e71", "outputId": "60d47dbd-00ed-44c8-88e2-886e8f4b8c4d", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "El producto de 1 por 1 es 1.\n", "El producto de 1 por 2 es 2.\n", "El producto de 1 por 3 es 3.\n", "==============================\n", "El producto de 4 por 1 es 4.\n", "El producto de 4 por 2 es 8.\n", "El producto de 4 por 3 es 12.\n", "==============================\n", "El producto de 7 por 1 es 7.\n", "El producto de 7 por 2 es 14.\n", "El producto de 7 por 3 es 21.\n", "==============================\n", "El producto de 10 por 1 es 10.\n", "El producto de 10 por 2 es 20.\n", "El producto de 10 por 3 es 30.\n", "==============================\n" ] } ], "source": [ "for i in range(1,11,3):\n", " for j in range(1,4):\n", " print(f\"El producto de {i} por {j} es {i*j}.\")\n", " print(\"=\"*30)" ] }, { "cell_type": "code", "execution_count": 7, "id": "5df0658f", "metadata": { "id": "5df0658f", "outputId": "ba21e4b9-19bf-478c-91c0-eac6f7616f63", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Vacaciones en España de tipo playa\n", "Vacaciones en España de tipo montaña\n", "Vacaciones en España de tipo rural\n", "Vacaciones en España de tipo ciudades\n", "========================================\n", "Vacaciones en Francia de tipo playa\n", "Vacaciones en Francia de tipo montaña\n", "Vacaciones en Francia de tipo rural\n", "Vacaciones en Francia de tipo ciudades\n", "========================================\n", "Vacaciones en Italia de tipo playa\n", "Vacaciones en Italia de tipo montaña\n", "Vacaciones en Italia de tipo rural\n", "Vacaciones en Italia de tipo ciudades\n", "========================================\n" ] } ], "source": [ "for i in ['España', 'Francia', 'Italia']:\n", " for j in ['playa', 'montaña', 'rural', 'ciudades']:\n", " print(f\"Vacaciones en {i} de tipo {j}\")\n", " print(\"=\"*40)" ] }, { "cell_type": "code", "execution_count": 3, "id": "dce0980f", "metadata": { "id": "dce0980f", "outputId": "a5de60d6-e4eb-411e-8c69-bde884327a24", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "1\n", "a\n", "b\n", "c\n", "2\n", "a\n", "b\n", "c\n", "3\n", "a\n", "b\n", "c\n" ] } ], "source": [ "for i in range(1,4):\n", " print(i)\n", " for j in ['a', 'b', 'c']:\n", " print(j)\n", " #print(\"\\t\"+str(j)) #con esta alternativa introducimos tabuladores" ] }, { "cell_type": "markdown", "id": "dcddc858", "metadata": { "id": "dcddc858" }, "source": [ "## Acceder a los elementos de una matriz" ] }, { "cell_type": "code", "execution_count": 4, "id": "b9df266f", "metadata": { "id": "b9df266f", "outputId": "351959f9-a84d-4e12-dca0-7eac3ac046ca", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "filas: 5\n", "columnas: 3\n", "~~~~~~~~~~~~~~~~~~~~\n", "a11 = A[0][0] = a\n", "a12 = A[0][1] = b\n", "a13 = A[0][2] = c\n", "~~~~~~~~~~~~~~~~~~~~\n", "a21 = A[1][0] = i\n", "a22 = A[1][1] = j\n", "a23 = A[1][2] = k\n", "~~~~~~~~~~~~~~~~~~~~\n", "a31 = A[2][0] = l\n", "a32 = A[2][1] = m\n", "a33 = A[2][2] = n\n", "~~~~~~~~~~~~~~~~~~~~\n", "a41 = A[3][0] = r\n", "a42 = A[3][1] = p\n", "a43 = A[3][2] = q\n", "~~~~~~~~~~~~~~~~~~~~\n", "a51 = A[4][0] = x\n", "a52 = A[4][1] = y\n", "a53 = A[4][2] = z\n", "~~~~~~~~~~~~~~~~~~~~\n" ] } ], "source": [ "A =[\n", " ['a','b','c'],\n", " ['i','j','k'],\n", " ['l','m','n'],\n", " ['r','p','q'],\n", " ['x','y','z']\n", "]\n", "\n", "filas = len(A)\n", "columnas = len(A[0])\n", "print(\"filas:\", filas)\n", "print(\"columnas:\", columnas)\n", "print(\"~\"*20)\n", "\n", "for i in range(filas):\n", " for j in range(columnas):\n", " print(f\"a{i+1}{j+1} = A[{i}][{j}] = {A[i][j]}\")\n", " print(\"~\"*20)" ] }, { "cell_type": "markdown", "id": "1288f067", "metadata": { "id": "1288f067" }, "source": [ "## Generación de números primos" ] }, { "cell_type": "markdown", "id": "267ce227", "metadata": { "id": "267ce227" }, "source": [ "Puede ver la generación de números primos en Python en las siguientes entradas del blog.\n", "- [Generación de números primos en Python](https://altocodigo.blogspot.com/2017/09/numeros-primos-en-python.html)\n", "- [Números primos en Python](https://altocodigo.blogspot.com/2021/01/numeros-primos-en-python.html)" ] }, { "cell_type": "code", "execution_count": 5, "id": "f510de38", "metadata": { "scrolled": true, "id": "f510de38", "outputId": "19002fa2-09fa-4994-d8b2-dbc3f56a4b17", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149," ] } ], "source": [ "print(2, end = ',')\n", "for i in range(3,150,2):\n", " for j in range(3,int(i**.5)+1,2):\n", " if i%j==0: break\n", " else: print(i, end = ',')" ] } ], "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": "0360_bucles_for_anidados.ipynb", "provenance": [], "include_colab_link": true } }, "nbformat": 4, "nbformat_minor": 5 }