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
167 changes: 138 additions & 29 deletions lab-python-list-comprehension.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,26 @@
},
{
"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"
"input = [[18, 20], [45, 2], [61, 12], [37, 6], [21, 21], [78, 9]]\n",
"\n",
"def open_or_senior(input):\n",
" return [\"Senior\" if age >= 55 and handicap > 7 else \"Open\" for age, handicap in input]\n",
"\n",
"result = open_or_senior(input)\n",
"print (result)"
]
},
{
Expand All @@ -95,12 +109,25 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"id": "53061dce-7aa3-4476-8b56-71413c5e135c",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"23\n"
]
}
],
"source": [
"# your code goes here"
"def sum_of_multiples(n: int) -> int:\n",
" \"\"\"Sum multiples of 3 or 5 below n. If n is negative, return 0.\"\"\"\n",
" if n <= 0:\n",
" return 0\n",
" return sum(i for i in range(n) if i % 3 == 0 or i % 5 == 0)\n",
"print(sum_of_multiples(10)) # Output: 23"
]
},
{
Expand All @@ -127,12 +154,22 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"id": "a8cb1579-7065-4fc0-bd53-f91c2ad1dad9",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1, 2, 3]\n"
]
}
],
"source": [
"# your code goes here"
"def digits(n: int):\n",
" return [int(ch) for ch in str(n)]\n",
"print(digits(123))"
]
},
{
Expand All @@ -155,12 +192,22 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 4,
"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(nums):\n",
" return [-n for n in nums]\n",
"print(invert([1,2,3,4,5]))"
]
},
{
Expand Down Expand Up @@ -193,12 +240,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"
"# your code goes here\n",
"squares_dict = {x: x**2 for x in range(1, 16)}\n",
"print(squares_dict)"
]
},
{
Expand All @@ -220,12 +277,24 @@
},
{
"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"
"# your code goes here\n",
"lista1= [1, 2]\n",
"lista2= [3, 4]\n",
"pairs = [(a, b) for a in lista1 for b in lista2]\n",
"print(pairs) "
]
},
{
Expand All @@ -246,12 +315,22 @@
},
{
"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', 2), ('b', 4), ('a', 1), ('b', 3)}\n"
]
}
],
"source": [
"# your code goes here"
"dict = {\"a\": [1, 2], \"b\": [3, 4]}\n",
"lista = {(k, v) for k, values in dict.items() for v in values}\n",
"print(lista)"
]
},
{
Expand Down Expand Up @@ -281,12 +360,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"
"# your code goes here\n",
"\n",
"lista = [([1, 2], [3, 4]), ([5, 6], [7, 8])]\n",
"tupla = [(a, b) for izquierda, derecha in lista for a in izquierda for b in derecha]\n",
"print(tupla)"
]
},
{
Expand All @@ -308,18 +399,36 @@
},
{
"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'), ('a', 'b'), ('c', 'd'), ('c', 'b')}\n"
]
}
],
"source": [
"# your code goes here"
"# your code goes here\n",
"\n",
"lista = [\"ab\", \"cd\"]\n",
"s1, s2 = lista\n",
"\n",
"# pares dentro de cada string + cruces concretos\n",
"tupla = [(s1[0], s1[1]), (s2[0], s2[1])]\n",
"tupla2 = [(s1[0], s2[1]), (s2[0], s1[1])]\n",
"\n",
"fin_tupla = set(tupla + tupla2)\n",
"print(fin_tupla)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -333,7 +442,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.12.10"
}
},
"nbformat": 4,
Expand Down