{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "name": "resumen50.ipynb", "provenance": [], "authorship_tag": "ABX9TyNpNuEtAdTKDcTHLhCu+HfY", "include_colab_link": true }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" } }, "cells": [ { "cell_type": "markdown", "metadata": { "id": "view-in-github", "colab_type": "text" }, "source": [ "\"Open" ] }, { "cell_type": "markdown", "source": [ "# Bucle `For`\n", "* Un bucle o **loop** es un proceso que se repite un cierto número de veces.\n", "* Es un proceso repetitivo, reiterativo\n", "* El bucle For se repite un número de veces conocido, un número de veces concreto.\n", "\n", "```python\n", "for in :\n", " \n", "```\n" ], "metadata": { "id": "5G7bh-W8_Dwr" } }, { "cell_type": "code", "source": [ "ciudades =[\"Roma\", \"Londres\", \"París\", \"Madrid\", \"Berlín\"]\n", "\n", "for ciudad in ciudades:\n", " print(ciudad)" ], "metadata": { "id": "QkVdMGiI_9na", "outputId": "650f242c-7985-425b-c992-7786a75a8fee", "colab": { "base_uri": "https://localhost:8080/" } }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Roma\n", "Londres\n", "París\n", "Madrid\n", "Berlín\n" ] } ] }, { "cell_type": "code", "source": [ "for ciudad in ciudades:\n", " if ciudad == \"Madrid\" or ciudad == \"Roma\":\n", " print(ciudad)" ], "metadata": { "id": "i9hj2xZhASJu", "outputId": "be47ff4d-2c59-4b79-8641-f1e5c99b446a", "colab": { "base_uri": "https://localhost:8080/" } }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Roma\n", "Madrid\n" ] } ] }, { "cell_type": "markdown", "source": [ "## `For` con `range`" ], "metadata": { "id": "a2UfeIXtA8ff" } }, { "cell_type": "code", "source": [ "# range crea un iterable de números enteros\n", "range(1,10)" ], "metadata": { "id": "X7wK2vf3BHVz", "outputId": "b2f880df-4d32-4e2a-8fd5-56a909f19050", "colab": { "base_uri": "https://localhost:8080/" } }, "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "range(1, 10)" ] }, "metadata": {}, "execution_count": 3 } ] }, { "cell_type": "code", "source": [ "# convertimos el rango en una lista\n", "list(range(1,10)) # el 10 no se incluye" ], "metadata": { "id": "clesPHuhBU6s", "outputId": "e291c2f1-11c8-45b5-ccf6-2e0744717d83", "colab": { "base_uri": "https://localhost:8080/" } }, "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "[1, 2, 3, 4, 5, 6, 7, 8, 9]" ] }, "metadata": {}, "execution_count": 4 } ] }, { "cell_type": "code", "source": [ "list(range(5)) # lista desde el 0 hasta el 4" ], "metadata": { "id": "m0SFZNm9BiPz", "outputId": "4a2105f8-e44a-4b2c-c290-3318a6c3a5d9", "colab": { "base_uri": "https://localhost:8080/" } }, "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "[0, 1, 2, 3, 4]" ] }, "metadata": {}, "execution_count": 5 } ] }, { "cell_type": "code", "source": [ "for i in range(5): # la variable i es una variable instrumental, hace referencia al index\n", " print(i)" ], "metadata": { "id": "nb-uko5ABtN3", "outputId": "eeab9095-e1d7-4824-e427-98da5435c1cc", "colab": { "base_uri": "https://localhost:8080/" } }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n" ] } ] }, { "cell_type": "code", "source": [ "ciudades =[\"Roma\", \"Londres\", \"París\", \"Madrid\", \"Berlín\"]\n", "\n", "for i in range(len(ciudades)):\n", " print(ciudades[i])" ], "metadata": { "id": "V7Ux4A_ECBeT", "outputId": "211fca16-1973-4437-f5ca-a7a31328d03a", "colab": { "base_uri": "https://localhost:8080/" } }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Roma\n", "Londres\n", "París\n", "Madrid\n", "Berlín\n" ] } ] }, { "cell_type": "markdown", "source": [ "## `For` ... `enumerate`" ], "metadata": { "id": "4J_SHPNfCRtj" } }, { "cell_type": "code", "source": [ "for i, v in enumerate(ciudades): # i hace referencia al index y v hace referencia al valor\n", " print(f'{i} → {v}')\n" ], "metadata": { "id": "hpyY_Wuxjqh8", "outputId": "826e4b52-6a49-494d-fd18-cfc4ae0492eb", "colab": { "base_uri": "https://localhost:8080/" } }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "0 → Roma\n", "1 → Londres\n", "2 → París\n", "3 → Madrid\n", "4 → Berlín\n" ] } ] }, { "cell_type": "markdown", "source": [ "## Bucle `For` en un diccionario" ], "metadata": { "id": "0yB8h9NkkztK" } }, { "cell_type": "code", "source": [ "paco = {'nombre':'Francisco', 'edad':25, 'asegurado':True}\n", "print(paco)\n", "paco.items() # muestra los items del diccionario que son las parejas clave, valor" ], "metadata": { "id": "RTthhRwuoUbj", "outputId": "e940110d-ca38-4bb9-cfd5-8db82b909861", "colab": { "base_uri": "https://localhost:8080/" } }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "{'nombre': 'Francisco', 'edad': 25, 'asegurado': True}\n" ] }, { "output_type": "execute_result", "data": { "text/plain": [ "dict_items([('nombre', 'Francisco'), ('edad', 25), ('asegurado', True)])" ] }, "metadata": {}, "execution_count": 12 } ] }, { "cell_type": "code", "source": [ "for k,v in paco.items(): # k es key, v es value\n", " print(f'{k} → {v}')" ], "metadata": { "id": "j-C-cO5Qo3eO", "outputId": "87178c0c-1c3d-4808-b945-5a93cfebca17", "colab": { "base_uri": "https://localhost:8080/" } }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "nombre → Francisco\n", "edad → 25\n", "asegurado → True\n" ] } ] } ] }