diff --git a/lab-python-list-comprehension.ipynb b/lab-python-list-comprehension.ipynb index 6bef7fd..7774cfb 100644 --- a/lab-python-list-comprehension.ipynb +++ b/lab-python-list-comprehension.ipynb @@ -74,9 +74,32 @@ "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 = [[18, 20], [45, 2], [61, 12], [37, 6], [21, 21], [78, 9]]\n", + "\n", + "# list comprehensive\n", + "output = [\"Senior\" if age >= 55 and handicap > 7 else \"Open\" for age, handicap in input]\n", + "\n", + "# long way\n", + "result = []\n", + "for memeber in input:\n", + " age = memeber[0]\n", + " handicap = memeber[1]\n", + "\n", + " if age >= 50 and handicap > 7:\n", + " result.append(\"Senior\")\n", + " else:\n", + " result.append(\"Open\")\n", + "print(result)" ] }, { @@ -98,9 +121,19 @@ "execution_count": null, "id": "53061dce-7aa3-4476-8b56-71413c5e135c", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "78\n" + ] + } + ], "source": [ - "# your code goes here" + "n = 20\n", + "result = sum([i for i in range(n) if i % 3 == 0 or i % 5 == 0]) if n > 0 else 0\n", + "print(result)" ] }, { @@ -127,12 +160,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 23, "id": "a8cb1579-7065-4fc0-bd53-f91c2ad1dad9", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2, 3, 4]\n" + ] + } + ], "source": [ - "# your code goes here" + "n = 1234\n", + "\n", + "result = [int(digit) for digit in str(n)]\n", + "print(result)" ] }, { @@ -158,9 +202,21 @@ "execution_count": null, "id": "86ebe759-76d8-4012-8590-c48a473a6099", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[-1, 2, 3, -4, 5, -6]\n" + ] + } + ], "source": [ - "# your code goes here" + "nums = [1, -2, -3, 4, -5, 6]\n", + "result = [n * -1 for n in nums]\n", + "print(result)\n", + "# or\n", + "output = [-x for x in nums]" ] }, { @@ -196,9 +252,18 @@ "execution_count": null, "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" + "square_dic = {value: value**2 for value in range(1, 16)}\n", + "print(square_dic)" ] }, { @@ -223,9 +288,20 @@ "execution_count": null, "id": "a1d55cea-96c3-4853-8220-17c0904a8816", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[(3, 8), (3, 9), (3, 10), (4, 8), (4, 9), (4, 10), (5, 8), (5, 9), (5, 10)]\n" + ] + } + ], "source": [ - "# your code goes here" + "list1 = [3, 4, 5]\n", + "list2 = [8, 9, 10]\n", + "output = [(a, b) for a in list1 for b in list2]\n", + "print(output)" ] }, { @@ -249,9 +325,19 @@ "execution_count": null, "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" + "dic = {\"a\": [1, 2], \"b\": [3, 4]}\n", + "output = [(a, c) for a, b in dic.items() for c in b]\n", + "print(output)" ] }, { @@ -284,9 +370,20 @@ "execution_count": null, "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" + "input1 = [([1, 2], [3, 4]), ([5, 6], [7, 8])]\n", + "\n", + "output = [(c, e) for list1, list2 in input1 for c in list1 for e in list2]\n", + "print(output)" ] }, { @@ -311,15 +408,25 @@ "execution_count": null, "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" + "exer = [\"ab\", \"cd\"]\n", + "output = {(a[0], b[1]) for a in exer for b in exer}\n", + "print(output)" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -333,7 +440,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,