Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
160 changes: 134 additions & 26 deletions lab-python-list-comprehension.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,21 @@
"execution_count": null,
"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"
"input_data = [[18, 20], [45, 2], [61, 12], [37, 6], [21, 21], [78, 9]]\n",
"\n",
"# Solución con List Comprehension\n",
"output = [\"Senior\" if age >= 55 and handicap > 7 else \"Open\" for age, handicap in input_data]\n",
"print(output)"
]
},
{
Expand All @@ -98,9 +110,22 @@
"execution_count": null,
"id": "53061dce-7aa3-4476-8b56-71413c5e135c",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"23\n"
]
}
],
"source": [
"# your code goes here"
"def sum_multiples(n):\n",
" if n < 0: return 0\n",
" # Sumamos los números 'i' que cumplen la condición\n",
" return sum([i for i in range(n) if i % 3 == 0 or i % 5 == 0])\n",
"\n",
"print(sum_multiples(10)) # Resultado: 23"
]
},
{
Expand Down Expand Up @@ -130,9 +155,22 @@
"execution_count": null,
"id": "a8cb1579-7065-4fc0-bd53-f91c2ad1dad9",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[8, 6, 7, 5, 3, 0, 9]\n"
]
}
],
"source": [
"# your code goes here"
"number = 8675309\n",
"\n",
"# Convertimos a string para iterar por cada carácter y luego a int de nuevo\n",
"digits = [int(d) for d in str(number)]\n",
"\n",
"print(digits) # [8, 6, 7, 5, 3, 0, 9]"
]
},
{
Expand All @@ -155,12 +193,23 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 11,
"id": "86ebe759-76d8-4012-8590-c48a473a6099",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[-1, 2, -3, -4, 5]\n"
]
}
],
"source": [
"# your code goes here"
"def invert(lst):\n",
" return [-x for x in lst]\n",
"\n",
"print(invert([1, -2, 3, 4, -5])) # [-1, 2, -3, -4, 5]"
]
},
{
Expand Down Expand Up @@ -193,12 +242,22 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 5,
"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"
"# Dict comprehension: {clave: valor for elemento in iterable}\n",
"squares_dict = {x: x**2 for x in range(1, 16)}\n",
"print(squares_dict)"
]
},
{
Expand All @@ -220,12 +279,25 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 6,
"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"
"list1 = [1, 2]\n",
"list2 = [3, 4]\n",
"\n",
"# Doble bucle for dentro de la comprensión\n",
"pairs = [(a, b) for a in list1 for b in list2]\n",
"print(pairs) # [(1, 3), (1, 4), (2, 3), (2, 4)]"
]
},
{
Expand All @@ -246,12 +318,24 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 7,
"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"
"data = {\"a\": [1, 2], \"b\": [3, 4]}\n",
"\n",
"# Accedemos a los items (key, list) y luego iteramos sobre la lista\n",
"kv_pairs = [(k, v) for k, v_list in data.items() for v in v_list]\n",
"print(kv_pairs) # [('a', 1), ('a', 2), ('b', 3), ('b', 4)]"
]
},
{
Expand Down Expand Up @@ -281,12 +365,24 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 8,
"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"
"input_bonus = [([1, 2], [3, 4]), ([5, 6], [7, 8])]\n",
"\n",
"# Triple iteración: por tupla, por elemento de lista1 y por elemento de lista2\n",
"bonus_output = [(x, y) for l1, l2 in input_bonus for x in l1 for y in l2]\n",
"print(bonus_output)"
]
},
{
Expand All @@ -308,18 +404,30 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 9,
"id": "c57d9e77-6859-45dd-a2e6-2c1898f1baa2",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{('a', 'd'), ('b', 'c'), ('b', 'd'), ('a', 'c')}\n"
]
}
],
"source": [
"# your code goes here"
"strings = [\"ab\", \"cd\"]\n",
"\n",
"# Set comprehension (usa llaves {})\n",
"char_pairs = {(char1, char2) for char1 in strings[0] for char2 in strings[1]}\n",
"print(char_pairs) # {('a', 'c'), ('a', 'd'), ('b', 'c'), ('b', 'd')}"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -333,7 +441,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.14.0"
}
},
"nbformat": 4,
Expand Down