From 545b2a4a4afb1db9efb045cbebe221ab3a4d5fe9 Mon Sep 17 00:00:00 2001 From: Alejandro de Tuero Date: Wed, 19 Nov 2025 20:44:36 +0100 Subject: [PATCH] solved lab --- lab-python-list-comprehension.ipynb | 320 +++++++++++++++++++++++++--- 1 file changed, 291 insertions(+), 29 deletions(-) diff --git a/lab-python-list-comprehension.ipynb b/lab-python-list-comprehension.ipynb index 6bef7fd..ab70be9 100644 --- a/lab-python-list-comprehension.ipynb +++ b/lab-python-list-comprehension.ipynb @@ -71,12 +71,51 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "id": "21625526-3fae-4c55-bab5-f91940070681", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['Open', 'Open', 'Senior', 'Open', 'Open', 'Senior']\n" + ] + } + ], "source": [ - "# your code goes here" + "# your code goes here\n", + "input = [[18, 20], [45, 2], [61, 12], [37, 6], [21, 21], [78, 9]]\n", + "\n", + "result = []\n", + "\n", + "for age, handicap in input:\n", + " if age >= 55 and handicap > 7:\n", + " result.append(\"Senior\")\n", + " else:\n", + " result.append(\"Open\")\n", + "\n", + "print(result)" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "3fbe6392", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['Open', 'Open', 'Senior', 'Open', 'Open', 'Senior']\n" + ] + } + ], + "source": [ + "result = [\"Senior\" if age >= 55 and handicap > 7 else \"Open\"\n", + " for age, handicap in input]\n", + "print(result)" ] }, { @@ -95,12 +134,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "id": "53061dce-7aa3-4476-8b56-71413c5e135c", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "23\n" + ] + } + ], "source": [ - "# your code goes here" + "# your code goes here\n", + "n = 10\n", + "\n", + "resultado = sum([x for x in range(n) if x % 3 == 0 or x % 5 == 0])\n", + "print(resultado)" ] }, { @@ -127,12 +178,40 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "id": "a8cb1579-7065-4fc0-bd53-f91c2ad1dad9", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2, 3]\n", + "[1]\n", + "[8, 6, 7, 5, 3, 0, 9]\n" + ] + } + ], "source": [ - "# your code goes here" + "# your code goes here\n", + "number = 123\n", + "digitos = []\n", + "\n", + "for d in str(number):\n", + " digitos.append(int(d))\n", + "print(digitos)\n", + "\n", + "num = 1\n", + "digit = []\n", + "for i in str(num):\n", + " digit.append(int(i))\n", + "print(digit)\n", + "\n", + "numeros = 8675309\n", + "lista_digitos = []\n", + "for n in str(numeros):\n", + " lista_digitos.append(int(n))\n", + "print(lista_digitos)" ] }, { @@ -155,12 +234,33 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "id": "86ebe759-76d8-4012-8590-c48a473a6099", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[-1, -2, -3, -4, -5]\n", + "[-1, 2, -3, 4, -5]\n" + ] + } + ], "source": [ - "# your code goes here" + "# your code goes here\n", + "lista = ([1,2,3,4,5])\n", + "def invertir(lista):\n", + " return [-x for x in lista]\n", + "lista_invertida = invertir(lista)\n", + "print(lista_invertida)\n", + "\n", + "numbers = ([1,-2,3,-4,5])\n", + "def invert(numbers):\n", + " return [-l for l in numbers]\n", + "list_invert = invert(numbers)\n", + "print(list_invert)\n", + "\n" ] }, { @@ -193,12 +293,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "id": "6711881b-450a-44cb-a3d0-367b2c6a4464", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81, 10: 100, 11: 121, 12: 144, 13: 169, 14: 196, 15: 225}\n" + ] + } + ], "source": [ - "# your code goes here" + "# your code goes here\n", + "diccionario = {i: i**2 for i in range(1, 16)}\n", + "print(diccionario)" ] }, { @@ -220,12 +330,52 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "id": "a1d55cea-96c3-4853-8220-17c0904a8816", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[(1, 3), (1, 4), (2, 3), (2, 4)]\n" + ] + } + ], + "source": [ + "# your code goes here\n", + "lista1 = [1, 2]\n", + "lista2 = [3, 4]\n", + "\n", + "pares = []\n", + "\n", + "for a in lista1:\n", + " for b in lista2:\n", + " pares.append((a, b))\n", + "\n", + "print(pares)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "d9d784fa", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[(1, 3), (1, 4), (2, 3), (2, 4)]\n" + ] + } + ], "source": [ - "# your code goes here" + "lista1 = [1, 2]\n", + "lista2 = [3, 4]\n", + "\n", + "pares = [(a, b) for a in lista1 for b in lista2]\n", + "print(pares)" ] }, { @@ -246,12 +396,49 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "id": "0175239c-87fa-4ba0-8776-d62567256db7", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[('a', 1), ('a', 2), ('b', 3), ('b', 4)]\n" + ] + } + ], "source": [ - "# your code goes here" + "# your code goes here\n", + "dict_3 = {\"a\": [1, 2], \"b\": [3, 4]}\n", + "par = []\n", + "\n", + "for key, valores in dict_3.items():\n", + " for valor in valores:\n", + " par.append((key, valor))\n", + "\n", + "print(par)" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "0257becc", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[('a', 1), ('a', 2), ('b', 3), ('b', 4)]\n" + ] + } + ], + "source": [ + "dict_3 = {\"a\": [1, 2], \"b\": [3, 4]}\n", + "\n", + "par = [(key, valor) for key, valores in dict_3.items() for valor in valores]\n", + "print(par)" ] }, { @@ -281,12 +468,50 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "id": "248918d0-f082-40a8-9d5c-c7b4ffd2bfca", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[(1, 3), (1, 4), (2, 3), (2, 4), (5, 7), (5, 8), (6, 7), (6, 8)]\n" + ] + } + ], "source": [ - "# your code goes here" + "# your code goes here\n", + "datos = [([1, 2], [3, 4]), ([5, 6], [7, 8])]\n", + "new_list = []\n", + "\n", + "for list1, list2 in datos:\n", + " for x in list1:\n", + " for y in list2:\n", + " new_list.append((x, y))\n", + "print(new_list)\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "549b00c9", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[(1, 3), (1, 4), (2, 3), (2, 4), (5, 7), (5, 8), (6, 7), (6, 8)]\n" + ] + } + ], + "source": [ + "datos = [([1, 2], [3, 4]), ([5, 6], [7, 8])]\n", + "\n", + "new_list = [(x, y)for list1, list2 in datos for x in list1 for y in list2]\n", + "print(new_list)" ] }, { @@ -308,18 +533,55 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "id": "c57d9e77-6859-45dd-a2e6-2c1898f1baa2", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{('b', 'c'), ('b', 'd'), ('a', 'c'), ('a', 'd')}\n" + ] + } + ], + "source": [ + "# your code goes here\n", + "strings =[\"ab\", \"cd\"]\n", + "resultado = set()\n", + "\n", + "str1, str2 = strings\n", + "for c1 in str1:\n", + " for c2 in str2:\n", + " resultado.add((c1, c2))\n", + "print(resultado)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "b147aaff", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{('b', 'c'), ('b', 'd'), ('a', 'c'), ('a', 'd')}\n" + ] + } + ], "source": [ - "# your code goes here" + "strings =[\"ab\", \"cd\"]\n", + "\n", + "resultado = {(c1, c2) for c1 in str1 for c2 in str2}\n", + "print(resultado)" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -333,7 +595,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.12.9" } }, "nbformat": 4,