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
169 changes: 143 additions & 26 deletions lab-python-list-comprehension.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,27 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"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"
"miembros = [[18, 20], [45, 2], [61, 12], [37, 6], [21, 21], [78, 9]]\n",
"\n",
"result = [\n",
" \"Senior\" if edad >= 55 and handicap > 7 else \"Open\"\n",
" for edad, handicap in miembros\n",
"]\n",
"\n",
"print(result)\n"
]
},
{
Expand All @@ -95,12 +110,29 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 5,
"id": "53061dce-7aa3-4476-8b56-71413c5e135c",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"23\n"
]
}
],
"source": [
"# your code goes here"
"def suma_multiplos(n):\n",
" if n < 0:\n",
" return 0\n",
" \n",
" return sum(\n",
" x for x in range(n)\n",
" if x % 3 == 0 or x % 5 == 0\n",
" )\n",
"\n",
"print(suma_multiplos(10)) \n"
]
},
{
Expand All @@ -127,12 +159,25 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 11,
"id": "a8cb1579-7065-4fc0-bd53-f91c2ad1dad9",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[2, 0, 2, 6]\n",
"[2, 0, 2, 2]\n"
]
}
],
"source": [
"# your code goes here"
"def descomposicion (n):\n",
" return [int(digit) for digit in str(n)]\n",
"\n",
"print (descomposicion(2026))\n",
"print (descomposicion(2022))"
]
},
{
Expand All @@ -155,12 +200,26 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 18,
"id": "86ebe759-76d8-4012-8590-c48a473a6099",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"[-2, -4, -8]"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# your code goes here"
"def invert(lst):\n",
" return [-num for num in lst]\n",
"\n",
"invert ([2, 4, 8])"
]
},
{
Expand Down Expand Up @@ -193,12 +252,39 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 20,
"id": "6711881b-450a-44cb-a3d0-367b2c6a4464",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"{1: 1,\n",
" 2: 4,\n",
" 3: 9,\n",
" 4: 16,\n",
" 5: 25,\n",
" 6: 36,\n",
" 7: 49,\n",
" 8: 64,\n",
" 9: 81,\n",
" 10: 100,\n",
" 11: 121,\n",
" 12: 144,\n",
" 13: 169,\n",
" 14: 196,\n",
" 15: 225}"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# your code goes here"
"squares = {x: x**2 for x in range(1, 16)}\n",
"\n",
"squares"
]
},
{
Expand All @@ -220,12 +306,30 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 29,
"id": "a1d55cea-96c3-4853-8220-17c0904a8816",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"<function __main__.all_pairs(list1, list2)>"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# your code goes here"
"\n",
"def all_pairs(list1, list2):\n",
" return [(x, y) for x in list1 for y in list2]\n",
"\n",
"list1 = [10, 20]\n",
"list2= [50,100]\n",
"\n",
"all_pairs"
]
},
{
Expand All @@ -246,12 +350,13 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 30,
"id": "0175239c-87fa-4ba0-8776-d62567256db7",
"metadata": {},
"outputs": [],
"source": [
"# your code goes here"
"def expand_dict(d):\n",
" return [(key, value) for key in d for value in d[key]]\n"
]
},
{
Expand Down Expand Up @@ -281,12 +386,18 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 32,
"id": "248918d0-f082-40a8-9d5c-c7b4ffd2bfca",
"metadata": {},
"outputs": [],
"source": [
"# your code goes here"
"def all_pairs_from_tuples(tup):\n",
" return [\n",
" (x, y)\n",
" for list1, list2 in tup\n",
" for x in list_1\n",
" for y in list_2\n",
" ]\n"
]
},
{
Expand All @@ -308,18 +419,24 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 33,
"id": "c57d9e77-6859-45dd-a2e6-2c1898f1baa2",
"metadata": {},
"outputs": [],
"source": [
"# your code goes here"
"def char_pairs(strings):\n",
" first, second = strings\n",
" return {\n",
" (c1, c2)\n",
" for c1 in first\n",
" for c2 in second\n",
" }\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -333,7 +450,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.9"
}
},
"nbformat": 4,
Expand Down