From 47404a89dc9d8b16c8a2ce07de164b53a8694f5e Mon Sep 17 00:00:00 2001 From: Diwakar Gupta <39624018+Diwakar-Gupta@users.noreply.github.com> Date: Sat, 23 Apr 2022 23:44:33 +0530 Subject: [PATCH] python complete --- .../Getting_Started_April_Batch.ipynb | 631 ++++++++++++++++-- 1 file changed, 588 insertions(+), 43 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 384e25b..9ee7b04 100644 --- a/22-04-16-Getting_Started/Getting_Started_April_Batch.ipynb +++ b/22-04-16-Getting_Started/Getting_Started_April_Batch.ipynb @@ -15,7 +15,7 @@ "VbcYYK0iAr07" ], "toc_visible": true, - "authorship_tag": "ABX9TyPip4HTAGhmm0TVRLw97UNm", + "authorship_tag": "ABX9TyMEpp/d1kmG4f2xaojBpQS6", "include_colab_link": true }, "kernelspec": { @@ -76,7 +76,11 @@ { "cell_type": "markdown", "source": [ - "# IO" + "# IO\n", + "\n", + "* Variables\n", + "* Data Types\n", + "* input from keyboard" ], "metadata": { "id": "HaX_RerVMbaK" @@ -92,9 +96,9 @@ "base_uri": "https://localhost:8080/" }, "id": "dVq6qoZsIK90", - "outputId": "98d0095e-a16d-4590-c9ec-63d6f93fa600" + "outputId": "42a2f0ea-2a98-40d6-9148-870dca16a92e" }, - "execution_count": null, + "execution_count": 36, "outputs": [ { "output_type": "stream", @@ -116,13 +120,13 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 37, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "dFFOIg5DH_dt", - "outputId": "f79b7667-3c94-45e0-c3a5-1870c7f1a90a" + "outputId": "9bf228a8-9208-48cd-e84d-b4aeec2c5376" }, "outputs": [ { @@ -162,9 +166,9 @@ "base_uri": "https://localhost:8080/" }, "id": "3j2tT9oxN7j0", - "outputId": "ee8049c9-0890-4359-b5db-b04048b9993d" + "outputId": "1913f1f8-1d62-4acd-91c3-66b0613dff42" }, - "execution_count": null, + "execution_count": 38, "outputs": [ { "output_type": "stream", @@ -187,16 +191,16 @@ "base_uri": "https://localhost:8080/" }, "id": "SNUIzHuIRaT8", - "outputId": "83d468e8-a882-49e8-d177-44df52c6fb66" + "outputId": "b852bff2-b8a2-4882-a056-2545b376cb12" }, - "execution_count": null, + "execution_count": 39, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ - "DIwakar\n", - "Hii DIwakar\n" + "w\n", + "Hii w\n" ] } ] @@ -219,9 +223,9 @@ "base_uri": "https://localhost:8080/" }, "id": "YSug64VMS2Zy", - "outputId": "54097d6e-a5e7-4619-eb8e-77ca3dce2f8c" + "outputId": "8305db06-4399-493a-87cf-18704cbfed64" }, - "execution_count": null, + "execution_count": 40, "outputs": [ { "output_type": "stream", @@ -327,7 +331,10 @@ { "cell_type": "markdown", "source": [ - "# Conditions" + "# Conditions\n", + "\n", + "* if else\n", + "* if elif else" ], "metadata": { "id": "r9sbVbBVclUY" @@ -1214,7 +1221,7 @@ "id": "qvoQs5i5wtzK", "outputId": "539da530-22e5-41c5-b711-6740ff675c77" }, - "execution_count": 16, + "execution_count": null, "outputs": [ { "output_type": "stream", @@ -1248,7 +1255,7 @@ "id": "N-uugWJF14ET", "outputId": "963b866b-16aa-4dfa-aa71-893e1401a740" }, - "execution_count": 20, + "execution_count": null, "outputs": [ { "output_type": "stream", @@ -1269,10 +1276,11 @@ "\"Shraiya\",\n", "\"Nidhi\",\n", "\"gokul\",\n", + "\"Mantraksha\"\n", "\"Harsh\",\n", "\"Harshit\",\n", "\"Himanshu\",\n", - "\"Nidhi\",\n", + "\"nidhi\",\n", "\"Nusrat\",\n", "\"prince\",\n", "\"Radhika\",\n", @@ -1284,7 +1292,7 @@ "metadata": { "id": "4lhQpsVl2XsP" }, - "execution_count": 28, + "execution_count": null, "outputs": [] }, { @@ -1311,7 +1319,7 @@ "id": "7kzPal2N5mlh", "outputId": "b8cd7a53-0b7b-4f62-b0a3-b89d036e275c" }, - "execution_count": 29, + "execution_count": null, "outputs": [ { "output_type": "execute_result", @@ -1360,7 +1368,7 @@ "id": "C7T99Qx_6r76", "outputId": "ccc9da21-8103-4229-b600-543427c91ac9" }, - "execution_count": 32, + "execution_count": null, "outputs": [ { "output_type": "stream", @@ -1384,7 +1392,7 @@ "id": "sXPKhZtB5Jju", "outputId": "a52663ef-ba23-4f17-f594-d74a1bbbb944" }, - "execution_count": 30, + "execution_count": null, "outputs": [ { "output_type": "stream", @@ -1440,7 +1448,7 @@ "id": "LPQHnSlY6nAJ", "outputId": "db0cfcec-1cfc-4e0d-d152-d29f4144099f" }, - "execution_count": 41, + "execution_count": null, "outputs": [ { "output_type": "stream", @@ -1516,7 +1524,7 @@ "id": "EmkLUtZH_eLO", "outputId": "ca5d5816-75c8-48d1-c80c-e889d17bed83" }, - "execution_count": 42, + "execution_count": null, "outputs": [ { "output_type": "stream", @@ -1565,7 +1573,7 @@ "id": "kFmE80pqAeqb", "outputId": "a7770321-4e68-4272-daf0-656f7f52a6a6" }, - "execution_count": 52, + "execution_count": null, "outputs": [ { "output_type": "stream", @@ -1607,7 +1615,7 @@ "id": "vBKGZ4MJCtz3", "outputId": "4984e6af-a4af-4251-a2f1-3a0e78e30cab" }, - "execution_count": 62, + "execution_count": null, "outputs": [ { "output_type": "stream", @@ -1634,7 +1642,7 @@ "id": "9YHM-WUJETNc", "outputId": "8a1fb0a7-ab7a-4b07-8bfa-a1da1e91adc4" }, - "execution_count": 74, + "execution_count": null, "outputs": [ { "output_type": "stream", @@ -1673,7 +1681,7 @@ "id": "DqMZiKmuGUvJ", "outputId": "ebe56be0-e9d5-49c7-bbc7-e3197a5f96d6" }, - "execution_count": 80, + "execution_count": null, "outputs": [ { "output_type": "stream", @@ -1699,7 +1707,7 @@ "id": "YY5dcFX8Hnak", "outputId": "2a537570-3b37-4c14-903c-5a84e8c4ad0c" }, - "execution_count": 82, + "execution_count": null, "outputs": [ { "output_type": "execute_result", @@ -1739,7 +1747,7 @@ "id": "c9ef3cqlISix", "outputId": "d28aaddf-f710-4695-b28a-1e0abb7424c3" }, - "execution_count": 91, + "execution_count": null, "outputs": [ { "output_type": "stream", @@ -1786,7 +1794,7 @@ "id": "Ob5ERylsOhna", "outputId": "f9796da8-bfe9-46c6-fc33-ed672970e685" }, - "execution_count": 97, + "execution_count": null, "outputs": [ { "output_type": "stream", @@ -1814,7 +1822,7 @@ "id": "whRYtB0VPrej", "outputId": "8af06f50-782b-4ce3-b692-8463d0d100ec" }, - "execution_count": 101, + "execution_count": null, "outputs": [ { "output_type": "stream", @@ -1845,7 +1853,7 @@ "id": "0vL-ux6fQUHi", "outputId": "139942a4-ada5-4732-8fe8-acf1e6f479b2" }, - "execution_count": 105, + "execution_count": null, "outputs": [ { "output_type": "stream", @@ -1891,7 +1899,7 @@ "id": "IQ6BvQolQt6g", "outputId": "c8881175-0eb7-4607-887a-b515a8eb4e01" }, - "execution_count": 115, + "execution_count": null, "outputs": [ { "output_type": "stream", @@ -1934,7 +1942,7 @@ "id": "J1F3mvBcSmAp", "outputId": "7a5f7df1-fc5a-4a6f-f7b2-e04b2a68dad3" }, - "execution_count": 118, + "execution_count": null, "outputs": [ { "output_type": "stream", @@ -1947,6 +1955,32 @@ } ] }, + { + "cell_type": "code", + "source": [ + "l = [1,2,3]\n", + "t = tuple(l)\n", + "\n", + "print(t)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "t40TEMB5G1Hk", + "outputId": "8e7ba518-657d-454c-ba79-a5ff32eed9f1" + }, + "execution_count": 1, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "(1, 2, 3)\n" + ] + } + ] + }, { "cell_type": "code", "source": [ @@ -1965,7 +1999,7 @@ "id": "xQ6-gDikSheC", "outputId": "bef9fdad-9852-4169-88bb-d54805361931" }, - "execution_count": 129, + "execution_count": null, "outputs": [ { "output_type": "stream", @@ -1993,7 +2027,7 @@ "id": "0t9xyfLJTnly", "outputId": "b5179f8c-2221-407f-8c03-924fadbffcb2" }, - "execution_count": 144, + "execution_count": null, "outputs": [ { "output_type": "stream", @@ -2017,7 +2051,7 @@ "id": "tGmYMQ2RT5KL", "outputId": "747ac0a9-933b-4c72-e933-5840dde28410" }, - "execution_count": 139, + "execution_count": null, "outputs": [ { "output_type": "stream", @@ -2045,7 +2079,7 @@ "id": "gKWvyvchUYMG", "outputId": "e3473e0c-f7dd-4232-f1c3-8077fbca2d16" }, - "execution_count": 151, + "execution_count": null, "outputs": [ { "output_type": "stream", @@ -2072,7 +2106,7 @@ "id": "oI4wjm48UiaU", "outputId": "74f671aa-309d-4cfb-bb3e-e18efc85fe54" }, - "execution_count": 155, + "execution_count": null, "outputs": [ { "output_type": "stream", @@ -2104,7 +2138,7 @@ "id": "BLsYbbT5W8k5", "outputId": "c5990e28-064f-44bc-8584-fdc695fa3afd" }, - "execution_count": 159, + "execution_count": null, "outputs": [ { "output_type": "stream", @@ -2133,7 +2167,7 @@ "id": "8mMOltGtZ5Hk", "outputId": "35f87959-cd30-44b7-a190-3640f865f9df" }, - "execution_count": 163, + "execution_count": null, "outputs": [ { "output_type": "stream", @@ -2144,16 +2178,527 @@ } ] }, + { + "cell_type": "markdown", + "source": [ + "# Set\n", + "\n", + "list without duplicates\n", + "\n", + "\n", + "\n", + "1. Set does not preserve insertion order\n", + "2. List item\n", + "\n", + "* `s = set()` # create empty set\n", + "* add\n", + "* remove\n", + "* union\n", + "* intersection\n", + "* difference\n", + "* `'value' in set`" + ], + "metadata": { + "id": "pCHYIWvlHE5G" + } + }, { "cell_type": "code", "source": [ - "" + "class_students = [\n", + "\"anubhav\",\n", + "\"Ayaan\",\n", + "\"chaitanya\",\n", + "\"Dhairya\",\n", + "\"Divyansh\",\n", + "\"Shraiya\",\n", + "\"Nidhi\",\n", + "\"gokul\",\n", + "\"Mantraksha\",\n", + "\"Harsh\",\n", + "\"Harshit\",\n", + "\"Harshit\",\n", + "\"Himanshu\",\n", + "\"nidhi\",\n", + "\"Nusrat\",\n", + "\"prince\",\n", + "\"Radhika\",\n", + "\"Vedant\",\n", + "\"Harshit\",\n", + "\"Divyansh\",\n", + "\"mantraksha\",]" ], "metadata": { "id": "CkxRNZ4CaXuz" }, + "execution_count": 7, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "print(len(class_students))" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "doTJwoU2HnJP", + "outputId": "1771ffc4-e26b-49f1-9b21-d1344b32a702" + }, + "execution_count": 8, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "21\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "total_student_withoutcase = set(class_students)\n", + "print(f'Total number of unique strings {len(total_student_withoutcase)}')\n", + "print(s)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "GyBt2aRmIfTP", + "outputId": "ab505761-b774-47bd-bd91-2162ba8bcefc" + }, + "execution_count": 14, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Total number of unique strings 18\n", + "{'chaitanya', 'anubhav', 'Shraiya', 'Himanshu', 'Vedant', 'Mantraksha', 'Divyansh', 'Harshit', 'Nusrat', 'gokul', 'nidhi', 'prince', 'mantraksha', 'Nidhi', 'Harsh', 'Radhika', 'Dhairya', 'Ayaan'}\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "total_student_case = []\n", + "\n", + "for name in class_students:\n", + " total_student_case.append(name.lower())\n", + "\n", + "s = set(total_student_case)\n", + "print(len(s))" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "4gQBte0SI8aM", + "outputId": "1a6bc98a-a3f1-4495-e063-8fbdd5d4e1bb" + }, + "execution_count": 18, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "16\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "total_student_case = set()\n", + "\n", + "for name in class_students:\n", + " total_student_case.add(name.lower())\n", + "\n", + "print(len(total_student_case))\n", + "print(total_student_case)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "rQFJamlCKU-s", + "outputId": "a4e5e560-3377-4c35-f3b6-90a326ec496c" + }, + "execution_count": 21, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "16\n", + "{'chaitanya', 'anubhav', 'harshit', 'vedant', 'harsh', 'prince', 'nidhi', 'gokul', 'mantraksha', 'dhairya', 'shraiya', 'nusrat', 'divyansh', 'ayaan', 'himanshu', 'radhika'}\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "s = set()\n", + "s.add(2)\n", + "print(s)\n", + "s.add(1)\n", + "print(s)\n", + "s.add(2)\n", + "print(s)\n", + "s.add(3)\n", + "print(s)\n", + "s.add('hii')\n", + "print(s)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "JjwUnfBdKzJl", + "outputId": "9f2c31c3-063b-491c-b5d3-c7c3ddb49237" + }, + "execution_count": 44, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "{2}\n", + "{1, 2}\n", + "{1, 2}\n", + "{1, 2, 3}\n", + "{1, 2, 3, 'hii'}\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "s = {'Diwakar', 'diwakar'}\n", + "print(s)\n", + "s.remove('diwakar')\n", + "print(s)\n", + "\n", + "print('Diwakar' in s)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "FD9R6YZ2K4h-", + "outputId": "68ea5657-35bc-40f2-cc9a-e4d4ed356965" + }, + "execution_count": 55, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "{'diwakar', 'Diwakar'}\n", + "{'Diwakar'}\n", + "True\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "print(dir(s))" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "i74jxIT9OJzP", + "outputId": "c18832de-888d-4494-9419-0458e53453ca" + }, + "execution_count": 52, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "['__and__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__iand__', '__init__', '__init_subclass__', '__ior__', '__isub__', '__iter__', '__ixor__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__or__', '__rand__', '__reduce__', '__reduce_ex__', '__repr__', '__ror__', '__rsub__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__xor__', 'add', 'clear', 'copy', 'difference', 'difference_update', 'discard', 'intersection', 'intersection_update', 'isdisjoint', 'issubset', 'issuperset', 'pop', 'remove', 'symmetric_difference', 'symmetric_difference_update', 'union', 'update']\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "s1 = {1,2,3,4}\n", + "s2 = {2,4,5,6,7,8}\n", + "\n", + "print(s1.union(s2))\n", + "print(s1.intersection(s2))\n", + "print(s2 - s1)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "PEx2JPuaK7NK", + "outputId": "f85abb70-96c9-48ef-fa00-b84ccf160382" + }, + "execution_count": 67, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "{1, 2, 3, 4, 5, 6, 7, 8}\n", + "{2, 4}\n", + "{8, 5, 6, 7}\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "Question: https://www.hackerrank.com/challenges/py-set-add/problem?isFullScreen=true" + ], + "metadata": { + "id": "-lHbLlpSVGc5" + } + }, + { + "cell_type": "code", + "source": [ + "n = int(input())\n", + "\n", + "s = set()\n", + "for i in range(n):\n", + " name = input()\n", + " s.add(name)\n", + "\n", + "print(len(s))" + ], + "metadata": { + "id": "zwg77zGrbglt" + }, "execution_count": null, "outputs": [] + }, + { + "cell_type": "markdown", + "source": [ + "# Dictionary / Map\n", + "\n", + "in other languages this is called `hashmap`\n", + "\n", + "key, value pair" + ], + "metadata": { + "id": "JcBwPdC1QQmy" + } + }, + { + "cell_type": "code", + "source": [ + "# student -> phone number\n", + "\n", + "d = {\n", + " 'Gokul': 12345,\n", + " 'Dhairya': 3456,\n", + " 789: '890'\n", + "}\n", + "\n", + "print(d)\n", + "print(d['Gokul'])\n", + "print('Diwakar' in d)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "dxWxnMGwQbK3", + "outputId": "fdd069ff-b9f9-42ff-b17b-811d25e7d1ac" + }, + "execution_count": 88, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "{'Gokul': 12345, 'Dhairya': 3456, 789: '890'}\n", + "12345\n", + "False\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "capital = {\n", + " 'India': 'Delhi',\n", + " 'CG': 'RAIPUR'\n", + "}\n", + "print(capital)\n", + "populations = {\n", + " 'India': 110,\n", + " 'Bangla': 32,\n", + "}\n", + "print(populations)\n", + "print(populations['India'])\n", + "print(populations.get('India'))\n", + "populations['India'] = 100\n", + "print(populations['India'])" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "idjzpKisQbCE", + "outputId": "5db8a0cd-f6ae-4cac-d985-b95567857e1c" + }, + "execution_count": 83, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "{'India': 'Delhi', 'CG': 'RAIPUR'}\n", + "{'India': 110, 'Bangla': 32}\n", + "110\n", + "110\n", + "100\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "print('UK' in populations)\n", + "populations['UK'] = 55\n", + "print('UK' in populations)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "7De97JfRTMXm", + "outputId": "1713941e-1e4b-4c32-af28-2b4a22709c96" + }, + "execution_count": 84, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "False\n", + "True\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "print(populations)\n", + "print(populations.keys())\n", + "print(populations.values())" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "UFK9HzvpT19p", + "outputId": "72c5be7a-72db-4d73-ae0e-c82da94777c7" + }, + "execution_count": 85, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "{'India': 100, 'Bangla': 32, 'UK': 55}\n", + "dict_keys(['India', 'Bangla', 'UK'])\n", + "dict_values([100, 32, 55])\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "# Class / Object" + ], + "metadata": { + "id": "ItDXf91oPp1u" + } + }, + { + "cell_type": "code", + "source": [ + "class MediaPlayer:\n", + " def playSong():\n", + " #\n", + "\n", + " def pauseSOng():\n", + " #\n", + "\n", + "class Person:\n", + " def sayHii():\n", + " #" + ], + "metadata": { + "id": "-9pMQoGHfp_y" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "class Person:\n", + "\n", + " def __init__(self, n='Diwakar', a=22):\n", + " self.name = n\n", + " self.age = a\n", + "\n", + " def sayHii(self):\n", + " print(self.name, self.age)\n", + "\n", + "p1 = Person('Diwakar')\n", + "p2 = Person('Ayaan', 21)\n", + "\n", + "# p2 = p1\n", + "\n", + "p1.age = 25\n", + "\n", + "p1.sayHii()\n", + "p2.sayHii()" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "yeQM4jmpeIqK", + "outputId": "9a2119e9-fae1-45eb-ebd9-7445dcb72dad" + }, + "execution_count": 103, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Diwakar 25\n", + "Ayaan 21\n" + ] + } + ] } ] } \ No newline at end of file