From d85f1b1a1f1c1458e67b42bc1718f513fe66e6f8 Mon Sep 17 00:00:00 2001 From: Diwakar Gupta <39624018+Diwakar-Gupta@users.noreply.github.com> Date: Fri, 22 Apr 2022 00:08:30 +0530 Subject: [PATCH] till list, tuple --- .../Getting_Started_April_Batch.ipynb | 1083 ++++++++++++++++- 1 file changed, 1048 insertions(+), 35 deletions(-) diff --git a/22-04-16-Getting_Started/Getting_Started_April_Batch.ipynb b/22-04-16-Getting_Started/Getting_Started_April_Batch.ipynb index 042f393..384e25b 100644 --- a/22-04-16-Getting_Started/Getting_Started_April_Batch.ipynb +++ b/22-04-16-Getting_Started/Getting_Started_April_Batch.ipynb @@ -5,9 +5,17 @@ "colab": { "name": "Getting Started April Batch.ipynb", "provenance": [], - "collapsed_sections": [], + "collapsed_sections": [ + "Go2q6eR4VjfR", + "r9sbVbBVclUY", + "c69tqOa4fUCI", + "_dgts8Q3fv1H", + "C3g9MkMB3uFD", + "JKP7sAhDBWPU", + "VbcYYK0iAr07" + ], "toc_visible": true, - "authorship_tag": "ABX9TyP+HBsvL331xvObEChknq6t", + "authorship_tag": "ABX9TyPip4HTAGhmm0TVRLw97UNm", "include_colab_link": true }, "kernelspec": { @@ -32,6 +40,8 @@ { "cell_type": "markdown", "source": [ + "Codes -> https://github.com/Diwakar-Gupta/Data-Science-APRIL\n", + "\n", "Notes -> https://1drv.ms/o/s!AmSu_rSXl5m3i0Pm8zsnFmpuqowO" ], "metadata": { @@ -41,21 +51,20 @@ { "cell_type": "markdown", "source": [ + "# Contents\n", + "\n", "Getting Started with python\n", "\n", "\n", "\n", "* IO\n", - " * Data Type\n", "* Conditions\n", - " * if else\n", - " * if elif else\n", "* Loop\n", "* Functions\n", "* String\n", "* List\n", - "* Set\n", "* Tuple\n", + "* Set\n", "* Map\n", "* Class Object\n", "\n" @@ -85,7 +94,7 @@ "id": "dVq6qoZsIK90", "outputId": "98d0095e-a16d-4590-c9ec-63d6f93fa600" }, - "execution_count": 1, + "execution_count": null, "outputs": [ { "output_type": "stream", @@ -180,7 +189,7 @@ "id": "SNUIzHuIRaT8", "outputId": "83d468e8-a882-49e8-d177-44df52c6fb66" }, - "execution_count": 2, + "execution_count": null, "outputs": [ { "output_type": "stream", @@ -212,7 +221,7 @@ "id": "YSug64VMS2Zy", "outputId": "54097d6e-a5e7-4619-eb8e-77ca3dce2f8c" }, - "execution_count": 5, + "execution_count": null, "outputs": [ { "output_type": "stream", @@ -346,7 +355,7 @@ "id": "HKrfgVl8cmjt", "outputId": "6d90194e-3d2f-434f-cd1b-af8301feca2f" }, - "execution_count": 8, + "execution_count": null, "outputs": [ { "output_type": "stream", @@ -467,7 +476,9 @@ "Create Ways\n", "* range(3) [0, 3)\n", "* range(2, 7) [2, 7)\n", - "* range(2, 7, 3) [2, 7) +3 2,5" + "* range(2, 7, 3) [2, 7) +3 2,5\n", + "* continue\n", + "* break\n" ], "metadata": { "id": "tsMSg7Ruj1SH" @@ -486,7 +497,7 @@ "id": "GuN4BbrPlNGl", "outputId": "423d043a-3efc-48a9-bbf8-ab5be66e29a7" }, - "execution_count": 85, + "execution_count": null, "outputs": [ { "output_type": "stream", @@ -515,7 +526,7 @@ "id": "t8HrWXVnm73a", "outputId": "e9c490e2-77a6-4302-e4f1-b8f5d0239cd3" }, - "execution_count": 26, + "execution_count": null, "outputs": [ { "output_type": "stream", @@ -542,7 +553,7 @@ "id": "4F_YlfnCoReH", "outputId": "2f96cbf4-9822-4590-e165-cf5ad4b0feda" }, - "execution_count": 27, + "execution_count": null, "outputs": [ { "output_type": "stream", @@ -570,7 +581,7 @@ }, "outputId": "5c341771-735e-485c-9356-87d9406b3cc9" }, - "execution_count": 13, + "execution_count": null, "outputs": [ { "output_type": "stream", @@ -610,7 +621,7 @@ "id": "Qp192BxIpTIH", "outputId": "d6321710-9d69-4731-cfaf-545470378fd6" }, - "execution_count": 82, + "execution_count": null, "outputs": [ { "output_type": "stream", @@ -637,7 +648,7 @@ "id": "gtG2KON5rhvc", "outputId": "2cbd6dfd-92c3-4be9-c0ed-988a6dd28087" }, - "execution_count": 35, + "execution_count": null, "outputs": [ { "output_type": "stream", @@ -701,7 +712,7 @@ "id": "oXMr-AAbr1YP", "outputId": "85196147-1cad-4cf8-b55b-a300e8a437c6" }, - "execution_count": 40, + "execution_count": null, "outputs": [ { "output_type": "stream", @@ -737,7 +748,7 @@ "id": "ACGgbt3rviGb", "outputId": "93ba06a1-1f68-4acb-cc43-230bcbeef6e1" }, - "execution_count": 43, + "execution_count": null, "outputs": [ { "output_type": "stream", @@ -763,7 +774,7 @@ "id": "FE9BjeIRyjtS", "outputId": "a573f4d6-f2df-4363-b2e6-8ac412f81e95" }, - "execution_count": 44, + "execution_count": null, "outputs": [ { "output_type": "stream", @@ -804,7 +815,7 @@ "id": "qAoSUH4Dzvqy", "outputId": "11e4b912-88a1-461a-ffd5-542e748698fb" }, - "execution_count": 46, + "execution_count": null, "outputs": [ { "output_type": "stream", @@ -845,7 +856,7 @@ "id": "Hx44Wh2s3vuj", "outputId": "fb774da3-c366-42bc-ca1c-ef8179133775" }, - "execution_count": 55, + "execution_count": null, "outputs": [ { "output_type": "stream", @@ -878,7 +889,7 @@ "id": "G7ZilHk737d8", "outputId": "0f2bc56b-29ee-46fe-82cc-fef24c698bc7" }, - "execution_count": 80, + "execution_count": null, "outputs": [ { "output_type": "stream", @@ -906,7 +917,7 @@ "id": "sjm9gz8a4sxC", "outputId": "e985cca5-e6c2-4540-df79-89f4ab777386" }, - "execution_count": 53, + "execution_count": null, "outputs": [ { "output_type": "execute_result", @@ -933,7 +944,8 @@ "* Escape Character\n", " * `\\' \\\" \\t \\\\`\n", "* strip\n", - "\n" + "* `'Hii {}'.format(variable)`\n", + "* `f'Hii {variable}'`\n" ], "metadata": { "id": "EDefB-bv9Gl6" @@ -954,7 +966,7 @@ "id": "_Pqn5sgB467e", "outputId": "087e18ad-6eb7-465b-a7b7-8d694fa0bbb8" }, - "execution_count": 91, + "execution_count": null, "outputs": [ { "output_type": "stream", @@ -980,7 +992,7 @@ "id": "1R_aQmz--Zks", "outputId": "97c76902-4870-4d0d-a162-d1aefbdc4354" }, - "execution_count": 106, + "execution_count": null, "outputs": [ { "output_type": "stream", @@ -1006,7 +1018,7 @@ "id": "vp_5oFvt-2Ie", "outputId": "6b648174-0011-4496-9855-4e42391706d1" }, - "execution_count": 107, + "execution_count": null, "outputs": [ { "output_type": "stream", @@ -1033,7 +1045,7 @@ "id": "t02VJSAi_shI", "outputId": "d954bf11-203c-488b-a1c7-14f7c0b62b2a" }, - "execution_count": 111, + "execution_count": null, "outputs": [ { "output_type": "execute_result", @@ -1063,7 +1075,7 @@ "id": "0TNNFHtsBnuB", "outputId": "09c0defd-5fc0-45bf-f5d0-bf050d7cddab" }, - "execution_count": 117, + "execution_count": null, "outputs": [ { "output_type": "stream", @@ -1100,7 +1112,7 @@ "id": "KNVvTyOFAPC4", "outputId": "46707a08-a55a-4a05-b73a-0c5374389ca1" }, - "execution_count": 128, + "execution_count": null, "outputs": [ { "output_type": "stream", @@ -1122,13 +1134,13 @@ " print(c)" ], "metadata": { + "id": "MgeLEkAcDYWU", + "outputId": "82219d81-3bc4-447f-bb70-5623ca2ca367", "colab": { "base_uri": "https://localhost:8080/" - }, - "id": "MgeLEkAcDYWU", - "outputId": "82219d81-3bc4-447f-bb70-5623ca2ca367" + } }, - "execution_count": 129, + "execution_count": null, "outputs": [ { "output_type": "stream", @@ -1141,6 +1153,1007 @@ ] } ] + }, + { + "cell_type": "markdown", + "source": [ + "# List\n", + "\n", + "* append\n", + "* extend : `[1,2,3] + [4,5,6] = [1,2,3,4,5,6]`\n", + "* Indexing\n", + "* Slicing\n", + "* `[1, *[2, 3], 4, 5]`\n", + "* `'Harsh' in class_list`\n", + "* `class_list.index('mantraksha')`" + ], + "metadata": { + "id": "5EkxS5903MQc" + } + }, + { + "cell_type": "markdown", + "source": [ + "\"Ayaan\",\n", + "\"chaitanya\",\n", + "\"Dhairya\",\n", + "\"Divyansh\",\n", + "\"Shraiya\",\n", + "\"Nidhi\",\n", + "\"gokul\",\n", + "\"Harsh\",\n", + "\"Harshit\",\n", + "\"Himanshu\",\n", + "\"Nidhi\",\n", + "\"Nusrat\",\n", + "\"prince\",\n", + "\"Radhika\",\n", + "\"Vedant\",\n", + "\"Harshit\",\n", + "\"Divyansh\",\n", + "\"mantraksha\"," + ], + "metadata": { + "id": "b0XTMhW1wMCH" + } + }, + { + "cell_type": "code", + "source": [ + "l = ['Ayaan', '''chaitanya''', \"Dhairya\", 123, True, False, 45.908]\n", + "\n", + "for name in l:\n", + " # print(name + ' Good Evening') # Gives Error\n", + " print('Hii', name , 'Good Evening') \n", + " # print(f'Hii {name} Good Evening')" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "qvoQs5i5wtzK", + "outputId": "539da530-22e5-41c5-b711-6740ff675c77" + }, + "execution_count": 16, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Hii Ayaan Good Evening\n", + "Hii chaitanya Good Evening\n", + "Hii Dhairya Good Evening\n", + "Hii 123 Good Evening\n", + "Hii True Good Evening\n", + "Hii False Good Evening\n", + "Hii 45.908 Good Evening\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "name1 = 'Ayaan'\n", + "name2 = 'Gokul'\n", + "# s = 'Good Evening '+name1+' and '+name2\n", + "# s = 'Good Evening {}, {}'.format(name1, name2)\n", + "s = f'Good Evening {name1} and {name2}'\n", + "print(s)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "N-uugWJF14ET", + "outputId": "963b866b-16aa-4dfa-aa71-893e1401a740" + }, + "execution_count": 20, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Good Evening Ayaan and Gokul\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "class_students = [\"Ayaan\",\n", + "\"chaitanya\",\n", + "\"Dhairya\",\n", + "\"Divyansh\",\n", + "\"Shraiya\",\n", + "\"Nidhi\",\n", + "\"gokul\",\n", + "\"Harsh\",\n", + "\"Harshit\",\n", + "\"Himanshu\",\n", + "\"Nidhi\",\n", + "\"Nusrat\",\n", + "\"prince\",\n", + "\"Radhika\",\n", + "\"Vedant\",\n", + "\"Harshit\",\n", + "\"Divyansh\",\n", + "\"mantraksha\",]" + ], + "metadata": { + "id": "4lhQpsVl2XsP" + }, + "execution_count": 28, + "outputs": [] + }, + { + "cell_type": "markdown", + "source": [ + "Append late joiners to class list" + ], + "metadata": { + "id": "lL9TRC3e6BWv" + } + }, + { + "cell_type": "code", + "source": [ + "class_students.append('Vaibhav')\n", + "class_students.append('Vidushi')\n", + "class_students.append('jonathan')\n", + "class_students" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "7kzPal2N5mlh", + "outputId": "b8cd7a53-0b7b-4f62-b0a3-b89d036e275c" + }, + "execution_count": 29, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "['Ayaan',\n", + " 'chaitanya',\n", + " 'Dhairya',\n", + " 'Divyansh',\n", + " 'Shraiya',\n", + " 'Nidhi',\n", + " 'gokul',\n", + " 'Harsh',\n", + " 'Harshit',\n", + " 'Himanshu',\n", + " 'Nidhi',\n", + " 'Nusrat',\n", + " 'prince',\n", + " 'Radhika',\n", + " 'Vedant',\n", + " 'Harshit',\n", + " 'Divyansh',\n", + " 'mantraksha',\n", + " 'Vaibhav',\n", + " 'Vidushi',\n", + " 'jonathan']" + ] + }, + "metadata": {}, + "execution_count": 29 + } + ] + }, + { + "cell_type": "code", + "source": [ + "l1 = [1,2,3, 'hii', 'hello']\n", + "l2 = [4, 5, 6]\n", + "l3 = l1+l2\n", + "print(l3)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "C7T99Qx_6r76", + "outputId": "ccc9da21-8103-4229-b600-543427c91ac9" + }, + "execution_count": 32, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "[1, 2, 3, 'hii', 'hello', 4, 5, 6]\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "for name in class_students:\n", + " print(f\"Hii {name}\")" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "sXPKhZtB5Jju", + "outputId": "a52663ef-ba23-4f17-f594-d74a1bbbb944" + }, + "execution_count": 30, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Hii Ayaan\n", + "Hii chaitanya\n", + "Hii Dhairya\n", + "Hii Divyansh\n", + "Hii Shraiya\n", + "Hii Nidhi\n", + "Hii gokul\n", + "Hii Harsh\n", + "Hii Harshit\n", + "Hii Himanshu\n", + "Hii Nidhi\n", + "Hii Nusrat\n", + "Hii prince\n", + "Hii Radhika\n", + "Hii Vedant\n", + "Hii Harshit\n", + "Hii Divyansh\n", + "Hii mantraksha\n", + "Hii Vaibhav\n", + "Hii Vidushi\n", + "Hii jonathan\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "import time\n", + "\n", + "income_tax_officer = 'Ajay'\n", + "\n", + "for name in class_students:\n", + " print(f'{income_tax_officer} is preparing to raid {name} house')\n", + " time.sleep(2)\n", + "\n", + " if name == 'Dhairya':\n", + " # skip remaining part of loop and proceed to next iteration\n", + " continue # ghus dena to Ajay\n", + "\n", + " print(f'{income_tax_officer} found black money in {name} house')\n", + " time.sleep(2)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "LPQHnSlY6nAJ", + "outputId": "db0cfcec-1cfc-4e0d-d152-d29f4144099f" + }, + "execution_count": 41, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Ajay is preparing to raid Ayaan house\n", + "Ajay found black money in Ayaan house\n", + "Ajay is preparing to raid chaitanya house\n", + "Ajay found black money in chaitanya house\n", + "Ajay is preparing to raid Dhairya house\n", + "Ajay is preparing to raid Divyansh house\n", + "Ajay found black money in Divyansh house\n", + "Ajay is preparing to raid Shraiya house\n", + "Ajay found black money in Shraiya house\n", + "Ajay is preparing to raid Nidhi house\n", + "Ajay found black money in Nidhi house\n", + "Ajay is preparing to raid gokul house\n", + "Ajay found black money in gokul house\n", + "Ajay is preparing to raid Harsh house\n", + "Ajay found black money in Harsh house\n", + "Ajay is preparing to raid Harshit house\n", + "Ajay found black money in Harshit house\n", + "Ajay is preparing to raid Himanshu house\n", + "Ajay found black money in Himanshu house\n", + "Ajay is preparing to raid Nidhi house\n", + "Ajay found black money in Nidhi house\n", + "Ajay is preparing to raid Nusrat house\n", + "Ajay found black money in Nusrat house\n", + "Ajay is preparing to raid prince house\n", + "Ajay found black money in prince house\n", + "Ajay is preparing to raid Radhika house\n", + "Ajay found black money in Radhika house\n", + "Ajay is preparing to raid Vedant house\n", + "Ajay found black money in Vedant house\n", + "Ajay is preparing to raid Harshit house\n", + "Ajay found black money in Harshit house\n", + "Ajay is preparing to raid Divyansh house\n", + "Ajay found black money in Divyansh house\n", + "Ajay is preparing to raid mantraksha house\n", + "Ajay found black money in mantraksha house\n", + "Ajay is preparing to raid Vaibhav house\n", + "Ajay found black money in Vaibhav house\n", + "Ajay is preparing to raid Vidushi house\n", + "Ajay found black money in Vidushi house\n", + "Ajay is preparing to raid jonathan house\n", + "Ajay found black money in jonathan house\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "import time\n", + "\n", + "income_tax_officer = 'Ajay'\n", + "\n", + "for name in class_students:\n", + " print(f'{income_tax_officer} is preparing to raid {name} house')\n", + " time.sleep(2)\n", + "\n", + " if name == 'Dhairya':\n", + " # terminates loop\n", + " break # ghus dena to Ajay\n", + "\n", + " print(f'{income_tax_officer} found black money in {name} house')\n", + " time.sleep(2)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "EmkLUtZH_eLO", + "outputId": "ca5d5816-75c8-48d1-c80c-e889d17bed83" + }, + "execution_count": 42, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Ajay is preparing to raid Ayaan house\n", + "Ajay found black money in Ayaan house\n", + "Ajay is preparing to raid chaitanya house\n", + "Ajay found black money in chaitanya house\n", + "Ajay is preparing to raid Dhairya house\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "## Question 1\n", + "\n", + "print all even squares from 0 to n-1\n", + "\n", + "Hint: %, modify range" + ], + "metadata": { + "id": "VbcYYK0iAr07" + } + }, + { + "cell_type": "code", + "source": [ + "n = 10\n", + "for i in range(n):\n", + " if i % 2 == 1:\n", + " # odd\n", + " continue\n", + " print(i*i)\n", + "\n", + "\n", + "for i in range(0, n, 2):\n", + " print(i*i)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "kFmE80pqAeqb", + "outputId": "a7770321-4e68-4272-daf0-656f7f52a6a6" + }, + "execution_count": 52, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "1\n", + "9\n", + "25\n", + "49\n", + "81\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "## Indexing" + ], + "metadata": { + "id": "zCRdhvEBCzR8" + } + }, + { + "cell_type": "code", + "source": [ + "# -6 -5 -4 -3 -2 -1 \n", + "# 0 1 2 3 4 5\n", + "l = ['a', 123, 'c', 'd', 44, 'f']\n", + "\n", + "print(f'{l[1]} + {l[-2]} = {l[1]+l[4]}')\n", + "print(l[0], l[-6])\n", + "print(f'Length of list = {len(l)}')" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "vBKGZ4MJCtz3", + "outputId": "4984e6af-a4af-4251-a2f1-3a0e78e30cab" + }, + "execution_count": 62, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "123 + 44 = 167\n", + "a a\n", + "Length of list = 6\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "l = list(range(20, 30))\n", + "\n", + "print(l)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "9YHM-WUJETNc", + "outputId": "8a1fb0a7-ab7a-4b07-8bfa-a1da1e91adc4" + }, + "execution_count": 74, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "[20, 21, 22, 23, 24, 25, 26, 27, 28, 29]\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "## Slicing" + ], + "metadata": { + "id": "bM8V4K9JGT7S" + } + }, + { + "cell_type": "code", + "source": [ + "l = list(range(3, 9))\n", + "print('l=', l)\n", + "l2 = l[1:4]\n", + "print(f'l[1:4] = {l2}')\n", + "\n", + "print(f'l[1:-2] = {l[1:-2]}')\n", + "\n", + "print(f'l[1:-1:2] = {l[1:-1:2]}')" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "DqMZiKmuGUvJ", + "outputId": "ebe56be0-e9d5-49c7-bbc7-e3197a5f96d6" + }, + "execution_count": 80, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "l= [3, 4, 5, 6, 7, 8]\n", + "l[1:4] = [4, 5, 6]\n", + "l[1:-2] = [4, 5, 6]\n", + "l[1:-1:2] = [4, 6]\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "l[-1:1:-1]" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "YY5dcFX8Hnak", + "outputId": "2a537570-3b37-4c14-903c-5a84e8c4ad0c" + }, + "execution_count": 82, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "[8, 7, 6, 5]" + ] + }, + "metadata": {}, + "execution_count": 82 + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "**Question reverse list using slicing**" + ], + "metadata": { + "id": "xkcf-It3Ihge" + } + }, + { + "cell_type": "code", + "source": [ + "print(l)\n", + "print(l[1:]) # no stoping point, no ending\n", + "print(l[:4])\n", + "print(l[:])\n", + "print(l[::2]) # start end\n", + "print(l[::-1]) # start and end are toggled" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "c9ef3cqlISix", + "outputId": "d28aaddf-f710-4695-b28a-1e0abb7424c3" + }, + "execution_count": 91, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "[3, 4, 5, 6, 7, 8]\n", + "[4, 5, 6, 7, 8]\n", + "[3, 4, 5, 6]\n", + "[3, 4, 5, 6, 7, 8]\n", + "[3, 5, 7]\n", + "[8, 7, 6, 5, 4, 3]\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "Questions\n", + "\n", + "\n", + "\n", + "1. https://nados.io/question/reverse-an-array?zen=true\n", + "2. https://nados.io/question/subarray-problem?zen=true\n", + "\n" + ], + "metadata": { + "id": "FYoCKra2ORuN" + } + }, + { + "cell_type": "code", + "source": [ + "l = [1,2,4]\n", + "\n", + "print(l)\n", + "print(1, 2, 3)\n", + "print(*l)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "Ob5ERylsOhna", + "outputId": "f9796da8-bfe9-46c6-fc33-ed672970e685" + }, + "execution_count": 97, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "[1, 2, 4]\n", + "1 2 3\n", + "1 2 4\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "l = [1,2,3,4, *[5,6,7]]\n", + "\n", + "for i in range(len(l)):\n", + " print(l[i])" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "whRYtB0VPrej", + "outputId": "8af06f50-782b-4ce3-b692-8463d0d100ec" + }, + "execution_count": 101, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "1\n", + "2\n", + "3\n", + "4\n", + "5\n", + "6\n", + "7\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "l1 = [2,3,4]\n", + "l2 = [1, *l1, 5,6,7]\n", + "print(l2)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "0vL-ux6fQUHi", + "outputId": "139942a4-ada5-4732-8fe8-acf1e6f479b2" + }, + "execution_count": 105, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "[1, 2, 3, 4, 5, 6, 7]\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "class_students = [\"Ayaan\",\n", + "\"chaitanya\",\n", + "\"Dhairya\",\n", + "\"Divyansh\",\n", + "\"Shraiya\",\n", + "\"Nidhi\",\n", + "\"gokul\",\n", + "\"Harsh\",\n", + "\"Harshit\",\n", + "\"Himanshu\",\n", + "\"Nidhi\",\n", + "\"Nusrat\",\n", + "\"prince\",\n", + "\"Radhika\",\n", + "\"Vedant\",\n", + "\"Harshit\",\n", + "\"Divyansh\",\n", + "\"mantraksha\",]\n", + "\n", + "res = \"Harshit\" in class_students\n", + "print(res)\n", + "\n", + "ind = class_students.index(\"mantraksha\")\n", + "print(ind, class_students[17])" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "IQ6BvQolQt6g", + "outputId": "c8881175-0eb7-4607-887a-b515a8eb4e01" + }, + "execution_count": 115, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "True\n", + "17 mantraksha\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "# Tuple\n", + "\n", + "* Immutable list\n", + "* `t = (1,2,3)`\n", + "* `t = (1,)` comma is necessary\n" + ], + "metadata": { + "id": "QhYk6yW_SjQ2" + } + }, + { + "cell_type": "code", + "source": [ + "l = ['a', 'b', 'd', 'd']\n", + "print(l)\n", + "l[2] = 'c'\n", + "print(l)\n", + "\n", + "l.append('e')\n", + "print(l)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "J1F3mvBcSmAp", + "outputId": "7a5f7df1-fc5a-4a6f-f7b2-e04b2a68dad3" + }, + "execution_count": 118, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "['a', 'b', 'd', 'd']\n", + "['a', 'b', 'c', 'd']\n", + "['a', 'b', 'c', 'd', 'e']\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "t = (1,2,3)\n", + "print(type(t), t)\n", + "\n", + "print(t[0], t[1], t[2])\n", + "# t[0] = 10 # error\n", + "# t.append(10) # error\n", + "print(t)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "xQ6-gDikSheC", + "outputId": "bef9fdad-9852-4169-88bb-d54805361931" + }, + "execution_count": 129, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + " (1, 2, 3)\n", + "1 2 3\n", + "(1, 2, 3)\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "t = ('abcd',) # if single element then , is necessary\n", + "t = 8\n", + "\n", + "print(type(t), t)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "0t9xyfLJTnly", + "outputId": "b5179f8c-2221-407f-8c03-924fadbffcb2" + }, + "execution_count": 144, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + " ('abcd',)\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "age, first_name, last_name = (22, 'Diwakar', 'Gupta')\n", + "print(age)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "tGmYMQ2RT5KL", + "outputId": "747ac0a9-933b-4c72-e933-5840dde28410" + }, + "execution_count": 139, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "22\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "t = (1, 2, [3, 'a', 'b'], 4 ,5)\n", + "print(t)\n", + "\n", + "t[2].append('c')\n", + "\n", + "print(t)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "gKWvyvchUYMG", + "outputId": "e3473e0c-f7dd-4232-f1c3-8077fbca2d16" + }, + "execution_count": 151, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "(1, 2, [3, 'a', 'b'], 4, 5)\n", + "(1, 2, [3, 'a', 'b', 'c'], 4, 5)\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "l = [1, *[2,3], 4] # = [1, 2,3, 4]\n", + "\n", + "for i in range(len(l)):\n", + " print(i, l[i])" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "oI4wjm48UiaU", + "outputId": "74f671aa-309d-4cfb-bb3e-e18efc85fe54" + }, + "execution_count": 155, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "0 1\n", + "1 2\n", + "2 3\n", + "3 4\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "def add(a, b, c):\n", + " return a+b+c\n", + "\n", + "print(add(2, 3, 4))\n", + "\n", + "l = [1,2,3]\n", + "print(add(*l)) # = add(l[0], l[1], l[2])" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "BLsYbbT5W8k5", + "outputId": "c5990e28-064f-44bc-8584-fdc695fa3afd" + }, + "execution_count": 159, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "9\n", + "6\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "# immutable\n", + "\n", + "days = ['sun', 'mon', 'tue', 'wed', 'thur', 'fri', 'sat']\n", + "days = tuple(days)\n", + "\n", + "print(type(days), days)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "8mMOltGtZ5Hk", + "outputId": "35f87959-cd30-44b7-a190-3640f865f9df" + }, + "execution_count": 163, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + " ('sun', 'mon', 'tue', 'wed', 'thur', 'fri', 'sat')\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "" + ], + "metadata": { + "id": "CkxRNZ4CaXuz" + }, + "execution_count": null, + "outputs": [] } ] } \ No newline at end of file