diff --git a/4. Display Feedback for Correct and Incorrect Answers/displayquiz.js b/4. Display Feedback for Correct and Incorrect Answers/displayquiz.js new file mode 100644 index 0000000..6d50e48 --- /dev/null +++ b/4. Display Feedback for Correct and Incorrect Answers/displayquiz.js @@ -0,0 +1,33 @@ +function showResults(questions, quizContainer, resultsContainer){ + + // gather answer containers from our quiz + var answerContainers = quizContainer.querySelectorAll('.answers'); + + // keep track of user's answers + var userAnswer = ''; + var numCorrect = 0; + + // for each question... + for(var i=0; i\"Open" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "uAGTn9K3_afs", + "outputId": "19b288cf-9602-4404-d1ce-721e2f4816e9" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "(23, 34, 56, 4.5, 6.7)\n", + "\n" + ] + } + ], + "source": [ + "#tuple\n", + "\n", + "t1 = (23,34,56,4.5,6.7)\n", + "print(t1)\n", + "print(type(t1))" + ] + }, + { + "cell_type": "code", + "source": [ + "t1[0]\n" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "pWKRMN5dACva", + "outputId": "6fd86b66-c616-4aa2-97d4-a1d691d62eeb" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "23" + ] + }, + "metadata": {}, + "execution_count": 6 + } + ] + }, + { + "cell_type": "code", + "source": [ + "t1[-1]" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "9X6XPrO5AQG8", + "outputId": "3417816e-fba1-480f-e772-a246f8219882" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "6.7" + ] + }, + "metadata": {}, + "execution_count": 7 + } + ] + }, + { + "cell_type": "code", + "source": [ + "t1[2:4]" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "5CqE_sLGARbN", + "outputId": "0cc5dfe0-3212-4ab4-87a2-43cad095e0d9" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "(56, 4.5)" + ] + }, + "metadata": {}, + "execution_count": 8 + } + ] + }, + { + "cell_type": "code", + "source": [ + "t1[0]=100" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 166 + }, + "id": "YdEjdHlRAacm", + "outputId": "7bdbf4e0-c397-4bf5-e24f-ae9b79ef54a1" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "error", + "ename": "TypeError", + "evalue": "ignored", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mt1\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m100\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m: 'tuple' object does not support item assignment" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "#modifying/editing a tuple\n", + "#create tuple\n", + "t2 = tuple((34,56,78))\n", + "t2" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "HeyYX_VYAgMV", + "outputId": "4125549b-b425-400f-e25e-2d57905cee6f" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "(34, 56, 78)" + ] + }, + "metadata": {}, + "execution_count": 10 + } + ] + }, + { + "cell_type": "code", + "source": [ + "l1 = list(t2)" + ], + "metadata": { + "id": "S2V679pvA-HR" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "l1" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "aNQ48IXGBMd0", + "outputId": "715b6703-ebc4-4cad-ee53-e1a5bda16fe9" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "[34, 56, 78]" + ] + }, + "metadata": {}, + "execution_count": 12 + } + ] + }, + { + "cell_type": "code", + "source": [ + "l1.append(100)\n", + "#convert back to tuple\n", + "t2 = tuple(l1)\n", + "t2" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "op3T7g6fBNVE", + "outputId": "b1f7e9fb-95e9-4111-ada9-2d9e605d4bcb" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "(34, 56, 78, 100)" + ] + }, + "metadata": {}, + "execution_count": 13 + } + ] + }, + { + "cell_type": "code", + "source": [ + "t1 = (1,2,3,[1,2],4)\n", + "t1[3][0]=9 #modifying data\n", + "print(t1)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "c5A0VeerBmtN", + "outputId": "943162d7-6d42-40bf-82a8-c7e864d55c62" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "(1, 2, 3, [9, 2], 4)\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "t2.count(100)\n", + " " + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "66E5kAowCB7b", + "outputId": "a4911e35-2c9f-49fb-ddd0-867c1cc09637" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "1" + ] + }, + "metadata": {}, + "execution_count": 15 + } + ] + }, + { + "cell_type": "code", + "source": [ + "t2.index(34)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "zRine2xECE8N", + "outputId": "9ed828ae-590e-44b8-b2ef-1902cf5d9add" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "0" + ] + }, + "metadata": {}, + "execution_count": 17 + } + ] + }, + { + "cell_type": "code", + "source": [ + "#unpacking the tuple\n", + "fruits = (\"apple\", \"banana\", \"cherry\")\n", + "fruits" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "E3SJl4TsCHtv", + "outputId": "7ce1d417-167e-481e-f33f-e4b01313e9f0" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "('apple', 'banana', 'cherry')" + ] + }, + "metadata": {}, + "execution_count": 18 + } + ] + }, + { + "cell_type": "code", + "source": [ + "(a,b,c) = fruits\n", + "print(a,b,c)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "acykN1pBCheo", + "outputId": "ae55692a-81b2-4092-c920-e4ef4c172cf8" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "apple banana cherry\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "#Sets\n", + "set1 = {11,22,'apple','cherry'}\n", + "print(set1)\n", + "print(type(set1))" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "RykqYOupCnkO", + "outputId": "559a4c30-63c4-4f24-ed8e-4ecb7105f920" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "{'apple', 'cherry', 11, 22}\n", + "\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "set1[2]=55" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 166 + }, + "id": "-m6dX1PaEFnk", + "outputId": "6e1a5fef-d5ab-4ca5-a6a2-9b5f0b5d7e93" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "error", + "ename": "TypeError", + "evalue": "ignored", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mset1\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m55\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m: 'set' object does not support item assignment" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "set1[0]" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 166 + }, + "id": "IQrTaHwvEqic", + "outputId": "f486e8fc-6e97-4eee-ad0c-66f627cc0095" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "error", + "ename": "TypeError", + "evalue": "ignored", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mset1\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m: 'set' object is not subscriptable" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "set1 = {11,11,22,22,'apple','cherry'}" + ], + "metadata": { + "id": "Gz2nAG4YEuCs" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "set1 #no duplicates allowed" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "7slpe2OSEzEO", + "outputId": "b32a4ffa-d04b-4927-9383-fa9cc3557b69" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "{11, 22, 'apple', 'cherry'}" + ] + }, + "metadata": {}, + "execution_count": 25 + } + ] + }, + { + "cell_type": "code", + "source": [ + "print('apple' in set1)\n" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "BXZnX8uREzop", + "outputId": "d66b0d54-0d4a-473d-9a11-62121aeb8e22" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "True\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "#convert into set\n", + "set2 = set(l1)\n", + "set2" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "jQfSpXh7E9Nu", + "outputId": "e86b0b19-bde0-4827-c555-51792a495670" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "{34, 56, 78, 100}" + ] + }, + "metadata": {}, + "execution_count": 27 + } + ] + }, + { + "cell_type": "code", + "source": [ + "#set methods\n", + "set2.add(\"hello\")\n", + "set2\n", + "\n" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "a7vcXvPgFJg4", + "outputId": "b300555d-125d-4df0-898f-db2e9b2406b9" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "{100, 34, 56, 78, 'hello'}" + ] + }, + "metadata": {}, + "execution_count": 28 + } + ] + }, + { + "cell_type": "code", + "source": [ + "set2.update(set1)\n", + "set2" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "H8TEYfOeFk7j", + "outputId": "f0497400-e2a5-4932-d266-7166e1953e32" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "{100, 11, 22, 34, 56, 78, 'apple', 'cherry', 'hello'}" + ] + }, + "metadata": {}, + "execution_count": 29 + } + ] + }, + { + "cell_type": "code", + "source": [ + "set2.remove(55) #error\n", + "set2" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 183 + }, + "id": "U9dyUflHF-lA", + "outputId": "218e1d88-5fc6-4ef7-d471-ea9c938e87f7" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "error", + "ename": "KeyError", + "evalue": "ignored", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mset2\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mremove\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m55\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0mset2\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mKeyError\u001b[0m: 55" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "set2.discard(55) #no error even when element not present in the set\n", + "set2" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "8W_AyhgLGLL-", + "outputId": "7b1886b9-df8b-484d-f29f-26d76ebd8313" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "{100, 11, 22, 34, 56, 'apple', 'cherry', 'hello'}" + ] + }, + "metadata": {}, + "execution_count": 32 + } + ] + }, + { + "cell_type": "code", + "source": [ + "#set operations\n", + "\n", + "x = {1,2,3,4,5}\n", + "y = {4,5,6,7,8}\n", + "\n" + ], + "metadata": { + "id": "gDU3IlvpGXvi" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "\n", + "A.union(B) #duplicates removed" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "kisequQ8HZc0", + "outputId": "a6be0a6c-830d-4d3a-889a-cd847b14ba9d" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "{1, 2, 3, 4, 5, 6, 7, 8}" + ] + }, + "metadata": {}, + "execution_count": 39 + } + ] + }, + { + "cell_type": "code", + "source": [ + "B.union(A)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "bC5_3VroHbYy", + "outputId": "0ab4cec0-45a0-4042-b981-839b5661eceb" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "{1, 2, 3, 4, 5, 6, 7, 8}" + ] + }, + "metadata": {}, + "execution_count": 40 + } + ] + }, + { + "cell_type": "code", + "source": [ + "A.intersection(B) #common elements\n" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "kUT-yOnSHgVc", + "outputId": "c273bf95-cfb4-40f9-8d41-8571b2d1e626" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "{4, 5}" + ] + }, + "metadata": {}, + "execution_count": 41 + } + ] + }, + { + "cell_type": "code", + "source": [ + "x.intersection_update(y)\n", + "x" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "xMfnO2iWHnYU", + "outputId": "d63783c9-b9b8-4030-ed63-043e70078d8f" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "{4, 5}" + ] + }, + "metadata": {}, + "execution_count": 44 + } + ] + }, + { + "cell_type": "code", + "source": [ + "#difference\n", + "A.difference(B) #all elements in one which aint in other" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "j92bpkorIB7k", + "outputId": "17ce652c-63fe-4baf-bd4e-8585bf7674b3" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "{1, 2, 3}" + ] + }, + "metadata": {}, + "execution_count": 45 + } + ] + }, + { + "cell_type": "code", + "source": [ + "B.difference(A)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "IFz45cXQISf1", + "outputId": "ccbc2649-ecca-424b-b54c-5f2af9d34727" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "{6, 7, 8}" + ] + }, + "metadata": {}, + "execution_count": 46 + } + ] + }, + { + "cell_type": "code", + "source": [ + "A.symmetric_difference(B) #uncommon elements" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "cIjp1Wk1Immb", + "outputId": "b290192e-8220-48ca-ac4b-3163dd60d8fe" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "{1, 2, 3, 6, 7, 8}" + ] + }, + "metadata": {}, + "execution_count": 47 + } + ] + }, + { + "cell_type": "code", + "source": [ + "para1=\"\"\"Data science combines math and statistics, specialized programming, advanced analytics, artificial intelligence (AI), and machine learning with specific subject matter expertise to uncover actionable insights hidden in an organization’s data. These insights can be used to guide decision making and strategic planning.\n", + "\n", + "The accelerating volume of data sources, and subsequently data has made data science is one of the fastest growing field across every industry. As a result, it is no surprise that the role of the data scientist was dubbed the “sexiest job of the 21st century” by Harvard Business Review (link resides outside of IBM). Organizations are increasingly reliant on them to interpret data and provide actionable recommendations to improve business outcomes.\n", + "The data science lifecycle involves various roles, tools, and processes, which enables analysts to glean actionable insights. Typically, a data science project undergoes the following stages\n", + "\"\"\"\n" + ], + "metadata": { + "id": "HpaJdy6dIwAG" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "para2=\"\"\"Data analytics is the science of \n", + "analyzing raw data to make conclusions about that information. Many of the techniques and processes of data analytics have been automated into mechanical processes and algorithms that work over raw data for human consumption.\n", + "\"\"\"" + ], + "metadata": { + "id": "iw-f-l4aKNJk" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "para1" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 226 + }, + "id": "J1E0X4LALUDb", + "outputId": "52763fac-3802-420d-e338-afbc1f8d8cf2" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "'Data science combines math and statistics, specialized programming, advanced analytics, artificial intelligence (AI), and machine learning with specific subject matter expertise to uncover actionable insights hidden in an organization’s data. These insights can be used to guide decision making and strategic planning.\\n\\nThe accelerating volume of data sources, and subsequently data has made data science is one of the fastest growing field across every industry. As a result, it is no surprise that the role of the data scientist was dubbed the “sexiest job of the 21st century” by Harvard Business Review (link resides outside of IBM). Organizations are increasingly reliant on them to interpret data and provide actionable recommendations to improve business outcomes.\\nThe data science lifecycle involves various roles, tools, and processes, which enables analysts to glean actionable insights. Typically, a data science project undergoes the following stages\\n'" + ], + "application/vnd.google.colaboratory.intrinsic+json": { + "type": "string" + } + }, + "metadata": {}, + "execution_count": 56 + } + ] + }, + { + "cell_type": "code", + "source": [ + "#removing unwanted char from both para\n", + "text1 = para1.lower()\n", + "text1 = text1.replace('\\n','')\n", + "text1 = text1.replace(',','')\n", + "text1 = text1.replace(':','')\n", + "text1 = text1.replace('.','')\n", + "text1 = text1.replace('/','')\n", + "text1 = text1.replace('\"','')\n", + "\n", + "text2= para2.lower()\n", + "text2 = text2.replace('\\n','')\n", + "text2 = text2.replace(',','')\n", + "text2 = text2.replace(':','')\n", + "text2 = text2.replace('.','')\n", + "text2 = text2.replace('/','')\n", + "text2 = text2.replace('\"','')\n", + "\n" + ], + "metadata": { + "id": "QCbJVK1MLVu9" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "print(text1)\n" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "bxEpCaI8MGJ5", + "outputId": "b339eed6-5011-4600-fac9-8616eb79e779" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "data science combines math and statistics specialized programming advanced analytics artificial intelligence (ai) and machine learning with specific subject matter expertise to uncover actionable insights hidden in an organization’s data these insights can be used to guide decision making and strategic planningthe accelerating volume of data sources and subsequently data has made data science is one of the fastest growing field across every industry as a result it is no surprise that the role of the data scientist was dubbed the “sexiest job of the 21st century” by harvard business review (link resides outside of ibm) organizations are increasingly reliant on them to interpret data and provide actionable recommendations to improve business outcomesthe data science lifecycle involves various roles tools and processes which enables analysts to glean actionable insights typically a data science project undergoes the following stages\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "print(text2)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "0ziKs6doNGZz", + "outputId": "302fad59-b51a-446a-bc50-44f1f24a56ef" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "data analytics is the science of analyzing raw data to make conclusions about that information many of the techniques and processes of data analytics have been automated into mechanical processes and algorithms that work over raw data for human consumption\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "#find unique words in text1\n", + "words1 = text1.split()\n", + "words1" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "B8CYXMdgMIeT", + "outputId": "6dca54be-e51f-433d-d64d-4932fc4d4f9e" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "['data',\n", + " 'science',\n", + " 'combines',\n", + " 'math',\n", + " 'and',\n", + " 'statistics',\n", + " 'specialized',\n", + " 'programming',\n", + " 'advanced',\n", + " 'analytics',\n", + " 'artificial',\n", + " 'intelligence',\n", + " '(ai)',\n", + " 'and',\n", + " 'machine',\n", + " 'learning',\n", + " 'with',\n", + " 'specific',\n", + " 'subject',\n", + " 'matter',\n", + " 'expertise',\n", + " 'to',\n", + " 'uncover',\n", + " 'actionable',\n", + " 'insights',\n", + " 'hidden',\n", + " 'in',\n", + " 'an',\n", + " 'organization’s',\n", + " 'data',\n", + " 'these',\n", + " 'insights',\n", + " 'can',\n", + " 'be',\n", + " 'used',\n", + " 'to',\n", + " 'guide',\n", + " 'decision',\n", + " 'making',\n", + " 'and',\n", + " 'strategic',\n", + " 'planningthe',\n", + " 'accelerating',\n", + " 'volume',\n", + " 'of',\n", + " 'data',\n", + " 'sources',\n", + " 'and',\n", + " 'subsequently',\n", + " 'data',\n", + " 'has',\n", + " 'made',\n", + " 'data',\n", + " 'science',\n", + " 'is',\n", + " 'one',\n", + " 'of',\n", + " 'the',\n", + " 'fastest',\n", + " 'growing',\n", + " 'field',\n", + " 'across',\n", + " 'every',\n", + " 'industry',\n", + " 'as',\n", + " 'a',\n", + " 'result',\n", + " 'it',\n", + " 'is',\n", + " 'no',\n", + " 'surprise',\n", + " 'that',\n", + " 'the',\n", + " 'role',\n", + " 'of',\n", + " 'the',\n", + " 'data',\n", + " 'scientist',\n", + " 'was',\n", + " 'dubbed',\n", + " 'the',\n", + " '“sexiest',\n", + " 'job',\n", + " 'of',\n", + " 'the',\n", + " '21st',\n", + " 'century”',\n", + " 'by',\n", + " 'harvard',\n", + " 'business',\n", + " 'review',\n", + " '(link',\n", + " 'resides',\n", + " 'outside',\n", + " 'of',\n", + " 'ibm)',\n", + " 'organizations',\n", + " 'are',\n", + " 'increasingly',\n", + " 'reliant',\n", + " 'on',\n", + " 'them',\n", + " 'to',\n", + " 'interpret',\n", + " 'data',\n", + " 'and',\n", + " 'provide',\n", + " 'actionable',\n", + " 'recommendations',\n", + " 'to',\n", + " 'improve',\n", + " 'business',\n", + " 'outcomesthe',\n", + " 'data',\n", + " 'science',\n", + " 'lifecycle',\n", + " 'involves',\n", + " 'various',\n", + " 'roles',\n", + " 'tools',\n", + " 'and',\n", + " 'processes',\n", + " 'which',\n", + " 'enables',\n", + " 'analysts',\n", + " 'to',\n", + " 'glean',\n", + " 'actionable',\n", + " 'insights',\n", + " 'typically',\n", + " 'a',\n", + " 'data',\n", + " 'science',\n", + " 'project',\n", + " 'undergoes',\n", + " 'the',\n", + " 'following',\n", + " 'stages']" + ] + }, + "metadata": {}, + "execution_count": 61 + } + ] + }, + { + "cell_type": "code", + "source": [ + "type(words1)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "skKB8kGEM3M7", + "outputId": "a0904718-3469-4bae-c935-e28a2dbe9fa8" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "list" + ] + }, + "metadata": {}, + "execution_count": 62 + } + ] + }, + { + "cell_type": "code", + "source": [ + "len(words1)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "SEHBlXMxNLMj", + "outputId": "8bc438b0-ab74-41c6-94c3-21ac782329fc" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "138" + ] + }, + "metadata": {}, + "execution_count": 63 + } + ] + }, + { + "cell_type": "code", + "source": [ + "# to set\n", + "unq_words1 = set(words1)\n", + "print(unq_words1)\n", + "print(len(unq_words1))" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "5PdjAY6pNMil", + "outputId": "2743aca5-7d60-4fa1-8d0c-61a02dcb3d64" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "{'no', 'advanced', 'scientist', 'of', 'science', 'accelerating', 'increasingly', 'job', '(link', 'the', 'matter', 'century”', 'combines', 'growing', 'hidden', 'dubbed', 'enables', 'outside', 'can', 'used', 'it', 'intelligence', 'to', 'which', 'an', 'fastest', 'subsequently', 'undergoes', 'analytics', 'made', 'was', 'these', 'them', 'roles', 'learning', 'strategic', 'with', 'as', 'in', 'making', 'uncover', 'planningthe', 'various', 'guide', 'role', 'processes', 'decision', 'insights', 'improve', 'industry', 'lifecycle', 'across', 'glean', 'be', 'typically', 'are', 'a', 'math', 'programming', '21st', 'subject', 'is', 'and', 'actionable', 'result', 'resides', 'data', 'specialized', 'specific', 'by', '(ai)', 'involves', 'provide', 'machine', 'that', 'artificial', 'organizations', '“sexiest', 'interpret', 'surprise', 'organization’s', 'ibm)', 'one', 'following', 'stages', 'review', 'tools', 'has', 'project', 'outcomesthe', 'statistics', 'expertise', 'analysts', 'every', 'reliant', 'harvard', 'field', 'business', 'sources', 'on', 'volume', 'recommendations'}\n", + "102\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "words2= text2.split()\n", + "words2" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "nQY7B3YoNWE0", + "outputId": "ac6ab8f3-53b0-4dc5-8c5e-e5f0957e8c02" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "['data',\n", + " 'analytics',\n", + " 'is',\n", + " 'the',\n", + " 'science',\n", + " 'of',\n", + " 'analyzing',\n", + " 'raw',\n", + " 'data',\n", + " 'to',\n", + " 'make',\n", + " 'conclusions',\n", + " 'about',\n", + " 'that',\n", + " 'information',\n", + " 'many',\n", + " 'of',\n", + " 'the',\n", + " 'techniques',\n", + " 'and',\n", + " 'processes',\n", + " 'of',\n", + " 'data',\n", + " 'analytics',\n", + " 'have',\n", + " 'been',\n", + " 'automated',\n", + " 'into',\n", + " 'mechanical',\n", + " 'processes',\n", + " 'and',\n", + " 'algorithms',\n", + " 'that',\n", + " 'work',\n", + " 'over',\n", + " 'raw',\n", + " 'data',\n", + " 'for',\n", + " 'human',\n", + " 'consumption']" + ] + }, + "metadata": {}, + "execution_count": 66 + } + ] + }, + { + "cell_type": "code", + "source": [ + "type(words2)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "SXKFBvOQNn_X", + "outputId": "9b7bb283-ab40-4236-a2de-6d3d43d42c02" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "list" + ] + }, + "metadata": {}, + "execution_count": 67 + } + ] + }, + { + "cell_type": "code", + "source": [ + "len(words2)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "s515VDxqNqgE", + "outputId": "d052cdea-0b74-4776-9b14-e58638d5560e" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "40" + ] + }, + "metadata": {}, + "execution_count": 68 + } + ] + }, + { + "cell_type": "code", + "source": [ + "unq_words2 = set(words2)\n", + "print(unq_words2)\n", + "print(len(unq_words2))\n" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "O5OYUORHNr1y", + "outputId": "37501303-1be8-4d01-a20e-5d56b5773354" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "{'about', 'mechanical', 'make', 'many', 'and', 'consumption', 'of', 'science', 'automated', 'techniques', 'been', 'into', 'have', 'data', 'the', 'processes', 'conclusions', 'work', 'algorithms', 'that', 'to', 'for', 'analytics', 'information', 'analyzing', 'human', 'over', 'is', 'raw'}\n", + "29\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "#lexical diversity = No of words/No of unique words\n", + "\n", + "ld1 = len(words1)/len(unq_words1)\n", + "\n", + "ld1\n" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "ew3D8u3ONzt0", + "outputId": "6b1fb5a4-2961-42a4-882b-42dc3c91d938" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "1.3529411764705883" + ] + }, + "metadata": {}, + "execution_count": 71 + } + ] + }, + { + "cell_type": "code", + "source": [ + "ld2 = len(words2)/len(unq_words2)\n", + "ld2" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "YcjUsoENONMe", + "outputId": "204bcb04-3817-4672-9318-86c4bc2ef4e7" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "1.3793103448275863" + ] + }, + "metadata": {}, + "execution_count": 72 + } + ] + }, + { + "cell_type": "code", + "source": [ + "#print common words in para1 and para2\n", + "print(unq_words1.intersection(unq_words2))\n", + "\n" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "syfMNRrkOVMH", + "outputId": "d760a442-29b0-4cbb-e4bb-6d758c501299" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "{'data', 'the', 'analytics', 'that', 'processes', 'and', 'of', 'science', 'to', 'is'}\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "print(len(unq_words1.intersection(unq_words2)))" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "k1QztHbMOvwO", + "outputId": "510e686f-6c2a-4b77-87ff-350fe8d159b0" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "10\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "print(unq_words1.difference(unq_words2)) #words in para1 not in para 2" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "bPDp-hSFO2Lu", + "outputId": "1ad05569-6b51-495e-e4fe-ed187a970535" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "{'no', 'advanced', 'scientist', 'accelerating', 'increasingly', 'job', '(link', 'matter', 'century”', 'combines', 'growing', 'hidden', 'dubbed', 'enables', 'outside', 'can', 'used', 'it', 'intelligence', 'which', 'an', 'fastest', 'subsequently', 'undergoes', 'made', 'was', 'these', 'them', 'roles', 'learning', 'strategic', 'with', 'as', 'in', 'making', 'uncover', 'planningthe', 'various', 'guide', 'role', 'decision', 'insights', 'improve', 'industry', 'lifecycle', 'across', 'glean', 'be', 'typically', 'are', 'a', 'math', 'programming', '21st', 'subject', 'actionable', 'result', 'resides', 'specialized', 'specific', 'by', '(ai)', 'involves', 'provide', 'machine', 'artificial', 'organizations', '“sexiest', 'interpret', 'surprise', 'organization’s', 'ibm)', 'one', 'following', 'stages', 'review', 'tools', 'has', 'project', 'outcomesthe', 'statistics', 'expertise', 'analysts', 'every', 'reliant', 'harvard', 'field', 'business', 'sources', 'on', 'volume', 'recommendations'}\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "#list of uncommon words in both\n", + "print(unq_words1.symmetric_difference(unq_words2))\n", + "print(len((unq_words1.symmetric_difference(unq_words2))))" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "-clc_IisPC1P", + "outputId": "771ef6e7-f408-481f-e18a-c299348e8402" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "{'no', 'about', 'make', 'many', 'advanced', 'actionable', 'consumption', 'scientist', 'accelerating', 'increasingly', 'techniques', 'job', 'result', '(link', 'resides', 'have', 'matter', 'specialized', 'century”', 'combines', 'growing', 'hidden', 'dubbed', 'specific', 'by', 'enables', 'work', 'outside', '(ai)', 'involves', 'provide', 'can', 'machine', 'algorithms', 'artificial', 'used', 'it', 'intelligence', 'organizations', 'which', 'an', 'fastest', 'subsequently', '“sexiest', 'undergoes', 'on', 'human', 'volume', 'made', 'was', 'raw', 'these', 'them', 'roles', 'learning', 'mechanical', 'interpret', 'strategic', 'with', 'as', 'in', 'making', 'uncover', 'planningthe', 'automated', 'surprise', 'various', 'been', 'into', 'guide', 'organization’s', 'role', 'ibm)', 'conclusions', 'decision', 'one', 'insights', 'following', 'stages', 'review', 'improve', 'industry', 'lifecycle', 'across', 'tools', 'glean', 'be', 'typically', 'has', 'project', 'outcomesthe', 'statistics', 'expertise', 'analysts', 'are', 'every', 'a', 'for', 'math', 'reliant', 'programming', 'information', 'harvard', 'field', 'business', '21st', 'analyzing', 'sources', 'subject', 'recommendations', 'over'}\n", + "111\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "#dictionary\n", + "\n", + "student = {'id': 12, 'Name':'John', 'Branch':'IT'}\n", + "print(student)\n", + "print(type(student))\n" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "EmvxHAgTPoF6", + "outputId": "398102f3-7ccd-4d6d-a013-e245d5c4a651" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "{'id': 12, 'Name': 'John', 'Branch': 'IT'}\n", + "\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "student.items()" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "rROtyf9GWue1", + "outputId": "9679cf31-8732-487f-c3e5-139828e21102" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "dict_items([('id', 12), ('Name', 'John'), ('Branch', 'IT')])" + ] + }, + "metadata": {}, + "execution_count": 81 + } + ] + }, + { + "cell_type": "code", + "source": [ + "student.keys()\n" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "ws7SnxdrXLwT", + "outputId": "c4789472-6375-4983-aae4-91d760bb019c" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "dict_keys(['id', 'Name', 'Branch'])" + ] + }, + "metadata": {}, + "execution_count": 83 + } + ] + }, + { + "cell_type": "code", + "source": [ + "student.values()" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "yXHswK8aXZrj", + "outputId": "a779ffc7-e968-412c-bcb0-7c6378a6183c" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "dict_values([12, 'John', 'IT'])" + ] + }, + "metadata": {}, + "execution_count": 84 + } + ] + }, + { + "cell_type": "code", + "source": [ + "#access a key value pair\n", + "student['Name']" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "id": "aOLhoeLzXdIJ", + "outputId": "56a5731c-cd68-461c-912c-5193701fea49" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "'John'" + ], + "application/vnd.google.colaboratory.intrinsic+json": { + "type": "string" + } + }, + "metadata": {}, + "execution_count": 85 + } + ] + }, + { + "cell_type": "code", + "source": [ + "student['Branch']" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "id": "61Clg_caXghP", + "outputId": "f9de45e3-7f66-454d-a692-40220449abec" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "'IT'" + ], + "application/vnd.google.colaboratory.intrinsic+json": { + "type": "string" + } + }, + "metadata": {}, + "execution_count": 86 + } + ] + }, + { + "cell_type": "code", + "source": [ + "student.get('Name')" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "id": "Q-SVEBkKXioQ", + "outputId": "e095acd9-74ed-43aa-ce59-e710a87c1208" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "'John'" + ], + "application/vnd.google.colaboratory.intrinsic+json": { + "type": "string" + } + }, + "metadata": {}, + "execution_count": 87 + } + ] + }, + { + "cell_type": "code", + "source": [ + "#change the key value\n", + "student['Name'] = 'Harry'" + ], + "metadata": { + "id": "sn-CLWNqXvSg" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "student.items()" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "Wb4tOVVfX-8w", + "outputId": "5383d1c4-6d5a-4098-ae6c-171cac205040" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "dict_items([('id', 12), ('Name', 'Harry'), ('Branch', 'IT')])" + ] + }, + "metadata": {}, + "execution_count": 89 + } + ] + }, + { + "cell_type": "code", + "source": [ + "student['Branch'] = 'Mechanical'\n", + "student.items()" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "12sSN47yYARA", + "outputId": "7a51f2a0-2ac1-43c5-c651-d992ea843312" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "dict_items([('id', 12), ('Name', 'Harry'), ('Branch', 'Mechanical')])" + ] + }, + "metadata": {}, + "execution_count": 90 + } + ] + }, + { + "cell_type": "code", + "source": [ + "#add new key value pair\n", + "student['Age'] = 22\n", + "student" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "MVNqgfEfYEcA", + "outputId": "8e691b91-2c19-4ac7-9b93-5c62bff97477" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "{'id': 12, 'Name': 'Harry', 'Branch': 'Mechanical', 'Age': 22}" + ] + }, + "metadata": {}, + "execution_count": 91 + } + ] + }, + { + "cell_type": "code", + "source": [ + "#membership operation\n", + "print('Branch' in student)\n" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "8IYYXoUPYZt-", + "outputId": "05363052-875b-4f84-ed54-f53e2ea7b003" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "True\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "#pop methhod\n", + "student.pop('Age')\n", + "student" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "0tvrIlbMY23Q", + "outputId": "c7013b16-3541-477a-d248-967ceba4814a" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "{'id': 12, 'Name': 'Harry', 'Branch': 'Mechanical'}" + ] + }, + "metadata": {}, + "execution_count": 95 + } + ] + }, + { + "cell_type": "code", + "source": [ + "#multiple values in dictionary\n", + "\n", + "players={'Name':['Virat', 'Rohit', 'Dhoni'], \n", + " 'Age':[31,32,33],\n", + " 'Designation':['Captain','VC','WK'], \n", + " 'Role': \"Cricketers\"}" + ], + "metadata": { + "id": "2QIU_IgdY9wg" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "players" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "WRccExLEZomf", + "outputId": "1f6b969d-77fd-43e4-e914-d4047e3afb87" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "{'Name': ['Virat', 'Rohit', 'Dhoni'],\n", + " 'Age': [31, 32, 33],\n", + " 'Designation': ['Captain', 'VC', 'WK'],\n", + " 'Role': 'Cricketers'}" + ] + }, + "metadata": {}, + "execution_count": 97 + } + ] + }, + { + "cell_type": "code", + "source": [ + "len(players)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "G_HbI9yDZp8X", + "outputId": "bb743a52-c03f-47cd-ff68-4064fa2678be" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "4" + ] + }, + "metadata": {}, + "execution_count": 98 + } + ] + }, + { + "cell_type": "code", + "source": [ + "players['Name']" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "i9TPd13RZtJF", + "outputId": "537dffff-9b31-498b-902b-a53db74c7b22" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "['Virat', 'Rohit', 'Dhoni']" + ] + }, + "metadata": {}, + "execution_count": 99 + } + ] + }, + { + "cell_type": "code", + "source": [ + "#access particular element by their index\n", + "players['Name'][0]" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "id": "WBrs1pjKZ0xn", + "outputId": "c6b43d34-29d5-4edd-85ae-cc8cfdb3017a" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "'Virat'" + ], + "application/vnd.google.colaboratory.intrinsic+json": { + "type": "string" + } + }, + "metadata": {}, + "execution_count": 100 + } + ] + }, + { + "cell_type": "code", + "source": [ + "players['Age'][1] = 55\n", + "players" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "-Z4TT92LaAN4", + "outputId": "3e629002-52a5-4d4d-c130-7eac263bd389" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "{'Name': ['Virat', 'Rohit', 'Dhoni'],\n", + " 'Age': [31, 55, 33],\n", + " 'Designation': ['Captain', 'VC', 'WK'],\n", + " 'Role': 'play'}" + ] + }, + "metadata": {}, + "execution_count": 105 + } + ] + }, + { + "cell_type": "code", + "source": [ + "players.update({'Role': 'play'})" + ], + "metadata": { + "id": "bJM4U_xhaRBr" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "players" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "2c4Fjpdtafrv", + "outputId": "883679c2-fe58-43ee-d833-6c8014442e84" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "{'Name': ['Virat', 'Rohit', 'Dhoni'],\n", + " 'Age': [31, 32, 33],\n", + " 'Designation': ['Captain', 'VC', 'WK'],\n", + " 'Role': 'play'}" + ] + }, + "metadata": {}, + "execution_count": 103 + } + ] + }, + { + "cell_type": "code", + "source": [ + "#conditional statements\n", + "#if-else\n", + "a = 10\n", + "b = 5\n", + "if a > 5:\n", + " print('a is greater')\n", + "else:\n", + " print('b is greater')\n" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "7p5jfdNEagvm", + "outputId": "d9b5ddea-dede-443e-da38-d0b732cae938" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "a is greater\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "a = input('Enter first number')\n", + "b = input('Enter second number')\n", + "print(a + b)\n", + "if a > b:\n", + " print('a is greater')\n", + "else:\n", + " print('b is greateR')" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "7lB-zRtjbkvk", + "outputId": "c708e205-4cf6-48b7-e45c-683f9514c832" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Enter first number10\n", + "Enter second number20\n", + "1020\n", + "b is greateR\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "a = int(input('Enter first number'))\n", + "b = int(input('Enter second number'))\n", + "print(a + b)\n", + "if a > b:\n", + " print('a is greater')\n", + "else:\n", + " print('b is greateR')" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "RMNeVNofcPKk", + "outputId": "ec30d501-79b8-4c65-ba31-0409009d7d6d" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Enter first number10\n", + "Enter second number10\n", + "20\n", + "b is greateR\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "#check if one can vote or not\n", + "\n", + "age = int(input('Please enter your age: '))\n", + "if age >= 18:\n", + " print('Yes, you can vote!')\n", + "else:\n", + " print(\"Sorry, you can't vote!\")\n" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "PVBGIoj6ch65", + "outputId": "67b61dea-baa8-45c7-9c76-04f542824e5d" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Please enter your age: 18\n", + "Yes, you can vote!\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "#print student marksheet\n", + "#checking multiple conditions\n", + "marks = int(input('Enter Marks'))\n", + "if marks >= 90:\n", + " print('A Grade')\n", + "elif marks < 90 and marks >= 80:\n", + " print('B Grade')\n", + "elif marks < 80 and marks >= 70:\n", + " print('C Grade')\n", + "else:\n", + " print('Try Next Time')" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "e7zOB3zPc8Vw", + "outputId": "33bb55d5-0cd4-419c-acf3-a3c3bdbacfb6" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Enter Marks30\n", + "Try Next Time\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "#employee salary\n", + "\n", + "salary = int(input('Enter your salary:'))\n", + "if salary<20000 and salary >= 10000:\n", + " salary = salary + salary * 0.20 + 10000\n", + "elif salary < 10000 and salary >= 5000:\n", + " salary = salary+salary*0.30+5000\n", + " \n", + "\n", + "else:\n", + " salary = salary+salary*0.40+2500\n", + "print(salary)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "8PU582p1ehpk", + "outputId": "945f7ee3-3bd5-4302-9eb9-a015aa9edaf6" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Enter your salary:10000\n", + "22000.0\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "#Project - store username and pass\n", + "#ask user to enter his username and pass - if credential matches, welcome\n", + "#else if wrong then ask user for signup, update that data in my storage\n", + "\n", + "cred={'Rohit': 'rohit01',\n", + " 'Virat': 'virat02',\n", + " 'Kapil': 'sharma23',}\n", + "cred.keys()" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "XMrqDFumfcuY", + "outputId": "ebf257ab-32bd-4217-f5d5-2ddf67a5ed08" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "dict_keys(['Rohit', 'Virat', 'Kapil'])" + ] + }, + "metadata": {}, + "execution_count": 123 + } + ] + }, + { + "cell_type": "code", + "source": [ + "cred.values()" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "yacYvhtxhywR", + "outputId": "daf707dd-5f5c-449b-aba5-2fd7c1293ace" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "dict_values(['rohit01', 'virat02', 'sharma23'])" + ] + }, + "metadata": {}, + "execution_count": 124 + } + ] + }, + { + "cell_type": "code", + "source": [ + "username = input('Please enter your username:')\n", + "password = input('Please enter your password:')\n", + "\n", + "if username not in cred.keys():\n", + " print('Username is not in the database!')\n", + " ask = input('Would you like to signup? y/n:')\n", + " if ask.lower() == 'y':\n", + " username = input('Please enter your username:')\n", + " password = input('Please enter your password:')\n", + "\n", + " cred.update({username:password})\n", + " print('Account added successfully!')\n", + " print(cred)\n", + " else:\n", + " print('Thankyou!!')\n", + "elif cred[username] == password:\n", + " print('Hello {} welcome'.format(username) )\n", + "else:\n", + " print('username or password is incorrect')\n", + "\n" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "Q7sdJ91ah4SA", + "outputId": "548db22e-5c4d-403b-c764-00053471d715" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Please enter your username:gayatri\n", + "Please enter your password:gayatri\n", + "Username is not in the database!\n", + "Would you like to signup? y/n:y\n", + "Please enter your username:gayatri\n", + "Please enter your password:bhosale\n", + "Account added successfully!\n", + "{'Rohit': 'rohit01', 'Virat': 'virat02', 'Kapil': 'sharma23', 'gayatri': 'bhosale'}\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "cred\n" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "pSb2-T5EjvwR", + "outputId": "ec73ae4b-e591-4061-b3bf-01cff7f491a4" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "{'Rohit': 'rohit01',\n", + " 'Virat': 'virat02',\n", + " 'Kapil': 'sharma23',\n", + " 'gayatri': 'bhosale'}" + ] + }, + "metadata": {}, + "execution_count": 127 + } + ] + }, + { + "cell_type": "code", + "source": [ + "#loops\n", + "list1 = [23,45,67,89]\n", + "for i in list1:\n", + " print(i)\n", + " " + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "RLzn_Y-Hk8Fd", + "outputId": "0c87a793-8e70-473f-ccee-a7bf89da7d2f" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "23\n", + "45\n", + "67\n", + "89\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "t1 = (23,45,67,67)\n", + "for i in t1:\n", + " print(i)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "lPs1DcPLlSfD", + "outputId": "0d04b134-c183-4a24-a78c-bf46e70794a0" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "23\n", + "45\n", + "67\n", + "67\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "#break keyword\n", + "list1 = [23,45,67,-1,89]\n", + "for i in list1:\n", + " if i < 0:\n", + " break #breaks current execution\n", + " print(i)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "wPsLxLI4lcr0", + "outputId": "e1614ff9-1b7f-49d1-e6f6-066e2adbad78" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "23\n", + "45\n", + "67\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "#continue keyword\n", + "\n", + "list1 = [23,45,-2, -22, 67,-1,89, -99]\n", + "for i in list1:\n", + " if i < 0:\n", + " continue #breaks current execution\n", + " print(i)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "pcVr5FNxl1B5", + "outputId": "b79fa112-6d08-49b6-bac1-f2ad94c09d0b" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "23\n", + "45\n", + "67\n", + "89\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "list2 = [10,10,10]\n", + "sum = 0\n", + "for i in list2:\n", + " sum += i\n", + "print(sum)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "J3cDJxSNmJsN", + "outputId": "d083fb81-0727-4dc0-c1bf-96df57b4b6ef" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "30\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "for i in range(10):\n", + " print(i)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "Kqih_h5_mkBx", + "outputId": "5d89bad7-27fc-451c-d181-57d5c2f7da08" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "0\n", + "1\n", + "2\n", + "3\n", + "4\n", + "5\n", + "6\n", + "7\n", + "8\n", + "9\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "for i in range(20,31):\n", + " print(i)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "aXk5p0evnViW", + "outputId": "aae04bbb-7a22-4e29-d7ce-75d9c8c6457e" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "20\n", + "21\n", + "22\n", + "23\n", + "24\n", + "25\n", + "26\n", + "27\n", + "28\n", + "29\n", + "30\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [], + "metadata": { + "id": "vQXluINzn7CQ" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "for i in range(2,20,2):\n", + " print(i)\n", + " " + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "E6yDSnSEna-S", + "outputId": "7ea6bf73-0b60-4198-81d1-18371aa360fa" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "2\n", + "4\n", + "6\n", + "8\n", + "10\n", + "12\n", + "14\n", + "16\n", + "18\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "number = int(input('Enter number'))\n", + "for i in range(1,11):\n", + " print(f'{number} * {i} = {number*i}')\n" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "4gSQy_VInlSB", + "outputId": "5d8322ca-1768-4961-d52f-f13ffd4385ec" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Enter number5\n", + "5 * 1 = 5\n", + "5 * 2 = 10\n", + "5 * 3 = 15\n", + "5 * 4 = 20\n", + "5 * 5 = 25\n", + "5 * 6 = 30\n", + "5 * 7 = 35\n", + "5 * 8 = 40\n", + "5 * 9 = 45\n", + "5 * 10 = 50\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "#for loop w else\n", + "cars = ['tata','bmw','honda']\n", + "for i in range(len(cars)):\n", + " print(\"I like\", cars[i])\n", + "else:\n", + " print('No items left in list')" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "mP9JUHoDojD_", + "outputId": "dc65f021-842c-4825-a6de-9e91744b4814" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "I like tata\n", + "I like bmw\n", + "I like honda\n", + "No items left in list\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "i = 1\n", + "while i <= 10:\n", + " print(i)\n", + " i += 1\n", + " " + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "ydWb3rcGpEN3", + "outputId": "963e8ef9-87f0-4af2-d5db-ec187331882b" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "1\n", + "2\n", + "3\n", + "4\n", + "5\n", + "6\n", + "7\n", + "8\n", + "9\n", + "10\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "i = 10\n", + "while i >= 1:\n", + " print(i)\n", + " i -= 1" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "aYBqKFNCp-o9", + "outputId": "cf0141a6-d736-462c-f4d8-58d0c3b3750b" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "10\n", + "9\n", + "8\n", + "7\n", + "6\n", + "5\n", + "4\n", + "3\n", + "2\n", + "1\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "#python session 3\n", + "#program to enter list elements through user\n", + "\n", + "list1 = []\n", + "#input from user \n", + "n = int(input('Enter number of elements in the list:'))\n", + "for i in range(0, n):\n", + " print('Enter the elements no {}'.format(i+1))\n", + " element = int(input())\n", + " list1.append(element)\n", + "print('The entered list is \\n', list1)\n", + "4" + ], + "metadata": { + "id": "wmM3icNaqN4L", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "49122aea-ba51-44c5-90dd-2b1fdef7e27f" + }, + "execution_count": 1, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Enter number of elements in the list:5\n", + "Enter the elements no 1\n", + "2\n", + "Enter the elements no 2\n", + "3\n", + "Enter the elements no 3\n", + "4\n", + "Enter the elements no 4\n", + "5\n", + "Enter the elements no 5\n", + "6\n", + "The entered list is \n", + " [2, 3, 4, 5, 6]\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "#create random numbers #pip install random\n", + "\n", + "import random\n", + "rand = []\n", + "for i in range(100):\n", + " num = random.randint(0,100) #creates random integers numbers list \n", + " rand.append(num)\n", + "print(rand)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "JNnDoi5RCMAy", + "outputId": "6d339056-e979-4852-a93f-b62336d1c877" + }, + "execution_count": 5, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "[4, 57, 6, 99, 12, 14, 100, 88, 77, 39, 94, 57, 74, 82, 12, 45, 62, 23, 48, 22, 14, 46, 68, 0, 34, 86, 50, 43, 34, 36, 12, 10, 5, 48, 58, 76, 12, 74, 65, 33, 75, 66, 73, 98, 23, 12, 85, 79, 68, 50, 16, 47, 15, 34, 83, 90, 96, 98, 21, 87, 72, 31, 44, 16, 4, 85, 77, 66, 63, 95, 15, 91, 87, 38, 46, 11, 35, 73, 70, 84, 5, 17, 1, 100, 8, 0, 71, 11, 46, 100, 90, 61, 30, 38, 59, 99, 53, 97, 97, 79]\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "#find and create a list of even numbers in this rand list\n", + "\n", + "rand2 = []\n", + "for i in rand:\n", + " if i != 0:\n", + " if i%2 == 0:\n", + " rand2.append(i)\n", + "print(rand2)\n" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "06N0DN3-DcyS", + "outputId": "b9be99ef-970d-4043-9178-268a3202dec0" + }, + "execution_count": 10, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "[4, 6, 12, 14, 100, 88, 94, 74, 82, 12, 62, 48, 22, 14, 46, 68, 34, 86, 50, 34, 36, 12, 10, 48, 58, 76, 12, 74, 66, 98, 12, 68, 50, 16, 34, 90, 96, 98, 72, 44, 16, 4, 66, 38, 46, 70, 84, 100, 8, 46, 100, 90, 30, 38]\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "#create a list of numbers divisible by 5\n", + "\n", + "rand3 = []\n", + "for i in rand:\n", + " if i%5 == 0:\n", + " rand3.append(i)\n", + "print(rand3)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "Owu6gVQZFHkG", + "outputId": "c3944c72-eaaf-4a72-dd6d-be0e24961746" + }, + "execution_count": 11, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "[100, 45, 0, 50, 10, 5, 65, 75, 85, 50, 15, 90, 85, 95, 15, 35, 70, 5, 100, 0, 100, 90, 30]\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "#use for loop with dictionary\n", + "emp = {'empid':1, 'name':'Rohit','Dept':'HR','Address': 'Mumbai'}\n", + "\n", + "for key in emp.keys(): #iterating over loop \n", + " print('key is',key,'maps to',emp[key])" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "8vlkU3slGFHN", + "outputId": "678f4d62-f384-4b73-95ee-b70985c1a583" + }, + "execution_count": 13, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "key is empid maps to 1\n", + "key is name maps to Rohit\n", + "key is Dept maps to HR\n", + "key is Address maps to Mumbai\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "#else with while loop\n", + "\n", + "numbers=[10,20,30,40,50]\n", + "\n", + "index = 0\n", + "while index < len(numbers):\n", + " print(numbers[index])\n", + " index += 1\n", + " print('current index', index)\n", + "else:\n", + " print('\\n empty now')" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "cdC3aaf4Hb1x", + "outputId": "5e39431b-7c4e-4cc1-ea5b-27b27eda3a56" + }, + "execution_count": 15, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "10\n", + "current index 1\n", + "20\n", + "current index 2\n", + "30\n", + "current index 3\n", + "40\n", + "current index 4\n", + "50\n", + "current index 5\n", + "\n", + " empty now\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "text=\"\"\"Data science is the study of data to extract meaningful insights for business. It is a multidisciplinary approach that combines principles and practices from the fields of mathematics, statistics, artificial intelligence, and computer engineering to analyze large amounts of data. This analysis helps data scientists to ask and answer questions like what happened, why it happened, what will happen, and what can be done with the results.\n", + "\"\"\"\n" + ], + "metadata": { + "id": "uc5nVS1qIIP5" + }, + "execution_count": 16, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "text\n" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 70 + }, + "id": "JkAKFvKUIzmp", + "outputId": "1699e16f-6728-4ccb-b84c-2a49b80ca458" + }, + "execution_count": 17, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "'Data science is the study of data to extract meaningful insights for business. It is a multidisciplinary approach that combines principles and practices from the fields of mathematics, statistics, artificial intelligence, and computer engineering to analyze large amounts of data. This analysis helps data scientists to ask and answer questions like what happened, why it happened, what will happen, and what can be done with the results.\\n'" + ], + "application/vnd.google.colaboratory.intrinsic+json": { + "type": "string" + } + }, + "metadata": {}, + "execution_count": 17 + } + ] + }, + { + "cell_type": "code", + "source": [ + "#list of words from text that starts with 'e'\n", + "#list of words from text that ends with 's'\n", + "#print words till word 'analysis'\n", + "\n", + "text1 = text.split()\n", + "text1" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "lw8XVyajI1bT", + "outputId": "5bc2d8a4-311a-44b5-e851-6b9068de38b1" + }, + "execution_count": 19, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "['Data',\n", + " 'science',\n", + " 'is',\n", + " 'the',\n", + " 'study',\n", + " 'of',\n", + " 'data',\n", + " 'to',\n", + " 'extract',\n", + " 'meaningful',\n", + " 'insights',\n", + " 'for',\n", + " 'business.',\n", + " 'It',\n", + " 'is',\n", + " 'a',\n", + " 'multidisciplinary',\n", + " 'approach',\n", + " 'that',\n", + " 'combines',\n", + " 'principles',\n", + " 'and',\n", + " 'practices',\n", + " 'from',\n", + " 'the',\n", + " 'fields',\n", + " 'of',\n", + " 'mathematics,',\n", + " 'statistics,',\n", + " 'artificial',\n", + " 'intelligence,',\n", + " 'and',\n", + " 'computer',\n", + " 'engineering',\n", + " 'to',\n", + " 'analyze',\n", + " 'large',\n", + " 'amounts',\n", + " 'of',\n", + " 'data.',\n", + " 'This',\n", + " 'analysis',\n", + " 'helps',\n", + " 'data',\n", + " 'scientists',\n", + " 'to',\n", + " 'ask',\n", + " 'and',\n", + " 'answer',\n", + " 'questions',\n", + " 'like',\n", + " 'what',\n", + " 'happened,',\n", + " 'why',\n", + " 'it',\n", + " 'happened,',\n", + " 'what',\n", + " 'will',\n", + " 'happen,',\n", + " 'and',\n", + " 'what',\n", + " 'can',\n", + " 'be',\n", + " 'done',\n", + " 'with',\n", + " 'the',\n", + " 'results.']" + ] + }, + "metadata": {}, + "execution_count": 19 + } + ] + }, + { + "cell_type": "code", + "source": [ + "for i in text1:\n", + " if i[0] == 'e':\n", + " print(i)\n", + " " + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "IUXJhCDaJpV7", + "outputId": "6e96dc85-01c2-4c24-b1de-074233bde446" + }, + "execution_count": 23, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "extract\n", + "engineering\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "end_e = []\n", + "for words in text1:\n", + " if words.endswith('e'):\n", + " end_e.append(words)\n", + "end_e" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "kwJw0lAgJ5c_", + "outputId": "5f00ec1a-e6bd-4b82-824f-bc267ffc6f2e" + }, + "execution_count": 27, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "['science', 'the', 'the', 'analyze', 'large', 'like', 'be', 'done', 'the']" + ] + }, + "metadata": {}, + "execution_count": 27 + } + ] + }, + { + "cell_type": "code", + "source": [ + "for i in text1:\n", + " if i == \"analysis\":\n", + " break\n", + " print(i)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "Mbsf5GMKLuAT", + "outputId": "e8f02689-d22c-44a2-f231-894e181f4be4" + }, + "execution_count": 40, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Data\n", + "science\n", + "is\n", + "the\n", + "study\n", + "of\n", + "data\n", + "to\n", + "extract\n", + "meaningful\n", + "insights\n", + "for\n", + "business.\n", + "It\n", + "is\n", + "a\n", + "multidisciplinary\n", + "approach\n", + "that\n", + "combines\n", + "principles\n", + "and\n", + "practices\n", + "from\n", + "the\n", + "fields\n", + "of\n", + "mathematics,\n", + "statistics,\n", + "artificial\n", + "intelligence,\n", + "and\n", + "computer\n", + "engineering\n", + "to\n", + "analyze\n", + "large\n", + "amounts\n", + "of\n", + "data.\n", + "This\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "#funtions\n", + "\n", + "def display():\n", + " ''' It prints hello world'''\n", + " print('hello world')\n", + "#calling the function\n", + "\n", + "display()" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "8u8mPLawMXZK", + "outputId": "b6f56264-1546-40ac-b8e1-6106345bf953" + }, + "execution_count": 43, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "hello world\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "#add two numbers\n", + "def add(x,y):\n", + " return x + y" + ], + "metadata": { + "id": "piZA_HtCOV32" + }, + "execution_count": 44, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "add(123,566)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "p2OZ2nfgPs3F", + "outputId": "6be94dd1-c711-4bf8-e804-595c76781e72" + }, + "execution_count": 45, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "689" + ] + }, + "metadata": {}, + "execution_count": 45 + } + ] + }, + { + "cell_type": "code", + "source": [ + "add(1,78987)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "u5e-JBSFPxVZ", + "outputId": "ac8a752a-a3da-4569-c53a-923b35e16507" + }, + "execution_count": 46, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "78988" + ] + }, + "metadata": {}, + "execution_count": 46 + } + ] + }, + { + "cell_type": "code", + "source": [ + "#create a function to do square of a number\n", + "def square(a):\n", + " return a*a\n", + " " + ], + "metadata": { + "id": "1qxC2E85P1Dv" + }, + "execution_count": 47, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "square(4)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "Oi6KKdzRQVoS", + "outputId": "45ddc5a4-34b8-4786-f6f9-b0fa8c7d4aae" + }, + "execution_count": 49, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "16" + ] + }, + "metadata": {}, + "execution_count": 49 + } + ] + }, + { + "cell_type": "code", + "source": [ + "#create sum of elements of a given range(1,25)\n", + "\n", + "def sum(a,b):\n", + " result = 0\n", + " for i in range(a,b+1):\n", + " result += i\n", + " \n", + " print('sum of numbers from {} to {} is {}'.format(a,b,result)) " + ], + "metadata": { + "id": "YIgdGXqMQW5w" + }, + "execution_count": 55, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "sum(100,300)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "n5-ek_DhRFP5", + "outputId": "27a812bc-7e08-4b8f-8761-e13cbb33015d" + }, + "execution_count": 56, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "sum of numbers from 100 to 300 is 40200\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "#return multiple value to variables\n", + "def square_cube(num):\n", + " print(num)\n", + " return num*num, num**3\n" + ], + "metadata": { + "id": "siB54wruRKN6" + }, + "execution_count": 59, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "square,cube = square_cube(4)\n", + "print(square,cube)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "o7oN7ayNSZ3a", + "outputId": "5aab1e18-3942-4259-9ca1-96c75d3d306e" + }, + "execution_count": 60, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "4\n", + "16 64\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "#example1 - passing a mutable object(list), func will change the list\n", + "\n", + "\n", + "def changelist(list1):\n", + " list1.append(20)\n", + " list1.append(30)\n", + " print('list inside the function', list1)" + ], + "metadata": { + "id": "1ev-BfEwSfuv" + }, + "execution_count": 61, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "list1 = [12,13,34,45]\n", + "changelist(list1)\n", + "print('list outside the function', list1)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "NREQsAskTUw0", + "outputId": "54e826b5-7951-4420-f951-d63531686051" + }, + "execution_count": 63, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "list inside the function [12, 13, 34, 45, 20, 30]\n", + "list outside the function [12, 13, 34, 45, 20, 30]\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "def change_str(str1):\n", + " str1 = str1 + 'python'\n", + " print('string inside the function', str1)\n" + ], + "metadata": { + "id": "yEICF498TbjJ" + }, + "execution_count": 67, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "str1 = 'hello this is'\n" + ], + "metadata": { + "id": "1SUigejiUgJY" + }, + "execution_count": 68, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "change_str(str1)\n", + "print('string outside the function',str1)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "jsEVkxPmUiKf", + "outputId": "40dcde1d-4ab2-4cdc-e207-870abb8c49d6" + }, + "execution_count": 69, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "string inside the function hello this ispython\n", + "string outside the function hello this is\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "from IPython.core.completer import position_to_cursor\n", + "#types of arguments\n", + "\n", + "#required arguments - exact match of argument with their position in the function call and func definition\n", + "#keyword arguments -\n", + "#default arguments\n", + "#variable length arguments\n" + ], + "metadata": { + "id": "pzR_FeudUp1B" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "#program to calculate simple interest\n", + "\n", + "def simple(p,t,r):\n", + " return (p*t*r)/100" + ], + "metadata": { + "id": "Y2kQeZ9iVMwR" + }, + "execution_count": 71, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "simple(100,2,3) #should be in order always" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "Z_ZV2519VKGX", + "outputId": "92f7dbab-6489-4e1f-f30c-494df38d7606" + }, + "execution_count": 72, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "6.0" + ] + }, + "metadata": {}, + "execution_count": 72 + } + ] + }, + { + "cell_type": "code", + "source": [ + "def details(place,pincode):\n", + " print(\"place={}' and pincode={}\".format(place,pincode))\n" + ], + "metadata": { + "id": "O1wyqjAuWBRt" + }, + "execution_count": 73, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "details('Hyderabad',500082)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "ID1XCA0IW8Op", + "outputId": "a7474029-8fe7-44bc-804e-090b8e4b7312" + }, + "execution_count": 74, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "place=Hyderabad' and pincode=500082\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "details(12344,'Delhi')" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "WxjIxOR0W_PD", + "outputId": "78266685-0c03-4e37-a0ce-723d20e11c28" + }, + "execution_count": 75, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "place=12344' and pincode=Delhi\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "#keyword arguments\n", + "\n", + "details(pincode=12345, place='Delhi') #very useful" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "NIRUPGJ_XDJf", + "outputId": "1eae6e71-413e-4629-f146-8c74843d1ed4" + }, + "execution_count": 76, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "place=Delhi' and pincode=12345\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "#default arguments\n", + "\n", + "def AOC(r, pi=3.14):\n", + " return pi*r*r\n" + ], + "metadata": { + "id": "LGmTqwMbXiZc" + }, + "execution_count": 80, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "AOC(5) #only one parameter can be passed as one is by default" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "oLyBQYZCYBgd", + "outputId": "285d180d-c5cf-432d-9f64-03e3da6e6009" + }, + "execution_count": 81, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "78.5" + ] + }, + "metadata": {}, + "execution_count": 81 + } + ] + }, + { + "cell_type": "code", + "source": [ + "AOC(5,3.144) #overridden" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "Hj9LuqD8YGxK", + "outputId": "1f83636b-9cd3-4182-8f34-ab55632cbf8c" + }, + "execution_count": 82, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "78.60000000000001" + ] + }, + "metadata": {}, + "execution_count": 82 + } + ] + }, + { + "cell_type": "code", + "source": [ + "#variable length argument\n", + "#when we dont know how many arguments should we pass in advance\n", + "#by tuples\n", + "\n", + "def display(*names):\n", + " print('data structure is', type(names))\n", + " print('printing the names')\n", + " for name in names:\n", + " print(name)" + ], + "metadata": { + "id": "5Z7zRnuKYX6y" + }, + "execution_count": 86, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "display('aby','john','sej','gayu','minku','kash','harry')" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "6YC2Th52ZG9I", + "outputId": "2911953f-2222-4347-fa2c-8c9d1e0d2795" + }, + "execution_count": 88, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "data structure is \n", + "printing the names\n", + "aby\n", + "john\n", + "sej\n", + "gayu\n", + "minku\n", + "kash\n", + "harry\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "#multiple keyword arguments\n", + "\n", + "def food(**kw):\n", + " print(kw)" + ], + "metadata": { + "id": "japDce_vZKsm" + }, + "execution_count": 96, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "food(fruits='apple', vege='carot', fastfood='burger')" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "XNGO4208aJAD", + "outputId": "8bda8dd0-4fa9-4ee1-9cf0-3f73cfe49940" + }, + "execution_count": 97, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "{'fruits': 'apple', 'vege': 'carot', 'fastfood': 'burger'}\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "#scope of variable\n", + "a = 10\n", + "def print_a():\n", + " a = 20\n", + " print(a)\n" + ], + "metadata": { + "id": "NlWVK1TBaZij" + }, + "execution_count": 110, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "print_a()" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "sx07gD-8ax0e", + "outputId": "55a73a35-04c9-4aee-fa4d-b755437c1fde" + }, + "execution_count": 111, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "20\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "print(a) #local scope" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "5TYQkSdOay-r", + "outputId": "d2645388-e109-41bf-ab2c-3963ead901ea" + }, + "execution_count": 112, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "10\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "#can function change value of a global variable?\n", + "\n", + "b = 20\n", + "def printb():\n", + " global b \n", + " print(\"global var is\", b)\n", + " b = 30\n", + " print('value inside the function', b)\n", + " " + ], + "metadata": { + "id": "3sZsbXBca6I7" + }, + "execution_count": 114, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "printb()" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "YmZ5ae5Pb_N4", + "outputId": "c835e2f6-59a6-4ac3-c650-994fa2944ae8" + }, + "execution_count": 115, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "global var is 20\n", + "value inside the function 30\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "print(b) #global changes the value!" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "ohV4jMFjcJSL", + "outputId": "211f3737-ecb3-4872-c204-3a5751f2859d" + }, + "execution_count": 117, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "30\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "#modify each element of list by 3\n", + "\n", + "def modify(l):\n", + " return [i*3 for i in l]\n", + "modify([1,2,3,4])" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "kyqMLdLxcXQR", + "outputId": "4bc98126-bf86-48f7-f9ee-a9d3d1b9f35e" + }, + "execution_count": 120, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "[3, 6, 9, 12]" + ] + }, + "metadata": {}, + "execution_count": 120 + } + ] + }, + { + "cell_type": "code", + "source": [ + "#lambda function \n", + "#small anonymous function\n", + "#function which doesn't have any name and can be written inside any code\n", + "#can take any number of arguments\n", + "#but can have only one expression\n", + "\n", + "\n", + "#syntax: lambda arguments:expression\n", + "\n", + "def add(a,b):\n", + " return a+b\n", + "\n", + " #more simple way\n", + "add_lam = lambda a,b:a+b\n", + "add_lam(5,6)\n" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "_sAULgrNiLQq", + "outputId": "271d13f6-f37a-4bfb-a372-0675e7a039fd" + }, + "execution_count": 121, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "11" + ] + }, + "metadata": {}, + "execution_count": 121 + } + ] + }, + { + "cell_type": "code", + "source": [ + "#find a number if even or odd\n", + "find_even = lambda x:x%2==0\n", + "find_even(10)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "0O2_8YhJjS8V", + "outputId": "1c589a73-5055-4022-f317-36313133d232" + }, + "execution_count": 122, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "True" + ] + }, + "metadata": {}, + "execution_count": 122 + } + ] + }, + { + "cell_type": "code", + "source": [ + "#if-else w lambda\n", + "\n", + "Max = lambda a,b:a if (a > b) else b\n" + ], + "metadata": { + "id": "oFgnr4svkDP6" + }, + "execution_count": 123, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "Max(34,67)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "lll8P0jGkPUT", + "outputId": "86903310-a486-433f-de66-6adcb8a1231f" + }, + "execution_count": 124, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "67" + ] + }, + "metadata": {}, + "execution_count": 124 + } + ] + }, + { + "cell_type": "code", + "source": [ + "#example of lambda inside code\n", + "#take one argument from user and that number will be multiplied w some unknown number\n", + "def myfunc(n):\n", + " return lambda a:a*n" + ], + "metadata": { + "id": "QjjIcBUXkQwH" + }, + "execution_count": 125, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "double = myfunc(3)\n" + ], + "metadata": { + "id": "5_t54Gklkw3p" + }, + "execution_count": 130, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "double(10)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "TsiCnbF0k7i9", + "outputId": "3c0de36b-c90b-4375-faf7-17d39b347bfc" + }, + "execution_count": 131, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "30" + ] + }, + "metadata": {}, + "execution_count": 131 + } + ] + }, + { + "cell_type": "code", + "source": [ + "a = myfunc(3)" + ], + "metadata": { + "id": "vF0jYpV6k83D" + }, + "execution_count": 132, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "a(10)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "yDcVbLIEmK7t", + "outputId": "dbcd92e7-1359-4f77-b64d-067466988fe7" + }, + "execution_count": 133, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "30" + ] + }, + "metadata": {}, + "execution_count": 133 + } + ] + }, + { + "cell_type": "code", + "source": [ + "#sort list of tuples using lambda\n", + "\n", + "subject_marks=[('english', 88),('science', 90), ('physics', 70),('social science', 75)]\n", + "print(subject_marks)\n", + "#sort the list\n", + "\n", + "subject_marks.sort(key = lambda x:x[1])\n", + "print(subject_marks)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "Ei9YQ1hRmLmx", + "outputId": "db401a65-18c6-424b-a918-27dfc65d25af" + }, + "execution_count": 135, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "[('english', 88), ('science', 90), ('physics', 70), ('social science', 75)]\n", + "[('physics', 70), ('social science', 75), ('english', 88), ('science', 90)]\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + " #map, filter, reduce\n", + "\n", + "#list of numbers from 300 to 700 with a step of 3\n", + "list1 = list(range(300,700,3))" + ], + "metadata": { + "id": "s4J7dkNvnNJ7" + }, + "execution_count": 136, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "list1\n" + ], + "metadata": { + "id": "toRjnGvypUtv" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "#filter the list with the help of function that check element is even or not\n", + "\n", + "list2 = list(filter(lambda x:x%2==0, list1))" + ], + "metadata": { + "id": "kr-NE5BupWQm" + }, + "execution_count": 138, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "list2" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "m-XYEWdCqNKN", + "outputId": "c554ac21-cbc4-4fe0-d96b-8b2a21e58a46" + }, + "execution_count": 139, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "[300,\n", + " 306,\n", + " 312,\n", + " 318,\n", + " 324,\n", + " 330,\n", + " 336,\n", + " 342,\n", + " 348,\n", + " 354,\n", + " 360,\n", + " 366,\n", + " 372,\n", + " 378,\n", + " 384,\n", + " 390,\n", + " 396,\n", + " 402,\n", + " 408,\n", + " 414,\n", + " 420,\n", + " 426,\n", + " 432,\n", + " 438,\n", + " 444,\n", + " 450,\n", + " 456,\n", + " 462,\n", + " 468,\n", + " 474,\n", + " 480,\n", + " 486,\n", + " 492,\n", + " 498,\n", + " 504,\n", + " 510,\n", + " 516,\n", + " 522,\n", + " 528,\n", + " 534,\n", + " 540,\n", + " 546,\n", + " 552,\n", + " 558,\n", + " 564,\n", + " 570,\n", + " 576,\n", + " 582,\n", + " 588,\n", + " 594,\n", + " 600,\n", + " 606,\n", + " 612,\n", + " 618,\n", + " 624,\n", + " 630,\n", + " 636,\n", + " 642,\n", + " 648,\n", + " 654,\n", + " 660,\n", + " 666,\n", + " 672,\n", + " 678,\n", + " 684,\n", + " 690,\n", + " 696]" + ] + }, + "metadata": {}, + "execution_count": 139 + } + ] + }, + { + "cell_type": "code", + "source": [ + "list2 = list(filter(lambda x:x<10, list1))" + ], + "metadata": { + "id": "YjWsGYPVqN1O" + }, + "execution_count": 142, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "list2" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "-Im19tlFq0RG", + "outputId": "a7a74b43-80b0-45e2-be2c-d01b2465dbab" + }, + "execution_count": 143, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "[]" + ] + }, + "metadata": {}, + "execution_count": 143 + } + ] + }, + { + "cell_type": "code", + "source": [ + "#create a string filter all numbers from string using filter fun\n", + "\n", + "str1 = \"Hello123\"\n", + "num = list(filter(lambda x: True if x in \"1234567890\" else False,str1))\n", + "print(num)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "OUrQbMyZq1qF", + "outputId": "6ec93d7e-a596-4f82-a9e4-0fd06d80f2b8" + }, + "execution_count": 144, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "['1', '2', '3']\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "\n", + "#MAP\n", + "#map() function returns a map object(which is an iterator of the results after applying function to each item) \n", + "\n", + "list1\n", + "len(list1)\n", + "#subtract all value of list w 300\n", + "list2=list(map(lambda x:x-300,list1))\n" + ], + "metadata": { + "id": "zkjrsPIwshj3" + }, + "execution_count": 151, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "list2" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "iA6v-a2_tvg6", + "outputId": "37bf1bec-6f6a-453d-a755-ac5cc24efbb4" + }, + "execution_count": 152, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "[0,\n", + " 3,\n", + " 6,\n", + " 9,\n", + " 12,\n", + " 15,\n", + " 18,\n", + " 21,\n", + " 24,\n", + " 27,\n", + " 30,\n", + " 33,\n", + " 36,\n", + " 39,\n", + " 42,\n", + " 45,\n", + " 48,\n", + " 51,\n", + " 54,\n", + " 57,\n", + " 60,\n", + " 63,\n", + " 66,\n", + " 69,\n", + " 72,\n", + " 75,\n", + " 78,\n", + " 81,\n", + " 84,\n", + " 87,\n", + " 90,\n", + " 93,\n", + " 96,\n", + " 99,\n", + " 102,\n", + " 105,\n", + " 108,\n", + " 111,\n", + " 114,\n", + " 117,\n", + " 120,\n", + " 123,\n", + " 126,\n", + " 129,\n", + " 132,\n", + " 135,\n", + " 138,\n", + " 141,\n", + " 144,\n", + " 147,\n", + " 150,\n", + " 153,\n", + " 156,\n", + " 159,\n", + " 162,\n", + " 165,\n", + " 168,\n", + " 171,\n", + " 174,\n", + " 177,\n", + " 180,\n", + " 183,\n", + " 186,\n", + " 189,\n", + " 192,\n", + " 195,\n", + " 198,\n", + " 201,\n", + " 204,\n", + " 207,\n", + " 210,\n", + " 213,\n", + " 216,\n", + " 219,\n", + " 222,\n", + " 225,\n", + " 228,\n", + " 231,\n", + " 234,\n", + " 237,\n", + " 240,\n", + " 243,\n", + " 246,\n", + " 249,\n", + " 252,\n", + " 255,\n", + " 258,\n", + " 261,\n", + " 264,\n", + " 267,\n", + " 270,\n", + " 273,\n", + " 276,\n", + " 279,\n", + " 282,\n", + " 285,\n", + " 288,\n", + " 291,\n", + " 294,\n", + " 297,\n", + " 300,\n", + " 303,\n", + " 306,\n", + " 309,\n", + " 312,\n", + " 315,\n", + " 318,\n", + " 321,\n", + " 324,\n", + " 327,\n", + " 330,\n", + " 333,\n", + " 336,\n", + " 339,\n", + " 342,\n", + " 345,\n", + " 348,\n", + " 351,\n", + " 354,\n", + " 357,\n", + " 360,\n", + " 363,\n", + " 366,\n", + " 369,\n", + " 372,\n", + " 375,\n", + " 378,\n", + " 381,\n", + " 384,\n", + " 387,\n", + " 390,\n", + " 393,\n", + " 396,\n", + " 399]" + ] + }, + "metadata": {}, + "execution_count": 152 + } + ] + }, + { + "cell_type": "code", + "source": [ + "len(list1)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "hOZyhclDt0uN", + "outputId": "a6570279-2811-400d-ff96-54c295e0f7cb" + }, + "execution_count": 153, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "134" + ] + }, + "metadata": {}, + "execution_count": 153 + } + ] + }, + { + "cell_type": "code", + "source": [ + "len(list2)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "GVUQt-BwuESh", + "outputId": "1a67b181-6554-4795-8e87-877bc80cbcad" + }, + "execution_count": 154, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "134" + ] + }, + "metadata": {}, + "execution_count": 154 + } + ] + }, + { + "cell_type": "code", + "source": [ + "#square all elements of list using map function\n", + "\n", + "listsquare = list(map(lambda x:x*x, list1))" + ], + "metadata": { + "id": "tDTQJUKduFvS" + }, + "execution_count": 157, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "listsquare" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "gn9hVnHrunqF", + "outputId": "1d6c8c03-7443-4ca8-b90d-7e46cfe47547" + }, + "execution_count": 158, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "[90000,\n", + " 91809,\n", + " 93636,\n", + " 95481,\n", + " 97344,\n", + " 99225,\n", + " 101124,\n", + " 103041,\n", + " 104976,\n", + " 106929,\n", + " 108900,\n", + " 110889,\n", + " 112896,\n", + " 114921,\n", + " 116964,\n", + " 119025,\n", + " 121104,\n", + " 123201,\n", + " 125316,\n", + " 127449,\n", + " 129600,\n", + " 131769,\n", + " 133956,\n", + " 136161,\n", + " 138384,\n", + " 140625,\n", + " 142884,\n", + " 145161,\n", + " 147456,\n", + " 149769,\n", + " 152100,\n", + " 154449,\n", + " 156816,\n", + " 159201,\n", + " 161604,\n", + " 164025,\n", + " 166464,\n", + " 168921,\n", + " 171396,\n", + " 173889,\n", + " 176400,\n", + " 178929,\n", + " 181476,\n", + " 184041,\n", + " 186624,\n", + " 189225,\n", + " 191844,\n", + " 194481,\n", + " 197136,\n", + " 199809,\n", + " 202500,\n", + " 205209,\n", + " 207936,\n", + " 210681,\n", + " 213444,\n", + " 216225,\n", + " 219024,\n", + " 221841,\n", + " 224676,\n", + " 227529,\n", + " 230400,\n", + " 233289,\n", + " 236196,\n", + " 239121,\n", + " 242064,\n", + " 245025,\n", + " 248004,\n", + " 251001,\n", + " 254016,\n", + " 257049,\n", + " 260100,\n", + " 263169,\n", + " 266256,\n", + " 269361,\n", + " 272484,\n", + " 275625,\n", + " 278784,\n", + " 281961,\n", + " 285156,\n", + " 288369,\n", + " 291600,\n", + " 294849,\n", + " 298116,\n", + " 301401,\n", + " 304704,\n", + " 308025,\n", + " 311364,\n", + " 314721,\n", + " 318096,\n", + " 321489,\n", + " 324900,\n", + " 328329,\n", + " 331776,\n", + " 335241,\n", + " 338724,\n", + " 342225,\n", + " 345744,\n", + " 349281,\n", + " 352836,\n", + " 356409,\n", + " 360000,\n", + " 363609,\n", + " 367236,\n", + " 370881,\n", + " 374544,\n", + " 378225,\n", + " 381924,\n", + " 385641,\n", + " 389376,\n", + " 393129,\n", + " 396900,\n", + " 400689,\n", + " 404496,\n", + " 408321,\n", + " 412164,\n", + " 416025,\n", + " 419904,\n", + " 423801,\n", + " 427716,\n", + " 431649,\n", + " 435600,\n", + " 439569,\n", + " 443556,\n", + " 447561,\n", + " 451584,\n", + " 455625,\n", + " 459684,\n", + " 463761,\n", + " 467856,\n", + " 471969,\n", + " 476100,\n", + " 480249,\n", + " 484416,\n", + " 488601]" + ] + }, + "metadata": {}, + "execution_count": 158 + } + ] + }, + { + "cell_type": "code", + "source": [ + "#add 2 lists using map function\n", + "\n", + "l1 = [1,2,3]\n", + "l2 = [2,3,4]\n", + "l3 = list(map(lambda x,y:x+y, l1,l2))\n", + "l3" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "9TKV2Ug0uoao", + "outputId": "1f3dc839-0a9b-4d5a-efb7-d287182b1ef2" + }, + "execution_count": 160, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "[3, 5, 7]" + ] + }, + "metadata": {}, + "execution_count": 160 + } + ] + }, + { + "cell_type": "code", + "source": [ + "fruit = ['Apple','banana','pear','orange','Apricot']\n", + "#filter list which words start with A\n", + "\n", + "l1 = list(filter(lambda x:x[0]=='A', fruit))\n", + "l1" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "bP-1bQp7viqg", + "outputId": "0f09d5fa-d355-4946-c2c4-64654df02c83" + }, + "execution_count": 163, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "['Apple', 'Apricot']" + ] + }, + "metadata": {}, + "execution_count": 163 + } + ] + }, + { + "cell_type": "code", + "source": [ + "#reduce\n", + "#aggregation of tasks\n", + "\n", + "import functools\n", + "from functools import reduce\n", + "\n", + "list1\n" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "KtI6XG4Qv9Qm", + "outputId": "32bbc24b-fa7c-44fa-aaea-379367add664" + }, + "execution_count": 167, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "[300,\n", + " 303,\n", + " 306,\n", + " 309,\n", + " 312,\n", + " 315,\n", + " 318,\n", + " 321,\n", + " 324,\n", + " 327,\n", + " 330,\n", + " 333,\n", + " 336,\n", + " 339,\n", + " 342,\n", + " 345,\n", + " 348,\n", + " 351,\n", + " 354,\n", + " 357,\n", + " 360,\n", + " 363,\n", + " 366,\n", + " 369,\n", + " 372,\n", + " 375,\n", + " 378,\n", + " 381,\n", + " 384,\n", + " 387,\n", + " 390,\n", + " 393,\n", + " 396,\n", + " 399,\n", + " 402,\n", + " 405,\n", + " 408,\n", + " 411,\n", + " 414,\n", + " 417,\n", + " 420,\n", + " 423,\n", + " 426,\n", + " 429,\n", + " 432,\n", + " 435,\n", + " 438,\n", + " 441,\n", + " 444,\n", + " 447,\n", + " 450,\n", + " 453,\n", + " 456,\n", + " 459,\n", + " 462,\n", + " 465,\n", + " 468,\n", + " 471,\n", + " 474,\n", + " 477,\n", + " 480,\n", + " 483,\n", + " 486,\n", + " 489,\n", + " 492,\n", + " 495,\n", + " 498,\n", + " 501,\n", + " 504,\n", + " 507,\n", + " 510,\n", + " 513,\n", + " 516,\n", + " 519,\n", + " 522,\n", + " 525,\n", + " 528,\n", + " 531,\n", + " 534,\n", + " 537,\n", + " 540,\n", + " 543,\n", + " 546,\n", + " 549,\n", + " 552,\n", + " 555,\n", + " 558,\n", + " 561,\n", + " 564,\n", + " 567,\n", + " 570,\n", + " 573,\n", + " 576,\n", + " 579,\n", + " 582,\n", + " 585,\n", + " 588,\n", + " 591,\n", + " 594,\n", + " 597,\n", + " 600,\n", + " 603,\n", + " 606,\n", + " 609,\n", + " 612,\n", + " 615,\n", + " 618,\n", + " 621,\n", + " 624,\n", + " 627,\n", + " 630,\n", + " 633,\n", + " 636,\n", + " 639,\n", + " 642,\n", + " 645,\n", + " 648,\n", + " 651,\n", + " 654,\n", + " 657,\n", + " 660,\n", + " 663,\n", + " 666,\n", + " 669,\n", + " 672,\n", + " 675,\n", + " 678,\n", + " 681,\n", + " 684,\n", + " 687,\n", + " 690,\n", + " 693,\n", + " 696,\n", + " 699]" + ] + }, + "metadata": {}, + "execution_count": 167 + } + ] + }, + { + "cell_type": "code", + "source": [ + "reduce(lambda x,y:x+y,list1 )\n" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "rX4_7Nc-xI5p", + "outputId": "9d4d0052-8756-46a9-c0be-b1b9b0c46e0f" + }, + "execution_count": 166, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "66933" + ] + }, + "metadata": {}, + "execution_count": 166 + } + ] + }, + { + "cell_type": "code", + "source": [ + "#find greatest number in a list\n", + "\n", + "list1 = [70,110,200,400,34]\n", + "reduce(lambda x,y:x if x>y else y, list1)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "smxY8dvDxsVp", + "outputId": "4ab6b01a-7cf1-4a20-8502-cff24256874b" + }, + "execution_count": 168, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "400" + ] + }, + "metadata": {}, + "execution_count": 168 + } + ] + }, + { + "cell_type": "code", + "source": [ + "red = lambda x,y:x if x>y else y\n", + "reduce(red,list1)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "TLzxRyL9yVIm", + "outputId": "5bca4ebb-b9aa-4280-df81-4434f419ae3a" + }, + "execution_count": 169, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "400" + ] + }, + "metadata": {}, + "execution_count": 169 + } + ] + }, + { + "cell_type": "code", + "source": [ + "help(reduce)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "KlA39LL1ywEa", + "outputId": "67f3ff07-8e80-42f5-9aee-ce13d5ad5a74" + }, + "execution_count": 170, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Help on built-in function reduce in module _functools:\n", + "\n", + "reduce(...)\n", + " reduce(function, sequence[, initial]) -> value\n", + " \n", + " Apply a function of two arguments cumulatively to the items of a sequence,\n", + " from left to right, so as to reduce the sequence to a single value.\n", + " For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates\n", + " ((((1+2)+3)+4)+5). If initial is present, it is placed before the items\n", + " of the sequence in the calculation, and serves as a default when the\n", + " sequence is empty.\n", + "\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [], + "metadata": { + "id": "O9ltR8yyzYXf" + }, + "execution_count": null, + "outputs": [] + } + ] +} \ No newline at end of file