From 5ff8211b316f2f84fe30cfadc7a809d176b1b6f1 Mon Sep 17 00:00:00 2001 From: Eylin Date: Fri, 30 Jan 2026 15:10:11 +0100 Subject: [PATCH] Add lab --- lab-python-list-comprehension.ipynb | 301 ++++++++++++++++++++++++++-- 1 file changed, 281 insertions(+), 20 deletions(-) diff --git a/lab-python-list-comprehension.ipynb b/lab-python-list-comprehension.ipynb index 6bef7fd..7620194 100644 --- a/lab-python-list-comprehension.ipynb +++ b/lab-python-list-comprehension.ipynb @@ -71,12 +71,35 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 15, "id": "21625526-3fae-4c55-bab5-f91940070681", "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "input = [[18, 20], [45, 2], [61, 12], [37, 6], [21, 21], [78, 9]]\n", + "membership = [\"Senior\" if a > 55 and h > 7 else \"Open\" for a , h in input]\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "75f6683b", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Open', 'Open', 'Senior', 'Open', 'Open', 'Senior']" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "membership" ] }, { @@ -95,12 +118,36 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 65, "id": "53061dce-7aa3-4476-8b56-71413c5e135c", "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "numbers = [0,1,2,3,4,5,6,7,8,9]\n", + "mul_3 = [3,5,6,9]\n", + "total = 0\n", + "product = [total:= total + x if x >= 0 else 0 for x in mul_3 ]" + ] + }, + { + "cell_type": "code", + "execution_count": 66, + "id": "b530ea5b", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[3, 8, 14, 23]" + ] + }, + "execution_count": 66, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "product" ] }, { @@ -127,12 +174,34 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 67, "id": "a8cb1579-7065-4fc0-bd53-f91c2ad1dad9", "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "digits = 8675309\n", + "new_list = [int(d) for d in str(digits)]\n" + ] + }, + { + "cell_type": "code", + "execution_count": 68, + "id": "2915f885", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[8, 6, 7, 5, 3, 0, 9]" + ] + }, + "execution_count": 68, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "new_list" ] }, { @@ -155,12 +224,34 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 69, "id": "86ebe759-76d8-4012-8590-c48a473a6099", "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "invert =[1,-2,3,-4,5]\n", + "new_invert = [-number for number in invert]" + ] + }, + { + "cell_type": "code", + "execution_count": 70, + "id": "5ec0ffd9", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[-1, 2, -3, 4, -5]" + ] + }, + "execution_count": 70, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "new_invert" ] }, { @@ -193,12 +284,57 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "id": "6711881b-450a-44cb-a3d0-367b2c6a4464", "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "list_numbers = {\"1\":1,\"2\":2,\"3\":3,\"4\":4, \"5\":5,\"6\":6,\"7\":7,\"8\":8,\"9\":9,\"10\":10,\"11\":11,\"12\":12, \"13\":13, \"14\":14, \"15\":15}" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "1d0b53c8", + "metadata": {}, + "outputs": [], + "source": [ + "new_list_numbers = {key:value**2 for (key,value) in list_numbers.items()}" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "6c4f7be6", + "metadata": {}, + "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": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "new_list_numbers" ] }, { @@ -220,12 +356,44 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "id": "a1d55cea-96c3-4853-8220-17c0904a8816", "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "input = [1, 2, 3, 4]\n", + "in_2 =[5,6]" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "058d7ff6", + "metadata": {}, + "outputs": [], + "source": [ + "output = [ (x, y) for x in input for y in in_2] " + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "dfa292d0", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[(1, 5), (1, 6), (2, 5), (2, 6), (3, 5), (3, 6), (4, 5), (4, 6)]" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "output" ] }, { @@ -246,12 +414,43 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 18, "id": "0175239c-87fa-4ba0-8776-d62567256db7", "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "dic = {\"product_1\": [20, 10], \"product_2\": [30, 40]}" + ] + }, + { + "cell_type": "code", + "execution_count": 81, + "id": "2cdcc1d6", + "metadata": {}, + "outputs": [], + "source": [ + "dict_1 = [(key,v) for key,values in dic.items() for v in values ]" + ] + }, + { + "cell_type": "code", + "execution_count": 83, + "id": "db45c635", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[('product_1', 20), ('product_1', 10), ('product_2', 30), ('product_2', 40)]" + ] + }, + "execution_count": 83, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dict_1" ] }, { @@ -281,12 +480,43 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 139, "id": "248918d0-f082-40a8-9d5c-c7b4ffd2bfca", "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "Input = [([1, 2], [3, 4]), ([5, 6], [7, 8])]" + ] + }, + { + "cell_type": "code", + "execution_count": 146, + "id": "aac39f66", + "metadata": {}, + "outputs": [], + "source": [ + "output = [(a, b) for x, y in Input for a in x for b in y]" + ] + }, + { + "cell_type": "code", + "execution_count": 147, + "id": "8d308ce4", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[(1, 3), (1, 4), (2, 3), (2, 4), (5, 7), (5, 8), (6, 7), (6, 8)]" + ] + }, + "execution_count": 147, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "output" ] }, { @@ -308,18 +538,49 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 154, "id": "c57d9e77-6859-45dd-a2e6-2c1898f1baa2", "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "Input_2 = [\"ab\", \"cd\"]" + ] + }, + { + "cell_type": "code", + "execution_count": 155, + "id": "8b8c14ab", + "metadata": {}, + "outputs": [], + "source": [ + "output_2 = {(a, b) for a in Input[0] for b in Input[1]}" + ] + }, + { + "cell_type": "code", + "execution_count": 156, + "id": "778f0f2f", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{('a', 'c'), ('a', 'd'), ('b', 'c'), ('b', 'd')}" + ] + }, + "execution_count": 156, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "output_2" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -333,7 +594,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4,