├── README.md └── General Python Programming.md ├── README.md ├── txt doc.txt ├── 2_PYTHON_overflow.ipynb ├── 1_PYTHON_introduction.ipynb ├── File_IO.ipynb ├── 3_PYTHON_first_program_&_Analysis.py ├── Understanding_if_elif_else_loops.ipynb ├── 8_PYTHON_Number_Data_Type.ipynb ├── 5_PYTHON_Comments.ipynb ├── strings.ipynb ├── 4_PYTHON_Syntax.ipynb ├── List_and_tuples.ipynb ├── Functions_in_python.ipynb ├── 7_PYTHON_Data_Types.ipynb ├── Dictionary_and_sets.ipynb ├── 9_PYTHON_String_Data_Type.ipynb └── 6_PYTHON_Variables.ipynb /README.md: -------------------------------------------------------------------------------- 1 | # Python-Programming 2 | -------------------------------------------------------------------------------- /General Python Programming.md/README.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /General Python Programming.md/txt doc.txt: -------------------------------------------------------------------------------- 1 | I am learning Python programming language -------------------------------------------------------------------------------- /General Python Programming.md/2_PYTHON_overflow.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "provenance": [] 7 | }, 8 | "kernelspec": { 9 | "name": "python3", 10 | "display_name": "Python 3" 11 | }, 12 | "language_info": { 13 | "name": "python" 14 | } 15 | }, 16 | "cells": [ 17 | { 18 | "cell_type": "markdown", 19 | "source": [ 20 | "##-PYTHON\n", 21 | "\n", 22 | "\n", 23 | "* Syntax\n", 24 | "* Variables\n", 25 | "\n", 26 | "##-DATA TYPES\n", 27 | "\n", 28 | "\n", 29 | "* String\n", 30 | "* Numbers\n", 31 | "* List\n", 32 | "* Tuples\n", 33 | "* Set\n", 34 | "* Bool\n", 35 | "\n", 36 | "##-CONTROL FLOW\n", 37 | "\n", 38 | "* Condition\n", 39 | "* Loops\n", 40 | "\n", 41 | "##-FUNCTIONS\n", 42 | "* Generators\n", 43 | "* Iterators\n", 44 | "\n", 45 | "##-CLASSES\n", 46 | "\n", 47 | "##-OOP\n", 48 | "* Inheritence\n", 49 | "* Abstraction\n", 50 | "\n", 51 | "\n", 52 | "##-DECORATOR\n", 53 | "\n", 54 | "##-EXCEPTIONS\n", 55 | "\n", 56 | "##-TESTING\n", 57 | "\n", 58 | "##-MODULES\n", 59 | "* Data Analysis\n", 60 | "* Machine learning\n", 61 | "* Artificial Intellegence\n", 62 | "* Web Development\n", 63 | "* Back-End Development\n", 64 | "#####And much more\n", 65 | "\n", 66 | "\n" 67 | ], 68 | "metadata": { 69 | "id": "WmZFfw-sjB42" 70 | } 71 | }, 72 | { 73 | "cell_type": "code", 74 | "source": [], 75 | "metadata": { 76 | "id": "5WMV_osSxV8H" 77 | }, 78 | "execution_count": null, 79 | "outputs": [] 80 | } 81 | ] 82 | } 83 | -------------------------------------------------------------------------------- /General Python Programming.md/1_PYTHON_introduction.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "provenance": [] 7 | }, 8 | "kernelspec": { 9 | "name": "python3", 10 | "display_name": "Python 3" 11 | }, 12 | "language_info": { 13 | "name": "python" 14 | } 15 | }, 16 | "cells": [ 17 | { 18 | "cell_type": "markdown", 19 | "source": [ 20 | "### Python Programming\n" 21 | ], 22 | "metadata": { 23 | "id": "BTJHiFd2OIXV" 24 | } 25 | }, 26 | { 27 | "cell_type": "markdown", 28 | "source": [ 29 | "PROGRAMMING: Computer programming is the process of performing a particular computation, usually by designing and building an executable computer program. Programming is a way of communication between humans and machines.\n", 30 | "And the language used for programming in known as programming language\n", 31 | "\n", 32 | "\n" 33 | ], 34 | "metadata": { 35 | "id": "bfAq19HSOPnD" 36 | } 37 | }, 38 | { 39 | "cell_type": "markdown", 40 | "source": [ 41 | "\n", 42 | "PYTHON: Python is a high-level, general-purpose programming language. Its design philosophy emphasizes code readability with the use of significant indentationPython was conceived in the late 1980s by Guido van Rossum.\n", 43 | "\n" 44 | ], 45 | "metadata": { 46 | "id": "fFtN6chJzv1x" 47 | } 48 | }, 49 | { 50 | "cell_type": "markdown", 51 | "source": [ 52 | "\n", 53 | "What can you do with python?\n", 54 | "* Data analysis and machine learning\n", 55 | "* Web development\n", 56 | "* Automation or scripting\n", 57 | "* Software testing and prototyping\n", 58 | "* Everyday tasks" 59 | ], 60 | "metadata": { 61 | "id": "7tlxSLHgzy0x" 62 | } 63 | }, 64 | { 65 | "cell_type": "markdown", 66 | "source": [ 67 | "## PYTHON INSTALLATION\n" 68 | ], 69 | "metadata": { 70 | "id": "rnYLSSzaz1kK" 71 | } 72 | }, 73 | { 74 | "cell_type": "markdown", 75 | "source": [ 76 | "Python can be installed from the official website of python organization that is [Python.org](https://www.python.org/) or you can also use [Download Python](https://www.python.org/downloads/) to directly go to the download page, select the latest version and download it." 77 | ], 78 | "metadata": { 79 | "id": "n3zVxBN1z8wK" 80 | } 81 | } 82 | ] 83 | } 84 | -------------------------------------------------------------------------------- /General Python Programming.md/File_IO.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "provenance": [] 7 | }, 8 | "kernelspec": { 9 | "name": "python3", 10 | "display_name": "Python 3" 11 | }, 12 | "language_info": { 13 | "name": "python" 14 | } 15 | }, 16 | "cells": [ 17 | { 18 | "cell_type": "code", 19 | "source": [ 20 | "f= open(\"/content/txt doc.txt\", \"r\")\n", 21 | "data = f.read()\n", 22 | "print(data)\n", 23 | "print(type(data))\n", 24 | "f.close()" 25 | ], 26 | "metadata": { 27 | "colab": { 28 | "base_uri": "https://localhost:8080/" 29 | }, 30 | "id": "ltdUWwNBfEt4", 31 | "outputId": "a9e6291e-bedb-477e-f057-7e77e342e5c8" 32 | }, 33 | "execution_count": null, 34 | "outputs": [ 35 | { 36 | "output_type": "stream", 37 | "name": "stdout", 38 | "text": [ 39 | "I am learning Python programming language\n", 40 | "\n" 41 | ] 42 | } 43 | ] 44 | }, 45 | { 46 | "cell_type": "code", 47 | "source": [ 48 | "f= open(\"/content/txt doc.txt\", \"r\")\n", 49 | "data=f.read()\n", 50 | "print(data)\n", 51 | "line1 = f.readline\n", 52 | "print(line1)" 53 | ], 54 | "metadata": { 55 | "colab": { 56 | "base_uri": "https://localhost:8080/" 57 | }, 58 | "id": "bnppwxA9gDVe", 59 | "outputId": "a16717da-d7a3-4810-919a-d7bf7c02f570" 60 | }, 61 | "execution_count": null, 62 | "outputs": [ 63 | { 64 | "output_type": "stream", 65 | "name": "stdout", 66 | "text": [ 67 | "I am learning Python programming language\n", 68 | "\n" 69 | ] 70 | } 71 | ] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "source": [ 76 | "#Write data\n", 77 | "f = open(\"/content/txt doc.txt\", \"w\")\n", 78 | "f.write(\"I want to learn Data Science from tomorrow 123\")\n", 79 | "f.close()" 80 | ], 81 | "metadata": { 82 | "id": "EbknjvkNhio3" 83 | }, 84 | "execution_count": null, 85 | "outputs": [] 86 | }, 87 | { 88 | "cell_type": "code", 89 | "source": [ 90 | "#append data\n", 91 | "f = open(\"/content/txt doc.txt\", \"a\")\n", 92 | "f.write(\"then I'll move to big data\")\n", 93 | "f.close()" 94 | ], 95 | "metadata": { 96 | "id": "vDh5x4oTilxV" 97 | }, 98 | "execution_count": null, 99 | "outputs": [] 100 | }, 101 | { 102 | "cell_type": "code", 103 | "source": [ 104 | "#giving next line character\n", 105 | "f = open(\"/content/txt doc.txt\", \"a\")\n", 106 | "f.write(\"\\n after that data ware house\")\n", 107 | "f.close()" 108 | ], 109 | "metadata": { 110 | "id": "GtF8IRc4jDhc" 111 | }, 112 | "execution_count": null, 113 | "outputs": [] 114 | }, 115 | { 116 | "cell_type": "code", 117 | "source": [ 118 | "with open(\"txt doc.txt\", \"r\") as f:\n", 119 | " data = f.read()\n", 120 | " print(data)" 121 | ], 122 | "metadata": { 123 | "id": "PqUwN1YCg7Sb" 124 | }, 125 | "execution_count": null, 126 | "outputs": [] 127 | }, 128 | { 129 | "cell_type": "code", 130 | "source": [ 131 | "import os\n", 132 | "os.remove(txt doc)" 133 | ], 134 | "metadata": { 135 | "id": "VXnNz02VhHWn" 136 | }, 137 | "execution_count": null, 138 | "outputs": [] 139 | } 140 | ] 141 | } -------------------------------------------------------------------------------- /General Python Programming.md/3_PYTHON_first_program_&_Analysis.py: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "provenance": [] 7 | }, 8 | "kernelspec": { 9 | "name": "python3", 10 | "display_name": "Python 3" 11 | }, 12 | "language_info": { 13 | "name": "python" 14 | } 15 | }, 16 | "cells": [ 17 | { 18 | "cell_type": "markdown", 19 | "source": [ 20 | "##First Python program\n", 21 | "#####There is a common practice of printing \"Hellow world\" whenever someone starts his/her first programming language, no matter what ever language it is, JAVA, Python or C++.\n", 22 | "printing \"Hello world\" is the easiest task in python programming out of all other languages. so let us do it." 23 | ], 24 | "metadata": { 25 | "id": "AHGOFlKPUNcR" 26 | } 27 | }, 28 | { 29 | "cell_type": "code", 30 | "source": [ 31 | "print (\"Hello World\")" 32 | ], 33 | "metadata": { 34 | "colab": { 35 | "base_uri": "https://localhost:8080/" 36 | }, 37 | "id": "8in8UbJSbq3z", 38 | "outputId": "e756edb1-849b-41f9-bd10-9851888fe46a" 39 | }, 40 | "execution_count": 3, 41 | "outputs": [ 42 | { 43 | "output_type": "stream", 44 | "name": "stdout", 45 | "text": [ 46 | "Hello World\n" 47 | ] 48 | } 49 | ] 50 | }, 51 | { 52 | "cell_type": "markdown", 53 | "source": [ 54 | "####Analsis of above code:\n", 55 | "In the above code, print(\"Hello world\"), things we are understanding is that, \n", 56 | "\n", 57 | "#####-In python programming, to print anything you have to use the command print with parentheses in this way: print(). And insinde the parentheses we have to write the command that we need to print.\n", 58 | "#####-Python is a case sensetive language therefor \"print\" , \"Print\" and \"PRINT\" are all three different words for python and hence writing \"Print\" in place of \"print\" will give an error to us. We have to always use lowercase letters to write the print statement\n", 59 | "#####-After writing print, inside the parentheses we can withe the things we want to print. In python, if we want to print a statement as it is, we have to use ellipsis(\"\"), this will print the lines as it is inside the ellipsis \n" 60 | ], 61 | "metadata": { 62 | "id": "DM1rMQ-IcRox" 63 | } 64 | }, 65 | { 66 | "cell_type": "code", 67 | "source": [ 68 | "print(Hello world)" 69 | ], 70 | "metadata": { 71 | "colab": { 72 | "base_uri": "https://localhost:8080/", 73 | "height": 134 74 | }, 75 | "id": "FfKnqG4TcUG1", 76 | "outputId": "b5d62a89-1c6d-4a88-a02e-7df6092efcbe" 77 | }, 78 | "execution_count": 4, 79 | "outputs": [ 80 | { 81 | "output_type": "error", 82 | "ename": "SyntaxError", 83 | "evalue": "ignored", 84 | "traceback": [ 85 | "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m print(Hello world)\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" 86 | ] 87 | } 88 | ] 89 | }, 90 | { 91 | "cell_type": "markdown", 92 | "source": [ 93 | "#####As elipsis is not used here, python is taking hello worls as any of his inbuilt code but cannot find any such code inside it, and shows error." 94 | ], 95 | "metadata": { 96 | "id": "_hL5MuH6fDhN" 97 | } 98 | }, 99 | { 100 | "cell_type": "code", 101 | "source": [ 102 | "print(5+10)" 103 | ], 104 | "metadata": { 105 | "colab": { 106 | "base_uri": "https://localhost:8080/" 107 | }, 108 | "id": "ABQ-uuBAfuhy", 109 | "outputId": "dcb89cac-e0c3-45b4-c6a9-a3469f152bc2" 110 | }, 111 | "execution_count": 5, 112 | "outputs": [ 113 | { 114 | "output_type": "stream", 115 | "name": "stdout", 116 | "text": [ 117 | "15\n" 118 | ] 119 | } 120 | ] 121 | }, 122 | { 123 | "cell_type": "markdown", 124 | "source": [ 125 | "You can also use print function to print arithmetic calculations of numbers as shown above." 126 | ], 127 | "metadata": { 128 | "id": "wZ9-OMLMf0Id" 129 | } 130 | }, 131 | { 132 | "cell_type": "code", 133 | "source": [ 134 | "print(\"Substracting 5 from 10 will give: \" , 10-5)" 135 | ], 136 | "metadata": { 137 | "colab": { 138 | "base_uri": "https://localhost:8080/" 139 | }, 140 | "id": "YCxd3hlBgHU4", 141 | "outputId": "fdfe99f2-90a3-4bea-d99f-be0fc0001404" 142 | }, 143 | "execution_count": 6, 144 | "outputs": [ 145 | { 146 | "output_type": "stream", 147 | "name": "stdout", 148 | "text": [ 149 | "Substracting 5 from 10 will give: 5\n" 150 | ] 151 | } 152 | ] 153 | }, 154 | { 155 | "cell_type": "markdown", 156 | "source": [ 157 | "I can use both elipsis and arithmetic calculations to make my output more understandable.\n", 158 | "I have seperated both of the outputs using a comma(,) " 159 | ], 160 | "metadata": { 161 | "id": "vdDr2bpjgbiM" 162 | } 163 | } 164 | ] 165 | } 166 | -------------------------------------------------------------------------------- /General Python Programming.md/Understanding_if_elif_else_loops.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "provenance": [] 7 | }, 8 | "kernelspec": { 9 | "name": "python3", 10 | "display_name": "Python 3" 11 | }, 12 | "language_info": { 13 | "name": "python" 14 | } 15 | }, 16 | "cells": [ 17 | { 18 | "cell_type": "code", 19 | "execution_count": 4, 20 | "metadata": { 21 | "id": "-IHwlN0cZckX", 22 | "colab": { 23 | "base_uri": "https://localhost:8080/" 24 | }, 25 | "outputId": "a0700b09-8bfc-432a-f1ae-30e114939a41" 26 | }, 27 | "outputs": [ 28 | { 29 | "output_type": "stream", 30 | "name": "stdout", 31 | "text": [ 32 | "light: black\n", 33 | "light is broken\n" 34 | ] 35 | } 36 | ], 37 | "source": [ 38 | "light=input(\"light: \")\n", 39 | "if(light==\"red\"):\n", 40 | " print(\"stop\")\n", 41 | "elif(light==\"yellow\"):\n", 42 | " print(\"ready\")\n", 43 | "elif(light==\"green\"):\n", 44 | " print(\"go\")\n", 45 | "else:\n", 46 | " print(\"light is broken\")" 47 | ] 48 | }, 49 | { 50 | "cell_type": "code", 51 | "source": [ 52 | "marks=int(input(\"marks: \"))\n", 53 | "if(marks >= 90):\n", 54 | " print(\"A\")\n", 55 | "elif(marks >= 80 and marks <90):\n", 56 | " print(\"B\")\n", 57 | "elif(marks >= 70 and marks <80):\n", 58 | " print(\"C\")\n", 59 | "else:\n", 60 | " print(\"D\")" 61 | ], 62 | "metadata": { 63 | "colab": { 64 | "base_uri": "https://localhost:8080/" 65 | }, 66 | "id": "nRFHyolNYZh8", 67 | "outputId": "108cb953-5f9e-44a1-c810-eacd2fc8931a" 68 | }, 69 | "execution_count": 9, 70 | "outputs": [ 71 | { 72 | "output_type": "stream", 73 | "name": "stdout", 74 | "text": [ 75 | "marks: 12\n", 76 | "D\n" 77 | ] 78 | } 79 | ] 80 | }, 81 | { 82 | "cell_type": "code", 83 | "source": [ 84 | "A = int(input(\"A: \"))\n", 85 | "G = input(\"M/F: \")\n", 86 | "if((A==1 or A==2) and G==\"M\"):\n", 87 | " print(\"Fee is 100\")\n", 88 | "elif(A==3 or A==4 or G==\"F\"):\n", 89 | " print(\"Fee is 200\")\n", 90 | "elif(A==5 and G==\"M\"):\n", 91 | " print(\"Fee is 300\")\n", 92 | "else:\n", 93 | " print(\"No Fee\")" 94 | ], 95 | "metadata": { 96 | "colab": { 97 | "base_uri": "https://localhost:8080/" 98 | }, 99 | "id": "wDUy43o3ZJh_", 100 | "outputId": "310c6927-8164-47ba-a956-01952468f9dd" 101 | }, 102 | "execution_count": 13, 103 | "outputs": [ 104 | { 105 | "output_type": "stream", 106 | "name": "stdout", 107 | "text": [ 108 | "A: 1\n", 109 | "M/F: m\n", 110 | "No Fee\n" 111 | ] 112 | } 113 | ] 114 | }, 115 | { 116 | "cell_type": "code", 117 | "source": [ 118 | "food=input(\"food: \")\n", 119 | "eat=\"Yes\" if food == \"cake\" else \"no\"\n", 120 | "print(eat)" 121 | ], 122 | "metadata": { 123 | "colab": { 124 | "base_uri": "https://localhost:8080/" 125 | }, 126 | "id": "MRqFZMzOavwq", 127 | "outputId": "88b6f823-3639-4383-b8f1-9bf8ee5739b4" 128 | }, 129 | "execution_count": 15, 130 | "outputs": [ 131 | { 132 | "output_type": "stream", 133 | "name": "stdout", 134 | "text": [ 135 | "food: cake\n", 136 | "Yes\n" 137 | ] 138 | } 139 | ] 140 | }, 141 | { 142 | "cell_type": "code", 143 | "source": [ 144 | "food=input(\"food: \")\n", 145 | "print(\"sweet\") if food==\"cake\" or food==\"jalebi\" else print(\"not sweet\")" 146 | ], 147 | "metadata": { 148 | "colab": { 149 | "base_uri": "https://localhost:8080/" 150 | }, 151 | "id": "CDFIVhEcb3i8", 152 | "outputId": "ca7d6df7-dfe4-442b-a9f0-3049fa7ce6a2" 153 | }, 154 | "execution_count": 16, 155 | "outputs": [ 156 | { 157 | "output_type": "stream", 158 | "name": "stdout", 159 | "text": [ 160 | "food: jalebi\n", 161 | "sweet\n" 162 | ] 163 | } 164 | ] 165 | }, 166 | { 167 | "cell_type": "code", 168 | "source": [ 169 | "age=int(input(\"age: \"))\n", 170 | "vote = (\"no\", \"yes\") [age>=18]\n", 171 | "print (vote)" 172 | ], 173 | "metadata": { 174 | "colab": { 175 | "base_uri": "https://localhost:8080/" 176 | }, 177 | "id": "GtSvzm_bcONS", 178 | "outputId": "31043787-8f7a-445d-adfe-25e016dca947" 179 | }, 180 | "execution_count": 20, 181 | "outputs": [ 182 | { 183 | "output_type": "stream", 184 | "name": "stdout", 185 | "text": [ 186 | "age: 18\n", 187 | "yes\n" 188 | ] 189 | } 190 | ] 191 | }, 192 | { 193 | "cell_type": "code", 194 | "source": [ 195 | "sal=float(input(\"salary: \"))\n", 196 | "tax=sal*(0.1,0.2) [sal>50000]\n", 197 | "print(tax)" 198 | ], 199 | "metadata": { 200 | "colab": { 201 | "base_uri": "https://localhost:8080/" 202 | }, 203 | "id": "QxKrWQCbcliM", 204 | "outputId": "9ecd6ab8-3ccd-4d19-c49e-c7aeeff75b7e" 205 | }, 206 | "execution_count": 21, 207 | "outputs": [ 208 | { 209 | "output_type": "stream", 210 | "name": "stdout", 211 | "text": [ 212 | "salary: 34004\n", 213 | "3400.4\n" 214 | ] 215 | } 216 | ] 217 | }, 218 | { 219 | "cell_type": "code", 220 | "source": [], 221 | "metadata": { 222 | "id": "uQjy8ZkadLxJ" 223 | }, 224 | "execution_count": null, 225 | "outputs": [] 226 | } 227 | ] 228 | } -------------------------------------------------------------------------------- /General Python Programming.md/8_PYTHON_Number_Data_Type.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "provenance": [] 7 | }, 8 | "kernelspec": { 9 | "name": "python3", 10 | "display_name": "Python 3" 11 | }, 12 | "language_info": { 13 | "name": "python" 14 | } 15 | }, 16 | "cells": [ 17 | { 18 | "cell_type": "markdown", 19 | "source": [ 20 | "##NUMBERS in PYTHON\n", 21 | "There are three numerical data types in python:\n", 22 | "* int\n", 23 | "* float\n", 24 | "* complex" 25 | ], 26 | "metadata": { 27 | "id": "I8ASaK9o1uu8" 28 | } 29 | }, 30 | { 31 | "cell_type": "code", 32 | "source": [ 33 | "#EXAMPLE\n", 34 | "x = 12 #int\n", 35 | "y = 12.5 #Float\n", 36 | "z = 1j #Complex\n", 37 | "print(type(x))\n", 38 | "print(type(y))\n", 39 | "print(type(z))" 40 | ], 41 | "metadata": { 42 | "colab": { 43 | "base_uri": "https://localhost:8080/" 44 | }, 45 | "id": "zfxRbVWm1x0y", 46 | "outputId": "c11baf97-ec0c-465b-877c-b06968e0e9d5" 47 | }, 48 | "execution_count": null, 49 | "outputs": [ 50 | { 51 | "output_type": "stream", 52 | "name": "stdout", 53 | "text": [ 54 | "\n", 55 | "\n", 56 | "\n" 57 | ] 58 | } 59 | ] 60 | }, 61 | { 62 | "cell_type": "markdown", 63 | "source": [ 64 | "##INT or INTEGER DATA TYPE\n", 65 | "In Python, integers are zero, positive or negative whole numbers without a fractional part and having unlimited precision, e.g. 0, 100, -10." 66 | ], 67 | "metadata": { 68 | "id": "5C83LwQu2fOz" 69 | } 70 | }, 71 | { 72 | "cell_type": "code", 73 | "source": [ 74 | "#INT EXAMPLES\n", 75 | "x = 8\n", 76 | "y = 12345678907532223929859357\n", 77 | "z = -23445667\n", 78 | "\n", 79 | "print(type(x))\n", 80 | "print(type(y))\n", 81 | "print(type(z))" 82 | ], 83 | "metadata": { 84 | "id": "Lc_L-6IB2ixa", 85 | "colab": { 86 | "base_uri": "https://localhost:8080/" 87 | }, 88 | "outputId": "1f7e28e3-74ab-4850-8ab6-519f73ccb578" 89 | }, 90 | "execution_count": 1, 91 | "outputs": [ 92 | { 93 | "output_type": "stream", 94 | "name": "stdout", 95 | "text": [ 96 | "\n", 97 | "\n", 98 | "\n" 99 | ] 100 | } 101 | ] 102 | }, 103 | { 104 | "cell_type": "markdown", 105 | "source": [ 106 | "###FLOAT DATAT TYPE\n", 107 | "Floating point numbers are decimal values or fractional numbers like 133.5, 2897.11, and 3571.213, whereas real numbers like 56, 2, and 33 are called integers." 108 | ], 109 | "metadata": { 110 | "id": "uV6RQfb8YAeX" 111 | } 112 | }, 113 | { 114 | "cell_type": "code", 115 | "source": [ 116 | "#FLOAT EXAMPLES\n", 117 | "x = 1222.3443\n", 118 | "y = 2.0\n", 119 | "z = -334059.1\n", 120 | "\n", 121 | "print(type(x))\n", 122 | "print(type(y))\n", 123 | "print(type(z))" 124 | ], 125 | "metadata": { 126 | "colab": { 127 | "base_uri": "https://localhost:8080/" 128 | }, 129 | "id": "GciC8G5BYN79", 130 | "outputId": "db3e06a0-91b6-455b-d8f6-559deee1f644" 131 | }, 132 | "execution_count": 2, 133 | "outputs": [ 134 | { 135 | "output_type": "stream", 136 | "name": "stdout", 137 | "text": [ 138 | "\n", 139 | "\n", 140 | "\n" 141 | ] 142 | } 143 | ] 144 | }, 145 | { 146 | "cell_type": "code", 147 | "source": [ 148 | "#Float can also be scientific numbers with an \"e\" to indicate the power of 10.\n", 149 | "x = 1222e10\n", 150 | "y = 10E8\n", 151 | "z = -34.7e200\n", 152 | "\n", 153 | "print(type(x))\n", 154 | "print(type(y))\n", 155 | "print(type(z))" 156 | ], 157 | "metadata": { 158 | "colab": { 159 | "base_uri": "https://localhost:8080/" 160 | }, 161 | "id": "TsZn4Aa7YZT-", 162 | "outputId": "e88affbd-b8f4-44ad-eed4-8cc58c4a7c83" 163 | }, 164 | "execution_count": 3, 165 | "outputs": [ 166 | { 167 | "output_type": "stream", 168 | "name": "stdout", 169 | "text": [ 170 | "\n", 171 | "\n", 172 | "\n" 173 | ] 174 | } 175 | ] 176 | }, 177 | { 178 | "cell_type": "markdown", 179 | "source": [ 180 | "###COMPLEX DATA TYPE\n", 181 | "The complex data type in python consists of two values, the first one is the real part of the complex number, and the second one is the imaginary part of the complex number. We usually denote the real part using i and the imaginary part with j. For example, (3 + 7j) or (3i + 7j)." 182 | ], 183 | "metadata": { 184 | "id": "2wNsT5EOYvWG" 185 | } 186 | }, 187 | { 188 | "cell_type": "code", 189 | "source": [ 190 | "#COMPLEX DATA TYPE EXAMPLE\n", 191 | "x = 12+55j\n", 192 | "y = 53654j\n", 193 | "z = -12345j\n", 194 | "\n", 195 | "print(type(x))\n", 196 | "print(type(y))\n", 197 | "print(type(z))" 198 | ], 199 | "metadata": { 200 | "colab": { 201 | "base_uri": "https://localhost:8080/" 202 | }, 203 | "id": "yB7VaEb3Y3Rl", 204 | "outputId": "a301adbe-3511-446b-90d7-29a597528cee" 205 | }, 206 | "execution_count": 5, 207 | "outputs": [ 208 | { 209 | "output_type": "stream", 210 | "name": "stdout", 211 | "text": [ 212 | "\n", 213 | "\n", 214 | "\n" 215 | ] 216 | } 217 | ] 218 | }, 219 | { 220 | "cell_type": "markdown", 221 | "source": [ 222 | "###CONVERSION OF DATATYPES\n", 223 | "You can convert from one type to another with the int(), float(), and complex() methods:" 224 | ], 225 | "metadata": { 226 | "id": "f8yKD5m5ZHdH" 227 | } 228 | }, 229 | { 230 | "cell_type": "code", 231 | "source": [ 232 | "#CONVERSION OF DATA TYPES\n", 233 | "x = 23 # int\n", 234 | "y = 12.8 # float\n", 235 | "z = 13j # complex\n", 236 | "\n", 237 | "#convert from int to float:\n", 238 | "a = float(x)\n", 239 | "\n", 240 | "#convert from float to int:\n", 241 | "b = int(y)\n", 242 | "\n", 243 | "#convert from int to complex:\n", 244 | "c = complex(x)\n", 245 | "\n", 246 | "print(a)\n", 247 | "print(b)\n", 248 | "print(c)\n", 249 | "\n", 250 | "print(type(a))\n", 251 | "print(type(b))\n", 252 | "print(type(c))" 253 | ], 254 | "metadata": { 255 | "colab": { 256 | "base_uri": "https://localhost:8080/" 257 | }, 258 | "id": "TqPCbg2BZMnf", 259 | "outputId": "6d194a4b-15ed-4ce1-d8cc-079f336bf041" 260 | }, 261 | "execution_count": 6, 262 | "outputs": [ 263 | { 264 | "output_type": "stream", 265 | "name": "stdout", 266 | "text": [ 267 | "23.0\n", 268 | "12\n", 269 | "(23+0j)\n", 270 | "\n", 271 | "\n", 272 | "\n" 273 | ] 274 | } 275 | ] 276 | } 277 | ] 278 | } 279 | -------------------------------------------------------------------------------- /General Python Programming.md/5_PYTHON_Comments.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "provenance": [] 7 | }, 8 | "kernelspec": { 9 | "name": "python3", 10 | "display_name": "Python 3" 11 | }, 12 | "language_info": { 13 | "name": "python" 14 | } 15 | }, 16 | "cells": [ 17 | { 18 | "cell_type": "markdown", 19 | "source": [ 20 | "## COMMENTING IN PYTHON" 21 | ], 22 | "metadata": { 23 | "id": "sSMdeFqC-Box" 24 | } 25 | }, 26 | { 27 | "cell_type": "markdown", 28 | "source": [ 29 | "### Comments in Python\n", 30 | "Comments in any programming language are used to increase the readability of the code. Similarly, in Python, when the program starts getting complicated, one of the best ways to maintain the readability of the code is to use Python comments. It is considered a good practice to include documentations and notes in the python syntax since it makes the code way more readable and understandable to other programmers as well, which comes in handy when multiple programmers are simultaneously working on the same project.\n", 31 | "The code can only explain how it does something and not why it does that, but Python comments can do that. With Python comments, we can make documentations for various explanations in our code itself.\n", 32 | "\n" 33 | ], 34 | "metadata": { 35 | "id": "fmT1Ii9r-Hyo" 36 | } 37 | }, 38 | { 39 | "cell_type": "markdown", 40 | "source": [ 41 | "### How to Comment in Python\n", 42 | "Comments are nothing but tagged lines of codes which increase the readability of a code and make it self-explanatory. There are different ways of creating comments depending on the type of comment we want to include in our code. Following are different kinds of comments that can be included in our Python program:\n", 43 | "* Single Line Comments\n", 44 | "* Docstring Comments\n", 45 | "* Multiline Comments\n" 46 | ], 47 | "metadata": { 48 | "id": "gd0pQknK-gZA" 49 | } 50 | }, 51 | { 52 | "cell_type": "markdown", 53 | "source": [ 54 | "###Single Line Python Comment\n", 55 | "Single line Python comments are marked with # character. These comments end at the end of the physical line, which means that all characters starting after the # character (and lasts till the end of the line) are part of the comment.\n" 56 | ], 57 | "metadata": { 58 | "id": "Oc_yrDa8-wcp" 59 | } 60 | }, 61 | { 62 | "cell_type": "code", 63 | "source": [ 64 | "# This is a single-line comment\n", 65 | "# This comment can be used to say what is a particular code is doing, Example:\n", 66 | "#Addition of 2 numbers in python\n", 67 | "a=10 #First number\n", 68 | "b=5 #Second number\n", 69 | "c=a+b #adding both\n", 70 | "print(\"Addition of both the number is:\", c)" 71 | ], 72 | "metadata": { 73 | "colab": { 74 | "base_uri": "https://localhost:8080/" 75 | }, 76 | "id": "s4kWt9pL-0p3", 77 | "outputId": "f08c740b-9918-42c0-d1b2-8713bb7d8672" 78 | }, 79 | "execution_count": 4, 80 | "outputs": [ 81 | { 82 | "output_type": "stream", 83 | "name": "stdout", 84 | "text": [ 85 | "Addition of both the number is: 15\n" 86 | ] 87 | } 88 | ] 89 | }, 90 | { 91 | "cell_type": "markdown", 92 | "source": [ 93 | "### Docstrings\n", 94 | "Python has the documentation strings (or docstrings) feature which is usually the first statement included in functions and modules. Rather than being ignored by the Python Interpreter like regular comments, docstrings can actually be accessed at the run time using the dot operator.\n", 95 | "It gives programmers an easy way of adding quick notes with every Python module, function, class, and method. To use this feature, we use triple quotes in the beginning of the documentation string or comment and the closing triple quotes at the end of the documentation comment. Docstrings can be one-liners as well as multi-liners." 96 | ], 97 | "metadata": { 98 | "id": "w3Kjk1oV_e2g" 99 | } 100 | }, 101 | { 102 | "cell_type": "code", 103 | "source": [ 104 | "def SayFunction():\n", 105 | " '''\n", 106 | "Strings written using ”’_”’ after a function represents docstring of func\n", 107 | "Python docstrings are not comments\n", 108 | "'''\n", 109 | "print(\"just a docstring\")\n", 110 | "print(\"Let us see how to print the docstring value\")\n", 111 | "print(SayFunction.__doc__)" 112 | ], 113 | "metadata": { 114 | "colab": { 115 | "base_uri": "https://localhost:8080/" 116 | }, 117 | "id": "B982IG8U_2v_", 118 | "outputId": "8b3440e3-2314-4024-e92f-b8665b31e10d" 119 | }, 120 | "execution_count": 8, 121 | "outputs": [ 122 | { 123 | "output_type": "stream", 124 | "name": "stdout", 125 | "text": [ 126 | "just a docstring\n", 127 | "Let us see how to print the docstring value\n", 128 | "\n", 129 | "Strings written using ”’_”’ after a function represents docstring of func\n", 130 | "Python docstrings are not comments\n", 131 | "\n" 132 | ] 133 | } 134 | ] 135 | }, 136 | { 137 | "cell_type": "markdown", 138 | "source": [ 139 | "### Multiline Comment in Python\n", 140 | "Unlike some programming languages that support multiline comments, such as C, Java, and more, there is no specific feature for multiline comments in Python. But that does not mean that it is totally impossible to make multiline comments in Python. There are two ways we can include comments that can span across multiple lines in our Python code\n", 141 | "* Python Block Comments: We can use several single line comments for a whole block. This type of comment is usually created to explain the block of code that follows the Block comment. Python Block comment is the only way of writing a real comment that can span across multiple lines. It is supported and preferred by Python’s PEP8 style guide since Block comments are ignored by Python interpreter or parser. However, nothing is stopping programmers from using the second ‘non-real’ way of writing multiline comments in Python which is explained below.\n", 142 | "* Using Docstrings: Docstrings are largely used as multiline comments in Python by many programmers since it is the closest thing to having a multiline comment feature in Python. While it is not wrong to use docstrings when we need to make multiline comments, it is important to keep in mind that there is a significant difference between docstrings and comments. Comments in Python are totally ignored by the Python Interpreter, while docstrings, when used inside the Python function, can be accessed at the run time.\n" 143 | ], 144 | "metadata": { 145 | "id": "2yI2pF2KAdTp" 146 | } 147 | }, 148 | { 149 | "cell_type": "code", 150 | "source": [ 151 | "test1= 7 * 2\n", 152 | "print(test1)\n", 153 | "\"\"\"\n", 154 | "line one\n", 155 | "line two\n", 156 | "line three\n", 157 | "\"\"\"" 158 | ], 159 | "metadata": { 160 | "colab": { 161 | "base_uri": "https://localhost:8080/", 162 | "height": 52 163 | }, 164 | "id": "j4mDZ0ebApXY", 165 | "outputId": "fe06583b-9213-44d0-c0fc-0e81692e9b88" 166 | }, 167 | "execution_count": 13, 168 | "outputs": [ 169 | { 170 | "output_type": "stream", 171 | "name": "stdout", 172 | "text": [ 173 | "14\n" 174 | ] 175 | }, 176 | { 177 | "output_type": "execute_result", 178 | "data": { 179 | "text/plain": [ 180 | "'\\nline one\\nline two\\nline three\\n'" 181 | ], 182 | "application/vnd.google.colaboratory.intrinsic+json": { 183 | "type": "string" 184 | } 185 | }, 186 | "metadata": {}, 187 | "execution_count": 13 188 | } 189 | ] 190 | } 191 | ] 192 | } 193 | -------------------------------------------------------------------------------- /General Python Programming.md/strings.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "provenance": [] 7 | }, 8 | "kernelspec": { 9 | "name": "python3", 10 | "display_name": "Python 3" 11 | }, 12 | "language_info": { 13 | "name": "python" 14 | } 15 | }, 16 | "cells": [ 17 | { 18 | "cell_type": "code", 19 | "source": [ 20 | "str1=\"this is a string\"\n", 21 | "str2='this is a string'\n", 22 | "str3=\"\"\"this is a string\"\"\"\n", 23 | "str4='''this is a string'''\n", 24 | "print(str1)\n", 25 | "print(str2)\n", 26 | "print(str3)\n", 27 | "print(str4)" 28 | ], 29 | "metadata": { 30 | "colab": { 31 | "base_uri": "https://localhost:8080/" 32 | }, 33 | "id": "uQjy8ZkadLxJ", 34 | "outputId": "182fb96d-f839-4ae3-c5f2-5e8dd7542e95" 35 | }, 36 | "execution_count": 22, 37 | "outputs": [ 38 | { 39 | "output_type": "stream", 40 | "name": "stdout", 41 | "text": [ 42 | "this is a string\n", 43 | "this is a string\n", 44 | "this is a string\n", 45 | "this is a string\n" 46 | ] 47 | } 48 | ] 49 | }, 50 | { 51 | "cell_type": "code", 52 | "source": [ 53 | "str1=\"this is a string and,\\n it is in python programming language\"\n", 54 | "print(str1)" 55 | ], 56 | "metadata": { 57 | "colab": { 58 | "base_uri": "https://localhost:8080/" 59 | }, 60 | "id": "Hkte-fwzfDP2", 61 | "outputId": "38b5888f-47bb-4a2f-c817-53195cbea712" 62 | }, 63 | "execution_count": 24, 64 | "outputs": [ 65 | { 66 | "output_type": "stream", 67 | "name": "stdout", 68 | "text": [ 69 | "this is a string and,\n", 70 | " it is in python programming language\n" 71 | ] 72 | } 73 | ] 74 | }, 75 | { 76 | "cell_type": "code", 77 | "source": [ 78 | "#Concatination- joining two or more than 2 strings\n", 79 | "str1=\"apple\"\n", 80 | "str2=\" is a fruit\"\n", 81 | "final=str1+str2\n", 82 | "print(final)" 83 | ], 84 | "metadata": { 85 | "colab": { 86 | "base_uri": "https://localhost:8080/" 87 | }, 88 | "id": "5kdR-PUkfmhQ", 89 | "outputId": "795805fd-465f-44e9-9882-af767dd8a4f7" 90 | }, 91 | "execution_count": 26, 92 | "outputs": [ 93 | { 94 | "output_type": "stream", 95 | "name": "stdout", 96 | "text": [ 97 | "apple is a fruit\n" 98 | ] 99 | } 100 | ] 101 | }, 102 | { 103 | "cell_type": "code", 104 | "source": [ 105 | "str1=\"python programming\"\n", 106 | "length=len(str1)\n", 107 | "print(length)" 108 | ], 109 | "metadata": { 110 | "colab": { 111 | "base_uri": "https://localhost:8080/" 112 | }, 113 | "id": "5LV2sYLIgB68", 114 | "outputId": "c1923e06-898e-4549-ce7b-da7031bf3356" 115 | }, 116 | "execution_count": 27, 117 | "outputs": [ 118 | { 119 | "output_type": "stream", 120 | "name": "stdout", 121 | "text": [ 122 | "18\n" 123 | ] 124 | } 125 | ] 126 | }, 127 | { 128 | "cell_type": "code", 129 | "source": [ 130 | "#INDEXING\n", 131 | "# P Y T H O N _ P R O G R A M M I N G\n", 132 | "# 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17\n", 133 | "str=\"python programming\"\n", 134 | "ch=str[5]\n", 135 | "print(ch)\n" 136 | ], 137 | "metadata": { 138 | "colab": { 139 | "base_uri": "https://localhost:8080/" 140 | }, 141 | "id": "ce5U_8UogbzJ", 142 | "outputId": "9a50e7b4-63ca-4b63-846e-b477d191acd2" 143 | }, 144 | "execution_count": 33, 145 | "outputs": [ 146 | { 147 | "output_type": "stream", 148 | "name": "stdout", 149 | "text": [ 150 | "n\n" 151 | ] 152 | } 153 | ] 154 | }, 155 | { 156 | "cell_type": "code", 157 | "source": [ 158 | "#SLICING\n", 159 | "str=\"python programing\"\n", 160 | "s=str[2:5]\n", 161 | "print(s)" 162 | ], 163 | "metadata": { 164 | "colab": { 165 | "base_uri": "https://localhost:8080/" 166 | }, 167 | "id": "PWK_hgm_hFIz", 168 | "outputId": "2dd0d0d7-8870-4d36-f8ce-62dd27a8d583" 169 | }, 170 | "execution_count": 37, 171 | "outputs": [ 172 | { 173 | "output_type": "stream", 174 | "name": "stdout", 175 | "text": [ 176 | "tho\n" 177 | ] 178 | } 179 | ] 180 | }, 181 | { 182 | "cell_type": "code", 183 | "source": [ 184 | "str=\"python programing\"\n", 185 | "s=str[2:]\n", 186 | "print(s)" 187 | ], 188 | "metadata": { 189 | "colab": { 190 | "base_uri": "https://localhost:8080/" 191 | }, 192 | "id": "t653st7Tl21C", 193 | "outputId": "677e25c3-fc8b-4e9e-8d98-5329a0fd2b76" 194 | }, 195 | "execution_count": 38, 196 | "outputs": [ 197 | { 198 | "output_type": "stream", 199 | "name": "stdout", 200 | "text": [ 201 | "thon programing\n" 202 | ] 203 | } 204 | ] 205 | }, 206 | { 207 | "cell_type": "code", 208 | "source": [ 209 | "#NEGATIVE INDEXES\n", 210 | "str=\"python programing\"\n", 211 | "s=str[-5:-2]\n", 212 | "print(s)" 213 | ], 214 | "metadata": { 215 | "colab": { 216 | "base_uri": "https://localhost:8080/" 217 | }, 218 | "id": "n6EA11uLl8-K", 219 | "outputId": "b87b8902-695c-4afb-a570-7c4d6929996b" 220 | }, 221 | "execution_count": 39, 222 | "outputs": [ 223 | { 224 | "output_type": "stream", 225 | "name": "stdout", 226 | "text": [ 227 | "ami\n" 228 | ] 229 | } 230 | ] 231 | }, 232 | { 233 | "cell_type": "code", 234 | "source": [ 235 | "#FUNCTIONS\n", 236 | "str=\"I am studying python programing\"\n", 237 | "print(str.endswith(\"ing\"))\n" 238 | ], 239 | "metadata": { 240 | "colab": { 241 | "base_uri": "https://localhost:8080/" 242 | }, 243 | "id": "f4llskPvmHAI", 244 | "outputId": "3bba1c47-e1ad-4a1f-9f66-45f5baa6f741" 245 | }, 246 | "execution_count": 47, 247 | "outputs": [ 248 | { 249 | "output_type": "stream", 250 | "name": "stdout", 251 | "text": [ 252 | "True\n" 253 | ] 254 | } 255 | ] 256 | }, 257 | { 258 | "cell_type": "code", 259 | "source": [ 260 | "str=\"I am studying python programing\"\n", 261 | "str=str.capitalize()\n", 262 | "print(str)" 263 | ], 264 | "metadata": { 265 | "colab": { 266 | "base_uri": "https://localhost:8080/" 267 | }, 268 | "id": "LJkA1iSsm_BY", 269 | "outputId": "3ea79a5c-4e3f-4290-d745-3a1d4bbc32e5" 270 | }, 271 | "execution_count": 51, 272 | "outputs": [ 273 | { 274 | "output_type": "stream", 275 | "name": "stdout", 276 | "text": [ 277 | "I am studying python programing\n" 278 | ] 279 | } 280 | ] 281 | }, 282 | { 283 | "cell_type": "code", 284 | "source": [ 285 | "str=\"I am studying python programing\"\n", 286 | "print(str.replace(\"python\", \"C++\"))" 287 | ], 288 | "metadata": { 289 | "colab": { 290 | "base_uri": "https://localhost:8080/" 291 | }, 292 | "id": "WJiGamCJnVep", 293 | "outputId": "3f89c1e5-3bbc-4774-8244-a85e86a1418d" 294 | }, 295 | "execution_count": 53, 296 | "outputs": [ 297 | { 298 | "output_type": "stream", 299 | "name": "stdout", 300 | "text": [ 301 | "I am studying C++ programing\n" 302 | ] 303 | } 304 | ] 305 | }, 306 | { 307 | "cell_type": "code", 308 | "source": [ 309 | "str = \"I am studying python programing\"\n", 310 | "print(str.find(\"python\"))" 311 | ], 312 | "metadata": { 313 | "colab": { 314 | "base_uri": "https://localhost:8080/" 315 | }, 316 | "id": "YLiwGIZroED9", 317 | "outputId": "6cb64ca9-a9bc-429f-d8f2-459dc3712882" 318 | }, 319 | "execution_count": 57, 320 | "outputs": [ 321 | { 322 | "output_type": "stream", 323 | "name": "stdout", 324 | "text": [ 325 | "14\n" 326 | ] 327 | } 328 | ] 329 | }, 330 | { 331 | "cell_type": "code", 332 | "source": [ 333 | "str = \"I am studying python programing\"\n", 334 | "print(str.count(\"o\"))" 335 | ], 336 | "metadata": { 337 | "colab": { 338 | "base_uri": "https://localhost:8080/" 339 | }, 340 | "id": "KYf5t_TSoWC0", 341 | "outputId": "88580903-8ad5-494f-9287-341f54df5243" 342 | }, 343 | "execution_count": 59, 344 | "outputs": [ 345 | { 346 | "output_type": "stream", 347 | "name": "stdout", 348 | "text": [ 349 | "2\n" 350 | ] 351 | } 352 | ] 353 | }, 354 | { 355 | "cell_type": "code", 356 | "source": [], 357 | "metadata": { 358 | "id": "RGrm2lxRo1k_" 359 | }, 360 | "execution_count": null, 361 | "outputs": [] 362 | } 363 | ] 364 | } -------------------------------------------------------------------------------- /General Python Programming.md/4_PYTHON_Syntax.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "provenance": [] 7 | }, 8 | "kernelspec": { 9 | "name": "python3", 10 | "display_name": "Python 3" 11 | }, 12 | "language_info": { 13 | "name": "python" 14 | } 15 | }, 16 | "cells": [ 17 | { 18 | "cell_type": "markdown", 19 | "source": [ 20 | "##SYNTAX IN PYTHON\n", 21 | "####The syntax of the Python programming language is the set of rules that defines how a Python program will be written and interpreted\n", 22 | "\n", 23 | "###Python Syntax can be Broadly classified into 8 categories\n", 24 | "* Line Structure\n", 25 | "* Comments\n", 26 | "* Docstrings\n", 27 | "* Indentation\n", 28 | "* Quotations\n", 29 | "* Identifiers\n", 30 | "* Variables\n", 31 | "* String Formatters." 32 | ], 33 | "metadata": { 34 | "id": "T9StiMe9Q2NE" 35 | } 36 | }, 37 | { 38 | "cell_type": "markdown", 39 | "source": [ 40 | "###LINE STRUCTUR\n", 41 | "#####Python coding style comprises physical lines as well as logical lines or statements. A physical line in a Python program is a sequence of characters, and the end of the line terminates the line sequence as opposed to some other languages, such as C and C++ where a semi-colon is used to mark the end of the statement. A logical line, on the other hand, is composed of one or more physical lines. The use of a semi-colon is not prohibited in Python, although it’s not mandatory. The NEWLINE token denotes the end of the logical line." 42 | ], 43 | "metadata": { 44 | "id": "5IFuSAYtngQg" 45 | } 46 | }, 47 | { 48 | "cell_type": "code", 49 | "source": [ 50 | "variable = 6" 51 | ], 52 | "metadata": { 53 | "id": "9L31M0qXzyVd" 54 | }, 55 | "execution_count": null, 56 | "outputs": [] 57 | }, 58 | { 59 | "cell_type": "markdown", 60 | "source": [ 61 | "NOTE: A logical line that only contains spaces, comments, or tabs are called blank lines and they are ignored by the interpreter." 62 | ], 63 | "metadata": { 64 | "id": "7XAmynz_z6yo" 65 | } 66 | }, 67 | { 68 | "cell_type": "markdown", 69 | "source": [ 70 | "## Python Multiline Statements\n", 71 | "As we saw that in Python, a new line simply means that a new statement has started. Although, Python does provide a way to split a statement into a multiline statement or to join multiple statements into one logical line. This can be helpful to increase the readability of the statement. Following are the two ways to split a line into two or more lines:" 72 | ], 73 | "metadata": { 74 | "id": "8gF7us7s0KPx" 75 | } 76 | }, 77 | { 78 | "cell_type": "markdown", 79 | "source": [ 80 | "####Explicit Line joining\n", 81 | "In explicit line joining, we use a backward slash to split a statement into a multiline statement." 82 | ], 83 | "metadata": { 84 | "id": "nyR6cKSV0dUa" 85 | } 86 | }, 87 | { 88 | "cell_type": "code", 89 | "source": [ 90 | "#Explicit Line joining\n", 91 | "print(\"This is a \\\n", 92 | "multi line statement\")" 93 | ], 94 | "metadata": { 95 | "colab": { 96 | "base_uri": "https://localhost:8080/" 97 | }, 98 | "id": "X3BjjhSl0o5W", 99 | "outputId": "9c1fe2b0-d1c8-433a-d9b9-8748ee5ff730" 100 | }, 101 | "execution_count": null, 102 | "outputs": [ 103 | { 104 | "output_type": "stream", 105 | "name": "stdout", 106 | "text": [ 107 | "This is a multi line statement\n" 108 | ] 109 | } 110 | ] 111 | }, 112 | { 113 | "cell_type": "markdown", 114 | "source": [ 115 | "####Implicit Line Joining\n", 116 | "Statements that reside inside [], {}, or () parentheses can be broken down into two or more physical lines without using a back slash.\n", 117 | "\n" 118 | ], 119 | "metadata": { 120 | "id": "O94MOyL-1C9z" 121 | } 122 | }, 123 | { 124 | "cell_type": "code", 125 | "source": [ 126 | "#Implicit Line Joining\n", 127 | "Fruits = [\"Apple\", \"Banana\",\n", 128 | " \"Grapes, Orange\", \"Plum\"]\n", 129 | "print(Fruits)" 130 | ], 131 | "metadata": { 132 | "colab": { 133 | "base_uri": "https://localhost:8080/" 134 | }, 135 | "id": "Q8XfxJ2m1pkp", 136 | "outputId": "3fcfcf95-918b-4251-86b6-9015d0b06ac5" 137 | }, 138 | "execution_count": null, 139 | "outputs": [ 140 | { 141 | "output_type": "stream", 142 | "name": "stdout", 143 | "text": [ 144 | "['Apple', 'Banana', 'Grapes, Orange', 'Plum']\n" 145 | ] 146 | } 147 | ] 148 | }, 149 | { 150 | "cell_type": "markdown", 151 | "source": [ 152 | "####Whitespaces and Indentation\n", 153 | "Unlike most of the programming languages, Python uses indentation to mark a block of code. According to Python coding style guideline, we should keep an indent size of four.\n", 154 | "Most of the programming languages provide indentation for better code formatting and don’t enforce to have it. But in Python it is mandatory. This is why indentation is so crucial in Python.\n" 155 | ], 156 | "metadata": { 157 | "id": "FiIp6gia2Om7" 158 | } 159 | }, 160 | { 161 | "cell_type": "code", 162 | "source": [ 163 | "#Indentation Error\n", 164 | "a=1\n", 165 | "if a>0 :\n", 166 | "print(\"There is no indentation in this statement\")" 167 | ], 168 | "metadata": { 169 | "colab": { 170 | "base_uri": "https://localhost:8080/", 171 | "height": 134 172 | }, 173 | "id": "g3hbNaO72jPq", 174 | "outputId": "641124d4-a284-4e8a-b5cd-c57d204697d8" 175 | }, 176 | "execution_count": null, 177 | "outputs": [ 178 | { 179 | "output_type": "error", 180 | "ename": "IndentationError", 181 | "evalue": "ignored", 182 | "traceback": [ 183 | "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m4\u001b[0m\n\u001b[0;31m print(\"There is no indentation in this statement\")\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mIndentationError\u001b[0m\u001b[0;31m:\u001b[0m expected an indented block\n" 184 | ] 185 | } 186 | ] 187 | }, 188 | { 189 | "cell_type": "code", 190 | "source": [ 191 | "#Correct answer for above Indentation Error will be:\n", 192 | "a=1\n", 193 | "if a>0 :\n", 194 | " print(\"There is no indentation in this statement\")\n", 195 | "#There is a requirement of a space after (:) in the next line." 196 | ], 197 | "metadata": { 198 | "id": "AhLxDJz821sf", 199 | "colab": { 200 | "base_uri": "https://localhost:8080/" 201 | }, 202 | "outputId": "8677ef7b-5163-40c5-c378-823ebbcedd81" 203 | }, 204 | "execution_count": null, 205 | "outputs": [ 206 | { 207 | "output_type": "stream", 208 | "name": "stdout", 209 | "text": [ 210 | "There is no indentation in this statement\n" 211 | ] 212 | } 213 | ] 214 | }, 215 | { 216 | "cell_type": "markdown", 217 | "source": [ 218 | "###Identifiers\n", 219 | "Identifiers in Python are nothing but user-defined names to represent programmable entities like (Python Variables, Python Functions, Python Class, Python Modules), or any other objects. But, there are a few rules that we need to follow while defining an identifier. They are:\n", 220 | "* We can use a sequence of letters [lowercase (a to z) or uppercase (A to Z)], and we can also mix up digits (0 to 9) or an underscore (_) while defining an identifier.\n", 221 | "* We can’t use digits to begin an identifier’s name.\n", 222 | "* We should not use Reserved Keywords to define an identifier.\n", 223 | "* Other than underscore (_), we are not allowed to use any other special characters.\n", 224 | "* Even though python doc says that we can name an identifier with unlimited length, it is not entirely true. Using a large name (more than 79 chars) would lead to the violation of a rule set by the PEP8 standard." 225 | ], 226 | "metadata": { 227 | "id": "LrPD9DY36iqv" 228 | } 229 | }, 230 | { 231 | "cell_type": "markdown", 232 | "source": [ 233 | "###Python Quotations\n", 234 | "In Python, single quotes and double quotes, both are supported for strings. If we have started a string with a single quote, it is mandatory to end it with a single quote only. The same goes with double quotes." 235 | ], 236 | "metadata": { 237 | "id": "glkhfcyW7znn" 238 | } 239 | }, 240 | { 241 | "cell_type": "code", 242 | "source": [ 243 | "#Quotation example\n", 244 | "print ('single quoted string')\n", 245 | "print (\"double quoted string\")" 246 | ], 247 | "metadata": { 248 | "colab": { 249 | "base_uri": "https://localhost:8080/" 250 | }, 251 | "id": "_fkBGFcW75WO", 252 | "outputId": "4a64c101-a31e-42ec-e308-866362d4b15b" 253 | }, 254 | "execution_count": 2, 255 | "outputs": [ 256 | { 257 | "output_type": "stream", 258 | "name": "stdout", 259 | "text": [ 260 | "single quoted string\n", 261 | "double quoted string\n" 262 | ] 263 | } 264 | ] 265 | }, 266 | { 267 | "cell_type": "markdown", 268 | "source": [ 269 | "###Reserved Words\n", 270 | "Reserved words are nothing but a set of special words, which are reserved by Python and have specific meanings. Remember that we are not allowed to use keywords as Variables in Python.\n", 271 | "\n", 272 | "Reserved words in Python are case sensitive. Following table shows all these reserved words used in Python.\n", 273 | "\n", 274 | "* False\n", 275 | "* class\n", 276 | "* finally\n", 277 | "*\tis\n", 278 | "*\treturn\n", 279 | "* None\n", 280 | "*\tcontinue\n", 281 | "*\tfor\n", 282 | "*\tlambda\n", 283 | "*\ttry\n", 284 | "* True\n", 285 | "*\tdef\n", 286 | "*\tfrom\n", 287 | "*\tnon\n", 288 | "* local\n", 289 | "*\twhile\n", 290 | "* and\n", 291 | "*\tdel\n", 292 | "*\tglobal\n", 293 | "*\tnot\n", 294 | "*\twith\n", 295 | "* as\n", 296 | "*\tel\n", 297 | "*\tif\n", 298 | "*\tor\n", 299 | "*\tyield\n", 300 | "* assert\n", 301 | "*\telse\n", 302 | "*\timport\n", 303 | "*\tpass\t\n", 304 | "* break\n", 305 | "*\texcept\n", 306 | "*\tin\n", 307 | "*\traise" 308 | ], 309 | "metadata": { 310 | "id": "9A87jpQS8Ir4" 311 | } 312 | }, 313 | { 314 | "cell_type": "code", 315 | "source": [], 316 | "metadata": { 317 | "id": "1gVm9zej8rBx" 318 | }, 319 | "execution_count": null, 320 | "outputs": [] 321 | } 322 | ] 323 | } 324 | -------------------------------------------------------------------------------- /General Python Programming.md/List_and_tuples.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "provenance": [] 7 | }, 8 | "kernelspec": { 9 | "name": "python3", 10 | "display_name": "Python 3" 11 | }, 12 | "language_info": { 13 | "name": "python" 14 | } 15 | }, 16 | "cells": [ 17 | { 18 | "cell_type": "code", 19 | "source": [ 20 | "marks = [67.8, 98.7, 78.9, 94.7, 89.9, 97.8, 58.7]\n", 21 | "print(marks)\n", 22 | "print(type(marks))\n", 23 | "print(marks[4])\n", 24 | "print(marks[1])" 25 | ], 26 | "metadata": { 27 | "colab": { 28 | "base_uri": "https://localhost:8080/" 29 | }, 30 | "id": "vJxDvCjEqHTG", 31 | "outputId": "83fca661-dd89-4e7b-8c56-86e757d051c2" 32 | }, 33 | "execution_count": 64, 34 | "outputs": [ 35 | { 36 | "output_type": "stream", 37 | "name": "stdout", 38 | "text": [ 39 | "[67.8, 98.7, 78.9, 94.7, 89.9, 97.8, 58.7]\n", 40 | "\n", 41 | "89.9\n", 42 | "98.7\n" 43 | ] 44 | } 45 | ] 46 | }, 47 | { 48 | "cell_type": "code", 49 | "source": [ 50 | "#STRINGS are mutable\n", 51 | "#LISTS are mutable" 52 | ], 53 | "metadata": { 54 | "id": "pBF9vO4LqmJT" 55 | }, 56 | "execution_count": 65, 57 | "outputs": [] 58 | }, 59 | { 60 | "cell_type": "code", 61 | "source": [ 62 | "student=[\"aman\", 94.7, 18, \"Indore\"]\n", 63 | "print(student[3])\n", 64 | "student[3]=\"patna\"\n", 65 | "print(student)" 66 | ], 67 | "metadata": { 68 | "colab": { 69 | "base_uri": "https://localhost:8080/" 70 | }, 71 | "id": "GGWBVn_mrJXR", 72 | "outputId": "e1c2fdc9-1829-4be8-9910-537defe93fcb" 73 | }, 74 | "execution_count": 68, 75 | "outputs": [ 76 | { 77 | "output_type": "stream", 78 | "name": "stdout", 79 | "text": [ 80 | "Indore\n", 81 | "['aman', 94.7, 18, 'patna']\n" 82 | ] 83 | } 84 | ] 85 | }, 86 | { 87 | "cell_type": "code", 88 | "source": [ 89 | " #LIST METHODS\n", 90 | " list=[1,2,3,4,5,6,7,8,9]\n", 91 | " list.append(4)\n", 92 | " print(list)" 93 | ], 94 | "metadata": { 95 | "colab": { 96 | "base_uri": "https://localhost:8080/" 97 | }, 98 | "id": "3HnHfrj8ratl", 99 | "outputId": "f15b6be7-02ad-4d7b-ee8c-d63b75035d34" 100 | }, 101 | "execution_count": 69, 102 | "outputs": [ 103 | { 104 | "output_type": "stream", 105 | "name": "stdout", 106 | "text": [ 107 | "[1, 2, 3, 4, 5, 6, 7, 8, 9, 4]\n" 108 | ] 109 | } 110 | ] 111 | }, 112 | { 113 | "cell_type": "code", 114 | "source": [ 115 | "list=[5,4,3,2,1,6,7,8,9]\n", 116 | "list.sort()\n", 117 | "print(list)" 118 | ], 119 | "metadata": { 120 | "colab": { 121 | "base_uri": "https://localhost:8080/" 122 | }, 123 | "id": "ezPUuZqPu_bX", 124 | "outputId": "622eeecf-0903-4017-d1d7-17836bb10fad" 125 | }, 126 | "execution_count": 71, 127 | "outputs": [ 128 | { 129 | "output_type": "stream", 130 | "name": "stdout", 131 | "text": [ 132 | "[1, 2, 3, 4, 5, 6, 7, 8, 9]\n" 133 | ] 134 | } 135 | ] 136 | }, 137 | { 138 | "cell_type": "code", 139 | "source": [ 140 | "list=[5,4,3,2,1,6,7,8,9]\n", 141 | "list.sort(reverse=True)\n", 142 | "print(list)" 143 | ], 144 | "metadata": { 145 | "colab": { 146 | "base_uri": "https://localhost:8080/" 147 | }, 148 | "id": "ZAe9i1RzvI7R", 149 | "outputId": "abc0c4cd-e00d-4092-946b-ac4332a6b900" 150 | }, 151 | "execution_count": 72, 152 | "outputs": [ 153 | { 154 | "output_type": "stream", 155 | "name": "stdout", 156 | "text": [ 157 | "[9, 8, 7, 6, 5, 4, 3, 2, 1]\n" 158 | ] 159 | } 160 | ] 161 | }, 162 | { 163 | "cell_type": "code", 164 | "source": [ 165 | "list=['apple', 'banana', 'litchi', 'grapes', 'mango']\n", 166 | "print(list.sort())\n", 167 | "print(list)" 168 | ], 169 | "metadata": { 170 | "colab": { 171 | "base_uri": "https://localhost:8080/" 172 | }, 173 | "id": "_5Q_Zzj6ypPM", 174 | "outputId": "895f7eb7-3588-4dd0-d75c-e2ea47e4295c" 175 | }, 176 | "execution_count": 77, 177 | "outputs": [ 178 | { 179 | "output_type": "stream", 180 | "name": "stdout", 181 | "text": [ 182 | "None\n", 183 | "['apple', 'banana', 'grapes', 'litchi', 'mango']\n" 184 | ] 185 | } 186 | ] 187 | }, 188 | { 189 | "cell_type": "code", 190 | "source": [ 191 | "list=[5,4,3,2,1,6,7,8,9]\n", 192 | "list.insert(1,5)\n", 193 | "print(list)" 194 | ], 195 | "metadata": { 196 | "colab": { 197 | "base_uri": "https://localhost:8080/" 198 | }, 199 | "id": "0n4Dv4abyxnU", 200 | "outputId": "c4bdd01d-a02d-45c4-b93c-014977752083" 201 | }, 202 | "execution_count": 79, 203 | "outputs": [ 204 | { 205 | "output_type": "stream", 206 | "name": "stdout", 207 | "text": [ 208 | "[5, 5, 4, 3, 2, 1, 6, 7, 8, 9]\n" 209 | ] 210 | } 211 | ] 212 | }, 213 | { 214 | "cell_type": "code", 215 | "source": [ 216 | "list=[5,4,3,2,1,6,7,8,9]\n", 217 | "list.pop(5)\n", 218 | "print(list)" 219 | ], 220 | "metadata": { 221 | "colab": { 222 | "base_uri": "https://localhost:8080/" 223 | }, 224 | "id": "sxg7IPL-1m9B", 225 | "outputId": "620afccc-1f00-4cbe-ec61-a1a6f8473e4c" 226 | }, 227 | "execution_count": 80, 228 | "outputs": [ 229 | { 230 | "output_type": "stream", 231 | "name": "stdout", 232 | "text": [ 233 | "[5, 4, 3, 2, 1, 7, 8, 9]\n" 234 | ] 235 | } 236 | ] 237 | }, 238 | { 239 | "cell_type": "code", 240 | "source": [ 241 | "list=[5,4,3,2,1,6,7,8,9]\n", 242 | "list.remove(2)\n", 243 | "print(list)" 244 | ], 245 | "metadata": { 246 | "colab": { 247 | "base_uri": "https://localhost:8080/" 248 | }, 249 | "id": "4NtLYcsL1yXU", 250 | "outputId": "eae3febc-9276-420e-8bfb-12ec9a2523de" 251 | }, 252 | "execution_count": 81, 253 | "outputs": [ 254 | { 255 | "output_type": "stream", 256 | "name": "stdout", 257 | "text": [ 258 | "[5, 4, 3, 1, 6, 7, 8, 9]\n" 259 | ] 260 | } 261 | ] 262 | }, 263 | { 264 | "cell_type": "code", 265 | "source": [ 266 | "#TUPLES\n", 267 | "#creates immutable lists" 268 | ], 269 | "metadata": { 270 | "id": "xfzTmdrb10cM" 271 | }, 272 | "execution_count": 82, 273 | "outputs": [] 274 | }, 275 | { 276 | "cell_type": "code", 277 | "source": [ 278 | "tup=(1,2,3,4,5,6,7,8,9)\n", 279 | "print(tup)\n", 280 | "print(type(tup[1:3]))" 281 | ], 282 | "metadata": { 283 | "colab": { 284 | "base_uri": "https://localhost:8080/" 285 | }, 286 | "id": "1tIwLeiI2JVN", 287 | "outputId": "80afd722-a330-4e3a-cfaf-25c34936bb00" 288 | }, 289 | "execution_count": 86, 290 | "outputs": [ 291 | { 292 | "output_type": "stream", 293 | "name": "stdout", 294 | "text": [ 295 | "(1, 2, 3, 4, 5, 6, 7, 8, 9)\n", 296 | "\n" 297 | ] 298 | } 299 | ] 300 | }, 301 | { 302 | "cell_type": "code", 303 | "source": [ 304 | "tup=(1,2,3,4,5,6,7,8,9)\n", 305 | "print(tup.index(2))" 306 | ], 307 | "metadata": { 308 | "colab": { 309 | "base_uri": "https://localhost:8080/" 310 | }, 311 | "id": "-6CKXRZJ2evC", 312 | "outputId": "63f8ab52-9dc0-45e6-f407-ac3d185ca68f" 313 | }, 314 | "execution_count": 87, 315 | "outputs": [ 316 | { 317 | "output_type": "stream", 318 | "name": "stdout", 319 | "text": [ 320 | "1\n" 321 | ] 322 | } 323 | ] 324 | }, 325 | { 326 | "cell_type": "code", 327 | "source": [ 328 | "tup=(1,2,2,2,3,4,5,6,7,8,9)\n", 329 | "print(tup.count(2))" 330 | ], 331 | "metadata": { 332 | "colab": { 333 | "base_uri": "https://localhost:8080/" 334 | }, 335 | "id": "_XtuVYKd29UD", 336 | "outputId": "827c3466-0113-46ae-b1ed-5f1f73d8f433" 337 | }, 338 | "execution_count": 89, 339 | "outputs": [ 340 | { 341 | "output_type": "stream", 342 | "name": "stdout", 343 | "text": [ 344 | "3\n" 345 | ] 346 | } 347 | ] 348 | }, 349 | { 350 | "cell_type": "code", 351 | "source": [ 352 | "#Program to ask the user to enter names of their 3 favourite movies and store them in a list\n", 353 | "movies=[]\n", 354 | "mov1=input(\"Enter the name of your first favourite movie: \")\n", 355 | "mov2=input(\"Enter the name of your second favourite movie: \")\n", 356 | "mov3=input(\"Enter the name of your third favourite movie: \")\n", 357 | "movies.append(mov1)\n", 358 | "movies.append(mov2)\n", 359 | "movies.append(mov3)\n", 360 | "print(movies)" 361 | ], 362 | "metadata": { 363 | "colab": { 364 | "base_uri": "https://localhost:8080/" 365 | }, 366 | "id": "dblSNkey3C7m", 367 | "outputId": "b543a868-cf5f-4f04-b8c9-e053cdcd3e88" 368 | }, 369 | "execution_count": 91, 370 | "outputs": [ 371 | { 372 | "output_type": "stream", 373 | "name": "stdout", 374 | "text": [ 375 | "Enter the name of your first favourite movie: Twilight\n", 376 | "Enter the name of your second favourite movie: John Wick\n", 377 | "Enter the name of your third favourite movie: Fast & Furious\n", 378 | "['Twilight', 'John Wick', 'Fast & Furious']\n" 379 | ] 380 | } 381 | ] 382 | }, 383 | { 384 | "cell_type": "code", 385 | "source": [ 386 | "#program to check if a list contains a palindrome of elements.(Hint: use copu() method())\n", 387 | "list1=[1,2,1]\n", 388 | "list2=[1,2,3]\n", 389 | "\n", 390 | "copy_list1 = list1.copy()\n", 391 | "copy_list1.reverse()\n", 392 | "\n", 393 | "if(copy_list1 == list2):\n", 394 | " print(\"True\")\n", 395 | "else:\n", 396 | " print(\"False\")" 397 | ], 398 | "metadata": { 399 | "colab": { 400 | "base_uri": "https://localhost:8080/" 401 | }, 402 | "id": "2Tlt6sfw3fjh", 403 | "outputId": "94435cc5-5a11-465d-fee6-32d4d2fd3ee7" 404 | }, 405 | "execution_count": 95, 406 | "outputs": [ 407 | { 408 | "output_type": "stream", 409 | "name": "stdout", 410 | "text": [ 411 | "False\n" 412 | ] 413 | } 414 | ] 415 | }, 416 | { 417 | "cell_type": "code", 418 | "source": [], 419 | "metadata": { 420 | "id": "GRAzQ8PL4eS8" 421 | }, 422 | "execution_count": null, 423 | "outputs": [] 424 | } 425 | ] 426 | } -------------------------------------------------------------------------------- /General Python Programming.md/Functions_in_python.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "provenance": [] 7 | }, 8 | "kernelspec": { 9 | "name": "python3", 10 | "display_name": "Python 3" 11 | }, 12 | "language_info": { 13 | "name": "python" 14 | } 15 | }, 16 | "cells": [ 17 | { 18 | "cell_type": "code", 19 | "source": [ 20 | "#functions in python\n", 21 | "#block of statements that perform a specific task" 22 | ], 23 | "metadata": { 24 | "id": "UJ_Lswnq88Z8" 25 | }, 26 | "execution_count": null, 27 | "outputs": [] 28 | }, 29 | { 30 | "cell_type": "code", 31 | "source": [ 32 | "#function defination\n", 33 | "def calc_sum(a,b):\n", 34 | " sum=a+b #function body(parameter)\n", 35 | " return sum #function call" 36 | ], 37 | "metadata": { 38 | "id": "vOff-GDv9NYx" 39 | }, 40 | "execution_count": null, 41 | "outputs": [] 42 | }, 43 | { 44 | "cell_type": "code", 45 | "source": [ 46 | "calc_sum(12,23)" 47 | ], 48 | "metadata": { 49 | "colab": { 50 | "base_uri": "https://localhost:8080/" 51 | }, 52 | "id": "T9RwgEbBDhDb", 53 | "outputId": "e41e4dca-8827-4e90-9af8-6e1b5fd83b30" 54 | }, 55 | "execution_count": null, 56 | "outputs": [ 57 | { 58 | "output_type": "execute_result", 59 | "data": { 60 | "text/plain": [ 61 | "35" 62 | ] 63 | }, 64 | "metadata": {}, 65 | "execution_count": 3 66 | } 67 | ] 68 | }, 69 | { 70 | "cell_type": "code", 71 | "source": [ 72 | "calc_sum(3,4)" 73 | ], 74 | "metadata": { 75 | "colab": { 76 | "base_uri": "https://localhost:8080/" 77 | }, 78 | "id": "KL2fn6HdDl9r", 79 | "outputId": "248967f8-1311-4398-ad37-032a06e6057b" 80 | }, 81 | "execution_count": null, 82 | "outputs": [ 83 | { 84 | "output_type": "execute_result", 85 | "data": { 86 | "text/plain": [ 87 | "7" 88 | ] 89 | }, 90 | "metadata": {}, 91 | "execution_count": 4 92 | } 93 | ] 94 | }, 95 | { 96 | "cell_type": "code", 97 | "source": [ 98 | "def calc_avg(a,b,c):\n", 99 | " sum=a+b+c\n", 100 | " avg=sum/3\n", 101 | " return avg" 102 | ], 103 | "metadata": { 104 | "id": "yjimuAL5DvGB" 105 | }, 106 | "execution_count": null, 107 | "outputs": [] 108 | }, 109 | { 110 | "cell_type": "code", 111 | "source": [ 112 | "calc_avg(12,23,34)" 113 | ], 114 | "metadata": { 115 | "colab": { 116 | "base_uri": "https://localhost:8080/" 117 | }, 118 | "id": "i-btoVBiEZlZ", 119 | "outputId": "313e375e-6351-41ff-b7d6-56ddbbab2344" 120 | }, 121 | "execution_count": null, 122 | "outputs": [ 123 | { 124 | "output_type": "execute_result", 125 | "data": { 126 | "text/plain": [ 127 | "23.0" 128 | ] 129 | }, 130 | "metadata": {}, 131 | "execution_count": 6 132 | } 133 | ] 134 | }, 135 | { 136 | "cell_type": "code", 137 | "source": [ 138 | "def print_hello():\n", 139 | " print(\"hello\")" 140 | ], 141 | "metadata": { 142 | "id": "xfAfWo3vw7fM" 143 | }, 144 | "execution_count": null, 145 | "outputs": [] 146 | }, 147 | { 148 | "cell_type": "code", 149 | "source": [ 150 | "print_hello()\n", 151 | "print_hello()\n", 152 | "print_hello()" 153 | ], 154 | "metadata": { 155 | "colab": { 156 | "base_uri": "https://localhost:8080/" 157 | }, 158 | "id": "TI6DipPOx01n", 159 | "outputId": "065e6b00-9787-419d-b234-8561876487cf" 160 | }, 161 | "execution_count": null, 162 | "outputs": [ 163 | { 164 | "output_type": "stream", 165 | "name": "stdout", 166 | "text": [ 167 | "hello\n", 168 | "hello\n", 169 | "hello\n" 170 | ] 171 | } 172 | ] 173 | }, 174 | { 175 | "cell_type": "code", 176 | "source": [ 177 | "def cal_prod(a=1, b=2):\n", 178 | " print(a*b)\n", 179 | " return a*b\n", 180 | "cal_prod(1)" 181 | ], 182 | "metadata": { 183 | "colab": { 184 | "base_uri": "https://localhost:8080/" 185 | }, 186 | "id": "Picvez-jx6k8", 187 | "outputId": "950ef68b-5d89-4efc-9202-cc7ce659b51f" 188 | }, 189 | "execution_count": null, 190 | "outputs": [ 191 | { 192 | "output_type": "stream", 193 | "name": "stdout", 194 | "text": [ 195 | "2\n" 196 | ] 197 | }, 198 | { 199 | "output_type": "execute_result", 200 | "data": { 201 | "text/plain": [ 202 | "2" 203 | ] 204 | }, 205 | "metadata": {}, 206 | "execution_count": 9 207 | } 208 | ] 209 | }, 210 | { 211 | "cell_type": "code", 212 | "source": [ 213 | "citis = [\"delhi\", \"mumbai\", \"indore\", \"banglore\", \"bhopal\"]\n", 214 | "heros = [\"ironman\", \"spiderman\", \"hulk\", \"blackpanther\"]\n", 215 | "\n", 216 | "def print_len(list):\n", 217 | " print(len(list))\n", 218 | "\n", 219 | "print_len(citis)\n", 220 | "print_len(heros)\n" 221 | ], 222 | "metadata": { 223 | "colab": { 224 | "base_uri": "https://localhost:8080/" 225 | }, 226 | "id": "FhQoOdNSy7n0", 227 | "outputId": "caa17b03-f9da-451c-dcd9-b78e47914897" 228 | }, 229 | "execution_count": null, 230 | "outputs": [ 231 | { 232 | "output_type": "stream", 233 | "name": "stdout", 234 | "text": [ 235 | "5\n", 236 | "4\n" 237 | ] 238 | } 239 | ] 240 | }, 241 | { 242 | "cell_type": "code", 243 | "source": [ 244 | "citis = [\"delhi\", \"mumbai\", \"indore\", \"banglore\", \"bhopal\"]\n", 245 | "heros = [\"ironman\", \"spiderman\", \"hulk\", \"blackpanther\"]\n", 246 | "\n", 247 | "print(heros[0], end=\" \")\n", 248 | "print(heros[1], end=\" \")\n", 249 | "\n", 250 | "def print_len(list):\n", 251 | " print(len(list))\n", 252 | "\n", 253 | "print_len(citis)\n", 254 | "print_len(heros)\n" 255 | ], 256 | "metadata": { 257 | "colab": { 258 | "base_uri": "https://localhost:8080/" 259 | }, 260 | "id": "inzNMzINzszj", 261 | "outputId": "d236269b-068d-42b7-e539-3c74aef715d9" 262 | }, 263 | "execution_count": null, 264 | "outputs": [ 265 | { 266 | "output_type": "stream", 267 | "name": "stdout", 268 | "text": [ 269 | "ironman spiderman 5\n", 270 | "4\n" 271 | ] 272 | } 273 | ] 274 | }, 275 | { 276 | "cell_type": "code", 277 | "source": [ 278 | "citis = [\"delhi\", \"mumbai\", \"indore\", \"banglore\", \"bhopal\"]\n", 279 | "heros = [\"ironman\", \"spiderman\", \"hulk\", \"blackpanther\"]\n", 280 | "\n", 281 | "def print_len(list):\n", 282 | " print(len(list))\n", 283 | "\n", 284 | "def print_list(list):\n", 285 | " for item in list:\n", 286 | " print(item, end=\" \")\n", 287 | "\n", 288 | "print_list(heros)" 289 | ], 290 | "metadata": { 291 | "colab": { 292 | "base_uri": "https://localhost:8080/" 293 | }, 294 | "id": "eOzaXsksz9WJ", 295 | "outputId": "3f1e0291-17ae-4fa4-c155-8464ad492f36" 296 | }, 297 | "execution_count": null, 298 | "outputs": [ 299 | { 300 | "output_type": "stream", 301 | "name": "stdout", 302 | "text": [ 303 | "ironman spiderman hulk blackpanther " 304 | ] 305 | } 306 | ] 307 | }, 308 | { 309 | "cell_type": "code", 310 | "source": [ 311 | "n=5\n", 312 | "def cal_fact(n):\n", 313 | " fact = 1\n", 314 | " for i in range(1, n+1):\n", 315 | " fact *= i\n", 316 | " print(fact)\n", 317 | "cal_fact(5)" 318 | ], 319 | "metadata": { 320 | "colab": { 321 | "base_uri": "https://localhost:8080/" 322 | }, 323 | "id": "-07jXy1d0w-T", 324 | "outputId": "be09c83b-fb23-4a0b-de21-83e997f46527" 325 | }, 326 | "execution_count": null, 327 | "outputs": [ 328 | { 329 | "output_type": "stream", 330 | "name": "stdout", 331 | "text": [ 332 | "120\n" 333 | ] 334 | } 335 | ] 336 | }, 337 | { 338 | "cell_type": "code", 339 | "source": [ 340 | "#USD to INR\n", 341 | "#1USD = 83INR\n", 342 | "def converter(usd_val):\n", 343 | " inr_val = usd_val * 83\n", 344 | " print(usd_val, \"USD = \", inr_val, \"INR\")\n", 345 | "converter(int(input(\"Enter the USD value:\")))" 346 | ], 347 | "metadata": { 348 | "colab": { 349 | "base_uri": "https://localhost:8080/" 350 | }, 351 | "id": "jFdgzLoJ2FeE", 352 | "outputId": "12418660-063c-435c-de4b-8a8d39535a94" 353 | }, 354 | "execution_count": null, 355 | "outputs": [ 356 | { 357 | "output_type": "stream", 358 | "name": "stdout", 359 | "text": [ 360 | "Enter the USD value:65\n", 361 | "65 USD = 5395 INR\n" 362 | ] 363 | } 364 | ] 365 | }, 366 | { 367 | "cell_type": "code", 368 | "source": [ 369 | "def eo(num):\n", 370 | " if num % 2 == 0:\n", 371 | " print(\"number is even\")\n", 372 | " else:\n", 373 | " print(\"number is odd\")\n", 374 | "eo(int(input(\"Enter the number:\")))" 375 | ], 376 | "metadata": { 377 | "id": "H_B1p1O72p3q", 378 | "colab": { 379 | "base_uri": "https://localhost:8080/" 380 | }, 381 | "outputId": "5279cfa1-0207-4925-cece-c74bcb70c580" 382 | }, 383 | "execution_count": null, 384 | "outputs": [ 385 | { 386 | "output_type": "stream", 387 | "name": "stdout", 388 | "text": [ 389 | "Enter the number:23\n", 390 | "number is odd\n" 391 | ] 392 | } 393 | ] 394 | }, 395 | { 396 | "cell_type": "code", 397 | "source": [ 398 | "#RECURSION\n", 399 | "def show(n):\n", 400 | " if(n == 0):\n", 401 | " return\n", 402 | " print(n)\n", 403 | " show(n-1)\n", 404 | "show(5)" 405 | ], 406 | "metadata": { 407 | "colab": { 408 | "base_uri": "https://localhost:8080/" 409 | }, 410 | "id": "EgMnO1hGpA3Z", 411 | "outputId": "436ebdfa-edfb-4c7f-b9e3-89871419c6b7" 412 | }, 413 | "execution_count": null, 414 | "outputs": [ 415 | { 416 | "output_type": "stream", 417 | "name": "stdout", 418 | "text": [ 419 | "5\n", 420 | "4\n", 421 | "3\n", 422 | "2\n", 423 | "1\n" 424 | ] 425 | } 426 | ] 427 | }, 428 | { 429 | "cell_type": "code", 430 | "source": [ 431 | "#factorial of a number\n", 432 | "def fact(n):\n", 433 | " if(n==0 or n==1):\n", 434 | " return 1\n", 435 | " return n * fact(n-1)\n", 436 | "print(fact(6))" 437 | ], 438 | "metadata": { 439 | "id": "O30rkxVJq5AE", 440 | "colab": { 441 | "base_uri": "https://localhost:8080/" 442 | }, 443 | "outputId": "e06fcfcd-0b5f-451b-bbd2-7356dd9a7965" 444 | }, 445 | "execution_count": 7, 446 | "outputs": [ 447 | { 448 | "output_type": "stream", 449 | "name": "stdout", 450 | "text": [ 451 | "720\n" 452 | ] 453 | } 454 | ] 455 | }, 456 | { 457 | "cell_type": "code", 458 | "source": [ 459 | "#calculate sum of first n natural numbers\n", 460 | "def calc_sum(n):\n", 461 | " if(n==0):\n", 462 | " return 0\n", 463 | " return calc_sum(n-1) + n\n", 464 | "\n", 465 | "sum = calc_sum(5)\n", 466 | "print(sum)" 467 | ], 468 | "metadata": { 469 | "colab": { 470 | "base_uri": "https://localhost:8080/" 471 | }, 472 | "id": "_M1UMwLdQjfA", 473 | "outputId": "0f2c72b3-9f0c-4187-d555-0f2e86f7f1f7" 474 | }, 475 | "execution_count": 8, 476 | "outputs": [ 477 | { 478 | "output_type": "stream", 479 | "name": "stdout", 480 | "text": [ 481 | "15\n" 482 | ] 483 | } 484 | ] 485 | }, 486 | { 487 | "cell_type": "code", 488 | "source": [ 489 | "#recursive function to print all emlements in a list\n", 490 | "def print_list(list, idx=0):\n", 491 | " if (idx == len(list)):\n", 492 | " return\n", 493 | " print(list[idx])\n", 494 | " print_list(list,idx+1)\n", 495 | "\n", 496 | "fruits = [\"mango\", \"litchi\", \"apple\", \"banana\"]\n", 497 | "print_list(fruits)" 498 | ], 499 | "metadata": { 500 | "colab": { 501 | "base_uri": "https://localhost:8080/" 502 | }, 503 | "id": "2dQN9qVASdIh", 504 | "outputId": "d3fdad6e-10b3-4a8a-80f2-7d8604c33fc1" 505 | }, 506 | "execution_count": 9, 507 | "outputs": [ 508 | { 509 | "output_type": "stream", 510 | "name": "stdout", 511 | "text": [ 512 | "mango\n", 513 | "litchi\n", 514 | "apple\n", 515 | "banana\n" 516 | ] 517 | } 518 | ] 519 | }, 520 | { 521 | "cell_type": "code", 522 | "source": [], 523 | "metadata": { 524 | "id": "r9S4D_YyTin2" 525 | }, 526 | "execution_count": null, 527 | "outputs": [] 528 | } 529 | ] 530 | } -------------------------------------------------------------------------------- /General Python Programming.md/7_PYTHON_Data_Types.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "provenance": [] 7 | }, 8 | "kernelspec": { 9 | "name": "python3", 10 | "display_name": "Python 3" 11 | }, 12 | "language_info": { 13 | "name": "python" 14 | } 15 | }, 16 | "cells": [ 17 | { 18 | "cell_type": "markdown", 19 | "source": [ 20 | "##DATA TYPES in PYTHON\n", 21 | "\n", 22 | "Data types are the classification or categorization of knowledge items. It represents the type useful that tells what operations are often performed on specific data. Since everything is an object in Python programming, data types are classes and variables are instances (object) of those classes.\n", 23 | "\n", 24 | "The following are the Data types in python : \n", 25 | "* NUMERIC\n", 26 | " * Integer\n", 27 | " * Complex Numbers\n", 28 | " * Float\n", 29 | "* SEQUENCE TYPE\n", 30 | " * String\n", 31 | " * List\n", 32 | " * Tuple\n", 33 | "* SET\n", 34 | "\n", 35 | "* BOOLEAN\n", 36 | "\n", 37 | "* DICTIONARY\n", 38 | "\n", 39 | "\n", 40 | "\n" 41 | ], 42 | "metadata": { 43 | "id": "ReaKqTtTq5dW" 44 | } 45 | }, 46 | { 47 | "cell_type": "markdown", 48 | "source": [ 49 | "###BUILT IN DATA TYPES\n", 50 | "Following are the built in data types in python:\n", 51 | "* TEXT TYPE: str\n", 52 | "* NUMERIC TYPE: int, float, complex\n", 53 | "* SEQUENCE TYPE: list, tuple, range\n", 54 | "* MAPPING TYPE: dict\n", 55 | "* SET TYPES: set, frozenset\n", 56 | "* BOOLEAN TYPE: bool\n", 57 | "* BINART TYPE: bytes, bytearray, memoryview\n", 58 | "* NONE TYPE: none type " 59 | ], 60 | "metadata": { 61 | "id": "jDN8iXfZsuet" 62 | } 63 | }, 64 | { 65 | "cell_type": "markdown", 66 | "source": [ 67 | "###SETTING A DATA TYPE\n", 68 | "In Python, the data type is set when you assign a value to a variable:" 69 | ], 70 | "metadata": { 71 | "id": "B4o5vqGCuNSl" 72 | } 73 | }, 74 | { 75 | "cell_type": "code", 76 | "source": [ 77 | "#EAMPLE 1\n", 78 | "x = \"Hello World\"\n", 79 | "print(type(x)) #str or string: In computer programming, a string is a sequence of characters. For example, \"hello\" is a string containing a sequence of characters 'h' , 'e' , 'l' , 'l' , and 'o' ." 80 | ], 81 | "metadata": { 82 | "colab": { 83 | "base_uri": "https://localhost:8080/" 84 | }, 85 | "id": "IlQ9M6XSuQ2z", 86 | "outputId": "30fb5048-0d00-4079-f7a9-9841c731c729" 87 | }, 88 | "execution_count": 3, 89 | "outputs": [ 90 | { 91 | "output_type": "stream", 92 | "name": "stdout", 93 | "text": [ 94 | "\n" 95 | ] 96 | } 97 | ] 98 | }, 99 | { 100 | "cell_type": "code", 101 | "source": [ 102 | "#EAMPLE 2\n", 103 | "x = 45\n", 104 | "print(type(x)) # Int, or integer: is a whole number, positive or negative, without decimals, of unlimited length." 105 | ], 106 | "metadata": { 107 | "colab": { 108 | "base_uri": "https://localhost:8080/" 109 | }, 110 | "id": "eOD8QWZrucIB", 111 | "outputId": "74bb5b9a-ac2f-40ad-83c5-bd78a406da24" 112 | }, 113 | "execution_count": 2, 114 | "outputs": [ 115 | { 116 | "output_type": "stream", 117 | "name": "stdout", 118 | "text": [ 119 | "\n" 120 | ] 121 | } 122 | ] 123 | }, 124 | { 125 | "cell_type": "code", 126 | "source": [ 127 | "#EAMPLE 3\n", 128 | "x = 45.2\n", 129 | "print(type(x)) #float or Floating point numbers are decimal values or fractional numbers like 133.5, 2897.11, and 3571.213, whereas real numbers like 56, 2, and 33 are called integers." 130 | ], 131 | "metadata": { 132 | "colab": { 133 | "base_uri": "https://localhost:8080/" 134 | }, 135 | "id": "QW2MT2nYumb6", 136 | "outputId": "64397222-a7f1-4cca-fe52-4be6b6d232e5" 137 | }, 138 | "execution_count": 18, 139 | "outputs": [ 140 | { 141 | "output_type": "stream", 142 | "name": "stdout", 143 | "text": [ 144 | "\n" 145 | ] 146 | } 147 | ] 148 | }, 149 | { 150 | "cell_type": "code", 151 | "source": [ 152 | "#EAMPLE 4\n", 153 | "x = 1j\n", 154 | "print(type(x)) #The complex data type in python consists of two values, the first one is the real part of the complex number, and the second one is the imaginary part of the complex number. We usually denote the real part using i and the imaginary part with j. For example, (3 + 7j) or (3i + 7j)." 155 | ], 156 | "metadata": { 157 | "colab": { 158 | "base_uri": "https://localhost:8080/" 159 | }, 160 | "id": "_VdR1_Kkvd9k", 161 | "outputId": "1f92e495-b286-4df0-c308-435394d6f7c4" 162 | }, 163 | "execution_count": 21, 164 | "outputs": [ 165 | { 166 | "output_type": "stream", 167 | "name": "stdout", 168 | "text": [ 169 | "\n" 170 | ] 171 | } 172 | ] 173 | }, 174 | { 175 | "cell_type": "code", 176 | "source": [ 177 | "#EAMPLE 5\n", 178 | "x = [\"Car\", \"Bikes\", \"Ships\"]\n", 179 | "print(type(x)) #List. Lists are used to store multiple items in a single variable. Lists are one of 4 built-in data types in Python used to store collections of data, the other 3 are Tuple, Set, and Dictionary, all with different qualities and usage." 180 | ], 181 | "metadata": { 182 | "colab": { 183 | "base_uri": "https://localhost:8080/" 184 | }, 185 | "id": "cVB6XQDeveL0", 186 | "outputId": "fb4b01a9-3212-4ad3-ac49-f4a76f3e07b9" 187 | }, 188 | "execution_count": 23, 189 | "outputs": [ 190 | { 191 | "output_type": "stream", 192 | "name": "stdout", 193 | "text": [ 194 | "\n" 195 | ] 196 | } 197 | ] 198 | }, 199 | { 200 | "cell_type": "code", 201 | "source": [ 202 | "#EAMPLE 6\n", 203 | "x = (\"Car\", \"Bikes\", \"Ships\")\n", 204 | "print(type(x)) #Tuples are used to store multiple items in a single variable. A tuple is a collection which is ordered and unchangeable." 205 | ], 206 | "metadata": { 207 | "colab": { 208 | "base_uri": "https://localhost:8080/" 209 | }, 210 | "id": "irfFnIKpveZc", 211 | "outputId": "7d2cc139-289e-4d53-c5da-d74f0aab2ee8" 212 | }, 213 | "execution_count": 25, 214 | "outputs": [ 215 | { 216 | "output_type": "stream", 217 | "name": "stdout", 218 | "text": [ 219 | "\n" 220 | ] 221 | } 222 | ] 223 | }, 224 | { 225 | "cell_type": "code", 226 | "source": [ 227 | "#EAMPLE 7\n", 228 | "x = range(6)\n", 229 | "print(x)\n", 230 | "print(type(x)) #It is an in-built function in Python which returns a sequence of numbers starting from 0 and increments to 1 until it reaches a specified number" 231 | ], 232 | "metadata": { 233 | "colab": { 234 | "base_uri": "https://localhost:8080/" 235 | }, 236 | "id": "Amc8VyBovem8", 237 | "outputId": "2f4a68ef-3f39-4286-d4b7-df287954e00a" 238 | }, 239 | "execution_count": 27, 240 | "outputs": [ 241 | { 242 | "output_type": "stream", 243 | "name": "stdout", 244 | "text": [ 245 | "range(0, 6)\n", 246 | "\n" 247 | ] 248 | } 249 | ] 250 | }, 251 | { 252 | "cell_type": "code", 253 | "source": [ 254 | "#EAMPLE 8\n", 255 | "x = {\"Car\" : \"Bike\", \"Ship\" : 21}\t\n", 256 | "print(type(x)) #Dictionaries are used to store data values in key:value pairs. A dictionary is a collection which is ordered*, changeable and do not allow duplicates" 257 | ], 258 | "metadata": { 259 | "colab": { 260 | "base_uri": "https://localhost:8080/" 261 | }, 262 | "id": "Nb75Pk4Bvexs", 263 | "outputId": "43bcdd50-1aff-4849-dee1-aae2e8917284" 264 | }, 265 | "execution_count": 30, 266 | "outputs": [ 267 | { 268 | "output_type": "stream", 269 | "name": "stdout", 270 | "text": [ 271 | "\n" 272 | ] 273 | } 274 | ] 275 | }, 276 | { 277 | "cell_type": "code", 278 | "source": [ 279 | "#EAMPLE 9\n", 280 | "x = {\"Cars\", \"Bikes\", \"Ships\"}\t\n", 281 | "print(type(x)) #A set is a collection which is unordered, unchangeable*, and unindexed." 282 | ], 283 | "metadata": { 284 | "colab": { 285 | "base_uri": "https://localhost:8080/" 286 | }, 287 | "id": "-bqLikGave7c", 288 | "outputId": "2a2dcb89-1527-405c-e3dd-6a01c60cddc9" 289 | }, 290 | "execution_count": 31, 291 | "outputs": [ 292 | { 293 | "output_type": "stream", 294 | "name": "stdout", 295 | "text": [ 296 | "\n" 297 | ] 298 | } 299 | ] 300 | }, 301 | { 302 | "cell_type": "code", 303 | "source": [ 304 | "#EAMPLE 10\n", 305 | "x = frozenset({\"Cars\", \"Bikes\", \"Ships\"})\n", 306 | "print(type(x)) #In Python, frozenset() is a function that takes an iterable and returns its frozenset object counterpart. A frozenset object is an immutable, unordered data type. A frozenset contains unique values, which makes it very similar to set data type. It is immutable like tuples." 307 | ], 308 | "metadata": { 309 | "colab": { 310 | "base_uri": "https://localhost:8080/" 311 | }, 312 | "id": "j9YmBWtPvfEU", 313 | "outputId": "938a438f-ac1e-4e0f-a713-b1523df1413a" 314 | }, 315 | "execution_count": 33, 316 | "outputs": [ 317 | { 318 | "output_type": "stream", 319 | "name": "stdout", 320 | "text": [ 321 | "\n" 322 | ] 323 | } 324 | ] 325 | }, 326 | { 327 | "cell_type": "code", 328 | "source": [ 329 | "#EAMPLE 11\n", 330 | "x = True\t\n", 331 | "print(type(x)) #Python boolean type is one of the built-in data types provided by Python, which represents one of the two values i.e. True or False" 332 | ], 333 | "metadata": { 334 | "colab": { 335 | "base_uri": "https://localhost:8080/" 336 | }, 337 | "id": "ZIWlFC6qvfM8", 338 | "outputId": "f06ba15a-b79b-49f7-8ad1-71978b29e50c" 339 | }, 340 | "execution_count": 35, 341 | "outputs": [ 342 | { 343 | "output_type": "stream", 344 | "name": "stdout", 345 | "text": [ 346 | "\n" 347 | ] 348 | } 349 | ] 350 | }, 351 | { 352 | "cell_type": "code", 353 | "source": [ 354 | "#EAMPLE 12\n", 355 | "x = b\"Hello\"\t\n", 356 | "print(type(x)) #The BYTE data type stores any kind of binary data in an undifferentiated byte stream." 357 | ], 358 | "metadata": { 359 | "colab": { 360 | "base_uri": "https://localhost:8080/" 361 | }, 362 | "id": "npFPxp_HvfVt", 363 | "outputId": "ad13f04c-4fb7-4d8e-a9f1-47d21ff6c916" 364 | }, 365 | "execution_count": 36, 366 | "outputs": [ 367 | { 368 | "output_type": "stream", 369 | "name": "stdout", 370 | "text": [ 371 | "\n" 372 | ] 373 | } 374 | ] 375 | }, 376 | { 377 | "cell_type": "code", 378 | "source": [ 379 | "#EAMPLE 13\n", 380 | "x = bytearray(5)\t\n", 381 | "print(type(x)) #The bytearray type is a mutable sequence of integers in the range 0 <= x < 256." 382 | ], 383 | "metadata": { 384 | "colab": { 385 | "base_uri": "https://localhost:8080/" 386 | }, 387 | "id": "SuHu4KMPvffM", 388 | "outputId": "be35cc4a-ec8e-43dc-a729-0883e7913257" 389 | }, 390 | "execution_count": 37, 391 | "outputs": [ 392 | { 393 | "output_type": "stream", 394 | "name": "stdout", 395 | "text": [ 396 | "\n" 397 | ] 398 | } 399 | ] 400 | }, 401 | { 402 | "cell_type": "code", 403 | "source": [ 404 | "#EXAMPLE 14\n", 405 | "x = memoryview(bytes(5))\n", 406 | "print(type(x)) #memoryview objects allow Python code to access the internal data of an object that supports the buffer protocol without copying. The memoryview() function allows direct read and write access to an object's byte-oriented data without needing to copy it first." 407 | ], 408 | "metadata": { 409 | "colab": { 410 | "base_uri": "https://localhost:8080/" 411 | }, 412 | "id": "7sWfe8d_z1rq", 413 | "outputId": "f6556514-a644-4f7d-a904-01bfc8a35e1a" 414 | }, 415 | "execution_count": 42, 416 | "outputs": [ 417 | { 418 | "output_type": "stream", 419 | "name": "stdout", 420 | "text": [ 421 | "\n" 422 | ] 423 | } 424 | ] 425 | }, 426 | { 427 | "cell_type": "code", 428 | "source": [ 429 | "#EXAMPLE 15\n", 430 | "x = None\t\n", 431 | "print(type(x)) #The None keyword is used to define a null value, or no value at all. None is not the same as 0, False, or an empty string. None is a data type of its own (NoneType) and only None can be None." 432 | ], 433 | "metadata": { 434 | "colab": { 435 | "base_uri": "https://localhost:8080/" 436 | }, 437 | "id": "8xBUOEfX0Iuq", 438 | "outputId": "cda56e17-7625-40b3-a70f-b3729b830dc9" 439 | }, 440 | "execution_count": 43, 441 | "outputs": [ 442 | { 443 | "output_type": "stream", 444 | "name": "stdout", 445 | "text": [ 446 | "\n" 447 | ] 448 | } 449 | ] 450 | } 451 | ] 452 | } 453 | -------------------------------------------------------------------------------- /General Python Programming.md/Dictionary_and_sets.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "provenance": [] 7 | }, 8 | "kernelspec": { 9 | "name": "python3", 10 | "display_name": "Python 3" 11 | }, 12 | "language_info": { 13 | "name": "python" 14 | } 15 | }, 16 | "cells": [ 17 | { 18 | "cell_type": "code", 19 | "source": [ 20 | "#Dictionary works in pair" 21 | ], 22 | "metadata": { 23 | "id": "4fHVjCcU5JI2" 24 | }, 25 | "execution_count": null, 26 | "outputs": [] 27 | }, 28 | { 29 | "cell_type": "code", 30 | "source": [ 31 | "info={\n", 32 | " \"Key\": \"value\",\n", 33 | " \"name\": \"aman\",\n", 34 | " \"learning\": \"python\",\n", 35 | " \"age\": 22,\n", 36 | " \"is_student\": True,\n", 37 | " \"marks\" : \"82.2\"\n", 38 | "}\n", 39 | "print(type(info))\n", 40 | "\n" 41 | ], 42 | "metadata": { 43 | "colab": { 44 | "base_uri": "https://localhost:8080/" 45 | }, 46 | "id": "uzZ4qfcr6cn1", 47 | "outputId": "01e3d225-e5d7-4301-ba7e-b7c2162dc936" 48 | }, 49 | "execution_count": null, 50 | "outputs": [ 51 | { 52 | "output_type": "stream", 53 | "name": "stdout", 54 | "text": [ 55 | "\n" 56 | ] 57 | } 58 | ] 59 | }, 60 | { 61 | "cell_type": "code", 62 | "source": [ 63 | "print(info[\"name\"])\n", 64 | "print(info[\"learning\"])\n", 65 | "print(info[\"age\"])\n", 66 | "print(info[\"is_student\"])\n", 67 | "print(info[\"marks\"])" 68 | ], 69 | "metadata": { 70 | "colab": { 71 | "base_uri": "https://localhost:8080/" 72 | }, 73 | "id": "mOIQNdcc8pJd", 74 | "outputId": "9f2e6a7e-beb9-45e8-95c3-875c3d7da8e0" 75 | }, 76 | "execution_count": null, 77 | "outputs": [ 78 | { 79 | "output_type": "stream", 80 | "name": "stdout", 81 | "text": [ 82 | "aman\n", 83 | "python\n", 84 | "22\n", 85 | "True\n", 86 | "82.2\n" 87 | ] 88 | } 89 | ] 90 | }, 91 | { 92 | "cell_type": "code", 93 | "source": [ 94 | "#NESTED DICTIONARY\n", 95 | "student = {\n", 96 | " \"name\": \"aman\",\n", 97 | " \"subjects\": {\n", 98 | " \"physics\": 78,\n", 99 | " \"chemistry\": 87,\n", 100 | " \"biology\": 97\n", 101 | " }\n", 102 | "}\n", 103 | "print(student)\n", 104 | "print(student[\"subjects\"])\n", 105 | "print(student[\"subjects\"][\"physics\"])" 106 | ], 107 | "metadata": { 108 | "colab": { 109 | "base_uri": "https://localhost:8080/" 110 | }, 111 | "id": "mt9HAi-U9udM", 112 | "outputId": "38c82116-d4a0-45f5-d60a-d633801674d6" 113 | }, 114 | "execution_count": null, 115 | "outputs": [ 116 | { 117 | "output_type": "stream", 118 | "name": "stdout", 119 | "text": [ 120 | "{'name': 'aman', 'subjects': {'physics': 78, 'chemistry': 87, 'biology': 97}}\n", 121 | "{'physics': 78, 'chemistry': 87, 'biology': 97}\n", 122 | "78\n" 123 | ] 124 | } 125 | ] 126 | }, 127 | { 128 | "cell_type": "code", 129 | "source": [ 130 | "#NESTED DICTIONARY\n", 131 | "student = {\n", 132 | " \"name\": \"aman\",\n", 133 | " \"subjects\": {\n", 134 | " \"physics\": 78,\n", 135 | " \"chemistry\": 87,\n", 136 | " \"biology\": 97\n", 137 | " }\n", 138 | "}\n", 139 | "print(len(student.keys()))\n", 140 | "print(student.keys())\n", 141 | "print(list(student.values()))\n", 142 | "print(list(student.items()))\n", 143 | "print(student.get(\"name\"))\n", 144 | "student.update({\"city\": \"Indore\" , \"age\" : \"22\"})\n", 145 | "print(student)" 146 | ], 147 | "metadata": { 148 | "colab": { 149 | "base_uri": "https://localhost:8080/" 150 | }, 151 | "id": "lhUVBubdC9A5", 152 | "outputId": "79432d72-5211-4bad-95aa-1553a7ea8209" 153 | }, 154 | "execution_count": 24, 155 | "outputs": [ 156 | { 157 | "output_type": "stream", 158 | "name": "stdout", 159 | "text": [ 160 | "2\n", 161 | "dict_keys(['name', 'subjects'])\n", 162 | "['aman', {'physics': 78, 'chemistry': 87, 'biology': 97}]\n", 163 | "[('name', 'aman'), ('subjects', {'physics': 78, 'chemistry': 87, 'biology': 97})]\n", 164 | "aman\n", 165 | "{'name': 'aman', 'subjects': {'physics': 78, 'chemistry': 87, 'biology': 97}, 'city': 'Indore', 'age': '22'}\n" 166 | ] 167 | } 168 | ] 169 | }, 170 | { 171 | "cell_type": "code", 172 | "source": [ 173 | "#STES\n", 174 | "#Collection of Unordered Items\n", 175 | "#Each element in set must be unique and immutable" 176 | ], 177 | "metadata": { 178 | "id": "FokLbXzdEHtQ" 179 | }, 180 | "execution_count": 26, 181 | "outputs": [] 182 | }, 183 | { 184 | "cell_type": "code", 185 | "source": [ 186 | " collection={1,2,3,4,5}\n", 187 | " print(type(collection))\n", 188 | " print(collection)" 189 | ], 190 | "metadata": { 191 | "colab": { 192 | "base_uri": "https://localhost:8080/" 193 | }, 194 | "id": "yj12e4fSeSMQ", 195 | "outputId": "4058d35e-59c7-4163-e80b-ce54d3342f6c" 196 | }, 197 | "execution_count": 28, 198 | "outputs": [ 199 | { 200 | "output_type": "stream", 201 | "name": "stdout", 202 | "text": [ 203 | "\n", 204 | "{1, 2, 3, 4, 5}\n" 205 | ] 206 | } 207 | ] 208 | }, 209 | { 210 | "cell_type": "code", 211 | "source": [ 212 | " collection={1,2,3,4,5,\"hello\",\"world\",\"world\"}\n", 213 | " print(type(collection))\n", 214 | " print(collection) #Duplicates not allowed" 215 | ], 216 | "metadata": { 217 | "colab": { 218 | "base_uri": "https://localhost:8080/" 219 | }, 220 | "id": "Pw1H4KH4iAuY", 221 | "outputId": "702529b3-7a7b-49e6-d846-4699380b9381" 222 | }, 223 | "execution_count": 30, 224 | "outputs": [ 225 | { 226 | "output_type": "stream", 227 | "name": "stdout", 228 | "text": [ 229 | "\n", 230 | "{1, 2, 3, 4, 5, 'hello', 'world'}\n" 231 | ] 232 | } 233 | ] 234 | }, 235 | { 236 | "cell_type": "code", 237 | "source": [ 238 | " collection={1,2,3,4,5,\"hello\",\"world\",\"world\"}\n", 239 | " print(type(collection))\n", 240 | " print(len(collection))" 241 | ], 242 | "metadata": { 243 | "colab": { 244 | "base_uri": "https://localhost:8080/" 245 | }, 246 | "id": "0ew6NnVYiJGO", 247 | "outputId": "079a689b-9aab-4c2c-b419-1f4b05d7ddd3" 248 | }, 249 | "execution_count": 31, 250 | "outputs": [ 251 | { 252 | "output_type": "stream", 253 | "name": "stdout", 254 | "text": [ 255 | "\n", 256 | "7\n" 257 | ] 258 | } 259 | ] 260 | }, 261 | { 262 | "cell_type": "code", 263 | "source": [ 264 | " collection={} #empty dict, not a set\n", 265 | " print(type(collection))" 266 | ], 267 | "metadata": { 268 | "colab": { 269 | "base_uri": "https://localhost:8080/" 270 | }, 271 | "id": "c5KTJs3-iQCu", 272 | "outputId": "0f9952e5-ed04-4bbe-9404-d73b16ea2157" 273 | }, 274 | "execution_count": 32, 275 | "outputs": [ 276 | { 277 | "output_type": "stream", 278 | "name": "stdout", 279 | "text": [ 280 | "\n" 281 | ] 282 | } 283 | ] 284 | }, 285 | { 286 | "cell_type": "code", 287 | "source": [ 288 | " collection=set() #set\n", 289 | " print(type(collection))" 290 | ], 291 | "metadata": { 292 | "colab": { 293 | "base_uri": "https://localhost:8080/" 294 | }, 295 | "id": "yqHx_VKBiZNH", 296 | "outputId": "24fd856c-c74a-4903-da49-8e74919ebad5" 297 | }, 298 | "execution_count": 33, 299 | "outputs": [ 300 | { 301 | "output_type": "stream", 302 | "name": "stdout", 303 | "text": [ 304 | "\n" 305 | ] 306 | } 307 | ] 308 | }, 309 | { 310 | "cell_type": "code", 311 | "source": [ 312 | "collection = set()\n", 313 | "collection.add(1)\n", 314 | "collection.add(2)\n", 315 | "collection.add(3)\n", 316 | "collection.add(4)\n", 317 | "collection.add(4) #dosent print duplicate elements\n", 318 | "print(collection)" 319 | ], 320 | "metadata": { 321 | "colab": { 322 | "base_uri": "https://localhost:8080/" 323 | }, 324 | "id": "ZsS80oYGihG3", 325 | "outputId": "7bf9d7ce-dc68-4504-ce32-af3a854f8db7" 326 | }, 327 | "execution_count": 35, 328 | "outputs": [ 329 | { 330 | "output_type": "stream", 331 | "name": "stdout", 332 | "text": [ 333 | "{1, 2, 3, 4}\n" 334 | ] 335 | } 336 | ] 337 | }, 338 | { 339 | "cell_type": "code", 340 | "source": [ 341 | "collection = set()\n", 342 | "collection.add(1)\n", 343 | "collection.add(2)\n", 344 | "collection.add(3)\n", 345 | "collection.add(4)\n", 346 | "collection.add(4) #dosent print duplicate elements\n", 347 | "collection.remove(4) #removes all duplicate and original elements\n", 348 | "print(collection)" 349 | ], 350 | "metadata": { 351 | "colab": { 352 | "base_uri": "https://localhost:8080/" 353 | }, 354 | "id": "IlaEDBMUjATQ", 355 | "outputId": "e8bef6f4-fbea-4afb-fe4e-5c990ce1fff5" 356 | }, 357 | "execution_count": 38, 358 | "outputs": [ 359 | { 360 | "output_type": "stream", 361 | "name": "stdout", 362 | "text": [ 363 | "{1, 2, 3}\n" 364 | ] 365 | } 366 | ] 367 | }, 368 | { 369 | "cell_type": "code", 370 | "source": [ 371 | "collection = set()\n", 372 | "collection.add(1)\n", 373 | "collection.add(2)\n", 374 | "collection.add(3)\n", 375 | "collection.add(4)\n", 376 | "collection.add(4)\n", 377 | "collection.remove(4)\n", 378 | "collection.clear\n", 379 | "print(len(collection))" 380 | ], 381 | "metadata": { 382 | "colab": { 383 | "base_uri": "https://localhost:8080/" 384 | }, 385 | "id": "20NWtWSnjJvQ", 386 | "outputId": "0a6ff119-2846-4ad7-a216-66e9396b6226" 387 | }, 388 | "execution_count": 41, 389 | "outputs": [ 390 | { 391 | "output_type": "stream", 392 | "name": "stdout", 393 | "text": [ 394 | "3\n" 395 | ] 396 | } 397 | ] 398 | }, 399 | { 400 | "cell_type": "code", 401 | "source": [ 402 | "collection={\"python\",\"java\",\"cpp\",\"html\",\"js\",\"css\"}\n", 403 | "print(collection.pop()) #randomly pop out i.e. picks out any element from the collection" 404 | ], 405 | "metadata": { 406 | "colab": { 407 | "base_uri": "https://localhost:8080/" 408 | }, 409 | "id": "Y-z0bqium8uB", 410 | "outputId": "1e727c6c-71d7-468e-e5f5-f830dfe719cc" 411 | }, 412 | "execution_count": 46, 413 | "outputs": [ 414 | { 415 | "output_type": "stream", 416 | "name": "stdout", 417 | "text": [ 418 | "css\n" 419 | ] 420 | } 421 | ] 422 | }, 423 | { 424 | "cell_type": "code", 425 | "source": [ 426 | "#UNION-combines both set values and returns new\n", 427 | "set1={1,2,3}\n", 428 | "set2={2,3,4}\n", 429 | "print(set1.union(set2))\n", 430 | "print(set1)\n", 431 | "print(set2)" 432 | ], 433 | "metadata": { 434 | "colab": { 435 | "base_uri": "https://localhost:8080/" 436 | }, 437 | "id": "gtgg0ca3nXY6", 438 | "outputId": "9b79fc3b-1928-498c-b439-ccfc2b04bd25" 439 | }, 440 | "execution_count": 49, 441 | "outputs": [ 442 | { 443 | "output_type": "stream", 444 | "name": "stdout", 445 | "text": [ 446 | "{1, 2, 3, 4}\n", 447 | "{1, 2, 3}\n", 448 | "{2, 3, 4}\n" 449 | ] 450 | } 451 | ] 452 | }, 453 | { 454 | "cell_type": "code", 455 | "source": [ 456 | "#INTERSECTION-combines both set values and returns new\n", 457 | "set1={1,2,3}\n", 458 | "set2={2,3,4}\n", 459 | "print(set1.intersection(set2))\n" 460 | ], 461 | "metadata": { 462 | "colab": { 463 | "base_uri": "https://localhost:8080/" 464 | }, 465 | "id": "mT6zjTa8oJCy", 466 | "outputId": "32c72714-f188-487c-e952-f19cc90bf577" 467 | }, 468 | "execution_count": 51, 469 | "outputs": [ 470 | { 471 | "output_type": "stream", 472 | "name": "stdout", 473 | "text": [ 474 | "{2, 3}\n" 475 | ] 476 | } 477 | ] 478 | }, 479 | { 480 | "cell_type": "code", 481 | "source": [ 482 | "#store the following word meanings in a dictionary\n", 483 | "#table : \"a piece of furniture\" , \"list of facts and figures\"\n", 484 | "#cat : \"a small animal\"\n", 485 | "dictionary= {\n", 486 | " \"table\": [\"a piece of furniture\", \"list of facts and figures\"],\n", 487 | " \"cat\": \"a small animal\"\n", 488 | "}\n", 489 | "print(dictionary)" 490 | ], 491 | "metadata": { 492 | "colab": { 493 | "base_uri": "https://localhost:8080/" 494 | }, 495 | "id": "TDT2LP-3ohz_", 496 | "outputId": "c03304fc-63ed-4aee-cfb2-19f105e27bce" 497 | }, 498 | "execution_count": 54, 499 | "outputs": [ 500 | { 501 | "output_type": "stream", 502 | "name": "stdout", 503 | "text": [ 504 | "{'table': ['a piece of furniture', 'list of facts and figures'], 'cat': 'a small animal'}\n" 505 | ] 506 | } 507 | ] 508 | }, 509 | { 510 | "cell_type": "code", 511 | "source": [ 512 | "'''you are given a list of subjects for students. Assume one class is required\n", 513 | "for 1 subject. How many classrooms are needed by all students\n", 514 | "\"python\", \"java\", \"C++\", \"python\", \"javascript\", \"java\", \"python\", \"C++\", \"C\" '''\n", 515 | "\n", 516 | "#CODE:\n", 517 | "subjects = {\n", 518 | " \"python\", \"java\", \"C++\", \"python\", \"javascript\", \"java\", \"python\", \"C++\", \"C\"\n", 519 | "}\n", 520 | "print(len(subjects))" 521 | ], 522 | "metadata": { 523 | "colab": { 524 | "base_uri": "https://localhost:8080/" 525 | }, 526 | "id": "yNujX3GZpJW4", 527 | "outputId": "b204cb24-983a-4067-8aa1-a03b4993e6d0" 528 | }, 529 | "execution_count": 61, 530 | "outputs": [ 531 | { 532 | "output_type": "stream", 533 | "name": "stdout", 534 | "text": [ 535 | "5\n" 536 | ] 537 | } 538 | ] 539 | } 540 | ] 541 | } -------------------------------------------------------------------------------- /General Python Programming.md/9_PYTHON_String_Data_Type.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "provenance": [] 7 | }, 8 | "kernelspec": { 9 | "name": "python3", 10 | "display_name": "Python 3" 11 | }, 12 | "language_info": { 13 | "name": "python" 14 | } 15 | }, 16 | "cells": [ 17 | { 18 | "cell_type": "markdown", 19 | "source": [ 20 | "#PYTHON STRINGS\n", 21 | "#####Python string is the collection of the characters surrounded by single quotes, double quotes, or triple quotes. The computer does not understand the characters; internally, it stores manipulated character as the combination of the 0's and 1's.\n", 22 | "#####Each character is encoded in the ASCII or Unicode character. So we can say that Python strings are also called the collection of Unicode characters.\n", 23 | "\n", 24 | "#####In Python, strings can be created by enclosing the character or the sequence of characters in the quotes. Python allows us to use single quotes, double quotes, or triple quotes to create the string.\n", 25 | "\n", 26 | "#####Strings in python are surroundes by double quotation and single quotation, however both are same" 27 | ], 28 | "metadata": { 29 | "id": "ctucmb4vY5-5" 30 | } 31 | }, 32 | { 33 | "cell_type": "code", 34 | "source": [ 35 | "#\"Python\" and 'Python' are same\n", 36 | "print(\"Python\")\n", 37 | "print('Python')" 38 | ], 39 | "metadata": { 40 | "id": "phzcfhp0Y69_", 41 | "colab": { 42 | "base_uri": "https://localhost:8080/" 43 | }, 44 | "outputId": "386c9bf1-0d32-445a-d6cf-ace7f78491ce" 45 | }, 46 | "execution_count": null, 47 | "outputs": [ 48 | { 49 | "output_type": "stream", 50 | "name": "stdout", 51 | "text": [ 52 | "Python\n", 53 | "Python\n" 54 | ] 55 | } 56 | ] 57 | }, 58 | { 59 | "cell_type": "code", 60 | "source": [ 61 | "#Assigning string to a variable\n", 62 | "x=\"Python\"\n", 63 | "print(x)" 64 | ], 65 | "metadata": { 66 | "colab": { 67 | "base_uri": "https://localhost:8080/" 68 | }, 69 | "id": "Hu7HIRSxzlia", 70 | "outputId": "19cb719e-cd95-46d4-f5fc-c568cd96bcd3" 71 | }, 72 | "execution_count": null, 73 | "outputs": [ 74 | { 75 | "output_type": "stream", 76 | "name": "stdout", 77 | "text": [ 78 | "Python\n" 79 | ] 80 | } 81 | ] 82 | }, 83 | { 84 | "cell_type": "markdown", 85 | "source": [ 86 | "###Multiline String\n", 87 | "Multiline strings can be assignes using triple quotation. This can be either single or double quotation (\"\"\"string\"\"\") or ('''string''')" 88 | ], 89 | "metadata": { 90 | "id": "_aSpCFeNz6lX" 91 | } 92 | }, 93 | { 94 | "cell_type": "code", 95 | "source": [ 96 | "#Multiline String Examole\n", 97 | "a=\"\"\"Apple, Mango, Banana,\n", 98 | "Orangr, Guava,\n", 99 | "Pineapple, Strawberry\"\"\"\n", 100 | "print(a)\n", 101 | "\n", 102 | "# OR\n", 103 | "\n", 104 | "b='''Apple, Mango, Banana,\n", 105 | "Orangr, Guava,\n", 106 | "Pineapple, Strawberry'''\n", 107 | "print(b)" 108 | ], 109 | "metadata": { 110 | "colab": { 111 | "base_uri": "https://localhost:8080/" 112 | }, 113 | "id": "ap6JcBJUz1pW", 114 | "outputId": "42e3a403-45d6-4fb1-b186-dec1486e0fdb" 115 | }, 116 | "execution_count": null, 117 | "outputs": [ 118 | { 119 | "output_type": "stream", 120 | "name": "stdout", 121 | "text": [ 122 | "Apple, Mango, Banana,\n", 123 | "Orangr, Guava,\n", 124 | "Pineapple, Strawberry\n", 125 | "Apple, Mango, Banana,\n", 126 | "Orangr, Guava,\n", 127 | "Pineapple, Strawberry\n" 128 | ] 129 | } 130 | ] 131 | }, 132 | { 133 | "cell_type": "markdown", 134 | "source": [ 135 | "###STRING INDEXING & SPLITTING\n", 136 | "#####The indexing of the Python strings starts from 0. For example, The string \"HELLO\" is indexed as given in the below figure.\n", 137 | "\n", 138 | "#### H----E----L----L----o\n", 139 | "#### 0----1----2----3----4" 140 | ], 141 | "metadata": { 142 | "id": "le3deEXwV_eF" 143 | } 144 | }, 145 | { 146 | "cell_type": "code", 147 | "source": [ 148 | "#String indexing example\n", 149 | "str=\"HELLO\"\n", 150 | "print(str[0])\n", 151 | "print(str[1])\n", 152 | "print(str[2])\n", 153 | "print(str[3])\n", 154 | "print(str[4])" 155 | ], 156 | "metadata": { 157 | "colab": { 158 | "base_uri": "https://localhost:8080/" 159 | }, 160 | "id": "XEVPgZTXWaHO", 161 | "outputId": "3af9bc19-540f-49d5-883b-da41d4de026e" 162 | }, 163 | "execution_count": 1, 164 | "outputs": [ 165 | { 166 | "output_type": "stream", 167 | "name": "stdout", 168 | "text": [ 169 | "H\n", 170 | "E\n", 171 | "L\n", 172 | "L\n", 173 | "O\n" 174 | ] 175 | } 176 | ] 177 | }, 178 | { 179 | "cell_type": "markdown", 180 | "source": [ 181 | "###STRING LENGTH\n", 182 | "To get the length of a string, use the len() function." 183 | ], 184 | "metadata": { 185 | "id": "ItqrJPrIYA5z" 186 | } 187 | }, 188 | { 189 | "cell_type": "code", 190 | "source": [ 191 | "a = \"Hello, World!\"\n", 192 | "print(len(a))" 193 | ], 194 | "metadata": { 195 | "colab": { 196 | "base_uri": "https://localhost:8080/" 197 | }, 198 | "id": "eEhMpfk5YGHE", 199 | "outputId": "830c5f07-fb22-4f4a-c20f-4f4f49b3f875" 200 | }, 201 | "execution_count": 11, 202 | "outputs": [ 203 | { 204 | "output_type": "stream", 205 | "name": "stdout", 206 | "text": [ 207 | "13\n" 208 | ] 209 | } 210 | ] 211 | }, 212 | { 213 | "cell_type": "markdown", 214 | "source": [ 215 | "#####In Python, the slice operator [] is used to access the individual characters of the string. However, we can use the : (colon) operator in Python to access the substring from the given string." 216 | ], 217 | "metadata": { 218 | "id": "P9Vjx9pZXDmD" 219 | } 220 | }, 221 | { 222 | "cell_type": "code", 223 | "source": [ 224 | "str=\"HELLO\"\n", 225 | "print(str[:])" 226 | ], 227 | "metadata": { 228 | "colab": { 229 | "base_uri": "https://localhost:8080/" 230 | }, 231 | "id": "3MVE6rmcWuLe", 232 | "outputId": "65b17f10-1a28-462d-8c3c-38188bc96f04" 233 | }, 234 | "execution_count": 2, 235 | "outputs": [ 236 | { 237 | "output_type": "stream", 238 | "name": "stdout", 239 | "text": [ 240 | "HELLO\n" 241 | ] 242 | } 243 | ] 244 | }, 245 | { 246 | "cell_type": "code", 247 | "source": [ 248 | "str=\"HELLO\"\n", 249 | "print(str[0:])" 250 | ], 251 | "metadata": { 252 | "colab": { 253 | "base_uri": "https://localhost:8080/" 254 | }, 255 | "id": "RQDvMjrmXRyh", 256 | "outputId": "19fc5619-9efe-4727-e755-426f848b2b17" 257 | }, 258 | "execution_count": 3, 259 | "outputs": [ 260 | { 261 | "output_type": "stream", 262 | "name": "stdout", 263 | "text": [ 264 | "HELLO\n" 265 | ] 266 | } 267 | ] 268 | }, 269 | { 270 | "cell_type": "code", 271 | "source": [ 272 | "str=\"HELLO\"\n", 273 | "print(str[:5])" 274 | ], 275 | "metadata": { 276 | "colab": { 277 | "base_uri": "https://localhost:8080/" 278 | }, 279 | "id": "rafel5rvXU8C", 280 | "outputId": "c0bdb628-3223-4673-bec5-eaafcc724c33" 281 | }, 282 | "execution_count": 4, 283 | "outputs": [ 284 | { 285 | "output_type": "stream", 286 | "name": "stdout", 287 | "text": [ 288 | "HELLO\n" 289 | ] 290 | } 291 | ] 292 | }, 293 | { 294 | "cell_type": "code", 295 | "source": [ 296 | "str=\"HELLO\"\n", 297 | "print(str[3:])" 298 | ], 299 | "metadata": { 300 | "colab": { 301 | "base_uri": "https://localhost:8080/" 302 | }, 303 | "id": "9NQhrcTjXYZa", 304 | "outputId": "644ef829-12a6-468c-c472-bea6c07f5d62" 305 | }, 306 | "execution_count": 5, 307 | "outputs": [ 308 | { 309 | "output_type": "stream", 310 | "name": "stdout", 311 | "text": [ 312 | "LO\n" 313 | ] 314 | } 315 | ] 316 | }, 317 | { 318 | "cell_type": "code", 319 | "source": [ 320 | "str=\"HELLO\"\n", 321 | "print(str[0:4])" 322 | ], 323 | "metadata": { 324 | "colab": { 325 | "base_uri": "https://localhost:8080/" 326 | }, 327 | "id": "DqmjpXuFXcPi", 328 | "outputId": "e30f0e1a-e0e8-4abc-b12f-ac5707622051" 329 | }, 330 | "execution_count": 6, 331 | "outputs": [ 332 | { 333 | "output_type": "stream", 334 | "name": "stdout", 335 | "text": [ 336 | "HELL\n" 337 | ] 338 | } 339 | ] 340 | }, 341 | { 342 | "cell_type": "code", 343 | "source": [ 344 | "str=\"HELLO\"\n", 345 | "print(str[1:3])" 346 | ], 347 | "metadata": { 348 | "colab": { 349 | "base_uri": "https://localhost:8080/" 350 | }, 351 | "id": "JM4n_hBKXd5K", 352 | "outputId": "928822b0-d409-451e-847d-801f38ffceec" 353 | }, 354 | "execution_count": 7, 355 | "outputs": [ 356 | { 357 | "output_type": "stream", 358 | "name": "stdout", 359 | "text": [ 360 | "EL\n" 361 | ] 362 | } 363 | ] 364 | }, 365 | { 366 | "cell_type": "markdown", 367 | "source": [ 368 | "#####We can do the negative slicing in the string; it starts from the rightmost character, which is indicated as -1. The second rightmost index indicates -2, and so on. " 369 | ], 370 | "metadata": { 371 | "id": "fp17MDUDXq9q" 372 | } 373 | }, 374 | { 375 | "cell_type": "code", 376 | "source": [ 377 | "str=\"HELLO\"\n", 378 | "print(str[-3:-1])" 379 | ], 380 | "metadata": { 381 | "colab": { 382 | "base_uri": "https://localhost:8080/" 383 | }, 384 | "id": "0eIxHYa5XsSB", 385 | "outputId": "8ef9f07d-a3d4-447d-b0fb-909c5c3da381" 386 | }, 387 | "execution_count": 8, 388 | "outputs": [ 389 | { 390 | "output_type": "stream", 391 | "name": "stdout", 392 | "text": [ 393 | "LL\n" 394 | ] 395 | } 396 | ] 397 | }, 398 | { 399 | "cell_type": "code", 400 | "source": [ 401 | "str=\"HELLO\"\n", 402 | "print(str[-4:-1])" 403 | ], 404 | "metadata": { 405 | "colab": { 406 | "base_uri": "https://localhost:8080/" 407 | }, 408 | "id": "JsGIJ3tIXzPa", 409 | "outputId": "a83f67ed-7d50-416c-9ab7-18280277612b" 410 | }, 411 | "execution_count": 9, 412 | "outputs": [ 413 | { 414 | "output_type": "stream", 415 | "name": "stdout", 416 | "text": [ 417 | "ELL\n" 418 | ] 419 | } 420 | ] 421 | }, 422 | { 423 | "cell_type": "code", 424 | "source": [ 425 | "str=\"HELLO\"\n", 426 | "print(str[-4])" 427 | ], 428 | "metadata": { 429 | "colab": { 430 | "base_uri": "https://localhost:8080/" 431 | }, 432 | "id": "nw20G0s-X16B", 433 | "outputId": "ebd6bca5-0f12-4531-f659-52351b8ffad6" 434 | }, 435 | "execution_count": 10, 436 | "outputs": [ 437 | { 438 | "output_type": "stream", 439 | "name": "stdout", 440 | "text": [ 441 | "E\n" 442 | ] 443 | } 444 | ] 445 | }, 446 | { 447 | "cell_type": "markdown", 448 | "source": [ 449 | "###DELETING A STRING\n", 450 | "As we know that strings are immutable. We cannot delete or remove the characters from the string. But we can delete the entire string using the del keyword." 451 | ], 452 | "metadata": { 453 | "id": "Y6ObqAvaYYwT" 454 | } 455 | }, 456 | { 457 | "cell_type": "code", 458 | "source": [ 459 | "str = \"HELLOWORLD\" \n", 460 | "del str[1] " 461 | ], 462 | "metadata": { 463 | "colab": { 464 | "base_uri": "https://localhost:8080/", 465 | "height": 182 466 | }, 467 | "id": "65cFhpYPYYep", 468 | "outputId": "d1f50cd4-a141-45d5-bcc5-2ec3b546411b" 469 | }, 470 | "execution_count": 13, 471 | "outputs": [ 472 | { 473 | "output_type": "error", 474 | "ename": "TypeError", 475 | "evalue": "ignored", 476 | "traceback": [ 477 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 478 | "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", 479 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mstr\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m\"HELLOWORLD\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0;32mdel\u001b[0m \u001b[0mstr\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 480 | "\u001b[0;31mTypeError\u001b[0m: 'str' object doesn't support item deletion" 481 | ] 482 | } 483 | ] 484 | }, 485 | { 486 | "cell_type": "markdown", 487 | "source": [ 488 | "###STRING OPERATORS\n", 489 | "\n", 490 | "#####**(+) operator:** It is known as concatenation operator used to join the strings given either side of the operator.\n", 491 | "\n", 492 | "\n", 493 | "#####**(*) operator:** It is known as repetition operator. It concatenates the multiple copies of the same string.\n", 494 | "\n", 495 | "\n", 496 | "#####**([ ]) operator:** It is known as slice operator. It is used to access the sub-strings of a particular string.\n", 497 | "\n", 498 | "\n", 499 | "#####**([:]) operator:** It is known as range slice operator. It is used to access the characters from the specified range.\n", 500 | "\n", 501 | "\n", 502 | "#####**(in) operator:** It is known as membership operator. It returns if a particular sub-string is present in the specified string.\n", 503 | "\n", 504 | "\n", 505 | "#####**(not in) operator:** It is also a membership operator and does the exact reverse of in. It returns true if a particular substring is not present in the specified string.\n", 506 | "\n", 507 | "\n", 508 | "#####**(r/R) operator:** It is used to specify the raw string. Raw strings are used in the cases where we need to print the actual meaning of escape characters such as \"C://python\". To define any string as a raw string, the character r or R is followed by the string.\n", 509 | "\n", 510 | "\n", 511 | "#####**(%) operator:** \tIt is used to perform string formatting. It makes use of the format specifiers used in C programming like %d or %f to map their values in python. We will discuss how formatting is done in python." 512 | ], 513 | "metadata": { 514 | "id": "n15lULa9ZI7i" 515 | } 516 | }, 517 | { 518 | "cell_type": "code", 519 | "source": [ 520 | "str = \"Hello\" \n", 521 | "str1 = \" world\"\n", 522 | "\n", 523 | "# prints HelloHelloHello \n", 524 | "print(str*3) \n", 525 | "\n", 526 | "# prints Hello world\n", 527 | "print(str+str1) \n", 528 | "\n", 529 | "# prints o\n", 530 | "print(str[4]) \n", 531 | "\n", 532 | "# prints ll\n", 533 | "print(str[2:4]); \n", 534 | "\n", 535 | "# prints false as w is not present in str\n", 536 | "print('w' in str) \n", 537 | "\n", 538 | "# prints false as wo is present in str1.\n", 539 | "print('wo' not in str1)\n", 540 | "\n", 541 | "# prints C://python37 as it is written \n", 542 | "print(r'C://python37') \n", 543 | "\n", 544 | "# prints The string str : Hello \n", 545 | "print(\"The string str : %s\"%(str)) " 546 | ], 547 | "metadata": { 548 | "colab": { 549 | "base_uri": "https://localhost:8080/" 550 | }, 551 | "id": "cPCrg1BiarrC", 552 | "outputId": "3d07e290-2d43-47dd-b24a-46a1f68209a9" 553 | }, 554 | "execution_count": 15, 555 | "outputs": [ 556 | { 557 | "output_type": "stream", 558 | "name": "stdout", 559 | "text": [ 560 | "HelloHelloHello\n", 561 | "Hello world\n", 562 | "o\n", 563 | "ll\n", 564 | "False\n", 565 | "False\n", 566 | "C://python37\n", 567 | "The string str : Hello\n" 568 | ] 569 | } 570 | ] 571 | }, 572 | { 573 | "cell_type": "code", 574 | "source": [], 575 | "metadata": { 576 | "id": "A5vO3oNea_E7" 577 | }, 578 | "execution_count": null, 579 | "outputs": [] 580 | } 581 | ] 582 | } -------------------------------------------------------------------------------- /General Python Programming.md/6_PYTHON_Variables.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "provenance": [] 7 | }, 8 | "kernelspec": { 9 | "name": "python3", 10 | "display_name": "Python 3" 11 | }, 12 | "language_info": { 13 | "name": "python" 14 | } 15 | }, 16 | "cells": [ 17 | { 18 | "cell_type": "markdown", 19 | "source": [ 20 | "## VARIABLES IN PYTHON\n", 21 | "#### VARIABLES\n", 22 | "Variables are used to store values. But the best thing in python is that unlike other programming languages, python has no command for declaring variables. A variable is created as soon as we assign a value to it." 23 | ], 24 | "metadata": { 25 | "id": "1jcmziZ5g9TF" 26 | } 27 | }, 28 | { 29 | "cell_type": "code", 30 | "source": [ 31 | "#Example for variable\n", 32 | "x=10 #Here x is type of int\n", 33 | "y=\"Aman Verma\" #Here y is the type of string\n", 34 | "print(x)\n", 35 | "print(y)" 36 | ], 37 | "metadata": { 38 | "colab": { 39 | "base_uri": "https://localhost:8080/" 40 | }, 41 | "id": "DEjFlmLVhl6x", 42 | "outputId": "a2f6a359-b1cf-46b3-efd4-b4394e4cc715" 43 | }, 44 | "execution_count": null, 45 | "outputs": [ 46 | { 47 | "output_type": "stream", 48 | "name": "stdout", 49 | "text": [ 50 | "10\n", 51 | "Aman Verma\n" 52 | ] 53 | } 54 | ] 55 | }, 56 | { 57 | "cell_type": "markdown", 58 | "source": [ 59 | "A variable name cannot be anything as we want . There are some rules which have to be taken care of while declaring a variable.\n", 60 | "#### Conditions to write a variable:\n", 61 | "* A variable name must start with letter or underscore character.\n", 62 | "* A variable cannot start with a number.\n", 63 | "* A variable name can only contain alpha-numeric character and underscore (A-z, 0-9 and _). #Symbols are not allowed\n", 64 | "* Variable names are case sensetive (age, Age and AGE are three different variables)\n", 65 | "* There can not be a space among variable name." 66 | ], 67 | "metadata": { 68 | "id": "vfdNfdNxiPR8" 69 | } 70 | }, 71 | { 72 | "cell_type": "code", 73 | "source": [ 74 | "#ALLOWED VARIABLE NAMES :\n", 75 | "myvar = \"Hello 1\"\n", 76 | "my_var = \"Hello 2\"\n", 77 | "_my_var = \"Hello 3\"\n", 78 | "myVar = \"Hello 4\"\n", 79 | "MYVAR = \"Hello 5\"\n", 80 | "myvar2 = \"Hello 6\"" 81 | ], 82 | "metadata": { 83 | "id": "5VnqB6nYkPzc" 84 | }, 85 | "execution_count": null, 86 | "outputs": [] 87 | }, 88 | { 89 | "cell_type": "code", 90 | "source": [ 91 | "#FORBIDDEN VARIABLE NAMES:\n", 92 | "2myvar = \"Hello 1\"\n", 93 | "my-var = \"Hello 2\"\n", 94 | "my var = \"Hello 3\"" 95 | ], 96 | "metadata": { 97 | "colab": { 98 | "base_uri": "https://localhost:8080/", 99 | "height": 131 100 | }, 101 | "id": "RROO8YfKkoRH", 102 | "outputId": "5844e4ba-b70f-48a7-c1af-84e1147a6ebf" 103 | }, 104 | "execution_count": null, 105 | "outputs": [ 106 | { 107 | "output_type": "error", 108 | "ename": "SyntaxError", 109 | "evalue": "ignored", 110 | "traceback": [ 111 | "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m2\u001b[0m\n\u001b[0;31m 2myvar = \"Hello 1\"\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" 112 | ] 113 | } 114 | ] 115 | }, 116 | { 117 | "cell_type": "markdown", 118 | "source": [ 119 | "####It is also possible to know the data type of a variable using \"type()\" function" 120 | ], 121 | "metadata": { 122 | "id": "DcDAIrQyl2uj" 123 | } 124 | }, 125 | { 126 | "cell_type": "code", 127 | "source": [ 128 | "#Example to check the type of variable\n", 129 | "a = 138\n", 130 | "b = 12.4\n", 131 | "c = \"python\"\n", 132 | "d = True\n", 133 | "print(type(a))\n", 134 | "print(type(b))\n", 135 | "print(type(c))\n", 136 | "print(type(d))\n" 137 | ], 138 | "metadata": { 139 | "colab": { 140 | "base_uri": "https://localhost:8080/" 141 | }, 142 | "id": "4qwVc48hlCcx", 143 | "outputId": "48aabfde-7c6b-4a88-a564-3c06b9a88b92" 144 | }, 145 | "execution_count": null, 146 | "outputs": [ 147 | { 148 | "output_type": "stream", 149 | "name": "stdout", 150 | "text": [ 151 | "\n", 152 | "\n", 153 | "\n", 154 | "\n" 155 | ] 156 | } 157 | ] 158 | }, 159 | { 160 | "cell_type": "markdown", 161 | "source": [ 162 | "\n", 163 | "\n", 164 | "---\n", 165 | "\n", 166 | "\n", 167 | "\n", 168 | "---\n", 169 | "\n" 170 | ], 171 | "metadata": { 172 | "id": "0DWqHKN-yhsk" 173 | } 174 | }, 175 | { 176 | "cell_type": "markdown", 177 | "source": [ 178 | "### ASSIGNING MULTIPLE VALUES -\n" 179 | ], 180 | "metadata": { 181 | "id": "FXuf6iKEm5j5" 182 | } 183 | }, 184 | { 185 | "cell_type": "code", 186 | "source": [ 187 | "#Assigning multiple values to variables\n", 188 | "x, y, z = \"hello\" , \"world\", \"python\"\n", 189 | "print(x)\n", 190 | "print(y)\n", 191 | "print(z)\n" 192 | ], 193 | "metadata": { 194 | "colab": { 195 | "base_uri": "https://localhost:8080/" 196 | }, 197 | "id": "HItDYg6pmbXk", 198 | "outputId": "4bc21649-5f71-4cc8-e101-3627649db3a3" 199 | }, 200 | "execution_count": null, 201 | "outputs": [ 202 | { 203 | "output_type": "stream", 204 | "name": "stdout", 205 | "text": [ 206 | "hello\n", 207 | "world\n", 208 | "python\n" 209 | ] 210 | } 211 | ] 212 | }, 213 | { 214 | "cell_type": "code", 215 | "source": [ 216 | "#assigning same value tomultiple variable\n", 217 | "x = y = z = \"python programming\"\n", 218 | "print(x)\n", 219 | "print(y)\n", 220 | "print(z)" 221 | ], 222 | "metadata": { 223 | "colab": { 224 | "base_uri": "https://localhost:8080/" 225 | }, 226 | "id": "X2ux2Yg6uoOY", 227 | "outputId": "76fde3ce-6cc0-4523-af19-9e50daccce1d" 228 | }, 229 | "execution_count": null, 230 | "outputs": [ 231 | { 232 | "output_type": "stream", 233 | "name": "stdout", 234 | "text": [ 235 | "python programming\n", 236 | "python programming\n", 237 | "python programming\n" 238 | ] 239 | } 240 | ] 241 | }, 242 | { 243 | "cell_type": "code", 244 | "source": [ 245 | "#Extracting from multiple values in a variable\n", 246 | "programming_languages = [\"Python\", \"Java\", \"C\", \"C++\"]\n", 247 | "a, b, c, d = programming_languages \n", 248 | "print(a)\n", 249 | "print(b)\n", 250 | "print(c)\n", 251 | "print(d)" 252 | ], 253 | "metadata": { 254 | "colab": { 255 | "base_uri": "https://localhost:8080/" 256 | }, 257 | "id": "eBarPveEu8DJ", 258 | "outputId": "864d3e40-e5c8-491c-b694-f60cd6968e1f" 259 | }, 260 | "execution_count": null, 261 | "outputs": [ 262 | { 263 | "output_type": "stream", 264 | "name": "stdout", 265 | "text": [ 266 | "Python\n", 267 | "Java\n", 268 | "C\n", 269 | "C++\n" 270 | ] 271 | } 272 | ] 273 | }, 274 | { 275 | "cell_type": "markdown", 276 | "source": [ 277 | "\n", 278 | "\n", 279 | "---\n", 280 | "\n", 281 | "\n", 282 | "\n", 283 | "---\n", 284 | "\n" 285 | ], 286 | "metadata": { 287 | "id": "D6NHaO4QylHd" 288 | } 289 | }, 290 | { 291 | "cell_type": "markdown", 292 | "source": [ 293 | "###OUTPUT VARIABLE / print() FUNCTION -\n", 294 | "print() function is used as output variable in python" 295 | ], 296 | "metadata": { 297 | "id": "NGIjQEn9v3BT" 298 | } 299 | }, 300 | { 301 | "cell_type": "code", 302 | "source": [ 303 | "#Exmple 1\n", 304 | "a=\"my name\"\n", 305 | "b=\"is\"\n", 306 | "c=\"Aman\"\n", 307 | "print(a,b,c)" 308 | ], 309 | "metadata": { 310 | "colab": { 311 | "base_uri": "https://localhost:8080/" 312 | }, 313 | "id": "T7tx25Y1wGMG", 314 | "outputId": "8654ade9-4c4b-4d79-b4f0-490d72ff5deb" 315 | }, 316 | "execution_count": null, 317 | "outputs": [ 318 | { 319 | "output_type": "stream", 320 | "name": "stdout", 321 | "text": [ 322 | "my name is Aman\n" 323 | ] 324 | } 325 | ] 326 | }, 327 | { 328 | "cell_type": "code", 329 | "source": [ 330 | "#Example 2\n", 331 | "a=\"my name\"\n", 332 | "b=\"is\"\n", 333 | "c=\"Aman\"\n", 334 | "print(a+b+c)\n", 335 | "#the operator (+) can be used to join these variables" 336 | ], 337 | "metadata": { 338 | "colab": { 339 | "base_uri": "https://localhost:8080/" 340 | }, 341 | "id": "9hdElF7-w1uw", 342 | "outputId": "e1483555-5366-4d57-d32a-09ea75472d9b" 343 | }, 344 | "execution_count": null, 345 | "outputs": [ 346 | { 347 | "output_type": "stream", 348 | "name": "stdout", 349 | "text": [ 350 | "my nameisAman\n" 351 | ] 352 | } 353 | ] 354 | }, 355 | { 356 | "cell_type": "code", 357 | "source": [ 358 | "#Example 3\n", 359 | "a = 10\n", 360 | "b = 120\n", 361 | "print(a+b)\n", 362 | "#With numbers the (+) operator works as mathematical operator" 363 | ], 364 | "metadata": { 365 | "colab": { 366 | "base_uri": "https://localhost:8080/" 367 | }, 368 | "id": "PT_SQ3fWxAIA", 369 | "outputId": "398268e4-9b2b-4c0c-eec6-d3dff3143e56" 370 | }, 371 | "execution_count": null, 372 | "outputs": [ 373 | { 374 | "output_type": "stream", 375 | "name": "stdout", 376 | "text": [ 377 | "130\n" 378 | ] 379 | } 380 | ] 381 | }, 382 | { 383 | "cell_type": "code", 384 | "source": [ 385 | "#Example 4\n", 386 | "a = 10\n", 387 | "b = \"Python\"\n", 388 | "print(a+b)\n", 389 | "#A string and int cannot be added together" 390 | ], 391 | "metadata": { 392 | "colab": { 393 | "base_uri": "https://localhost:8080/", 394 | "height": 201 395 | }, 396 | "id": "6heCl14Exa9X", 397 | "outputId": "59cda66e-0119-4c1d-83ba-297637fb3aea" 398 | }, 399 | "execution_count": null, 400 | "outputs": [ 401 | { 402 | "output_type": "error", 403 | "ename": "TypeError", 404 | "evalue": "ignored", 405 | "traceback": [ 406 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 407 | "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", 408 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0ma\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m10\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0mb\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m\"Python\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m+\u001b[0m\u001b[0mb\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 409 | "\u001b[0;31mTypeError\u001b[0m: unsupported operand type(s) for +: 'int' and 'str'" 410 | ] 411 | } 412 | ] 413 | }, 414 | { 415 | "cell_type": "code", 416 | "source": [ 417 | "#Example 5\n", 418 | "a = 10\n", 419 | "b = \"Python\"\n", 420 | "print(a, b)\n" 421 | ], 422 | "metadata": { 423 | "colab": { 424 | "base_uri": "https://localhost:8080/" 425 | }, 426 | "id": "VqA-gSNBxowY", 427 | "outputId": "1ddad586-f746-4dce-9bc1-870cc6ed13cb" 428 | }, 429 | "execution_count": null, 430 | "outputs": [ 431 | { 432 | "output_type": "stream", 433 | "name": "stdout", 434 | "text": [ 435 | "10 Python\n" 436 | ] 437 | } 438 | ] 439 | }, 440 | { 441 | "cell_type": "markdown", 442 | "source": [ 443 | "\n", 444 | "\n", 445 | "---\n", 446 | "\n", 447 | "\n", 448 | "\n", 449 | "---\n", 450 | "\n" 451 | ], 452 | "metadata": { 453 | "id": "YWbTNasXynIT" 454 | } 455 | }, 456 | { 457 | "cell_type": "markdown", 458 | "source": [ 459 | "###GLOBAL VARIABLES -" 460 | ], 461 | "metadata": { 462 | "id": "iv6IAIXhyQ0R" 463 | } 464 | }, 465 | { 466 | "cell_type": "markdown", 467 | "source": [ 468 | "To understand global variables we also need to understand Functions in python.\n", 469 | "###FUNCTIONS:\n", 470 | "A function is a block of code which only runs when it is called.\n", 471 | "You can pass data, known as parameters, into a function.\n", 472 | "A function can return data as a result.\n", 473 | "\n", 474 | "Functions will be discussed more in detail later." 475 | ], 476 | "metadata": { 477 | "id": "EqKub7-ey1ue" 478 | } 479 | }, 480 | { 481 | "cell_type": "code", 482 | "source": [ 483 | "#Example of function\n", 484 | "def my_function(): #Function can be created using (def) command and then name of the function, here it is (my_function)\n", 485 | " print(\"Hello from a function\") #This command is now under the function (def my_function)\n", 486 | "my_function()" 487 | ], 488 | "metadata": { 489 | "id": "wW6p1zPzyU8z", 490 | "colab": { 491 | "base_uri": "https://localhost:8080/" 492 | }, 493 | "outputId": "d1d52309-d0e5-48be-df49-5d455ebf0cb7" 494 | }, 495 | "execution_count": 2, 496 | "outputs": [ 497 | { 498 | "output_type": "stream", 499 | "name": "stdout", 500 | "text": [ 501 | "Hello from a function\n" 502 | ] 503 | } 504 | ] 505 | }, 506 | { 507 | "cell_type": "markdown", 508 | "source": [ 509 | "### GLOBAL VARIABLE:\n", 510 | "Variables that are created outside of a function (as in all of the examples above) are known as global variables.\n", 511 | "\n", 512 | "Global variables can be used by everyone, both inside of functions and outside." 513 | ], 514 | "metadata": { 515 | "id": "AWy02-aX7SmQ" 516 | } 517 | }, 518 | { 519 | "cell_type": "code", 520 | "source": [ 521 | "#Example 1- Creating variable outside a function, and using it inside the function\n", 522 | "x = \"programming language\"\n", 523 | "\n", 524 | "def my_function():\n", 525 | " print(\"Python is a\" , x)\n", 526 | "\n", 527 | "my_function()" 528 | ], 529 | "metadata": { 530 | "colab": { 531 | "base_uri": "https://localhost:8080/" 532 | }, 533 | "id": "zyNy6_vS7dfm", 534 | "outputId": "28094be5-e256-4135-d200-40a0a3d57c2c" 535 | }, 536 | "execution_count": 4, 537 | "outputs": [ 538 | { 539 | "output_type": "stream", 540 | "name": "stdout", 541 | "text": [ 542 | "Python is a programming language\n" 543 | ] 544 | } 545 | ] 546 | }, 547 | { 548 | "cell_type": "markdown", 549 | "source": [ 550 | "If you create a variable with the same name inside a function, this variable will be local, and can only be used inside the function. The global variable with the same name will remain as it was, global and with the original value." 551 | ], 552 | "metadata": { 553 | "id": "6sxhsehA8HaV" 554 | } 555 | }, 556 | { 557 | "cell_type": "markdown", 558 | "source": [ 559 | "###LOCAL VARIABLE:\n", 560 | "Local variables in python are those variables that are declared inside the function. Alternatively, they are said to defined within a local scope. A user can only access a local variable inside the function but never outside it." 561 | ], 562 | "metadata": { 563 | "id": "76rDB2To8Ykt" 564 | } 565 | }, 566 | { 567 | "cell_type": "code", 568 | "source": [ 569 | "#Example 2- Creating a variable inside a function , with same name as global variable\n", 570 | "x=\"programming language\" #Here the (X) outside the function will be GLOBAL VARIABLE\n", 571 | "\n", 572 | "def my_function():\n", 573 | " x=\"open source language\" #Here the (x) inside a function will be a LOCAL VARIABLE\n", 574 | " print(\"Python is\", x)\n", 575 | "\n", 576 | "my_function()\n", 577 | "\n", 578 | "print(\"Python is \", x)" 579 | ], 580 | "metadata": { 581 | "colab": { 582 | "base_uri": "https://localhost:8080/" 583 | }, 584 | "id": "Got_PzbV78w1", 585 | "outputId": "3b7d125a-8264-4409-ca31-470cd270267e" 586 | }, 587 | "execution_count": 5, 588 | "outputs": [ 589 | { 590 | "output_type": "stream", 591 | "name": "stdout", 592 | "text": [ 593 | "Python is open source language\n", 594 | "Python is programming language\n" 595 | ] 596 | } 597 | ] 598 | }, 599 | { 600 | "cell_type": "markdown", 601 | "source": [ 602 | "###UNDERSTANDING GLOBAL KEYWORD\n", 603 | "Normally, when you create a variable inside a function, that variable is local, and can only be used inside that function.\n", 604 | "\n", 605 | "To create a global variable inside a function, you can use the global keyword." 606 | ], 607 | "metadata": { 608 | "id": "LAO4XzVE-IWv" 609 | } 610 | }, 611 | { 612 | "cell_type": "code", 613 | "source": [ 614 | "#Example- If you use the global keyword, the variable belongs to the global scope:\n", 615 | "def myfunc():\n", 616 | " global x\n", 617 | " x = \"programming language\"\n", 618 | "\n", 619 | "myfunc()\n", 620 | "\n", 621 | "print(\"Python is \" + x)" 622 | ], 623 | "metadata": { 624 | "colab": { 625 | "base_uri": "https://localhost:8080/" 626 | }, 627 | "id": "zLeijOSK9xRF", 628 | "outputId": "010c28c6-7652-4b5f-c01d-ced2ccf40cfb" 629 | }, 630 | "execution_count": 8, 631 | "outputs": [ 632 | { 633 | "output_type": "stream", 634 | "name": "stdout", 635 | "text": [ 636 | "Python is programming language\n" 637 | ] 638 | } 639 | ] 640 | }, 641 | { 642 | "cell_type": "code", 643 | "source": [ 644 | "#Example- To change the value of a global variable inside a function, refer to the variable by using the global keyword:\n", 645 | "x = \"awesome\"\n", 646 | "\n", 647 | "def myfunc():\n", 648 | " global x\n", 649 | " x = \"fantastic\"\n", 650 | "\n", 651 | "myfunc()\n", 652 | "\n", 653 | "print(\"Python is \" + x)" 654 | ], 655 | "metadata": { 656 | "colab": { 657 | "base_uri": "https://localhost:8080/" 658 | }, 659 | "id": "6w-tpFZw-RnT", 660 | "outputId": "0a0cb1a8-ef89-42d2-8f33-2b727584193c" 661 | }, 662 | "execution_count": 10, 663 | "outputs": [ 664 | { 665 | "output_type": "stream", 666 | "name": "stdout", 667 | "text": [ 668 | "Python is fantastic\n" 669 | ] 670 | } 671 | ] 672 | }, 673 | { 674 | "cell_type": "code", 675 | "source": [], 676 | "metadata": { 677 | "id": "F2IeYITi-oeT" 678 | }, 679 | "execution_count": null, 680 | "outputs": [] 681 | } 682 | ] 683 | } 684 | --------------------------------------------------------------------------------