├── .gitignore ├── Class_Modular Programmimg.ipynb ├── Date Time_Time Delta_Module .ipynb ├── File Handling.ipynb ├── For_Loop Whiel_Loop.ipynb ├── Function awrg_kwarg and Lambda function.ipynb ├── Functions_1.ipynb ├── Garbage Collection.ipynb ├── If Else.ipynb ├── Inheritance _Encapsulation.ipynb ├── Inheritance_Example_Super().ipynb ├── LICENSE ├── List, Tuple, Dictionary, Sets.ipynb ├── Logging Examples.ipynb ├── Logging.ipynb ├── Logging_Updated.ipynb ├── Multi threading.ipynb ├── OOPS -Basic-1.ipynb ├── OOPS_Class.ipynb ├── OOPS_Class_Modular Programmimg.ipynb ├── OS Module.ipynb ├── Polymorphism_Overriding_Overloading.ipynb ├── Python Networking Programming ├── Python Network Programming.ipynb ├── client.ipynb └── server.ipynb ├── README.md ├── Regular Expressions .ipynb ├── Shallow Copy Vs deep copy.ipynb ├── Try and Exception Handling.ipynb ├── Zip file _Unzip file.ipynb ├── iterator generator.ipynb ├── lambdafunction.ipynb └── tkinter & turtle.ipynb /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | pip-wheel-metadata/ 24 | share/python-wheels/ 25 | *.egg-info/ 26 | .installed.cfg 27 | *.egg 28 | MANIFEST 29 | 30 | # PyInstaller 31 | # Usually these files are written by a python script from a template 32 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 33 | *.manifest 34 | *.spec 35 | 36 | # Installer logs 37 | pip-log.txt 38 | pip-delete-this-directory.txt 39 | 40 | # Unit test / coverage reports 41 | htmlcov/ 42 | .tox/ 43 | .nox/ 44 | .coverage 45 | .coverage.* 46 | .cache 47 | nosetests.xml 48 | coverage.xml 49 | *.cover 50 | *.py,cover 51 | .hypothesis/ 52 | .pytest_cache/ 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | local_settings.py 61 | db.sqlite3 62 | db.sqlite3-journal 63 | 64 | # Flask stuff: 65 | instance/ 66 | .webassets-cache 67 | 68 | # Scrapy stuff: 69 | .scrapy 70 | 71 | # Sphinx documentation 72 | docs/_build/ 73 | 74 | # PyBuilder 75 | target/ 76 | 77 | # Jupyter Notebook 78 | .ipynb_checkpoints 79 | 80 | # IPython 81 | profile_default/ 82 | ipython_config.py 83 | 84 | # pyenv 85 | .python-version 86 | 87 | # pipenv 88 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 89 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 90 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 91 | # install all needed dependencies. 92 | #Pipfile.lock 93 | 94 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow 95 | __pypackages__/ 96 | 97 | # Celery stuff 98 | celerybeat-schedule 99 | celerybeat.pid 100 | 101 | # SageMath parsed files 102 | *.sage.py 103 | 104 | # Environments 105 | .env 106 | .venv 107 | env/ 108 | venv/ 109 | ENV/ 110 | env.bak/ 111 | venv.bak/ 112 | 113 | # Spyder project settings 114 | .spyderproject 115 | .spyproject 116 | 117 | # Rope project settings 118 | .ropeproject 119 | 120 | # mkdocs documentation 121 | /site 122 | 123 | # mypy 124 | .mypy_cache/ 125 | .dmypy.json 126 | dmypy.json 127 | 128 | # Pyre type checker 129 | .pyre/ 130 | -------------------------------------------------------------------------------- /Class_Modular Programmimg.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 8, 6 | "id": "869c01f3", 7 | "metadata": {}, 8 | "outputs": [ 9 | { 10 | "name": "stdout", 11 | "output_type": "stream", 12 | "text": [ 13 | "Hello, my name is Nikhil\n" 14 | ] 15 | } 16 | ], 17 | "source": [ 18 | "# A Sample class with init method\n", 19 | "class Person:\n", 20 | " \n", 21 | " # init method or constructor\n", 22 | " def __init__(self, name):\n", 23 | " self.name = name\n", 24 | " \n", 25 | " # Sample Method\n", 26 | " def say_hi(self):\n", 27 | " print('Hello, my name is', self.name)\n", 28 | " \n", 29 | " \n", 30 | "p = Person('Nikhil')\n", 31 | "p.say_hi()" 32 | ] 33 | }, 34 | { 35 | "cell_type": "code", 36 | "execution_count": 9, 37 | "id": "ebaa6a16", 38 | "metadata": {}, 39 | "outputs": [ 40 | { 41 | "name": "stdout", 42 | "output_type": "stream", 43 | "text": [ 44 | "my name is jalpa\n" 45 | ] 46 | } 47 | ], 48 | "source": [ 49 | "print(\"my name is jalpa\")" 50 | ] 51 | }, 52 | { 53 | "cell_type": "code", 54 | "execution_count": 19, 55 | "id": "5322d16d", 56 | "metadata": {}, 57 | "outputs": [], 58 | "source": [ 59 | "class person:\n", 60 | " def __init__(self, name,age, gender, nationality):\n", 61 | " self.name = name\n", 62 | " self.age = age\n", 63 | " self.gender = gender\n", 64 | " self.nationality = nationality\n", 65 | " def wish(self):\n", 66 | " print(\"my name is \", self.name)\n", 67 | " print(\"my name is \", self.age)\n", 68 | " print(\"I am \", self.gender)\n", 69 | " print(\"I am an\", self.nationality)\n", 70 | " " 71 | ] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "execution_count": 20, 76 | "id": "7d1fba3c", 77 | "metadata": {}, 78 | "outputs": [ 79 | { 80 | "name": "stdout", 81 | "output_type": "stream", 82 | "text": [ 83 | "my name is jalpa\n", 84 | "my name is 26\n", 85 | "I am Female\n", 86 | "I am an Indian\n" 87 | ] 88 | } 89 | ], 90 | "source": [ 91 | "p1 = person(\"jalpa\", 26, \"Female\", \"Indian\")\n", 92 | "p1.wish()" 93 | ] 94 | }, 95 | { 96 | "cell_type": "code", 97 | "execution_count": 1, 98 | "id": "931d877d", 99 | "metadata": {}, 100 | "outputs": [ 101 | { 102 | "name": "stdout", 103 | "output_type": "stream", 104 | "text": [ 105 | "Enter first number: 6\n", 106 | "Enter second number: 7\n", 107 | "13\n", 108 | "42\n", 109 | "0.8571428571428571\n", 110 | "-1\n" 111 | ] 112 | } 113 | ], 114 | "source": [ 115 | "# Simple calculator using class\n", 116 | "class cal():\n", 117 | " \"\"\"This is a simple calculation using class and function for two variable\"\"\"\n", 118 | " def __init__(self,a,b):\n", 119 | " self.a=a\n", 120 | " self.b=b\n", 121 | " def add(self):\n", 122 | " return self.a+self.b\n", 123 | " def mul(self):\n", 124 | " return self.a*self.b\n", 125 | " def div(self):\n", 126 | " return self.a/self.b\n", 127 | " def sub(self):\n", 128 | " return self.a-self.b\n", 129 | "a=int(input(\"Enter first number: \"))\n", 130 | "b=int(input(\"Enter second number: \"))\n", 131 | "num = cal(a,b)\n", 132 | "#num = cal(4,8)\n", 133 | "print(num.add())\n", 134 | "print(num.mul())\n", 135 | "print(num.div())\n", 136 | "print(num.sub())" 137 | ] 138 | }, 139 | { 140 | "cell_type": "code", 141 | "execution_count": 9, 142 | "id": "b4c0129b", 143 | "metadata": {}, 144 | "outputs": [ 145 | { 146 | "name": "stdout", 147 | "output_type": "stream", 148 | "text": [ 149 | "This is negative number\n" 150 | ] 151 | } 152 | ], 153 | "source": [ 154 | "# Number is positive or negative using class\n", 155 | "class num():\n", 156 | " \"\"\"This is a program to find out the number is negative of positive\"\"\"\n", 157 | "\n", 158 | " def __init__(self,a):\n", 159 | " self.a=a\n", 160 | " def num_p(self):\n", 161 | " try:\n", 162 | " if self.a >= 0:\n", 163 | " print(\"This is positive number\")\n", 164 | " else:\n", 165 | " print(\"This is negative number\")\n", 166 | " except Exception as e:\n", 167 | " print(e)\n", 168 | " num1 = int(input(\"Enter the number : \"))\n", 169 | "num1 = num(-8)\n", 170 | "num1.num_p()" 171 | ] 172 | }, 173 | { 174 | "cell_type": "code", 175 | "execution_count": 2, 176 | "id": "0bfe2d40", 177 | "metadata": {}, 178 | "outputs": [ 179 | { 180 | "name": "stdout", 181 | "output_type": "stream", 182 | "text": [ 183 | "Enter Weight of the Person (Kg) : j\n", 184 | "Error in our program\n" 185 | ] 186 | } 187 | ], 188 | "source": [ 189 | "# BMI calculation using Class \n", 190 | "class BMI():\n", 191 | " \"\"\" This is a program for to calculate body mass index for person\"\"\"\n", 192 | " def __init__(self,w,h):\n", 193 | " self.w = w\n", 194 | " self.h = h\n", 195 | " def bmi_cal(self):\n", 196 | " try:\n", 197 | " bmi = self.w / ((self.h) * (self.h))\n", 198 | " print(bmi)\n", 199 | " except Exception as e:\n", 200 | " print(e)\n", 201 | "try:\n", 202 | " w=float(input(\"Enter Weight of the Person (Kg) : \"))\n", 203 | " h=float(input(\"Enter Height of the Person (Meters): \"))\n", 204 | " obj=BMI(w,h)\n", 205 | " obj.bmi_cal()\n", 206 | "\n", 207 | "except:\n", 208 | " print(\"Error in our program\")\n", 209 | " " 210 | ] 211 | }, 212 | { 213 | "cell_type": "code", 214 | "execution_count": 3, 215 | "id": "3385fd9c", 216 | "metadata": {}, 217 | "outputs": [ 218 | { 219 | "name": "stdout", 220 | "output_type": "stream", 221 | "text": [ 222 | "After Converting Dictionary to Class : \n", 223 | "Geeks 1223 Python\n", 224 | "\n" 225 | ] 226 | } 227 | ], 228 | "source": [ 229 | "# Turns a dictionary into a class\n", 230 | "class Dict2Class(object):\n", 231 | " \n", 232 | " def __init__(self, my_dict):\n", 233 | " \n", 234 | " for key in my_dict:\n", 235 | " setattr(self, key, my_dict[key])\n", 236 | " \n", 237 | "# Driver Code\n", 238 | "if __name__ == \"__main__\":\n", 239 | " \n", 240 | " # Creating the dictionary\n", 241 | " my_dict = {\"Name\": \"Geeks\",\n", 242 | " \"Rank\": \"1223\",\n", 243 | " \"Subject\": \"Python\"}\n", 244 | " \n", 245 | " result = Dict2Class(my_dict)\n", 246 | " \n", 247 | " # printing the result\n", 248 | " print(\"After Converting Dictionary to Class : \")\n", 249 | " print(result.Name, result.Rank, result.Subject)\n", 250 | " print(type(result))" 251 | ] 252 | }, 253 | { 254 | "cell_type": "code", 255 | "execution_count": null, 256 | "id": "c5693c3f", 257 | "metadata": {}, 258 | "outputs": [], 259 | "source": [ 260 | "class cal():\n", 261 | " def __init__(self,a,b):\n", 262 | " self.a=a\n", 263 | " self.b=b\n", 264 | " def add(self):\n", 265 | " return self.a+self.b\n", 266 | " def mul(self):\n", 267 | " return self.a*self.b\n", 268 | " def div(self):\n", 269 | " return self.a/self.b\n", 270 | " def sub(self):\n", 271 | " return self.a-self.b\n", 272 | "a=int(input(\"Enter first number: \"))\n", 273 | "b=int(input(\"Enter second number: \"))\n", 274 | "obj=cal(a,b)\n", 275 | "choice=1\n", 276 | "while choice!=0:\n", 277 | " print(\"0. Exit\")\n", 278 | " print(\"1. Add\")\n", 279 | " print(\"2. Subtraction\")\n", 280 | " print(\"3. Multiplication\")\n", 281 | " print(\"4. Division\")\n", 282 | " choice=int(input(\"Enter choice: \"))\n", 283 | " if choice==1:\n", 284 | " print(\"Result: \",obj.add())\n", 285 | " elif choice==2:\n", 286 | " print(\"Result: \",obj.sub())\n", 287 | " elif choice==3:\n", 288 | " print(\"Result: \",obj.mul())\n", 289 | " elif choice==4:\n", 290 | " print(\"Result: \",round(obj.div(),2))\n", 291 | " elif choice==0:\n", 292 | " print(\"Exiting!\")\n", 293 | " else:\n", 294 | " print(\"Invalid choice!!\")\n", 295 | "print()" 296 | ] 297 | } 298 | ], 299 | "metadata": { 300 | "kernelspec": { 301 | "display_name": "Python 3 (ipykernel)", 302 | "language": "python", 303 | "name": "python3" 304 | }, 305 | "language_info": { 306 | "codemirror_mode": { 307 | "name": "ipython", 308 | "version": 3 309 | }, 310 | "file_extension": ".py", 311 | "mimetype": "text/x-python", 312 | "name": "python", 313 | "nbconvert_exporter": "python", 314 | "pygments_lexer": "ipython3", 315 | "version": "3.9.12" 316 | } 317 | }, 318 | "nbformat": 4, 319 | "nbformat_minor": 5 320 | } 321 | -------------------------------------------------------------------------------- /Date Time_Time Delta_Module .ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "id": "e3345749", 7 | "metadata": {}, 8 | "outputs": [], 9 | "source": [ 10 | "#In Python, date and time are not a data type of their own, but a module named datetime can be imported to work with the date as well as time. \n", 11 | "#Python Datetime module comes built into Python, so there is no need to install it externally. " 12 | ] 13 | }, 14 | { 15 | "cell_type": "code", 16 | "execution_count": 16, 17 | "id": "66f1634b", 18 | "metadata": {}, 19 | "outputs": [ 20 | { 21 | "name": "stdout", 22 | "output_type": "stream", 23 | "text": [ 24 | "2022-09-20 12:59:44.710933\n" 25 | ] 26 | } 27 | ], 28 | "source": [ 29 | "import datetime\n", 30 | "\n", 31 | "datetime_object = datetime.datetime.now()\n", 32 | "print(datetime_object)" 33 | ] 34 | }, 35 | { 36 | "cell_type": "code", 37 | "execution_count": 3, 38 | "id": "0eedebcb", 39 | "metadata": {}, 40 | "outputs": [ 41 | { 42 | "name": "stdout", 43 | "output_type": "stream", 44 | "text": [ 45 | "Date passed as argument is 1984-12-11\n" 46 | ] 47 | } 48 | ], 49 | "source": [ 50 | "#Date object representing date in Python\n", 51 | "# Python program to demonstrate date class\n", 52 | "# import the date class\n", 53 | "from datetime import date\n", 54 | "# initializing constructor\n", 55 | "# and passing arguments in the\n", 56 | "# format year, month, date\n", 57 | "my_date = date(1984, 12, 11)\n", 58 | " \n", 59 | "print(\"Date passed as argument is\", my_date)" 60 | ] 61 | }, 62 | { 63 | "cell_type": "code", 64 | "execution_count": 4, 65 | "id": "e0ea734e", 66 | "metadata": {}, 67 | "outputs": [ 68 | { 69 | "name": "stdout", 70 | "output_type": "stream", 71 | "text": [ 72 | "Today's date is 2022-09-20\n" 73 | ] 74 | } 75 | ], 76 | "source": [ 77 | "# Write a python code to Get Current Date\n", 78 | "from datetime import date\n", 79 | "# calling the today\n", 80 | "# function of date class\n", 81 | "today = date.today()\n", 82 | " \n", 83 | "print(\"Today's date is\", today)" 84 | ] 85 | }, 86 | { 87 | "cell_type": "code", 88 | "execution_count": 1, 89 | "id": "f142d027", 90 | "metadata": {}, 91 | "outputs": [ 92 | { 93 | "name": "stdout", 94 | "output_type": "stream", 95 | "text": [ 96 | "Current year: 2022\n", 97 | "Current month: 9\n", 98 | "Current day: 21\n" 99 | ] 100 | } 101 | ], 102 | "source": [ 103 | "# Write a python code to Get Today’s Year, Month, and Date\n", 104 | "from datetime import date\n", 105 | "today = date.today()\n", 106 | " \n", 107 | "print(\"Current year:\", today.year)\n", 108 | "print(\"Current month:\", today.month)\n", 109 | "print(\"Current day:\", today.day)" 110 | ] 111 | }, 112 | { 113 | "cell_type": "code", 114 | "execution_count": null, 115 | "id": "ec97ec91", 116 | "metadata": {}, 117 | "outputs": [], 118 | "source": [ 119 | "import numpy as np\n", 120 | "import pandas as pd" 121 | ] 122 | }, 123 | { 124 | "cell_type": "code", 125 | "execution_count": 6, 126 | "id": "c71f515d", 127 | "metadata": {}, 128 | "outputs": [ 129 | { 130 | "name": "stdout", 131 | "output_type": "stream", 132 | "text": [ 133 | "Datetime from timestamp: 2029-10-25 21:47:48\n" 134 | ] 135 | } 136 | ], 137 | "source": [ 138 | "# Write a python code to Get date from Timestamp\n", 139 | "from datetime import datetime\n", 140 | " \n", 141 | "# Getting Datetime from timestamp\n", 142 | "date_time = datetime.fromtimestamp(1887639468)\n", 143 | "print(\"Datetime from timestamp:\", date_time)" 144 | ] 145 | }, 146 | { 147 | "cell_type": "code", 148 | "execution_count": 2, 149 | "id": "44d95628", 150 | "metadata": {}, 151 | "outputs": [ 152 | { 153 | "name": "stdout", 154 | "output_type": "stream", 155 | "text": [ 156 | "String Representation 2022-09-21\n", 157 | "\n" 158 | ] 159 | } 160 | ], 161 | "source": [ 162 | "# Write a python code to Convert Date to String\n", 163 | "today = date.today() # calling the today\n", 164 | "# Converting the date to the string\n", 165 | "Str = date.isoformat(today)\n", 166 | "print(\"String Representation\", Str)\n", 167 | "print(type(Str))\n", 168 | " " 169 | ] 170 | }, 171 | { 172 | "cell_type": "code", 173 | "execution_count": 9, 174 | "id": "e24489fc", 175 | "metadata": {}, 176 | "outputs": [ 177 | { 178 | "name": "stdout", 179 | "output_type": "stream", 180 | "text": [ 181 | "Entered time 13:24:56\n", 182 | "\n", 183 | "Time with one argument 00:12:00\n", 184 | "\n", 185 | "Time without argument 00:00:00\n" 186 | ] 187 | } 188 | ], 189 | "source": [ 190 | "# demonstrate time class\n", 191 | " \n", 192 | "from datetime import time\n", 193 | " \n", 194 | "# calling the constructor\n", 195 | "my_time = time(13, 24, 56)\n", 196 | " \n", 197 | "print(\"Entered time\", my_time)\n", 198 | " \n", 199 | "# calling constructor with 1 argument\n", 200 | "my_time = time(minute=12)\n", 201 | "print(\"\\nTime with one argument\", my_time)\n", 202 | " \n", 203 | "# Calling constructor with 0 argument\n", 204 | "my_time = time()\n", 205 | "print(\"\\nTime without argument\", my_time)" 206 | ] 207 | }, 208 | { 209 | "cell_type": "code", 210 | "execution_count": 10, 211 | "id": "a47e9a34", 212 | "metadata": {}, 213 | "outputs": [ 214 | { 215 | "name": "stdout", 216 | "output_type": "stream", 217 | "text": [ 218 | "hour = 11\n", 219 | "minute = 34\n", 220 | "second = 56\n", 221 | "microsecond = 0\n" 222 | ] 223 | } 224 | ], 225 | "source": [ 226 | "# Write a python program Get hours, minutes, seconds, and microseconds\n", 227 | "from datetime import time\n", 228 | " \n", 229 | "Time = time(11, 34, 56)\n", 230 | " \n", 231 | "print(\"hour =\", Time.hour)\n", 232 | "print(\"minute =\", Time.minute)\n", 233 | "print(\"second =\", Time.second)\n", 234 | "print(\"microsecond =\", Time.microsecond)" 235 | ] 236 | }, 237 | { 238 | "cell_type": "code", 239 | "execution_count": 11, 240 | "id": "6a32b870", 241 | "metadata": {}, 242 | "outputs": [ 243 | { 244 | "name": "stdout", 245 | "output_type": "stream", 246 | "text": [ 247 | "String Representation: 12:24:36.001212\n", 248 | "\n" 249 | ] 250 | } 251 | ], 252 | "source": [ 253 | "# Time object to String\n", 254 | "from datetime import time\n", 255 | " \n", 256 | "# Creating Time object\n", 257 | "Time = time(12,24,36,1212)\n", 258 | " \n", 259 | "# Converting Time object to string\n", 260 | "Str = Time.isoformat()\n", 261 | "print(\"String Representation:\", Str)\n", 262 | "print(type(Str))" 263 | ] 264 | }, 265 | { 266 | "cell_type": "code", 267 | "execution_count": 13, 268 | "id": "bca9a442", 269 | "metadata": {}, 270 | "outputs": [ 271 | { 272 | "name": "stdout", 273 | "output_type": "stream", 274 | "text": [ 275 | "2022-12-12 00:00:00\n", 276 | "2022-12-12 12:12:12.034230\n" 277 | ] 278 | } 279 | ], 280 | "source": [ 281 | "# DateTime object representing DateTime in Python \n", 282 | "from datetime import datetime\n", 283 | " \n", 284 | "# Initializing constructor\n", 285 | "a = datetime(2022, 12, 12)\n", 286 | "print(a)\n", 287 | " \n", 288 | "# Initializing constructor\n", 289 | "# with time parameters as well\n", 290 | "a = datetime(2022, 12, 12, 12, 12, 12, 34230)\n", 291 | "print(a)" 292 | ] 293 | }, 294 | { 295 | "cell_type": "code", 296 | "execution_count": 14, 297 | "id": "75c0fc62", 298 | "metadata": {}, 299 | "outputs": [ 300 | { 301 | "name": "stdout", 302 | "output_type": "stream", 303 | "text": [ 304 | "year = 1999\n", 305 | "month = 12\n", 306 | "hour = 12\n", 307 | "minute = 12\n", 308 | "timestamp = 944980932.0\n" 309 | ] 310 | } 311 | ], 312 | "source": [ 313 | "# Get year, month, hour, minute, and timestamp\n", 314 | "from datetime import datetime\n", 315 | " \n", 316 | "a = datetime(1999, 12, 12, 12, 12, 12)\n", 317 | " \n", 318 | "print(\"year =\", a.year)\n", 319 | "print(\"month =\", a.month)\n", 320 | "print(\"hour =\", a.hour)\n", 321 | "print(\"minute =\", a.minute)\n", 322 | "print(\"timestamp =\", a.timestamp())" 323 | ] 324 | }, 325 | { 326 | "cell_type": "raw", 327 | "id": "76056c30", 328 | "metadata": {}, 329 | "source": [ 330 | "timedelta – A duration expressing the difference between two date, time, or datetime instances to microsecond resolution." 331 | ] 332 | }, 333 | { 334 | "cell_type": "code", 335 | "execution_count": 18, 336 | "id": "756c65e2", 337 | "metadata": {}, 338 | "outputs": [ 339 | { 340 | "name": "stdout", 341 | "output_type": "stream", 342 | "text": [ 343 | "Difference in dates: 31 days, 0:00:00\n", 344 | "Date 1 after 4 days: 2020-01-07 00:00:00\n", 345 | "Date 1 before 15 days: 2019-12-23 00:00:00\n" 346 | ] 347 | } 348 | ], 349 | "source": [ 350 | "# Timedelta function demonstration\n", 351 | " \n", 352 | "from datetime import datetime, timedelta\n", 353 | " \n", 354 | "# creating datetime objects\n", 355 | "date1 = datetime(2020, 1, 3)\n", 356 | "date2 = datetime(2020, 2, 3)\n", 357 | " \n", 358 | "# difference between dates\n", 359 | "diff = date2 - date1\n", 360 | "print(\"Difference in dates:\", diff)\n", 361 | " \n", 362 | "# Adding days to date1\n", 363 | "date1 += timedelta(days = 4)\n", 364 | "print(\"Date 1 after 4 days:\", date1)\n", 365 | " \n", 366 | "# Subtracting days from date1\n", 367 | "date1 -= timedelta(15)\n", 368 | "print(\"Date 1 before 15 days:\", date1)" 369 | ] 370 | }, 371 | { 372 | "cell_type": "code", 373 | "execution_count": 19, 374 | "id": "c78e8fee", 375 | "metadata": {}, 376 | "outputs": [ 377 | { 378 | "name": "stdout", 379 | "output_type": "stream", 380 | "text": [ 381 | "Minimum value of timedelta object -999999999 days, 0:00:00\n", 382 | "Maximum value of timedelta object 999999999 days, 23:59:59.999999\n" 383 | ] 384 | } 385 | ], 386 | "source": [ 387 | "from datetime import timedelta\n", 388 | " \n", 389 | "# Getting minimum value\n", 390 | "Min = timedelta.min\n", 391 | "print(\"Minimum value of timedelta object\", Min)\n", 392 | " \n", 393 | "# Getting minimum value\n", 394 | "Max = timedelta.max\n", 395 | "print(\"Maximum value of timedelta object\", Max)" 396 | ] 397 | }, 398 | { 399 | "cell_type": "markdown", 400 | "id": "48860aed", 401 | "metadata": {}, 402 | "source": [ 403 | "# Calendar Module" 404 | ] 405 | }, 406 | { 407 | "cell_type": "code", 408 | "execution_count": 21, 409 | "id": "c76df0ba", 410 | "metadata": {}, 411 | "outputs": [ 412 | { 413 | "name": "stdout", 414 | "output_type": "stream", 415 | "text": [ 416 | " September 2022\n", 417 | "Mo Tu We Th Fr Sa Su\n", 418 | " 1 2 3 4\n", 419 | " 5 6 7 8 9 10 11\n", 420 | "12 13 14 15 16 17 18\n", 421 | "19 20 21 22 23 24 25\n", 422 | "26 27 28 29 30\n", 423 | "\n" 424 | ] 425 | } 426 | ], 427 | "source": [ 428 | "import calendar\n", 429 | " \n", 430 | "yy = 2022\n", 431 | "mm = 9\n", 432 | " \n", 433 | "# display the calendar\n", 434 | "print(calendar.month(yy, mm))" 435 | ] 436 | }, 437 | { 438 | "cell_type": "code", 439 | "execution_count": 23, 440 | "id": "217b78e3", 441 | "metadata": {}, 442 | "outputs": [ 443 | { 444 | "name": "stdout", 445 | "output_type": "stream", 446 | "text": [ 447 | "The calendar of year 2022 is : \n", 448 | " 2022\n", 449 | "\n", 450 | " January February March\n", 451 | "Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su\n", 452 | " 1 2 1 2 3 4 5 6 1 2 3 4 5 6\n", 453 | " 3 4 5 6 7 8 9 7 8 9 10 11 12 13 7 8 9 10 11 12 13\n", 454 | "10 11 12 13 14 15 16 14 15 16 17 18 19 20 14 15 16 17 18 19 20\n", 455 | "17 18 19 20 21 22 23 21 22 23 24 25 26 27 21 22 23 24 25 26 27\n", 456 | "24 25 26 27 28 29 30 28 28 29 30 31\n", 457 | "31\n", 458 | "\n", 459 | " April May June\n", 460 | "Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su\n", 461 | " 1 2 3 1 1 2 3 4 5\n", 462 | " 4 5 6 7 8 9 10 2 3 4 5 6 7 8 6 7 8 9 10 11 12\n", 463 | "11 12 13 14 15 16 17 9 10 11 12 13 14 15 13 14 15 16 17 18 19\n", 464 | "18 19 20 21 22 23 24 16 17 18 19 20 21 22 20 21 22 23 24 25 26\n", 465 | "25 26 27 28 29 30 23 24 25 26 27 28 29 27 28 29 30\n", 466 | " 30 31\n", 467 | "\n", 468 | " July August September\n", 469 | "Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su\n", 470 | " 1 2 3 1 2 3 4 5 6 7 1 2 3 4\n", 471 | " 4 5 6 7 8 9 10 8 9 10 11 12 13 14 5 6 7 8 9 10 11\n", 472 | "11 12 13 14 15 16 17 15 16 17 18 19 20 21 12 13 14 15 16 17 18\n", 473 | "18 19 20 21 22 23 24 22 23 24 25 26 27 28 19 20 21 22 23 24 25\n", 474 | "25 26 27 28 29 30 31 29 30 31 26 27 28 29 30\n", 475 | "\n", 476 | " October November December\n", 477 | "Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su\n", 478 | " 1 2 1 2 3 4 5 6 1 2 3 4\n", 479 | " 3 4 5 6 7 8 9 7 8 9 10 11 12 13 5 6 7 8 9 10 11\n", 480 | "10 11 12 13 14 15 16 14 15 16 17 18 19 20 12 13 14 15 16 17 18\n", 481 | "17 18 19 20 21 22 23 21 22 23 24 25 26 27 19 20 21 22 23 24 25\n", 482 | "24 25 26 27 28 29 30 28 29 30 26 27 28 29 30 31\n", 483 | "31\n", 484 | "\n" 485 | ] 486 | } 487 | ], 488 | "source": [ 489 | "# Display calendar of the given year.\n", 490 | "import calendar\n", 491 | " \n", 492 | "# using calendar to print calendar of year\n", 493 | "# prints calendar of 2018\n", 494 | "print (\"The calendar of year 2022 is : \")\n", 495 | "print (calendar.calendar(2022))" 496 | ] 497 | }, 498 | { 499 | "cell_type": "code", 500 | "execution_count": 26, 501 | "id": "e091b398", 502 | "metadata": { 503 | "scrolled": true 504 | }, 505 | "outputs": [ 506 | { 507 | "name": "stdout", 508 | "output_type": "stream", 509 | "text": [ 510 | "\n", 511 | "\n", 512 | "\n", 513 | "\n", 514 | "\n", 515 | "\n", 516 | "\n", 517 | "\n", 518 | "
September 2022
MonTueWedThuFriSatSun
   1234
567891011
12131415161718
19202122232425
2627282930  
\n", 519 | "\n" 520 | ] 521 | } 522 | ], 523 | "source": [ 524 | "# import html calendar \n", 525 | "import calendar\n", 526 | " \n", 527 | "text_cal = calendar.HTMLCalendar(firstweekday = 0)\n", 528 | " \n", 529 | "year = 2022\n", 530 | "month = 9\n", 531 | "# default value of width is 0\n", 532 | " \n", 533 | "# printing formatmonth\n", 534 | "print(text_cal.formatmonth(year, month))" 535 | ] 536 | }, 537 | { 538 | "cell_type": "code", 539 | "execution_count": null, 540 | "id": "3866883a", 541 | "metadata": {}, 542 | "outputs": [], 543 | "source": [] 544 | } 545 | ], 546 | "metadata": { 547 | "kernelspec": { 548 | "display_name": "Python 3 (ipykernel)", 549 | "language": "python", 550 | "name": "python3" 551 | }, 552 | "language_info": { 553 | "codemirror_mode": { 554 | "name": "ipython", 555 | "version": 3 556 | }, 557 | "file_extension": ".py", 558 | "mimetype": "text/x-python", 559 | "name": "python", 560 | "nbconvert_exporter": "python", 561 | "pygments_lexer": "ipython3", 562 | "version": "3.9.12" 563 | } 564 | }, 565 | "nbformat": 4, 566 | "nbformat_minor": 5 567 | } 568 | -------------------------------------------------------------------------------- /Garbage Collection.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "id": "dfc5f5cc", 7 | "metadata": {}, 8 | "outputs": [], 9 | "source": [ 10 | "b = 5\n" 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": 2, 16 | "id": "8caa55b0", 17 | "metadata": {}, 18 | "outputs": [], 19 | "source": [ 20 | "b =9" 21 | ] 22 | }, 23 | { 24 | "cell_type": "code", 25 | "execution_count": 3, 26 | "id": "c70cd4a3", 27 | "metadata": {}, 28 | "outputs": [ 29 | { 30 | "name": "stdout", 31 | "output_type": "stream", 32 | "text": [ 33 | "9\n" 34 | ] 35 | } 36 | ], 37 | "source": [ 38 | "print(b)" 39 | ] 40 | }, 41 | { 42 | "cell_type": "code", 43 | "execution_count": 9, 44 | "id": "6125d9a8", 45 | "metadata": {}, 46 | "outputs": [ 47 | { 48 | "name": "stdout", 49 | "output_type": "stream", 50 | "text": [ 51 | "1604505528880\n" 52 | ] 53 | } 54 | ], 55 | "source": [ 56 | "print(id(b))" 57 | ] 58 | }, 59 | { 60 | "cell_type": "markdown", 61 | "id": "9800299a", 62 | "metadata": {}, 63 | "source": [ 64 | "# Automatic Garbage Collection" 65 | ] 66 | }, 67 | { 68 | "cell_type": "raw", 69 | "id": "eb6e432e", 70 | "metadata": {}, 71 | "source": [ 72 | "Because reference cycles take computational work to discover, garbage collection must be a scheduled activity. \n", 73 | "Python schedules garbage collection based upon a threshold of object allocations and object deallocations. When the number \n", 74 | "of allocations minus the number of deallocations is greater than the threshold number, the garbage collector is run. \n", 75 | "One can inspect the threshold for new objects (objects in Python known as generation 0 objects) by\n", 76 | "importing the gc module and asking for garbage collection thresholds: \n", 77 | " " 78 | ] 79 | }, 80 | { 81 | "cell_type": "code", 82 | "execution_count": 14, 83 | "id": "5b49fd93", 84 | "metadata": {}, 85 | "outputs": [ 86 | { 87 | "name": "stdout", 88 | "output_type": "stream", 89 | "text": [ 90 | "Garbage collection thresholds: (700, 10, 10)\n" 91 | ] 92 | } 93 | ], 94 | "source": [ 95 | "# loading gc\n", 96 | "import gc\n", 97 | "print(\"Garbage collection thresholds:\",\n", 98 | " gc.get_threshold())" 99 | ] 100 | }, 101 | { 102 | "cell_type": "raw", 103 | "id": "341755d9", 104 | "metadata": {}, 105 | "source": [ 106 | "Here, the default threshold on the above system is 700. This means when the number of allocations vs. the number of deallocations is greater than 700 the automatic garbage collector will run. Thus any portion of your code which frees up large blocks of memory is a good candidate for running manual garbage collection. " 107 | ] 108 | }, 109 | { 110 | "cell_type": "markdown", 111 | "id": "0c29b9bc", 112 | "metadata": {}, 113 | "source": [ 114 | "# Manual Garbage Collection" 115 | ] 116 | }, 117 | { 118 | "cell_type": "raw", 119 | "id": "87e52439", 120 | "metadata": {}, 121 | "source": [ 122 | "Invoking the garbage collector manually during the execution of a program can be a good idea on how to handle memory \n", 123 | "being consumed by reference cycles. \n", 124 | "The garbage collection can be invoked manually in the following way: " 125 | ] 126 | }, 127 | { 128 | "cell_type": "code", 129 | "execution_count": 15, 130 | "id": "a0f020ec", 131 | "metadata": {}, 132 | "outputs": [ 133 | { 134 | "name": "stdout", 135 | "output_type": "stream", 136 | "text": [ 137 | "Garbage collector: collected 1392 objects.\n" 138 | ] 139 | } 140 | ], 141 | "source": [ 142 | "# Importing gc module\n", 143 | "import gc\n", 144 | " \n", 145 | "# Returns the number of\n", 146 | "# objects it has collected\n", 147 | "# and deallocated\n", 148 | "collected = gc.collect()\n", 149 | " \n", 150 | "# Prints Garbage collector\n", 151 | "# as 0 object\n", 152 | "print(\"Garbage collector: collected\",\n", 153 | " \"%d objects.\" % collected)" 154 | ] 155 | } 156 | ], 157 | "metadata": { 158 | "kernelspec": { 159 | "display_name": "Python 3 (ipykernel)", 160 | "language": "python", 161 | "name": "python3" 162 | }, 163 | "language_info": { 164 | "codemirror_mode": { 165 | "name": "ipython", 166 | "version": 3 167 | }, 168 | "file_extension": ".py", 169 | "mimetype": "text/x-python", 170 | "name": "python", 171 | "nbconvert_exporter": "python", 172 | "pygments_lexer": "ipython3", 173 | "version": "3.9.12" 174 | } 175 | }, 176 | "nbformat": 4, 177 | "nbformat_minor": 5 178 | } 179 | -------------------------------------------------------------------------------- /If Else.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "id": "b0b6b1b7", 7 | "metadata": {}, 8 | "outputs": [], 9 | "source": [ 10 | "a = \"jalpa\"" 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": 2, 16 | "id": "856fbb43", 17 | "metadata": {}, 18 | "outputs": [ 19 | { 20 | "name": "stdout", 21 | "output_type": "stream", 22 | "text": [ 23 | "jalpa\n" 24 | ] 25 | } 26 | ], 27 | "source": [ 28 | "print(a)" 29 | ] 30 | }, 31 | { 32 | "cell_type": "code", 33 | "execution_count": 3, 34 | "id": "c3441da2", 35 | "metadata": {}, 36 | "outputs": [ 37 | { 38 | "data": { 39 | "text/plain": [ 40 | "'a'" 41 | ] 42 | }, 43 | "execution_count": 3, 44 | "metadata": {}, 45 | "output_type": "execute_result" 46 | } 47 | ], 48 | "source": [ 49 | "a[1]" 50 | ] 51 | }, 52 | { 53 | "cell_type": "code", 54 | "execution_count": 4, 55 | "id": "a68184ab", 56 | "metadata": {}, 57 | "outputs": [ 58 | { 59 | "data": { 60 | "text/plain": [ 61 | "'jalp'" 62 | ] 63 | }, 64 | "execution_count": 4, 65 | "metadata": {}, 66 | "output_type": "execute_result" 67 | } 68 | ], 69 | "source": [ 70 | "a[0:4]" 71 | ] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "execution_count": 6, 76 | "id": "2c08b476", 77 | "metadata": {}, 78 | "outputs": [ 79 | { 80 | "data": { 81 | "text/plain": [ 82 | "'jalpa'" 83 | ] 84 | }, 85 | "execution_count": 6, 86 | "metadata": {}, 87 | "output_type": "execute_result" 88 | } 89 | ], 90 | "source": [ 91 | "a" 92 | ] 93 | }, 94 | { 95 | "cell_type": "code", 96 | "execution_count": 13, 97 | "id": "095ad8f2", 98 | "metadata": {}, 99 | "outputs": [ 100 | { 101 | "data": { 102 | "text/plain": [ 103 | "'jal'" 104 | ] 105 | }, 106 | "execution_count": 13, 107 | "metadata": {}, 108 | "output_type": "execute_result" 109 | } 110 | ], 111 | "source": [ 112 | "a[0:-2]" 113 | ] 114 | }, 115 | { 116 | "cell_type": "code", 117 | "execution_count": 14, 118 | "id": "211557a0", 119 | "metadata": {}, 120 | "outputs": [ 121 | { 122 | "data": { 123 | "text/plain": [ 124 | "'jalpa'" 125 | ] 126 | }, 127 | "execution_count": 14, 128 | "metadata": {}, 129 | "output_type": "execute_result" 130 | } 131 | ], 132 | "source": [ 133 | "a" 134 | ] 135 | }, 136 | { 137 | "cell_type": "code", 138 | "execution_count": 18, 139 | "id": "2979e273", 140 | "metadata": {}, 141 | "outputs": [ 142 | { 143 | "data": { 144 | "text/plain": [ 145 | "'aplaj'" 146 | ] 147 | }, 148 | "execution_count": 18, 149 | "metadata": {}, 150 | "output_type": "execute_result" 151 | } 152 | ], 153 | "source": [ 154 | "a[::-1]" 155 | ] 156 | }, 157 | { 158 | "cell_type": "code", 159 | "execution_count": 8, 160 | "id": "cfa09c07", 161 | "metadata": {}, 162 | "outputs": [ 163 | { 164 | "name": "stdout", 165 | "output_type": "stream", 166 | "text": [ 167 | "4\n" 168 | ] 169 | } 170 | ], 171 | "source": [ 172 | "if a < 10:\n", 173 | " print(a)" 174 | ] 175 | }, 176 | { 177 | "cell_type": "code", 178 | "execution_count": 13, 179 | "id": "14865a24", 180 | "metadata": {}, 181 | "outputs": [ 182 | { 183 | "name": "stdout", 184 | "output_type": "stream", 185 | "text": [ 186 | "enter the value 6\n" 187 | ] 188 | } 189 | ], 190 | "source": [ 191 | "a = int(input(\"enter the value \"))" 192 | ] 193 | }, 194 | { 195 | "cell_type": "code", 196 | "execution_count": 15, 197 | "id": "47da605d", 198 | "metadata": {}, 199 | "outputs": [ 200 | { 201 | "name": "stdout", 202 | "output_type": "stream", 203 | "text": [ 204 | "6\n" 205 | ] 206 | } 207 | ], 208 | "source": [ 209 | "if a < 7:\n", 210 | " print(a)\n", 211 | "else:\n", 212 | " print(\"nan\")" 213 | ] 214 | }, 215 | { 216 | "cell_type": "code", 217 | "execution_count": 16, 218 | "id": "0017fa0b", 219 | "metadata": {}, 220 | "outputs": [ 221 | { 222 | "name": "stdout", 223 | "output_type": "stream", 224 | "text": [ 225 | "my name is jalpa\n" 226 | ] 227 | } 228 | ], 229 | "source": [ 230 | "if a < 15 :\n", 231 | " print(\"my name is jalpa\")" 232 | ] 233 | }, 234 | { 235 | "cell_type": "code", 236 | "execution_count": 18, 237 | "id": "29123db1", 238 | "metadata": {}, 239 | "outputs": [], 240 | "source": [ 241 | "if 24 < 15 :\n", 242 | " print(\"my name is jalpa\")" 243 | ] 244 | }, 245 | { 246 | "cell_type": "code", 247 | "execution_count": 19, 248 | "id": "3a98cf0b", 249 | "metadata": {}, 250 | "outputs": [ 251 | { 252 | "data": { 253 | "text/plain": [ 254 | "False" 255 | ] 256 | }, 257 | "execution_count": 19, 258 | "metadata": {}, 259 | "output_type": "execute_result" 260 | } 261 | ], 262 | "source": [ 263 | "24 < 15" 264 | ] 265 | }, 266 | { 267 | "cell_type": "code", 268 | "execution_count": 21, 269 | "id": "edacb528", 270 | "metadata": {}, 271 | "outputs": [ 272 | { 273 | "name": "stdout", 274 | "output_type": "stream", 275 | "text": [ 276 | "if statemnt is wrong \n" 277 | ] 278 | } 279 | ], 280 | "source": [ 281 | "if 11<10 :\n", 282 | " print(\" 10 is lesser then 3 \")\n", 283 | "else :\n", 284 | " print(\"if statemnt is wrong \")" 285 | ] 286 | }, 287 | { 288 | "cell_type": "code", 289 | "execution_count": 23, 290 | "id": "1b645f91", 291 | "metadata": {}, 292 | "outputs": [ 293 | { 294 | "name": "stdout", 295 | "output_type": "stream", 296 | "text": [ 297 | "6\n", 298 | "i will be able to buy phone\n" 299 | ] 300 | } 301 | ], 302 | "source": [ 303 | "income = int(input())\n", 304 | "if income < 50:\n", 305 | " print(\"i will be able to buy phone\")\n", 306 | "elif income < 70 :\n", 307 | " print(\"i will be able to buy car \")\n", 308 | "elif income < 90 :\n", 309 | " print(\"i will be able to rent a house \")\n", 310 | "else :\n", 311 | " print(\"i wont be able to buy anything\")" 312 | ] 313 | }, 314 | { 315 | "cell_type": "code", 316 | "execution_count": 25, 317 | "id": "74f12abd", 318 | "metadata": {}, 319 | "outputs": [ 320 | { 321 | "name": "stdout", 322 | "output_type": "stream", 323 | "text": [ 324 | "70000\n", 325 | "Discount will be 14000.0\n" 326 | ] 327 | } 328 | ], 329 | "source": [ 330 | "total_income = int(input())\n", 331 | "\n", 332 | "if total_income > 20000 :\n", 333 | " discount = total_income * .20\n", 334 | " print(\"Discount will be \" , discount)\n", 335 | "elif total_income <= 7000 :\n", 336 | " discount = total_income *.05\n", 337 | " print(\"dicount will be \" , discount)\n", 338 | "else :\n", 339 | " print( \"wont be able to give any discount \")" 340 | ] 341 | }, 342 | { 343 | "cell_type": "code", 344 | "execution_count": 28, 345 | "id": "16010b4e", 346 | "metadata": {}, 347 | "outputs": [ 348 | { 349 | "name": "stdout", 350 | "output_type": "stream", 351 | "text": [ 352 | "11\n", 353 | "it will be difficult \n" 354 | ] 355 | } 356 | ], 357 | "source": [ 358 | "study_hour = int(input())\n", 359 | "if study_hour < 1:\n", 360 | " print(\"it may take 8- 9 month of time to make a transitino \")\n", 361 | "elif study_hour < 4 and study_hour > 1:\n", 362 | " print(\"it may take 6 month for transition\")\n", 363 | "elif study_hour < 10 :\n", 364 | " print(\" it will take 3 month of time for transition \")\n", 365 | "else :\n", 366 | " print(\"it will be difficult \")" 367 | ] 368 | }, 369 | { 370 | "cell_type": "code", 371 | "execution_count": 29, 372 | "id": "61da47f4", 373 | "metadata": {}, 374 | "outputs": [ 375 | { 376 | "name": "stdout", 377 | "output_type": "stream", 378 | "text": [ 379 | "valid\n" 380 | ] 381 | } 382 | ], 383 | "source": [ 384 | "a = 9 \n", 385 | "if a == 9 : \n", 386 | " print(\"valid\")" 387 | ] 388 | }, 389 | { 390 | "cell_type": "code", 391 | "execution_count": 33, 392 | "id": "4f5319eb", 393 | "metadata": {}, 394 | "outputs": [ 395 | { 396 | "name": "stdout", 397 | "output_type": "stream", 398 | "text": [ 399 | "Enter the value of a : 7\n", 400 | "7 is a positive number\n" 401 | ] 402 | } 403 | ], 404 | "source": [ 405 | "'''this programm for positive and negative of a input number'''\n", 406 | "a = int(input(\"Enter the value of a : \"))\n", 407 | "if a > 0:\n", 408 | " print(a, \"is a positive number\")\n", 409 | "elif a == 0:\n", 410 | " print(a, \"is going to be zero\")\n", 411 | "else:\n", 412 | " print(a, \" is a negative number\")" 413 | ] 414 | }, 415 | { 416 | "cell_type": "code", 417 | "execution_count": 34, 418 | "id": "1dbf11dd", 419 | "metadata": {}, 420 | "outputs": [ 421 | { 422 | "name": "stdout", 423 | "output_type": "stream", 424 | "text": [ 425 | "Enter a number: .9\n", 426 | "Positive number\n" 427 | ] 428 | } 429 | ], 430 | "source": [ 431 | "num = float(input(\"Enter a number: \"))\n", 432 | "if num >= 0:\n", 433 | " if num == 0:\n", 434 | " print(\"Zero\")\n", 435 | " else:\n", 436 | " print(\"Positive number\")\n", 437 | "else:\n", 438 | " print(\"Negative number\")" 439 | ] 440 | }, 441 | { 442 | "cell_type": "code", 443 | "execution_count": 35, 444 | "id": "e2a93711", 445 | "metadata": {}, 446 | "outputs": [], 447 | "source": [ 448 | "a = .9" 449 | ] 450 | }, 451 | { 452 | "cell_type": "code", 453 | "execution_count": 36, 454 | "id": "69034d4f", 455 | "metadata": {}, 456 | "outputs": [ 457 | { 458 | "data": { 459 | "text/plain": [ 460 | "float" 461 | ] 462 | }, 463 | "execution_count": 36, 464 | "metadata": {}, 465 | "output_type": "execute_result" 466 | } 467 | ], 468 | "source": [ 469 | "type(a)" 470 | ] 471 | }, 472 | { 473 | "cell_type": "code", 474 | "execution_count": 37, 475 | "id": "e821438e", 476 | "metadata": {}, 477 | "outputs": [], 478 | "source": [ 479 | "a = 3" 480 | ] 481 | }, 482 | { 483 | "cell_type": "code", 484 | "execution_count": 38, 485 | "id": "bf39dfef", 486 | "metadata": {}, 487 | "outputs": [ 488 | { 489 | "name": "stdout", 490 | "output_type": "stream", 491 | "text": [ 492 | "3\n" 493 | ] 494 | } 495 | ], 496 | "source": [ 497 | "print(a)" 498 | ] 499 | }, 500 | { 501 | "cell_type": "code", 502 | "execution_count": 39, 503 | "id": "6d2c39f1", 504 | "metadata": {}, 505 | "outputs": [ 506 | { 507 | "data": { 508 | "text/plain": [ 509 | "int" 510 | ] 511 | }, 512 | "execution_count": 39, 513 | "metadata": {}, 514 | "output_type": "execute_result" 515 | } 516 | ], 517 | "source": [ 518 | "type(3)" 519 | ] 520 | }, 521 | { 522 | "cell_type": "code", 523 | "execution_count": 41, 524 | "id": "e202c16d", 525 | "metadata": {}, 526 | "outputs": [], 527 | "source": [ 528 | "g=4+4j" 529 | ] 530 | }, 531 | { 532 | "cell_type": "code", 533 | "execution_count": 42, 534 | "id": "95d3ccc3", 535 | "metadata": {}, 536 | "outputs": [ 537 | { 538 | "data": { 539 | "text/plain": [ 540 | "complex" 541 | ] 542 | }, 543 | "execution_count": 42, 544 | "metadata": {}, 545 | "output_type": "execute_result" 546 | } 547 | ], 548 | "source": [ 549 | "type(g)" 550 | ] 551 | }, 552 | { 553 | "cell_type": "code", 554 | "execution_count": 43, 555 | "id": "c940b29d", 556 | "metadata": {}, 557 | "outputs": [], 558 | "source": [ 559 | "s = \"sparsh\"" 560 | ] 561 | }, 562 | { 563 | "cell_type": "code", 564 | "execution_count": 44, 565 | "id": "9169963c", 566 | "metadata": {}, 567 | "outputs": [ 568 | { 569 | "data": { 570 | "text/plain": [ 571 | "str" 572 | ] 573 | }, 574 | "execution_count": 44, 575 | "metadata": {}, 576 | "output_type": "execute_result" 577 | } 578 | ], 579 | "source": [ 580 | "type(s)" 581 | ] 582 | }, 583 | { 584 | "cell_type": "code", 585 | "execution_count": null, 586 | "id": "874dc87a", 587 | "metadata": {}, 588 | "outputs": [], 589 | "source": [] 590 | } 591 | ], 592 | "metadata": { 593 | "kernelspec": { 594 | "display_name": "Python 3 (ipykernel)", 595 | "language": "python", 596 | "name": "python3" 597 | }, 598 | "language_info": { 599 | "codemirror_mode": { 600 | "name": "ipython", 601 | "version": 3 602 | }, 603 | "file_extension": ".py", 604 | "mimetype": "text/x-python", 605 | "name": "python", 606 | "nbconvert_exporter": "python", 607 | "pygments_lexer": "ipython3", 608 | "version": "3.9.7" 609 | } 610 | }, 611 | "nbformat": 4, 612 | "nbformat_minor": 5 613 | } 614 | -------------------------------------------------------------------------------- /Inheritance_Example_Super().ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 14, 6 | "id": "007cbc24", 7 | "metadata": {}, 8 | "outputs": [ 9 | { 10 | "data": { 11 | "text/plain": [ 12 | "'petrol'" 13 | ] 14 | }, 15 | "execution_count": 14, 16 | "metadata": {}, 17 | "output_type": "execute_result" 18 | } 19 | ], 20 | "source": [ 21 | "## Parents Class\n", 22 | "class car:\n", 23 | " def __init__ (self, engine, color):\n", 24 | " self.engine = engine\n", 25 | " self.color = color\n", 26 | " def func1(self):\n", 27 | " return self.engine\n", 28 | " ## Child Class\n", 29 | "class car1(car):\n", 30 | " def __init__ (self, engine, color,horse_power, window):\n", 31 | " super().__init__(engine, color)\n", 32 | " self.horse_power = horse_power\n", 33 | " self.window = window\n", 34 | " \n", 35 | " def func2(self):\n", 36 | " return self.window\n", 37 | " \n", 38 | "obj = car1('petrol','black','120hz',4)\n", 39 | "obj.func1() " 40 | ] 41 | }, 42 | { 43 | "cell_type": "code", 44 | "execution_count": 32, 45 | "id": "a7260328", 46 | "metadata": {}, 47 | "outputs": [ 48 | { 49 | "data": { 50 | "text/plain": [ 51 | "' My Name is sandesh and Roll is 12 salary is 1200'" 52 | ] 53 | }, 54 | "execution_count": 32, 55 | "metadata": {}, 56 | "output_type": "execute_result" 57 | } 58 | ], 59 | "source": [ 60 | "class A:\n", 61 | " def __init__(self,name,roll):\n", 62 | " self.name = name\n", 63 | " self.roll = roll \n", 64 | " def get(self):\n", 65 | " return f\"{self.name} and {self.roll}\"\n", 66 | "\n", 67 | "class B(A):\n", 68 | " def __init__(self,name,roll,salary):\n", 69 | " self.salary = salary\n", 70 | " A.__init__(self,name,roll)\n", 71 | " def get(self):\n", 72 | " return f\" My Name is {self.name} and Roll is {self.roll} salary is {self.salary}\"\n", 73 | " \n", 74 | "\n", 75 | "b = B('sandesh',12,1200)\n", 76 | "b.get()" 77 | ] 78 | }, 79 | { 80 | "cell_type": "code", 81 | "execution_count": 4, 82 | "id": "a17a82d2", 83 | "metadata": {}, 84 | "outputs": [ 85 | { 86 | "name": "stdout", 87 | "output_type": "stream", 88 | "text": [ 89 | "sparrows can fly\n" 90 | ] 91 | } 92 | ], 93 | "source": [ 94 | "class bird:\n", 95 | " def intro(self):\n", 96 | " print(\"there are many types of bird\")\n", 97 | " def flight(self):\n", 98 | " print(\"Most birds can fly but few cannot\")\n", 99 | " \n", 100 | "class sparrow(bird):\n", 101 | " def flight(self):\n", 102 | " print(\"sparrows can fly\")\n", 103 | " \n", 104 | "class ostrich(bird):\n", 105 | " def flight(self):\n", 106 | " print(\"Ostrich cannot fly\")\n", 107 | " \n", 108 | "titli=sparrow()\n", 109 | "kitty=ostrich()\n", 110 | "bird1 = bird()\n", 111 | "\n", 112 | "titli.flight()" 113 | ] 114 | }, 115 | { 116 | "cell_type": "code", 117 | "execution_count": 25, 118 | "id": "c066e601", 119 | "metadata": {}, 120 | "outputs": [ 121 | { 122 | "name": "stdout", 123 | "output_type": "stream", 124 | "text": [ 125 | "This is my id : 107\n" 126 | ] 127 | }, 128 | { 129 | "data": { 130 | "text/plain": [ 131 | "('jalpa', 'patel')" 132 | ] 133 | }, 134 | "execution_count": 25, 135 | "metadata": {}, 136 | "output_type": "execute_result" 137 | } 138 | ], 139 | "source": [ 140 | "## Parent Class\n", 141 | "class Person:\n", 142 | " def __init__(self, fname, lname, year):\n", 143 | " self.fname = fname\n", 144 | " self.lname = lname\n", 145 | " self.year = year\n", 146 | " def func1(self):\n", 147 | " return self.fname,self.lname\n", 148 | "## Child Class \n", 149 | "class Student(Person):\n", 150 | " def __init__(self, fname, lname, year, id):\n", 151 | " super().__init__(fname, lname, year)\n", 152 | " self.id = id\n", 153 | " def funct2(self):\n", 154 | " print(\"This is my id : \", self.id )\n", 155 | " \n", 156 | "obj = Student('jalpa','patel',2000,107)\n", 157 | "\n", 158 | "obj.funct2()\n", 159 | "obj.func1()\n" 160 | ] 161 | }, 162 | { 163 | "cell_type": "code", 164 | "execution_count": 30, 165 | "id": "2832f126", 166 | "metadata": {}, 167 | "outputs": [ 168 | { 169 | "ename": "AttributeError", 170 | "evalue": "'B' object has no attribute 'name'", 171 | "output_type": "error", 172 | "traceback": [ 173 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 174 | "\u001b[1;31mAttributeError\u001b[0m Traceback (most recent call last)", 175 | "Input \u001b[1;32mIn [30]\u001b[0m, in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 8\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mroll \u001b[38;5;241m=\u001b[39m roll\n\u001b[0;32m 11\u001b[0m \u001b[38;5;28mobject\u001b[39m \u001b[38;5;241m=\u001b[39m B(\u001b[38;5;241m23\u001b[39m)\n\u001b[1;32m---> 12\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;28;43mobject\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mname\u001b[49m)\n", 176 | "\u001b[1;31mAttributeError\u001b[0m: 'B' object has no attribute 'name'" 177 | ] 178 | } 179 | ], 180 | "source": [ 181 | "class A:\n", 182 | " def __init__(self, name):\n", 183 | " self.name = n\n", 184 | " \n", 185 | " \n", 186 | "class B(A):\n", 187 | " def __init__(self, roll):\n", 188 | " self.roll = roll\n", 189 | " \n", 190 | " \n", 191 | "object = B(23)\n", 192 | "print(object.name)" 193 | ] 194 | }, 195 | { 196 | "cell_type": "code", 197 | "execution_count": 31, 198 | "id": "9db153a5", 199 | "metadata": {}, 200 | "outputs": [ 201 | { 202 | "name": "stdout", 203 | "output_type": "stream", 204 | "text": [ 205 | "jalpa\n" 206 | ] 207 | } 208 | ], 209 | "source": [ 210 | "class A:\n", 211 | " def __init__(self, name):\n", 212 | " self.name = name\n", 213 | " \n", 214 | " \n", 215 | "class B(A):\n", 216 | " def __init__(self, name,roll):\n", 217 | " super().__init__(name)\n", 218 | " self.roll = roll\n", 219 | " \n", 220 | " \n", 221 | "object = B('jalpa',23)\n", 222 | "print(object.name)" 223 | ] 224 | }, 225 | { 226 | "cell_type": "code", 227 | "execution_count": null, 228 | "id": "15889d13", 229 | "metadata": {}, 230 | "outputs": [], 231 | "source": [] 232 | } 233 | ], 234 | "metadata": { 235 | "kernelspec": { 236 | "display_name": "Python 3 (ipykernel)", 237 | "language": "python", 238 | "name": "python3" 239 | }, 240 | "language_info": { 241 | "codemirror_mode": { 242 | "name": "ipython", 243 | "version": 3 244 | }, 245 | "file_extension": ".py", 246 | "mimetype": "text/x-python", 247 | "name": "python", 248 | "nbconvert_exporter": "python", 249 | "pygments_lexer": "ipython3", 250 | "version": "3.9.12" 251 | } 252 | }, 253 | "nbformat": 4, 254 | "nbformat_minor": 5 255 | } 256 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Jalpa Patel Desai 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Logging Examples.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 2, 6 | "id": "48b63298-8207-4562-8b68-01f2e4e66379", 7 | "metadata": {}, 8 | "outputs": [], 9 | "source": [ 10 | "## Logging in a File Operation\n", 11 | "import logging\n", 12 | "\n", 13 | "# Configure logging\n", 14 | "logging.basicConfig(filename='file_operations.log', level=logging.INFO)\n", 15 | "\n", 16 | "def read_file(file_path):\n", 17 | " try:\n", 18 | " with open(file_path, 'r') as file:\n", 19 | " content = file.read()\n", 20 | " logging.info(f\"Successfully read the file: {file_path}\")\n", 21 | " return content\n", 22 | " except FileNotFoundError as e:\n", 23 | " logging.error(f\"File not found: {file_path}\")\n", 24 | " logging.exception(\"Exception occurred\")\n", 25 | " return None\n", 26 | " except Exception as e:\n", 27 | " logging.error(f\"An error occurred while reading the file: {file_path}\")\n", 28 | " logging.exception(\"Exception details:\")\n", 29 | " return None\n", 30 | "\n", 31 | "# Test the function\n", 32 | "read_file('text.txt')\n" 33 | ] 34 | }, 35 | { 36 | "cell_type": "code", 37 | "execution_count": 2, 38 | "id": "4aba6bf8-24e5-4dbe-9961-78a4ef340982", 39 | "metadata": {}, 40 | "outputs": [], 41 | "source": [ 42 | "## Logging in a Network Operation\n", 43 | "import logging\n", 44 | "import requests\n", 45 | "\n", 46 | "# Configure logging\n", 47 | "logging.basicConfig(filename='network_operations.log', level=logging.WARNING,format ='%(asctime)s %(levelname)s %(message)s')\n", 48 | "\n", 49 | "def fetch_data(url):\n", 50 | " try:\n", 51 | " response = requests.get(url)\n", 52 | " response.raise_for_status()\n", 53 | " logging.info(f\"Successfully fetched data from {url}\")\n", 54 | " return response.text\n", 55 | " except requests.exceptions.HTTPError as e:\n", 56 | " logging.warning(f\"HTTP error occurred: {e}\")\n", 57 | " logging.exception(\"Exception occurred\")\n", 58 | " return None\n", 59 | " except requests.exceptions.RequestException as e:\n", 60 | " logging.error(f\"Error occurred during request to {url}\")\n", 61 | " logging.exception(\"Exception details:\")\n", 62 | " return None\n", 63 | "\n", 64 | "# Test the function\n", 65 | "fetch_data('http://example.com/non_existent_page')\n", 66 | "#fetch_data('https://www.google.com/')\n" 67 | ] 68 | }, 69 | { 70 | "cell_type": "code", 71 | "execution_count": 1, 72 | "id": "c31b29f3-5f8f-4541-a2ca-6ac3180f1c7d", 73 | "metadata": {}, 74 | "outputs": [ 75 | { 76 | "data": { 77 | "text/plain": [ 78 | "[1, 2, 3, 4]" 79 | ] 80 | }, 81 | "execution_count": 1, 82 | "metadata": {}, 83 | "output_type": "execute_result" 84 | } 85 | ], 86 | "source": [ 87 | "## Custom Logger with Exception Handling\n", 88 | "import logging\n", 89 | "\n", 90 | "# Configure custom logger\n", 91 | "logger = logging.getLogger('custom_logger')\n", 92 | "handler = logging.FileHandler('custom_app.log')\n", 93 | "formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')\n", 94 | "handler.setFormatter(formatter)\n", 95 | "logger.addHandler(handler)\n", 96 | "logger.setLevel(logging.DEBUG)\n", 97 | "\n", 98 | "def process_data(data):\n", 99 | " try:\n", 100 | " result = [int(i) for i in data]\n", 101 | " logger.info(\"Data processing successful\")\n", 102 | " return result\n", 103 | " except ValueError as e:\n", 104 | " logger.error(\"Error processing data\")\n", 105 | " logger.exception(\"Exception occurred\")\n", 106 | " return None\n", 107 | "\n", 108 | "# Test the function\n", 109 | "process_data(['1', '2', '3', '4'])\n" 110 | ] 111 | }, 112 | { 113 | "cell_type": "code", 114 | "execution_count": 3, 115 | "id": "d9001a78-0117-4e77-b3d7-bae09ac853fd", 116 | "metadata": {}, 117 | "outputs": [ 118 | { 119 | "name": "stderr", 120 | "output_type": "stream", 121 | "text": [ 122 | "DEBUG - Square root of 4 is 2.0\n" 123 | ] 124 | }, 125 | { 126 | "data": { 127 | "text/plain": [ 128 | "2.0" 129 | ] 130 | }, 131 | "execution_count": 3, 132 | "metadata": {}, 133 | "output_type": "execute_result" 134 | } 135 | ], 136 | "source": [ 137 | "## Logging with Multiple Handlers\n", 138 | "import logging\n", 139 | "\n", 140 | "# Configure logging\n", 141 | "logger = logging.getLogger('multi_handler_logger')\n", 142 | "logger.setLevel(logging.DEBUG)\n", 143 | "\n", 144 | "# File handler\n", 145 | "file_handler = logging.FileHandler('multi_handler.log')\n", 146 | "file_handler.setLevel(logging.ERROR)\n", 147 | "file_formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')\n", 148 | "file_handler.setFormatter(file_formatter)\n", 149 | "\n", 150 | "# Console handler\n", 151 | "console_handler = logging.StreamHandler()\n", 152 | "console_handler.setLevel(logging.DEBUG)\n", 153 | "console_formatter = logging.Formatter('%(levelname)s - %(message)s')\n", 154 | "console_handler.setFormatter(console_formatter)\n", 155 | "\n", 156 | "# Add handlers to logger\n", 157 | "logger.addHandler(file_handler)\n", 158 | "logger.addHandler(console_handler)\n", 159 | "\n", 160 | "def calculate_square_root(x):\n", 161 | " try:\n", 162 | " if x < 0:\n", 163 | " raise ValueError(\"Cannot calculate square root of a negative number\")\n", 164 | " result = x ** 0.5\n", 165 | " logger.debug(f\"Square root of {x} is {result}\")\n", 166 | " return result\n", 167 | " except ValueError as e:\n", 168 | " logger.error(\"Error occurred in calculate_square_root\")\n", 169 | " logger.exception(\"Exception details:\")\n", 170 | " return None\n", 171 | "\n", 172 | "# Test the function\n", 173 | "calculate_square_root(4)\n" 174 | ] 175 | }, 176 | { 177 | "cell_type": "code", 178 | "execution_count": null, 179 | "id": "236aa98f-9bfe-4235-9263-f22d25dc67dc", 180 | "metadata": {}, 181 | "outputs": [], 182 | "source": [] 183 | } 184 | ], 185 | "metadata": { 186 | "kernelspec": { 187 | "display_name": "Python 3 (ipykernel)", 188 | "language": "python", 189 | "name": "python3" 190 | }, 191 | "language_info": { 192 | "codemirror_mode": { 193 | "name": "ipython", 194 | "version": 3 195 | }, 196 | "file_extension": ".py", 197 | "mimetype": "text/x-python", 198 | "name": "python", 199 | "nbconvert_exporter": "python", 200 | "pygments_lexer": "ipython3", 201 | "version": "3.10.9" 202 | } 203 | }, 204 | "nbformat": 4, 205 | "nbformat_minor": 5 206 | } 207 | -------------------------------------------------------------------------------- /Logging.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "raw", 5 | "metadata": {}, 6 | "source": [ 7 | "Logging is a means of tracking events that happen when some software runs. Logging is important for software developing, debugging, and running.\n", 8 | "\n", 9 | "If you don’t have any logging record and your program crashes, there are very few chances that you detect the cause of the problem. \n", 10 | "\n", 11 | "And if you detect the cause, it will consume a lot of time. With logging, you can leave a trail of breadcrumbs so that if something goes wrong, we can determine the cause of the problem. " 12 | ] 13 | }, 14 | { 15 | "cell_type": "raw", 16 | "metadata": {}, 17 | "source": [ 18 | "Debug : These are used to give Detailed information, typically of interest only when diagnosing problems.\n", 19 | "\n", 20 | "Info : These are used to confirm that things are working as expected\n", 21 | "\n", 22 | "Warning : These are used an indication that something unexpected happened, or is indicative of some problem in the near future\n", 23 | "\n", 24 | "Error : This tells that due to a more serious problem, the software has not been able to perform some function\n", 25 | "\n", 26 | "Critical : This tells serious error, indicating that the program itself may be unable to continue running" 27 | ] 28 | }, 29 | { 30 | "cell_type": "code", 31 | "execution_count": null, 32 | "metadata": {}, 33 | "outputs": [], 34 | "source": [ 35 | "import logging" 36 | ] 37 | }, 38 | { 39 | "cell_type": "code", 40 | "execution_count": 3, 41 | "metadata": {}, 42 | "outputs": [], 43 | "source": [ 44 | "logging.basicConfig(filename = \"test.log\" ,level = logging.INFO)" 45 | ] 46 | }, 47 | { 48 | "cell_type": "code", 49 | "execution_count": 4, 50 | "metadata": {}, 51 | "outputs": [], 52 | "source": [ 53 | "logging.info(\"this is my info log \")\n", 54 | "logging.warning(\"this is my warning log \")\n", 55 | "logging.error(\"this is my error log\")" 56 | ] 57 | }, 58 | { 59 | "cell_type": "code", 60 | "execution_count": 5, 61 | "metadata": {}, 62 | "outputs": [], 63 | "source": [ 64 | "logging.shutdown()" 65 | ] 66 | }, 67 | { 68 | "cell_type": "code", 69 | "execution_count": 6, 70 | "metadata": {}, 71 | "outputs": [ 72 | { 73 | "data": { 74 | "text/plain": [ 75 | "'C:\\\\Users\\\\vaibh\\\\Jalpa Desai'" 76 | ] 77 | }, 78 | "execution_count": 6, 79 | "metadata": {}, 80 | "output_type": "execute_result" 81 | } 82 | ], 83 | "source": [ 84 | "pwd()" 85 | ] 86 | }, 87 | { 88 | "cell_type": "code", 89 | "execution_count": null, 90 | "metadata": {}, 91 | "outputs": [], 92 | "source": [ 93 | "DEBUG \n", 94 | "INFO\n", 95 | "WARNING\n", 96 | "ERROR\n", 97 | "CRITICAL\n" 98 | ] 99 | }, 100 | { 101 | "cell_type": "code", 102 | "execution_count": 1, 103 | "metadata": {}, 104 | "outputs": [], 105 | "source": [ 106 | "import logging\n", 107 | "logging.basicConfig(filename = \"test2.log\" , level = logging.DEBUG , format ='%(asctime)s %(levelname)s %(message)s' )" 108 | ] 109 | }, 110 | { 111 | "cell_type": "code", 112 | "execution_count": 17, 113 | "metadata": {}, 114 | "outputs": [], 115 | "source": [ 116 | "logging.shutdown()" 117 | ] 118 | }, 119 | { 120 | "cell_type": "code", 121 | "execution_count": 2, 122 | "metadata": {}, 123 | "outputs": [ 124 | { 125 | "name": "stderr", 126 | "output_type": "stream", 127 | "text": [ 128 | "WARNING:root:this is my warniing log \n", 129 | "ERROR:root:this is my an error log\n" 130 | ] 131 | } 132 | ], 133 | "source": [ 134 | "logging.info(\"this is my info log\")\n", 135 | "logging.warning(\"this is my warniing log \")\n", 136 | "logging.debug(\"this is my debug log \")\n", 137 | "logging.error(\"this is my an error log\")" 138 | ] 139 | }, 140 | { 141 | "cell_type": "code", 142 | "execution_count": null, 143 | "metadata": {}, 144 | "outputs": [], 145 | "source": [ 146 | "ERROR\n", 147 | "WARNING\n", 148 | "INFO\n", 149 | "DEBUG\n" 150 | ] 151 | }, 152 | { 153 | "cell_type": "code", 154 | "execution_count": 1, 155 | "metadata": {}, 156 | "outputs": [], 157 | "source": [ 158 | "import logging\n", 159 | "logging.basicConfig(filename = \"test3.log\" , level = logging.DEBUG , format ='%(asctime)s %(levelname)s %(message)s' )" 160 | ] 161 | }, 162 | { 163 | "cell_type": "code", 164 | "execution_count": 2, 165 | "metadata": {}, 166 | "outputs": [], 167 | "source": [ 168 | "def divbyzero(a,b) :\n", 169 | " logging.info(\"this is a start of my code and i am try to enter %s and %s\" , a,b)\n", 170 | " try:\n", 171 | " div = a/b\n", 172 | " logging.info(\"executed succ\")\n", 173 | " except Exception as e :\n", 174 | " logging.error(\"Errog has happend \")\n", 175 | " logging.exception(\"Exception occured \" + str(e))" 176 | ] 177 | }, 178 | { 179 | "cell_type": "code", 180 | "execution_count": 3, 181 | "metadata": {}, 182 | "outputs": [], 183 | "source": [ 184 | "divbyzero(4,5)" 185 | ] 186 | }, 187 | { 188 | "cell_type": "code", 189 | "execution_count": 4, 190 | "metadata": {}, 191 | "outputs": [], 192 | "source": [ 193 | "divbyzero(4,0)" 194 | ] 195 | }, 196 | { 197 | "cell_type": "code", 198 | "execution_count": 1, 199 | "metadata": {}, 200 | "outputs": [ 201 | { 202 | "name": "stderr", 203 | "output_type": "stream", 204 | "text": [ 205 | "ERROR:root:Exception occurred\n", 206 | "Traceback (most recent call last):\n", 207 | " File \"C:\\Users\\vaibh\\AppData\\Local\\Temp\\ipykernel_6648\\2363292298.py\", line 6, in \n", 208 | " c = a / b\n", 209 | "ZeroDivisionError: division by zero\n" 210 | ] 211 | } 212 | ], 213 | "source": [ 214 | "import logging\n", 215 | "\n", 216 | "a = 5\n", 217 | "b = 0\n", 218 | "try:\n", 219 | " c = a / b\n", 220 | "except Exception as e:\n", 221 | " logging.exception(\"Exception occurred\")" 222 | ] 223 | }, 224 | { 225 | "cell_type": "code", 226 | "execution_count": 4, 227 | "metadata": {}, 228 | "outputs": [ 229 | { 230 | "name": "stderr", 231 | "output_type": "stream", 232 | "text": [ 233 | "DEBUG:root:Harmless debug Message\n", 234 | "INFO:root:Information only\n", 235 | "WARNING:root:Its a Warning Mesage\n", 236 | "ERROR:root:Did you try to divide by zero\n", 237 | "CRITICAL:root:Internet speed is down\n" 238 | ] 239 | } 240 | ], 241 | "source": [ 242 | "import logging\n", 243 | " \n", 244 | "# Create and configure logger\n", 245 | "logging.basicConfig(filename=\"test12.log\",\n", 246 | " format='%(asctime)s %(message)s',\n", 247 | " filemode='w')\n", 248 | " \n", 249 | "# Creating an object\n", 250 | "logger = logging.getLogger()\n", 251 | " \n", 252 | "# Setting the threshold of logger to DEBUG\n", 253 | "logger.setLevel(logging.DEBUG)\n", 254 | " \n", 255 | "# Test messages\n", 256 | "logger.debug(\"Harmless debug Message\")\n", 257 | "logger.info(\"Information only\")\n", 258 | "logger.warning(\"Its a Warning Mesage\")\n", 259 | "logger.error(\"Did you try to divide by zero\")\n", 260 | "logger.critical(\"Internet speed is down\")" 261 | ] 262 | } 263 | ], 264 | "metadata": { 265 | "kernelspec": { 266 | "display_name": "Python 3 (ipykernel)", 267 | "language": "python", 268 | "name": "python3" 269 | }, 270 | "language_info": { 271 | "codemirror_mode": { 272 | "name": "ipython", 273 | "version": 3 274 | }, 275 | "file_extension": ".py", 276 | "mimetype": "text/x-python", 277 | "name": "python", 278 | "nbconvert_exporter": "python", 279 | "pygments_lexer": "ipython3", 280 | "version": "3.9.12" 281 | } 282 | }, 283 | "nbformat": 4, 284 | "nbformat_minor": 4 285 | } 286 | -------------------------------------------------------------------------------- /Logging_Updated.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "raw", 5 | "metadata": {}, 6 | "source": [ 7 | "Logging is a means of tracking events that happen when some software runs. Logging is important for software developing, debugging, and running.\n", 8 | "\n", 9 | "If you don’t have any logging record and your program crashes, there are very few chances that you detect the cause of the problem. \n", 10 | "\n", 11 | "And if you detect the cause, it will consume a lot of time. With logging, you can leave a trail of breadcrumbs so that if something goes wrong, we can determine the cause of the problem. " 12 | ] 13 | }, 14 | { 15 | "cell_type": "raw", 16 | "metadata": {}, 17 | "source": [ 18 | "Debug : These are used to give Detailed information, typically of interest only when diagnosing problems.\n", 19 | "\n", 20 | "Info : These are used to confirm that things are working as expected\n", 21 | "\n", 22 | "Warning : These are used an indication that something unexpected happened, or is indicative of some problem in the near future\n", 23 | "\n", 24 | "Error : This tells that due to a more serious problem, the software has not been able to perform some function\n", 25 | "\n", 26 | "Critical : This tells serious error, indicating that the program itself may be unable to continue running" 27 | ] 28 | }, 29 | { 30 | "cell_type": "code", 31 | "execution_count": 1, 32 | "metadata": {}, 33 | "outputs": [], 34 | "source": [ 35 | "import logging" 36 | ] 37 | }, 38 | { 39 | "cell_type": "code", 40 | "execution_count": 2, 41 | "metadata": {}, 42 | "outputs": [], 43 | "source": [ 44 | "logging.basicConfig(filename = \"test.log\" ,level = logging.INFO)" 45 | ] 46 | }, 47 | { 48 | "cell_type": "code", 49 | "execution_count": 3, 50 | "metadata": {}, 51 | "outputs": [], 52 | "source": [ 53 | "logging.info(\"this is my info log \")\n", 54 | "logging.warning(\"this is my warning log \")\n", 55 | "logging.error(\"this is my error log\")" 56 | ] 57 | }, 58 | { 59 | "cell_type": "code", 60 | "execution_count": 4, 61 | "metadata": {}, 62 | "outputs": [], 63 | "source": [ 64 | "logging.shutdown()" 65 | ] 66 | }, 67 | { 68 | "cell_type": "code", 69 | "execution_count": 6, 70 | "metadata": {}, 71 | "outputs": [ 72 | { 73 | "data": { 74 | "text/plain": [ 75 | "'C:\\\\Users\\\\vaibh\\\\Jalpa Desai'" 76 | ] 77 | }, 78 | "execution_count": 6, 79 | "metadata": {}, 80 | "output_type": "execute_result" 81 | } 82 | ], 83 | "source": [ 84 | "pwd()" 85 | ] 86 | }, 87 | { 88 | "cell_type": "code", 89 | "execution_count": null, 90 | "metadata": {}, 91 | "outputs": [], 92 | "source": [ 93 | "DEBUG \n", 94 | "INFO\n", 95 | "WARNING\n", 96 | "ERROR\n", 97 | "CRITICAL\n" 98 | ] 99 | }, 100 | { 101 | "cell_type": "code", 102 | "execution_count": 2, 103 | "metadata": {}, 104 | "outputs": [], 105 | "source": [ 106 | "import logging\n", 107 | "logging.basicConfig(filename = \"test30.log\" , level = logging.DEBUG , format ='%(asctime)s %(levelname)s %(message)s' )" 108 | ] 109 | }, 110 | { 111 | "cell_type": "code", 112 | "execution_count": 2, 113 | "metadata": {}, 114 | "outputs": [], 115 | "source": [ 116 | "logging.info(\"this is my info log\")\n", 117 | "logging.warning(\"this is my warniing log \")\n", 118 | "logging.debug(\"this is my debug log \")\n", 119 | "logging.error(\"this is my an error log\")" 120 | ] 121 | }, 122 | { 123 | "cell_type": "code", 124 | "execution_count": null, 125 | "metadata": {}, 126 | "outputs": [], 127 | "source": [ 128 | "logging.shutdown()" 129 | ] 130 | }, 131 | { 132 | "cell_type": "code", 133 | "execution_count": null, 134 | "metadata": {}, 135 | "outputs": [], 136 | "source": [ 137 | "ERROR\n", 138 | "WARNING\n", 139 | "INFO\n", 140 | "DEBUG\n" 141 | ] 142 | }, 143 | { 144 | "cell_type": "code", 145 | "execution_count": 8, 146 | "metadata": {}, 147 | "outputs": [], 148 | "source": [ 149 | "import logging\n", 150 | "logging.basicConfig(filename = \"test45.log\" , level = logging.DEBUG , format ='%(asctime)s %(levelname)s %(message)s' )" 151 | ] 152 | }, 153 | { 154 | "cell_type": "code", 155 | "execution_count": 1, 156 | "metadata": {}, 157 | "outputs": [], 158 | "source": [ 159 | "import logging\n", 160 | "def divbyzero(a,b) :\n", 161 | " logging.info(\"this is a start of my code and i am try to enter %s and %s\" , a,b)\n", 162 | " try:\n", 163 | " div = a/b\n", 164 | " logging.info(\"executed succ\")\n", 165 | " except Exception as e :\n", 166 | " logging.error(\"Error has happend \")\n", 167 | " logging.exception(\"Exception occured \" + str(e) )\n", 168 | " " 169 | ] 170 | }, 171 | { 172 | "cell_type": "code", 173 | "execution_count": 2, 174 | "metadata": {}, 175 | "outputs": [ 176 | { 177 | "name": "stderr", 178 | "output_type": "stream", 179 | "text": [ 180 | "ERROR:root:Error has happend \n", 181 | "ERROR:root:Exception occured division by zero\n", 182 | "Traceback (most recent call last):\n", 183 | " File \"C:\\Users\\vaibh\\AppData\\Local\\Temp\\ipykernel_3544\\1837150678.py\", line 5, in divbyzero\n", 184 | " div = a/b\n", 185 | "ZeroDivisionError: division by zero\n" 186 | ] 187 | } 188 | ], 189 | "source": [ 190 | "divbyzero(4,0)" 191 | ] 192 | }, 193 | { 194 | "cell_type": "code", 195 | "execution_count": 7, 196 | "metadata": {}, 197 | "outputs": [], 198 | "source": [ 199 | "logging.shutdown()" 200 | ] 201 | }, 202 | { 203 | "cell_type": "code", 204 | "execution_count": 4, 205 | "metadata": {}, 206 | "outputs": [], 207 | "source": [ 208 | "divbyzero(4,0)" 209 | ] 210 | }, 211 | { 212 | "cell_type": "code", 213 | "execution_count": 8, 214 | "metadata": {}, 215 | "outputs": [], 216 | "source": [ 217 | "import logging as lg\n", 218 | "\n", 219 | "a = 5\n", 220 | "b = 0\n", 221 | "try:\n", 222 | " c = a / b\n", 223 | "except Exception as e:\n", 224 | " lg.exception(\"Exception occurred\")" 225 | ] 226 | }, 227 | { 228 | "cell_type": "code", 229 | "execution_count": 2, 230 | "metadata": {}, 231 | "outputs": [], 232 | "source": [ 233 | "import logging\n", 234 | " \n", 235 | "# Create and configure logger\n", 236 | "logging.basicConfig(filename=\"test12345.log\",\n", 237 | " format='%(asctime)s %(message)s',\n", 238 | " filemode='w')\n", 239 | " \n", 240 | "# Creating an object\n", 241 | "logger = logging.getLogger()\n", 242 | " \n", 243 | "# Setting the threshold of logger to DEBUG\n", 244 | "logger.setLevel(logging.DEBUG)\n", 245 | " \n", 246 | "# Test messages\n", 247 | "logger.debug(\"Harmless debug Message\")\n", 248 | "logger.info(\"Information only\")\n", 249 | "logger.warning(\"Its a Warning Mesage\")\n", 250 | "logger.error(\"Did you try to divide by zero\")\n", 251 | "logger.critical(\"Internet speed is down\")" 252 | ] 253 | }, 254 | { 255 | "cell_type": "code", 256 | "execution_count": null, 257 | "metadata": {}, 258 | "outputs": [], 259 | "source": [] 260 | }, 261 | { 262 | "cell_type": "code", 263 | "execution_count": null, 264 | "metadata": {}, 265 | "outputs": [], 266 | "source": [] 267 | } 268 | ], 269 | "metadata": { 270 | "kernelspec": { 271 | "display_name": "Python 3 (ipykernel)", 272 | "language": "python", 273 | "name": "python3" 274 | }, 275 | "language_info": { 276 | "codemirror_mode": { 277 | "name": "ipython", 278 | "version": 3 279 | }, 280 | "file_extension": ".py", 281 | "mimetype": "text/x-python", 282 | "name": "python", 283 | "nbconvert_exporter": "python", 284 | "pygments_lexer": "ipython3", 285 | "version": "3.9.12" 286 | } 287 | }, 288 | "nbformat": 4, 289 | "nbformat_minor": 4 290 | } 291 | -------------------------------------------------------------------------------- /Multi threading.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "id": "68132f24", 7 | "metadata": {}, 8 | "outputs": [], 9 | "source": [ 10 | "Application of multi threding\n", 11 | "## Multimedia Graphic\n", 12 | "## Animations\n", 13 | "## Video Games\n", 14 | "## Web Servers\n", 15 | "## Application Servers" 16 | ] 17 | }, 18 | { 19 | "cell_type": "raw", 20 | "id": "c16b39be", 21 | "metadata": {}, 22 | "source": [ 23 | "Banking application [Multi Thread]" 24 | ] 25 | }, 26 | { 27 | "cell_type": "raw", 28 | "id": "0cc4e600", 29 | "metadata": {}, 30 | "source": [ 31 | "## Creating a Thread (Thread is a class)\n", 32 | "Thread class of threading module is used t create threads. \n", 33 | "To create our own thread we need to create an object of Thread Class\n", 34 | "** Following are the ways of creating threads:-\n", 35 | "1. Creating a thread without using a class\n", 36 | "2. Creatinh a thread by creating a child class to Thread class\n", 37 | "3. Creating a thread without creating child class to Thread class" 38 | ] 39 | }, 40 | { 41 | "cell_type": "code", 42 | "execution_count": 3, 43 | "id": "7d8d51d8", 44 | "metadata": {}, 45 | "outputs": [ 46 | { 47 | "name": "stdout", 48 | "output_type": "stream", 49 | "text": [ 50 | "Thread Running : 10 20\n" 51 | ] 52 | } 53 | ], 54 | "source": [ 55 | "## Creating a thread without using a class \n", 56 | "from threading import Thread\n", 57 | "#thread_obj = Thread(target=function_name,args=(arg1,arg2,...))\n", 58 | "\n", 59 | "def disp(a,b):\n", 60 | " print(\"Thread Running : \", a,b)\n", 61 | "t = Thread (target=disp, args=(10,20))\n", 62 | "t.start() ## starting threading" 63 | ] 64 | }, 65 | { 66 | "cell_type": "code", 67 | "execution_count": 4, 68 | "id": "a061bf0b", 69 | "metadata": {}, 70 | "outputs": [ 71 | { 72 | "name": "stdout", 73 | "output_type": "stream", 74 | "text": [ 75 | "Thread Running : Thread Running : 10 20\n", 76 | "10 20\n", 77 | "Thread Running : 10 20\n", 78 | "Thread Running : 10 20\n", 79 | "Thread Running : 10 20\n" 80 | ] 81 | } 82 | ], 83 | "source": [ 84 | "from threading import Thread\n", 85 | "def disp(a,b):\n", 86 | " print(\"Thread Running : \", a,b)\n", 87 | "for i in range(5):\n", 88 | " t = Thread(target=disp, args=(10,20))\n", 89 | " t.start()" 90 | ] 91 | }, 92 | { 93 | "cell_type": "code", 94 | "execution_count": 5, 95 | "id": "25cc4d0f", 96 | "metadata": {}, 97 | "outputs": [ 98 | { 99 | "name": "stdout", 100 | "output_type": "stream", 101 | "text": [ 102 | "Child ThreadMain Thread\n", 103 | "Main Thread\n", 104 | "Main Thread\n", 105 | "Main Thread\n", 106 | "Main Thread\n", 107 | "\n" 108 | ] 109 | } 110 | ], 111 | "source": [ 112 | "from threading import Thread\n", 113 | "def disp():\n", 114 | " print(\"Child Thread\")\n", 115 | "t = Thread(target=disp)\n", 116 | "t.start()\n", 117 | "\n", 118 | "for i in range(5):\n", 119 | " print(\"Main Thread\")" 120 | ] 121 | }, 122 | { 123 | "cell_type": "code", 124 | "execution_count": 8, 125 | "id": "86130727", 126 | "metadata": {}, 127 | "outputs": [ 128 | { 129 | "name": "stdout", 130 | "output_type": "stream", 131 | "text": [ 132 | "Child Thread\n", 133 | "Child Thread\n", 134 | "Child Thread\n", 135 | "Child Thread\n", 136 | "Child Thread\n", 137 | "Main Thread\n", 138 | "Main Thread\n", 139 | "Main Thread\n", 140 | "Main Thread\n", 141 | "Main Thread\n" 142 | ] 143 | } 144 | ], 145 | "source": [ 146 | "from threading import Thread\n", 147 | "def disp():\n", 148 | " for i in range(5):\n", 149 | " print(\"Child Thread\")\n", 150 | "\n", 151 | "t = Thread(target=disp)\n", 152 | "t.start()\n", 153 | "\n", 154 | "for i in range(5):\n", 155 | " print(\"Main Thread\")" 156 | ] 157 | }, 158 | { 159 | "cell_type": "code", 160 | "execution_count": 9, 161 | "id": "51b295ee", 162 | "metadata": {}, 163 | "outputs": [ 164 | { 165 | "name": "stdout", 166 | "output_type": "stream", 167 | "text": [ 168 | "Publish Video CPublish Video M\n", 169 | "Publish Video M\n", 170 | "Publish Video M\n", 171 | "Publish Video M\n", 172 | "Publish Video M\n", 173 | "\n", 174 | "Publish Video C\n", 175 | "Publish Video C\n", 176 | "Publish Video C\n", 177 | "Publish Video C\n" 178 | ] 179 | } 180 | ], 181 | "source": [ 182 | "from threading import Thread\n", 183 | "def disp():\n", 184 | " for i in range(5):\n", 185 | " print(\"Publish Video C\")\n", 186 | "\n", 187 | "t = Thread(target=disp)\n", 188 | "\n", 189 | "t.start()\n", 190 | "\n", 191 | "for i in range(5):\n", 192 | " print(\"Publish Video M\")" 193 | ] 194 | }, 195 | { 196 | "cell_type": "markdown", 197 | "id": "6a2a9fa9", 198 | "metadata": {}, 199 | "source": [ 200 | "# Multitasking using Multiple Thread" 201 | ] 202 | }, 203 | { 204 | "cell_type": "code", 205 | "execution_count": null, 206 | "id": "ee76abb2", 207 | "metadata": {}, 208 | "outputs": [], 209 | "source": [ 210 | "## When multiple tasks are executed at a time, then it is called multi-tasking.\n", 211 | "for this purpose we need more than one thread and when \n", 212 | "we use more that one thread, it is called multi threading." 213 | ] 214 | }, 215 | { 216 | "cell_type": "code", 217 | "execution_count": 10, 218 | "id": "20c2dca9", 219 | "metadata": {}, 220 | "outputs": [ 221 | { 222 | "name": "stdout", 223 | "output_type": "stream", 224 | "text": [ 225 | "Take order from table 1\n", 226 | "Take order from table 2\n", 227 | "Take order from table 3\n", 228 | "Take order from table 4\n", 229 | "Take order from table 5\n", 230 | "Serve order to table 1\n", 231 | "Serve order to table 2\n", 232 | "Serve order to table 3\n", 233 | "Serve order to table 4\n", 234 | "Serve order to table 5\n" 235 | ] 236 | } 237 | ], 238 | "source": [ 239 | "## Two task using Two Thread\n", 240 | "from threading import Thread\n", 241 | "\n", 242 | "class Hotel:\n", 243 | " def __init__(self,t):\n", 244 | " self.t = t\n", 245 | " def food(self):\n", 246 | " for i in range(1,6):\n", 247 | " print(self.t,i)\n", 248 | " \n", 249 | "h1 = Hotel('Take order from table')\n", 250 | "h2 = Hotel('Serve order to table')\n", 251 | "t1 = Thread(target=h1.food)\n", 252 | "t2 = Thread(target=h2.food)\n", 253 | "t1.start()\n", 254 | "t2.start()" 255 | ] 256 | }, 257 | { 258 | "cell_type": "code", 259 | "execution_count": 15, 260 | "id": "88d51afa", 261 | "metadata": {}, 262 | "outputs": [ 263 | { 264 | "name": "stdout", 265 | "output_type": "stream", 266 | "text": [ 267 | "Available Seats: Available Seats: 1\n", 268 | "1 seat is alloted for Jalpa\n", 269 | " 1\n", 270 | "Sorry! All seats has alloted\n" 271 | ] 272 | } 273 | ], 274 | "source": [ 275 | "from threading import Thread, current_thread\n", 276 | "class Flight:\n", 277 | " def __init__(self, available_seat):\n", 278 | " self.available_seat = available_seat\n", 279 | " def reserve(self, need_seat):\n", 280 | " print('Available Seats: ', self.available_seat)\n", 281 | " if(self.available_seat >= need_seat):\n", 282 | " name = current_thread().name\n", 283 | " print(f'{need_seat} seat is alloted for {name}')\n", 284 | " self.available_seat -= need_seat\n", 285 | " else:\n", 286 | " print('Sorry! All seats has alloted')\n", 287 | " \n", 288 | "f = Flight(1)\n", 289 | "t1 = Thread(target=f.reserve, args=(1,), name='Jalpa') \n", 290 | "t2 = Thread(target=f.reserve, args=(1,), name='xyz')\n", 291 | "t1.start()\n", 292 | "t2.start()" 293 | ] 294 | }, 295 | { 296 | "cell_type": "raw", 297 | "id": "5a7c7908", 298 | "metadata": {}, 299 | "source": [ 300 | "#Race condition\n", 301 | "It is situation that occurs when threads are acting in an unexpected sequence, thus leading to unreliable output.\n", 302 | "Solution : This can be eliminated using thread synchronization" 303 | ] 304 | }, 305 | { 306 | "cell_type": "markdown", 307 | "id": "bfb7c3cd", 308 | "metadata": {}, 309 | "source": [ 310 | "# Thread Synchronization Locks in Python\n", 311 | "acquire ( ) Method\n", 312 | "release ( ) Method" 313 | ] 314 | }, 315 | { 316 | "cell_type": "code", 317 | "execution_count": 31, 318 | "id": "703e4902", 319 | "metadata": {}, 320 | "outputs": [ 321 | { 322 | "name": "stdout", 323 | "output_type": "stream", 324 | "text": [ 325 | "Available Seats: 5\n", 326 | "1 seat is alloted for Jalpa\n", 327 | "Available Seats: 4\n", 328 | "1 seat is alloted for xyz\n", 329 | "Available Seats: 3\n", 330 | "1 seat is alloted for abc\n" 331 | ] 332 | } 333 | ], 334 | "source": [ 335 | "## acquire(), release()\n", 336 | "from threading import *\n", 337 | "class Flight:\n", 338 | " def __init__(self, available_seat):\n", 339 | " self.available_seat = available_seat\n", 340 | " self.l = Lock()\n", 341 | " def reserve(self, need_seat):\n", 342 | " self.l.acquire() ## acquire(blocking=True, timeout=-1)\n", 343 | " print('Available Seats: ', self.available_seat)\n", 344 | " if(self.available_seat >= need_seat):\n", 345 | " name = current_thread().name\n", 346 | " print(f'{need_seat} seat is alloted for {name}')\n", 347 | " self.available_seat -= need_seat\n", 348 | " else:\n", 349 | " print('Sorry! All seats has alloted')\n", 350 | " self.l.release()\n", 351 | " \n", 352 | "f = Flight(5)\n", 353 | "t1 = Thread(target=f.reserve, args=(1,), name='Jalpa') \n", 354 | "t2 = Thread(target=f.reserve, args=(1,), name='xyz')\n", 355 | "t3 = Thread(target=f.reserve, args=(1,), name = 'abc')\n", 356 | "t1.start()\n", 357 | "t2.start()\n", 358 | "t3.start()" 359 | ] 360 | }, 361 | { 362 | "cell_type": "code", 363 | "execution_count": 16, 364 | "id": "6872f630", 365 | "metadata": {}, 366 | "outputs": [ 367 | { 368 | "name": "stdout", 369 | "output_type": "stream", 370 | "text": [ 371 | "Available Seats: 5\n", 372 | "1 seat is alloted for Jalpa\n", 373 | "Available Seats: 4\n", 374 | "1 seat is alloted for xyz\n", 375 | "Available Seats: 3\n", 376 | "1 seat is alloted for abc\n", 377 | "Main Thread\n" 378 | ] 379 | } 380 | ], 381 | "source": [ 382 | "## join() \n", 383 | "##join is a synchronization method that blocks the calling thread (that is, the thread that calls the method) \n", 384 | "##until the thread whose Join method is called has completed. \n", 385 | "from threading import *\n", 386 | "class Flight:\n", 387 | " def __init__(self, available_seat):\n", 388 | " self.available_seat = available_seat\n", 389 | " self.l = Lock()\n", 390 | " def reserve(self, need_seat):\n", 391 | " self.l.acquire() ## acquire(blocking=True, timeout=-1)\n", 392 | " print('Available Seats: ', self.available_seat)\n", 393 | " if(self.available_seat >= need_seat):\n", 394 | " name = current_thread().name\n", 395 | " print(f'{need_seat} seat is alloted for {name}')\n", 396 | " self.available_seat -= need_seat\n", 397 | " else:\n", 398 | " print('Sorry! All seats has alloted')\n", 399 | " self.l.release()\n", 400 | " \n", 401 | "f = Flight(5)\n", 402 | "t1 = Thread(target=f.reserve, args=(1,), name='Jalpa') \n", 403 | "t2 = Thread(target=f.reserve, args=(1,), name='xyz')\n", 404 | "t3 = Thread(target=f.reserve, args=(1,), name = 'abc')\n", 405 | "t1.start()\n", 406 | "t2.start()\n", 407 | "t3.start()\n", 408 | "t1.join()\n", 409 | "t2.join()\n", 410 | "t3.join()\n", 411 | "print(\"Main Thread\")" 412 | ] 413 | } 414 | ], 415 | "metadata": { 416 | "kernelspec": { 417 | "display_name": "Python 3 (ipykernel)", 418 | "language": "python", 419 | "name": "python3" 420 | }, 421 | "language_info": { 422 | "codemirror_mode": { 423 | "name": "ipython", 424 | "version": 3 425 | }, 426 | "file_extension": ".py", 427 | "mimetype": "text/x-python", 428 | "name": "python", 429 | "nbconvert_exporter": "python", 430 | "pygments_lexer": "ipython3", 431 | "version": "3.9.12" 432 | } 433 | }, 434 | "nbformat": 4, 435 | "nbformat_minor": 5 436 | } 437 | -------------------------------------------------------------------------------- /OOPS_Class.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 8, 6 | "id": "869c01f3", 7 | "metadata": {}, 8 | "outputs": [ 9 | { 10 | "name": "stdout", 11 | "output_type": "stream", 12 | "text": [ 13 | "Hello, my name is Nikhil\n" 14 | ] 15 | } 16 | ], 17 | "source": [ 18 | "# A Sample class with init method\n", 19 | "class Person:\n", 20 | " \n", 21 | " # init method or constructor\n", 22 | " def __init__(self, name):\n", 23 | " self.name = name\n", 24 | " \n", 25 | " # Sample Method\n", 26 | " def say_hi(self):\n", 27 | " print('Hello, my name is', self.name)\n", 28 | " \n", 29 | " \n", 30 | "p = Person('Nikhil')\n", 31 | "p.say_hi()" 32 | ] 33 | }, 34 | { 35 | "cell_type": "code", 36 | "execution_count": 9, 37 | "id": "ebaa6a16", 38 | "metadata": {}, 39 | "outputs": [ 40 | { 41 | "name": "stdout", 42 | "output_type": "stream", 43 | "text": [ 44 | "my name is jalpa\n" 45 | ] 46 | } 47 | ], 48 | "source": [ 49 | "print(\"my name is jalpa\")" 50 | ] 51 | }, 52 | { 53 | "cell_type": "code", 54 | "execution_count": 19, 55 | "id": "5322d16d", 56 | "metadata": {}, 57 | "outputs": [], 58 | "source": [ 59 | "class person:\n", 60 | " def __init__(self, name,age, gender, nationality):\n", 61 | " self.name = name\n", 62 | " self.age = age\n", 63 | " self.gender = gender\n", 64 | " self.nationality = nationality\n", 65 | " def wish(self):\n", 66 | " print(\"my name is \", self.name)\n", 67 | " print(\"my name is \", self.age)\n", 68 | " print(\"I am \", self.gender)\n", 69 | " print(\"I am an\", self.nationality)\n", 70 | " " 71 | ] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "execution_count": 20, 76 | "id": "7d1fba3c", 77 | "metadata": {}, 78 | "outputs": [ 79 | { 80 | "name": "stdout", 81 | "output_type": "stream", 82 | "text": [ 83 | "my name is jalpa\n", 84 | "my name is 26\n", 85 | "I am Female\n", 86 | "I am an Indian\n" 87 | ] 88 | } 89 | ], 90 | "source": [ 91 | "p1 = person(\"jalpa\", 26, \"Female\", \"Indian\")\n", 92 | "p1.wish()" 93 | ] 94 | }, 95 | { 96 | "cell_type": "code", 97 | "execution_count": 1, 98 | "id": "931d877d", 99 | "metadata": {}, 100 | "outputs": [ 101 | { 102 | "name": "stdout", 103 | "output_type": "stream", 104 | "text": [ 105 | "Enter first number: 6\n", 106 | "Enter second number: 7\n", 107 | "13\n", 108 | "42\n", 109 | "0.8571428571428571\n", 110 | "-1\n" 111 | ] 112 | } 113 | ], 114 | "source": [ 115 | "# Simple calculator using class\n", 116 | "class cal():\n", 117 | " \"\"\"This is a simple calculation using class and function for two variable\"\"\"\n", 118 | " def __init__(self,a,b):\n", 119 | " self.a=a\n", 120 | " self.b=b\n", 121 | " def add(self):\n", 122 | " return self.a+self.b\n", 123 | " def mul(self):\n", 124 | " return self.a*self.b\n", 125 | " def div(self):\n", 126 | " return self.a/self.b\n", 127 | " def sub(self):\n", 128 | " return self.a-self.b\n", 129 | "a=int(input(\"Enter first number: \"))\n", 130 | "b=int(input(\"Enter second number: \"))\n", 131 | "num = cal(a,b)\n", 132 | "#num = cal(4,8)\n", 133 | "print(num.add())\n", 134 | "print(num.mul())\n", 135 | "print(num.div())\n", 136 | "print(num.sub())" 137 | ] 138 | }, 139 | { 140 | "cell_type": "code", 141 | "execution_count": 9, 142 | "id": "b4c0129b", 143 | "metadata": {}, 144 | "outputs": [ 145 | { 146 | "name": "stdout", 147 | "output_type": "stream", 148 | "text": [ 149 | "This is negative number\n" 150 | ] 151 | } 152 | ], 153 | "source": [ 154 | "# Number is positive or negative using class\n", 155 | "class num():\n", 156 | " \"\"\"This is a program to find out the number is negative of positive\"\"\"\n", 157 | "\n", 158 | " def __init__(self,a):\n", 159 | " self.a=a\n", 160 | " def num_p(self):\n", 161 | " try:\n", 162 | " if self.a >= 0:\n", 163 | " print(\"This is positive number\")\n", 164 | " else:\n", 165 | " print(\"This is negative number\")\n", 166 | " except Exception as e:\n", 167 | " print(e)\n", 168 | " num1 = int(input(\"Enter the number : \"))\n", 169 | "num1 = num(-8)\n", 170 | "num1.num_p()" 171 | ] 172 | }, 173 | { 174 | "cell_type": "code", 175 | "execution_count": 2, 176 | "id": "0bfe2d40", 177 | "metadata": {}, 178 | "outputs": [ 179 | { 180 | "name": "stdout", 181 | "output_type": "stream", 182 | "text": [ 183 | "Enter Weight of the Person (Kg) : j\n", 184 | "Error in our program\n" 185 | ] 186 | } 187 | ], 188 | "source": [ 189 | "# BMI calculation using Class \n", 190 | "class BMI():\n", 191 | " \"\"\" This is a program for to calculate body mass index for person\"\"\"\n", 192 | " def __init__(self,w,h):\n", 193 | " self.w = w\n", 194 | " self.h = h\n", 195 | " def bmi_cal(self):\n", 196 | " try:\n", 197 | " bmi = self.w / ((self.h) * (self.h))\n", 198 | " print(bmi)\n", 199 | " except Exception as e:\n", 200 | " print(e)\n", 201 | "try:\n", 202 | " w=float(input(\"Enter Weight of the Person (Kg) : \"))\n", 203 | " h=float(input(\"Enter Height of the Person (Meters): \"))\n", 204 | " obj=BMI(w,h)\n", 205 | " obj.bmi_cal()\n", 206 | "\n", 207 | "except:\n", 208 | " print(\"Error in our program\")\n", 209 | " " 210 | ] 211 | }, 212 | { 213 | "cell_type": "code", 214 | "execution_count": 3, 215 | "id": "3385fd9c", 216 | "metadata": {}, 217 | "outputs": [ 218 | { 219 | "name": "stdout", 220 | "output_type": "stream", 221 | "text": [ 222 | "After Converting Dictionary to Class : \n", 223 | "Geeks 1223 Python\n", 224 | "\n" 225 | ] 226 | } 227 | ], 228 | "source": [ 229 | "# Turns a dictionary into a class\n", 230 | "class Dict2Class(object):\n", 231 | " \n", 232 | " def __init__(self, my_dict):\n", 233 | " \n", 234 | " for key in my_dict:\n", 235 | " setattr(self, key, my_dict[key])\n", 236 | " \n", 237 | "# Driver Code\n", 238 | "if __name__ == \"__main__\":\n", 239 | " \n", 240 | " # Creating the dictionary\n", 241 | " my_dict = {\"Name\": \"Geeks\",\n", 242 | " \"Rank\": \"1223\",\n", 243 | " \"Subject\": \"Python\"}\n", 244 | " \n", 245 | " result = Dict2Class(my_dict)\n", 246 | " \n", 247 | " # printing the result\n", 248 | " print(\"After Converting Dictionary to Class : \")\n", 249 | " print(result.Name, result.Rank, result.Subject)\n", 250 | " print(type(result))" 251 | ] 252 | }, 253 | { 254 | "cell_type": "code", 255 | "execution_count": null, 256 | "id": "c5693c3f", 257 | "metadata": {}, 258 | "outputs": [], 259 | "source": [ 260 | "class cal():\n", 261 | " def __init__(self,a,b):\n", 262 | " self.a=a\n", 263 | " self.b=b\n", 264 | " def add(self):\n", 265 | " return self.a+self.b\n", 266 | " def mul(self):\n", 267 | " return self.a*self.b\n", 268 | " def div(self):\n", 269 | " return self.a/self.b\n", 270 | " def sub(self):\n", 271 | " return self.a-self.b\n", 272 | "a=int(input(\"Enter first number: \"))\n", 273 | "b=int(input(\"Enter second number: \"))\n", 274 | "obj=cal(a,b)\n", 275 | "choice=1\n", 276 | "while choice!=0:\n", 277 | " print(\"0. Exit\")\n", 278 | " print(\"1. Add\")\n", 279 | " print(\"2. Subtraction\")\n", 280 | " print(\"3. Multiplication\")\n", 281 | " print(\"4. Division\")\n", 282 | " choice=int(input(\"Enter choice: \"))\n", 283 | " if choice==1:\n", 284 | " print(\"Result: \",obj.add())\n", 285 | " elif choice==2:\n", 286 | " print(\"Result: \",obj.sub())\n", 287 | " elif choice==3:\n", 288 | " print(\"Result: \",obj.mul())\n", 289 | " elif choice==4:\n", 290 | " print(\"Result: \",round(obj.div(),2))\n", 291 | " elif choice==0:\n", 292 | " print(\"Exiting!\")\n", 293 | " else:\n", 294 | " print(\"Invalid choice!!\")\n", 295 | "print()" 296 | ] 297 | } 298 | ], 299 | "metadata": { 300 | "kernelspec": { 301 | "display_name": "Python 3 (ipykernel)", 302 | "language": "python", 303 | "name": "python3" 304 | }, 305 | "language_info": { 306 | "codemirror_mode": { 307 | "name": "ipython", 308 | "version": 3 309 | }, 310 | "file_extension": ".py", 311 | "mimetype": "text/x-python", 312 | "name": "python", 313 | "nbconvert_exporter": "python", 314 | "pygments_lexer": "ipython3", 315 | "version": "3.9.12" 316 | } 317 | }, 318 | "nbformat": 4, 319 | "nbformat_minor": 5 320 | } 321 | -------------------------------------------------------------------------------- /OOPS_Class_Modular Programmimg.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 8, 6 | "id": "869c01f3", 7 | "metadata": {}, 8 | "outputs": [ 9 | { 10 | "name": "stdout", 11 | "output_type": "stream", 12 | "text": [ 13 | "Hello, my name is Nikhil\n" 14 | ] 15 | } 16 | ], 17 | "source": [ 18 | "# A Sample class with init method\n", 19 | "class Person:\n", 20 | " \n", 21 | " # init method or constructor\n", 22 | " def __init__(self, name):\n", 23 | " self.name = name\n", 24 | " \n", 25 | " # Sample Method\n", 26 | " def say_hi(self):\n", 27 | " print('Hello, my name is', self.name)\n", 28 | " \n", 29 | " \n", 30 | "p = Person('Nikhil')\n", 31 | "p.say_hi()" 32 | ] 33 | }, 34 | { 35 | "cell_type": "code", 36 | "execution_count": 9, 37 | "id": "ebaa6a16", 38 | "metadata": {}, 39 | "outputs": [ 40 | { 41 | "name": "stdout", 42 | "output_type": "stream", 43 | "text": [ 44 | "my name is jalpa\n" 45 | ] 46 | } 47 | ], 48 | "source": [ 49 | "print(\"my name is jalpa\")" 50 | ] 51 | }, 52 | { 53 | "cell_type": "code", 54 | "execution_count": 19, 55 | "id": "5322d16d", 56 | "metadata": {}, 57 | "outputs": [], 58 | "source": [ 59 | "class person:\n", 60 | " def __init__(self, name,age, gender, nationality):\n", 61 | " self.name = name\n", 62 | " self.age = age\n", 63 | " self.gender = gender\n", 64 | " self.nationality = nationality\n", 65 | " def wish(self):\n", 66 | " print(\"my name is \", self.name)\n", 67 | " print(\"my name is \", self.age)\n", 68 | " print(\"I am \", self.gender)\n", 69 | " print(\"I am an\", self.nationality)\n", 70 | " " 71 | ] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "execution_count": 20, 76 | "id": "7d1fba3c", 77 | "metadata": {}, 78 | "outputs": [ 79 | { 80 | "name": "stdout", 81 | "output_type": "stream", 82 | "text": [ 83 | "my name is jalpa\n", 84 | "my name is 26\n", 85 | "I am Female\n", 86 | "I am an Indian\n" 87 | ] 88 | } 89 | ], 90 | "source": [ 91 | "p1 = person(\"jalpa\", 26, \"Female\", \"Indian\")\n", 92 | "p1.wish()" 93 | ] 94 | }, 95 | { 96 | "cell_type": "code", 97 | "execution_count": 1, 98 | "id": "931d877d", 99 | "metadata": {}, 100 | "outputs": [ 101 | { 102 | "name": "stdout", 103 | "output_type": "stream", 104 | "text": [ 105 | "Enter first number: 7\n", 106 | "Enter second number: 87\n", 107 | "94\n", 108 | "609\n", 109 | "0.08045977011494253\n", 110 | "-80\n" 111 | ] 112 | } 113 | ], 114 | "source": [ 115 | "# Simple calculator using class\n", 116 | "class cal():\n", 117 | " \"\"\"This is a simple calculation using class and function for two variable\"\"\"\n", 118 | " def __init__(self,a,b):\n", 119 | " self.a=a\n", 120 | " self.b=b\n", 121 | " def add(self):\n", 122 | " return self.a+self.b\n", 123 | " def mul(self):\n", 124 | " return self.a*self.b\n", 125 | " def div(self):\n", 126 | " return self.a/self.b\n", 127 | " def sub(self):\n", 128 | " return self.a-self.b\n", 129 | "a=int(input(\"Enter first number: \"))\n", 130 | "b=int(input(\"Enter second number: \"))\n", 131 | "num = cal(a,b)\n", 132 | "#num = cal(4,8)\n", 133 | "print(num.add())\n", 134 | "print(num.mul())\n", 135 | "print(num.div())\n", 136 | "print(num.sub())" 137 | ] 138 | }, 139 | { 140 | "cell_type": "code", 141 | "execution_count": 2, 142 | "id": "b4c0129b", 143 | "metadata": {}, 144 | "outputs": [ 145 | { 146 | "name": "stdout", 147 | "output_type": "stream", 148 | "text": [ 149 | "This is positive number\n" 150 | ] 151 | } 152 | ], 153 | "source": [ 154 | "# Number is positive or negative using class\n", 155 | "class num():\n", 156 | " \"\"\"This is a program to find out the number is negative of positive\"\"\"\n", 157 | "\n", 158 | " def __init__(self,a):\n", 159 | " self.a=a\n", 160 | " def num_p(self):\n", 161 | " try:\n", 162 | " if self.a >= 0:\n", 163 | " print(\"This is positive number\")\n", 164 | " else:\n", 165 | " print(\"This is negative number\")\n", 166 | " except Exception as e:\n", 167 | " print(e)\n", 168 | " num1 = int(input(\"Enter the number : \"))\n", 169 | "num1 = num(7)\n", 170 | "num1.num_p()" 171 | ] 172 | }, 173 | { 174 | "cell_type": "code", 175 | "execution_count": 4, 176 | "id": "0bfe2d40", 177 | "metadata": {}, 178 | "outputs": [ 179 | { 180 | "name": "stdout", 181 | "output_type": "stream", 182 | "text": [ 183 | "Enter Weight of the Person (Kg) : 60\n", 184 | "Enter Height of the Person (Meters): 0\n", 185 | "float division by zero\n" 186 | ] 187 | } 188 | ], 189 | "source": [ 190 | "# BMI calculation using Class \n", 191 | "class BMI():\n", 192 | " \"\"\" This is a program for to calculate body mass index for person\"\"\"\n", 193 | " def __init__(self,w,h):\n", 194 | " self.w = w\n", 195 | " self.h = h\n", 196 | " def bmi_cal(self):\n", 197 | " try:\n", 198 | " bmi = self.w / ((self.h) * (self.h))\n", 199 | " print(bmi)\n", 200 | " except Exception as e:\n", 201 | " print(e)\n", 202 | " \n", 203 | "try:\n", 204 | " w=float(input(\"Enter Weight of the Person (Kg) : \"))\n", 205 | " h=float(input(\"Enter Height of the Person (Meters): \"))\n", 206 | " obj=BMI(w,h)\n", 207 | " obj.bmi_cal()\n", 208 | "\n", 209 | "except:\n", 210 | " print(\"Error in our program\")\n", 211 | " " 212 | ] 213 | }, 214 | { 215 | "cell_type": "code", 216 | "execution_count": 3, 217 | "id": "3385fd9c", 218 | "metadata": {}, 219 | "outputs": [ 220 | { 221 | "name": "stdout", 222 | "output_type": "stream", 223 | "text": [ 224 | "After Converting Dictionary to Class : \n", 225 | "Geeks 1223 Python\n", 226 | "\n" 227 | ] 228 | } 229 | ], 230 | "source": [ 231 | "# Turns a dictionary into a class\n", 232 | "class Dict2Class(object):\n", 233 | " \n", 234 | " def __init__(self, my_dict):\n", 235 | " \n", 236 | " for key in my_dict:\n", 237 | " setattr(self, key, my_dict[key])\n", 238 | " \n", 239 | "# Driver Code\n", 240 | "if __name__ == \"__main__\":\n", 241 | " \n", 242 | " # Creating the dictionary\n", 243 | " my_dict = {\"Name\": \"Geeks\",\n", 244 | " \"Rank\": \"1223\",\n", 245 | " \"Subject\": \"Python\"}\n", 246 | " \n", 247 | " result = Dict2Class(my_dict)\n", 248 | " \n", 249 | " # printing the result\n", 250 | " print(\"After Converting Dictionary to Class : \")\n", 251 | " print(result.Name, result.Rank, result.Subject)\n", 252 | " print(type(result))" 253 | ] 254 | }, 255 | { 256 | "cell_type": "code", 257 | "execution_count": null, 258 | "id": "c5693c3f", 259 | "metadata": {}, 260 | "outputs": [], 261 | "source": [ 262 | "class cal():\n", 263 | " def __init__(self,a,b):\n", 264 | " self.a=a\n", 265 | " self.b=b\n", 266 | " def add(self):\n", 267 | " return self.a+self.b\n", 268 | " def mul(self):\n", 269 | " return self.a*self.b\n", 270 | " def div(self):\n", 271 | " return self.a/self.b\n", 272 | " def sub(self):\n", 273 | " return self.a-self.b\n", 274 | "a=int(input(\"Enter first number: \"))\n", 275 | "b=int(input(\"Enter second number: \"))\n", 276 | "obj=cal(a,b)\n", 277 | "choice=1\n", 278 | "while choice!=0:\n", 279 | " print(\"0. Exit\")\n", 280 | " print(\"1. Add\")\n", 281 | " print(\"2. Subtraction\")\n", 282 | " print(\"3. Multiplication\")\n", 283 | " print(\"4. Division\")\n", 284 | " choice=int(input(\"Enter choice: \"))\n", 285 | " if choice==1:\n", 286 | " print(\"Result: \",obj.add())\n", 287 | " elif choice==2:\n", 288 | " print(\"Result: \",obj.sub())\n", 289 | " elif choice==3:\n", 290 | " print(\"Result: \",obj.mul())\n", 291 | " elif choice==4:\n", 292 | " print(\"Result: \",round(obj.div(),2))\n", 293 | " elif choice==0:\n", 294 | " print(\"Exiting!\")\n", 295 | " else:\n", 296 | " print(\"Invalid choice!!\")\n", 297 | "print()" 298 | ] 299 | } 300 | ], 301 | "metadata": { 302 | "kernelspec": { 303 | "display_name": "Python 3 (ipykernel)", 304 | "language": "python", 305 | "name": "python3" 306 | }, 307 | "language_info": { 308 | "codemirror_mode": { 309 | "name": "ipython", 310 | "version": 3 311 | }, 312 | "file_extension": ".py", 313 | "mimetype": "text/x-python", 314 | "name": "python", 315 | "nbconvert_exporter": "python", 316 | "pygments_lexer": "ipython3", 317 | "version": "3.9.12" 318 | } 319 | }, 320 | "nbformat": 4, 321 | "nbformat_minor": 5 322 | } 323 | -------------------------------------------------------------------------------- /OS Module.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 7, 6 | "id": "b0dfd4bd", 7 | "metadata": {}, 8 | "outputs": [], 9 | "source": [ 10 | "import os" 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": 8, 16 | "id": "97b214dc", 17 | "metadata": {}, 18 | "outputs": [ 19 | { 20 | "name": "stdout", 21 | "output_type": "stream", 22 | "text": [ 23 | "['DirEntry', 'F_OK', 'GenericAlias', 'Mapping', 'MutableMapping', 'O_APPEND', 'O_BINARY', 'O_CREAT', 'O_EXCL', 'O_NOINHERIT', 'O_RANDOM', 'O_RDONLY', 'O_RDWR', 'O_SEQUENTIAL', 'O_SHORT_LIVED', 'O_TEMPORARY', 'O_TEXT', 'O_TRUNC', 'O_WRONLY', 'P_DETACH', 'P_NOWAIT', 'P_NOWAITO', 'P_OVERLAY', 'P_WAIT', 'PathLike', 'R_OK', 'SEEK_CUR', 'SEEK_END', 'SEEK_SET', 'TMP_MAX', 'W_OK', 'X_OK', '_AddedDllDirectory', '_Environ', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_check_methods', '_execvpe', '_exists', '_exit', '_fspath', '_get_exports_list', '_walk', '_wrap_close', 'abc', 'abort', 'access', 'add_dll_directory', 'altsep', 'chdir', 'chmod', 'close', 'closerange', 'cpu_count', 'curdir', 'defpath', 'device_encoding', 'devnull', 'dup', 'dup2', 'environ', 'error', 'execl', 'execle', 'execlp', 'execlpe', 'execv', 'execve', 'execvp', 'execvpe', 'extsep', 'fdopen', 'fsdecode', 'fsencode', 'fspath', 'fstat', 'fsync', 'ftruncate', 'get_exec_path', 'get_handle_inheritable', 'get_inheritable', 'get_terminal_size', 'getcwd', 'getcwdb', 'getenv', 'getlogin', 'getpid', 'getppid', 'isatty', 'kill', 'linesep', 'link', 'listdir', 'lseek', 'lstat', 'makedirs', 'mkdir', 'name', 'open', 'pardir', 'path', 'pathsep', 'pipe', 'popen', 'putenv', 'read', 'readlink', 'remove', 'removedirs', 'rename', 'renames', 'replace', 'rmdir', 'scandir', 'sep', 'set_handle_inheritable', 'set_inheritable', 'spawnl', 'spawnle', 'spawnv', 'spawnve', 'st', 'startfile', 'stat', 'stat_result', 'statvfs_result', 'strerror', 'supports_bytes_environ', 'supports_dir_fd', 'supports_effective_ids', 'supports_fd', 'supports_follow_symlinks', 'symlink', 'sys', 'system', 'terminal_size', 'times', 'times_result', 'truncate', 'umask', 'uname_result', 'unlink', 'unsetenv', 'urandom', 'utime', 'waitpid', 'waitstatus_to_exitcode', 'walk', 'write']\n" 24 | ] 25 | } 26 | ], 27 | "source": [ 28 | "print(dir(os))" 29 | ] 30 | }, 31 | { 32 | "cell_type": "code", 33 | "execution_count": 11, 34 | "id": "105fcd8e", 35 | "metadata": {}, 36 | "outputs": [ 37 | { 38 | "name": "stdout", 39 | "output_type": "stream", 40 | "text": [ 41 | "C:\\Users\\vaibh\\Jalpa Desai\n" 42 | ] 43 | } 44 | ], 45 | "source": [ 46 | "print(os.getcwd())" 47 | ] 48 | }, 49 | { 50 | "cell_type": "code", 51 | "execution_count": 12, 52 | "id": "106dca05", 53 | "metadata": {}, 54 | "outputs": [ 55 | { 56 | "data": { 57 | "text/plain": [ 58 | "'C:\\\\Users\\\\vaibh\\\\Jalpa Desai'" 59 | ] 60 | }, 61 | "execution_count": 12, 62 | "metadata": {}, 63 | "output_type": "execute_result" 64 | } 65 | ], 66 | "source": [ 67 | "pwd()" 68 | ] 69 | }, 70 | { 71 | "cell_type": "code", 72 | "execution_count": 10, 73 | "id": "d8b7fb55", 74 | "metadata": {}, 75 | "outputs": [], 76 | "source": [ 77 | "os.chdir(\"C:\\\\Users\\\\vaibh\\\\Jalpa Desai\")" 78 | ] 79 | }, 80 | { 81 | "cell_type": "code", 82 | "execution_count": 13, 83 | "id": "f82c8cce", 84 | "metadata": {}, 85 | "outputs": [ 86 | { 87 | "name": "stdout", 88 | "output_type": "stream", 89 | "text": [ 90 | "C:\\Users\\vaibh\\Jalpa Desai\n" 91 | ] 92 | } 93 | ], 94 | "source": [ 95 | "print(os.getcwd())" 96 | ] 97 | }, 98 | { 99 | "cell_type": "code", 100 | "execution_count": 14, 101 | "id": "eb7f0766", 102 | "metadata": {}, 103 | "outputs": [ 104 | { 105 | "data": { 106 | "text/plain": [ 107 | "['.ipynb_checkpoints',\n", 108 | " 'Data Visualization',\n", 109 | " 'Date Time_Time Delta_Module .ipynb',\n", 110 | " 'EDA',\n", 111 | " 'file sysetm.ipynb',\n", 112 | " 'files.zip',\n", 113 | " 'File_Handling.ipynb',\n", 114 | " 'for loop while loop(2).ipynb',\n", 115 | " 'For Loop(2).ipynb',\n", 116 | " 'For Loop.ipynb',\n", 117 | " 'for loops while loop(1).ipynb',\n", 118 | " 'Functions',\n", 119 | " 'google_1.txt',\n", 120 | " 'If Else.ipynb',\n", 121 | " 'Inheritance .ipynb',\n", 122 | " 'iterator generator.ipynb',\n", 123 | " 'List, Tuple, Dictionary, Sets.ipynb',\n", 124 | " 'loc_iloc.ipynb',\n", 125 | " 'Machine Learning',\n", 126 | " 'OOPS',\n", 127 | " 'OS Module.ipynb',\n", 128 | " 'Pro-Kabaddi-Data-Analysis-master',\n", 129 | " 'readme.csv',\n", 130 | " 'readme.txt',\n", 131 | " 'Regular Expressions .ipynb',\n", 132 | " 'Shallow Copy Vs deep copy.ipynb',\n", 133 | " 'SQL',\n", 134 | " 'Statistics',\n", 135 | " 'Statistics_Basics.ipynb',\n", 136 | " 'test4.txt',\n", 137 | " 'test5.txt',\n", 138 | " 'test6.txt',\n", 139 | " 'Try and Exception Handling.ipynb',\n", 140 | " 'Untitled.ipynb',\n", 141 | " 'Untitled1.ipynb',\n", 142 | " 'Untitled2.ipynb',\n", 143 | " 'Untitled3.ipynb',\n", 144 | " 'Untitled4.ipynb',\n", 145 | " 'Untitled5.ipynb',\n", 146 | " 'Untitled6.ipynb',\n", 147 | " 'Untitled7.ipynb',\n", 148 | " 'Zip file _Unzip file.ipynb']" 149 | ] 150 | }, 151 | "execution_count": 14, 152 | "metadata": {}, 153 | "output_type": "execute_result" 154 | } 155 | ], 156 | "source": [ 157 | "os.listdir(\"C:\\\\Users\\\\vaibh\\\\Jalpa Desai\")" 158 | ] 159 | }, 160 | { 161 | "cell_type": "code", 162 | "execution_count": 15, 163 | "id": "d4a7635f", 164 | "metadata": {}, 165 | "outputs": [], 166 | "source": [ 167 | "os.mkdir(\"THIS\") ## make folder" 168 | ] 169 | }, 170 | { 171 | "cell_type": "code", 172 | "execution_count": 20, 173 | "id": "2d9d56d9", 174 | "metadata": {}, 175 | "outputs": [ 176 | { 177 | "ename": "FileNotFoundError", 178 | "evalue": "[Errno 2] No such file or directory: 'jalpa11.txt'", 179 | "output_type": "error", 180 | "traceback": [ 181 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 182 | "\u001b[1;31mFileNotFoundError\u001b[0m Traceback (most recent call last)", 183 | "Input \u001b[1;32mIn [20]\u001b[0m, in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[0m f \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mopen\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mjalpa11.txt\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\n", 184 | "\u001b[1;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: 'jalpa11.txt'" 185 | ] 186 | } 187 | ], 188 | "source": [ 189 | "f = open(\"jalpa11.txt\")" 190 | ] 191 | }, 192 | { 193 | "cell_type": "code", 194 | "execution_count": 23, 195 | "id": "6e1141b3", 196 | "metadata": {}, 197 | "outputs": [ 198 | { 199 | "name": "stdout", 200 | "output_type": "stream", 201 | "text": [ 202 | "C:\\ProgramData\\Anaconda3;C:\\ProgramData\\Anaconda3\\Library\\mingw-w64\\bin;C:\\ProgramData\\Anaconda3\\Library\\usr\\bin;C:\\ProgramData\\Anaconda3\\Library\\bin;C:\\ProgramData\\Anaconda3\\Scripts;C:\\WINDOWS\\system32;C:\\WINDOWS;C:\\WINDOWS\\System32\\Wbem;C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\;C:\\WINDOWS\\System32\\OpenSSH\\;C:\\Program Files\\nodejs\\;C:\\Program Files\\MySQL\\MySQL Shell 8.0\\bin\\;C:\\Users\\vaibh\\AppData\\Local\\Microsoft\\WindowsApps;;C:\\Users\\vaibh\\AppData\\Local\\Programs\\Microsoft VS Code\\bin;C:\\Users\\vaibh\\AppData\\Roaming\\npm\n" 203 | ] 204 | } 205 | ], 206 | "source": [ 207 | "print(os.environ.get('Path'))" 208 | ] 209 | }, 210 | { 211 | "cell_type": "code", 212 | "execution_count": 24, 213 | "id": "90525d55", 214 | "metadata": {}, 215 | "outputs": [ 216 | { 217 | "name": "stdout", 218 | "output_type": "stream", 219 | "text": [ 220 | "Do you wish to shutdown your computer ? (yes / no): no\n" 221 | ] 222 | } 223 | ], 224 | "source": [ 225 | " import os\n", 226 | " \n", 227 | "shutdown = input(\"Do you wish to shutdown your computer ? (yes / no): \")\n", 228 | " \n", 229 | "if shutdown == 'no':\n", 230 | " exit()\n", 231 | "else:\n", 232 | " os.system(\"shutdown /s /t 1\")" 233 | ] 234 | }, 235 | { 236 | "cell_type": "code", 237 | "execution_count": null, 238 | "id": "063949f4", 239 | "metadata": {}, 240 | "outputs": [], 241 | "source": [ 242 | "import os\n", 243 | "os.system(\"shutdown /s\")" 244 | ] 245 | }, 246 | { 247 | "cell_type": "code", 248 | "execution_count": 3, 249 | "id": "827b3d6b", 250 | "metadata": {}, 251 | "outputs": [ 252 | { 253 | "data": { 254 | "text/plain": [ 255 | "'C:\\\\Users\\\\vaibh\\\\Jalpa Desai'" 256 | ] 257 | }, 258 | "execution_count": 3, 259 | "metadata": {}, 260 | "output_type": "execute_result" 261 | } 262 | ], 263 | "source": [ 264 | "pwd()" 265 | ] 266 | }, 267 | { 268 | "cell_type": "code", 269 | "execution_count": 4, 270 | "id": "0f7617db", 271 | "metadata": {}, 272 | "outputs": [ 273 | { 274 | "name": "stdout", 275 | "output_type": "stream", 276 | "text": [ 277 | "Source path renamed to destination path successfully.\n" 278 | ] 279 | } 280 | ], 281 | "source": [ 282 | "# importing os module \n", 283 | "import os\n", 284 | " \n", 285 | " \n", 286 | "# Source file path\n", 287 | "source = 'C:\\\\Users\\\\vaibh\\\\Jalpa Desai\\\\test4.txt'\n", 288 | " \n", 289 | "# destination file path\n", 290 | "dest = 'C:\\\\Users\\\\vaibh\\\\Jalpa Desai\\\\newfile.txt'\n", 291 | " \n", 292 | " \n", 293 | "# Now rename the source path\n", 294 | "\n", 295 | "os.rename(source, dest)\n", 296 | "print(\"Source path renamed to destination path successfully.\")" 297 | ] 298 | }, 299 | { 300 | "cell_type": "code", 301 | "execution_count": 2, 302 | "id": "65d85a5d", 303 | "metadata": {}, 304 | "outputs": [ 305 | { 306 | "name": "stdout", 307 | "output_type": "stream", 308 | "text": [ 309 | " Volume in drive C is Windows\n", 310 | " Volume Serial Number is FC42-E37E\n", 311 | "\n", 312 | " Directory of C:\\Users\\vaibh\\Jalpa Desai\n", 313 | "\n", 314 | "30-09-2022 14:30 .\n", 315 | "27-09-2022 17:41 ..\n", 316 | "30-09-2022 14:05 .ipynb_checkpoints\n", 317 | "29-09-2022 14:48 Data Visualization\n", 318 | "29-09-2022 09:44 16,731 Date Time_Time Delta_Module .ipynb\n", 319 | "25-09-2022 10:23 EDA\n", 320 | "25-09-2022 10:59 24,741 file sysetm.ipynb\n", 321 | "27-09-2022 16:39 34,255 File_Handling.ipynb\n", 322 | "27-09-2022 14:09 273 files.zip\n", 323 | "12-09-2022 13:43 9,876 for loop while loop(2).ipynb\n", 324 | "29-09-2022 17:24 21,607 For Loop(2).ipynb\n", 325 | "08-09-2022 09:57 12,161 For Loop.ipynb\n", 326 | "28-09-2022 14:59 28,884 for loops while loop(1).ipynb\n", 327 | "30-09-2022 13:10 Functions\n", 328 | "27-09-2022 16:37 3,077 google_1.txt\n", 329 | "27-09-2022 16:00 11,155 If Else.ipynb\n", 330 | "12-09-2022 13:45 41,235 Inheritance .ipynb\n", 331 | "27-09-2022 11:57 21,693,278 iterator generator.ipynb\n", 332 | "25-09-2022 12:45 21,077 List, Tuple, Dictionary, Sets.ipynb\n", 333 | "17-09-2022 14:54 10,129 loc_iloc.ipynb\n", 334 | "13-09-2022 15:32 Machine Learning\n", 335 | "24-09-2022 12:08 OOPS\n", 336 | "30-09-2022 14:30 8,557 OS Module.ipynb\n", 337 | "27-09-2022 14:16 Pro-Kabaddi-Data-Analysis-master\n", 338 | "25-09-2022 10:57 23 readme.csv\n", 339 | "25-09-2022 10:55 36 readme.txt\n", 340 | "26-09-2022 11:44 14,801 Regular Expressions .ipynb\n", 341 | "28-09-2022 11:40 7,485 Shallow Copy Vs deep copy.ipynb\n", 342 | "24-09-2022 17:32 SQL\n", 343 | "26-09-2022 17:06 Statistics\n", 344 | "29-09-2022 14:01 15,861 Statistics_Basics.ipynb\n", 345 | "27-09-2022 16:32 39 test4.txt\n", 346 | "27-09-2022 16:29 226 test5.txt\n", 347 | "27-09-2022 16:30 50 test6.txt\n", 348 | "30-09-2022 14:18 THIS\n", 349 | "11-09-2022 12:04 19,164 Try and Exception Handling.ipynb\n", 350 | "29-09-2022 17:04 40,632 Untitled.ipynb\n", 351 | "21-09-2022 17:12 8,120 Untitled1.ipynb\n", 352 | "25-09-2022 12:46 13,685 Untitled2.ipynb\n", 353 | "25-09-2022 13:53 5,087 Untitled3.ipynb\n", 354 | "27-09-2022 15:13 10,069 Untitled4.ipynb\n", 355 | "28-09-2022 15:45 14,668 Untitled5.ipynb\n", 356 | "28-09-2022 17:31 23,370 Untitled6.ipynb\n", 357 | "29-09-2022 13:45 589 Untitled7.ipynb\n", 358 | "28-09-2022 12:10 2,526 Zip file _Unzip file.ipynb\n", 359 | " 33 File(s) 22,113,467 bytes\n", 360 | " 12 Dir(s) 191,982,866,432 bytes free\n" 361 | ] 362 | } 363 | ], 364 | "source": [ 365 | "ls" 366 | ] 367 | }, 368 | { 369 | "cell_type": "code", 370 | "execution_count": 8, 371 | "id": "cb5c52d2", 372 | "metadata": {}, 373 | "outputs": [], 374 | "source": [ 375 | "import os\n", 376 | "os.mkdir(\"D:\\Tutorialspoint\") ## make directory" 377 | ] 378 | }, 379 | { 380 | "cell_type": "code", 381 | "execution_count": 9, 382 | "id": "3c6ced10", 383 | "metadata": {}, 384 | "outputs": [], 385 | "source": [ 386 | "import os \n", 387 | "os.rmdir(\"D:\\Tutorialspoint\") ## delete directory" 388 | ] 389 | }, 390 | { 391 | "cell_type": "code", 392 | "execution_count": 10, 393 | "id": "efee40e3", 394 | "metadata": {}, 395 | "outputs": [], 396 | "source": [ 397 | "import os\n", 398 | "os.mkdir(\"D:\\Tutorialspoint\")\n", 399 | "os.rename(\"D:\\Tutorialspoint\",\"D:\\Tutorialspoint2\")" 400 | ] 401 | }, 402 | { 403 | "cell_type": "code", 404 | "execution_count": 11, 405 | "id": "9512a4b6", 406 | "metadata": {}, 407 | "outputs": [], 408 | "source": [ 409 | "file = os.popen(\"Hello.txt\", 'w')" 410 | ] 411 | }, 412 | { 413 | "cell_type": "code", 414 | "execution_count": 12, 415 | "id": "cd33ca30", 416 | "metadata": {}, 417 | "outputs": [ 418 | { 419 | "data": { 420 | "text/plain": [ 421 | "45" 422 | ] 423 | }, 424 | "execution_count": 12, 425 | "metadata": {}, 426 | "output_type": "execute_result" 427 | } 428 | ], 429 | "source": [ 430 | "file = os.popen(\"Hello.txt\", 'w')\n", 431 | "file.write(\"Hello there! This is a tutorialspoint article\")" 432 | ] 433 | }, 434 | { 435 | "cell_type": "code", 436 | "execution_count": null, 437 | "id": "c3f557e3", 438 | "metadata": {}, 439 | "outputs": [], 440 | "source": [] 441 | } 442 | ], 443 | "metadata": { 444 | "kernelspec": { 445 | "display_name": "Python 3 (ipykernel)", 446 | "language": "python", 447 | "name": "python3" 448 | }, 449 | "language_info": { 450 | "codemirror_mode": { 451 | "name": "ipython", 452 | "version": 3 453 | }, 454 | "file_extension": ".py", 455 | "mimetype": "text/x-python", 456 | "name": "python", 457 | "nbconvert_exporter": "python", 458 | "pygments_lexer": "ipython3", 459 | "version": "3.9.12" 460 | } 461 | }, 462 | "nbformat": 4, 463 | "nbformat_minor": 5 464 | } 465 | -------------------------------------------------------------------------------- /Polymorphism_Overriding_Overloading.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "b43058ae", 6 | "metadata": {}, 7 | "source": [ 8 | "# Polymorphism" 9 | ] 10 | }, 11 | { 12 | "cell_type": "raw", 13 | "id": "24e9d70c", 14 | "metadata": {}, 15 | "source": [ 16 | "Polymorphism means the same function name (but different signatures) being used for different types." 17 | ] 18 | }, 19 | { 20 | "cell_type": "code", 21 | "execution_count": 1, 22 | "id": "774acd67", 23 | "metadata": {}, 24 | "outputs": [], 25 | "source": [ 26 | "def test(a,b):\n", 27 | " return a+b" 28 | ] 29 | }, 30 | { 31 | "cell_type": "code", 32 | "execution_count": 2, 33 | "id": "52a8fd19", 34 | "metadata": {}, 35 | "outputs": [ 36 | { 37 | "data": { 38 | "text/plain": [ 39 | "7" 40 | ] 41 | }, 42 | "execution_count": 2, 43 | "metadata": {}, 44 | "output_type": "execute_result" 45 | } 46 | ], 47 | "source": [ 48 | "test(3,4) # addition" 49 | ] 50 | }, 51 | { 52 | "cell_type": "code", 53 | "execution_count": 3, 54 | "id": "304b071a", 55 | "metadata": {}, 56 | "outputs": [ 57 | { 58 | "data": { 59 | "text/plain": [ 60 | "'JalpaPatel'" 61 | ] 62 | }, 63 | "execution_count": 3, 64 | "metadata": {}, 65 | "output_type": "execute_result" 66 | } 67 | ], 68 | "source": [ 69 | "test(\"Jalpa\",\"Patel\") # concat" 70 | ] 71 | }, 72 | { 73 | "cell_type": "code", 74 | "execution_count": 1, 75 | "id": "ca1235ef", 76 | "metadata": {}, 77 | "outputs": [ 78 | { 79 | "name": "stdout", 80 | "output_type": "stream", 81 | "text": [ 82 | "5\n", 83 | "3\n" 84 | ] 85 | } 86 | ], 87 | "source": [ 88 | "# len() being used for a string\n", 89 | "print(len(\"jalpa\"))\n", 90 | " \n", 91 | "# len() being used for a list\n", 92 | "print(len([1, 2, 3]))" 93 | ] 94 | }, 95 | { 96 | "cell_type": "code", 97 | "execution_count": 3, 98 | "id": "a2ff397c", 99 | "metadata": {}, 100 | "outputs": [ 101 | { 102 | "name": "stdout", 103 | "output_type": "stream", 104 | "text": [ 105 | "3\n", 106 | "4\n", 107 | "2\n" 108 | ] 109 | } 110 | ], 111 | "source": [ 112 | "print(len(\"Bit\"))\n", 113 | "print(len([\"Python\", \"Java\", \"C\", \"C++\"]))\n", 114 | "print(len({\"Name\": \"Jalpa\", \"Address\": \"India\"}))" 115 | ] 116 | }, 117 | { 118 | "cell_type": "raw", 119 | "id": "8d396fe2", 120 | "metadata": {}, 121 | "source": [ 122 | "Here, we can see that many data types such as string, list, tuple, set, and dictionary can work with the len() function. However, we can see that it returns specific information about specific data types." 123 | ] 124 | }, 125 | { 126 | "cell_type": "code", 127 | "execution_count": 6, 128 | "id": "f3f5dca8", 129 | "metadata": {}, 130 | "outputs": [], 131 | "source": [ 132 | "class insta:\n", 133 | " def share_stories(self):\n", 134 | " print(\"This will share my insta story\")\n", 135 | " \n", 136 | "class facebook:\n", 137 | " def share_stories(self):\n", 138 | " print(\"This will share my facebook story\")\n" 139 | ] 140 | }, 141 | { 142 | "cell_type": "code", 143 | "execution_count": 7, 144 | "id": "acdd3c1e", 145 | "metadata": {}, 146 | "outputs": [], 147 | "source": [ 148 | "def sharestory (app):\n", 149 | " app.share_stories()" 150 | ] 151 | }, 152 | { 153 | "cell_type": "code", 154 | "execution_count": 9, 155 | "id": "8976b598", 156 | "metadata": {}, 157 | "outputs": [ 158 | { 159 | "name": "stdout", 160 | "output_type": "stream", 161 | "text": [ 162 | "This will share my insta story\n", 163 | "This will share my facebook story\n" 164 | ] 165 | } 166 | ], 167 | "source": [ 168 | "i = insta()\n", 169 | "f = facebook()\n", 170 | "sharestory(i)\n", 171 | "sharestory(f)" 172 | ] 173 | }, 174 | { 175 | "cell_type": "code", 176 | "execution_count": 2, 177 | "id": "96bb2989", 178 | "metadata": {}, 179 | "outputs": [ 180 | { 181 | "name": "stdout", 182 | "output_type": "stream", 183 | "text": [ 184 | "New Delhi is the capital of India.\n", 185 | "Hindi is the most widely spoken language of India.\n", 186 | "India is a developing country.\n", 187 | "Washington, D.C. is the capital of USA.\n", 188 | "English is the primary language of USA.\n", 189 | "USA is a developed country.\n" 190 | ] 191 | } 192 | ], 193 | "source": [ 194 | "#Polymorphism with class method\n", 195 | "class India():\n", 196 | " def capital(self):\n", 197 | " print(\"New Delhi is the capital of India.\")\n", 198 | " \n", 199 | " def language(self):\n", 200 | " print(\"Hindi is the most widely spoken language of India.\")\n", 201 | " \n", 202 | " def type(self):\n", 203 | " print(\"India is a developing country.\")\n", 204 | " \n", 205 | "class USA():\n", 206 | " def capital(self):\n", 207 | " print(\"Washington, D.C. is the capital of USA.\")\n", 208 | " \n", 209 | " def language(self):\n", 210 | " print(\"English is the primary language of USA.\")\n", 211 | " \n", 212 | " def type(self):\n", 213 | " print(\"USA is a developed country.\")\n", 214 | " \n", 215 | "obj_ind = India()\n", 216 | "obj_usa = USA()\n", 217 | "\n", 218 | "for i in (obj_ind, obj_usa):\n", 219 | " i.capital()\n", 220 | " i.language()\n", 221 | " i.type()" 222 | ] 223 | }, 224 | { 225 | "cell_type": "code", 226 | "execution_count": 12, 227 | "id": "de8cbbf4", 228 | "metadata": {}, 229 | "outputs": [], 230 | "source": [ 231 | "class test(Exception):\n", 232 | " def __init__(self , msg , myval) :\n", 233 | " self.msg = msg\n", 234 | " self.myval =myval" 235 | ] 236 | }, 237 | { 238 | "cell_type": "code", 239 | "execution_count": 15, 240 | "id": "fa70647a", 241 | "metadata": {}, 242 | "outputs": [ 243 | { 244 | "name": "stdout", 245 | "output_type": "stream", 246 | "text": [ 247 | "('this is my own exception class ', 'bit')\n" 248 | ] 249 | } 250 | ], 251 | "source": [ 252 | "try :\n", 253 | " raise(test(\"this is my own exception class \" , \"bit\"))\n", 254 | "except test as t :\n", 255 | " print(t)" 256 | ] 257 | }, 258 | { 259 | "cell_type": "code", 260 | "execution_count": 16, 261 | "id": "e1abb5b1", 262 | "metadata": {}, 263 | "outputs": [ 264 | { 265 | "name": "stdout", 266 | "output_type": "stream", 267 | "text": [ 268 | "('this is my own exception class ', 5)\n" 269 | ] 270 | } 271 | ], 272 | "source": [ 273 | "try :\n", 274 | " raise(test(\"this is my own exception class \",5))\n", 275 | "except test as t :\n", 276 | " print(t)" 277 | ] 278 | }, 279 | { 280 | "cell_type": "raw", 281 | "id": "c5017072", 282 | "metadata": {}, 283 | "source": [ 284 | "\"\"\"Q1 create your own class for to achive multiple , multilevel inheritance \n", 285 | "Q2 create your own class to represenet ploymorprism \n", 286 | "Q3 create your own class for custome exception\n", 287 | "Q4 create your own class to achive encaptulation\n", 288 | "Q5 create your own class to achive method overloading and overriding . \"\"\"" 289 | ] 290 | }, 291 | { 292 | "cell_type": "markdown", 293 | "id": "5d204ef4", 294 | "metadata": {}, 295 | "source": [ 296 | "# Method Overriding " 297 | ] 298 | }, 299 | { 300 | "cell_type": "raw", 301 | "id": "ca31ec49", 302 | "metadata": {}, 303 | "source": [ 304 | "Key features of Method Overriding in Python\n", 305 | "These are some of the key features and advantages of method overriding in Python --\n", 306 | "\n", 307 | "Method Overriding is derived from the concept of object oriented programming\n", 308 | "Method Overriding allows us to change the implementation of a function in the child class which is defined in the parent class.\n", 309 | "Method Overriding is a part of the inheritance mechanism\n", 310 | "Method Overriding avoids duplication of code\n", 311 | "Method Overriding also enhances the code adding some additional properties.\n", 312 | "Prerequisites for method overriding\n", 313 | "There are certain prerequisites for method overriding in Python. They're discussed below --\n", 314 | "\n", 315 | "Method overriding cannot be done within a class. So,we need to derive a child class from a parent class. Hence Inheritance is mandatory.\n", 316 | "The method must have the same name as in the parent class\n", 317 | "The method must have the same number of parameters as in the parent class." 318 | ] 319 | }, 320 | { 321 | "cell_type": "code", 322 | "execution_count": 41, 323 | "id": "3b594ace", 324 | "metadata": {}, 325 | "outputs": [ 326 | { 327 | "name": "stdout", 328 | "output_type": "stream", 329 | "text": [ 330 | "Hi \n", 331 | "My name is jalpa\n" 332 | ] 333 | } 334 | ], 335 | "source": [ 336 | "# Method Overriding (Function define with same name for different arguments)\n", 337 | "\n", 338 | "class xyz:\n", 339 | " def test(self):\n", 340 | " print(\"Hi \")\n", 341 | " \n", 342 | " def test2(self):\n", 343 | " print(\"My name is jalpa\")\n", 344 | " \n", 345 | "class xyz1(xyz):\n", 346 | " def test(self):\n", 347 | " print(\"Hello world\")\n", 348 | " \n", 349 | " def test2(self):\n", 350 | " print(\"Good morning\")\n", 351 | " \n", 352 | "obj = xyz1()\n", 353 | "\n", 354 | "obj.test()\n", 355 | "obj.test2()\n" 356 | ] 357 | }, 358 | { 359 | "cell_type": "code", 360 | "execution_count": 5, 361 | "id": "47ddce5e", 362 | "metadata": {}, 363 | "outputs": [ 364 | { 365 | "name": "stdout", 366 | "output_type": "stream", 367 | "text": [ 368 | "Circle\n", 369 | "I am a two-dimensional shape.\n", 370 | "Squares have each angle equal to 90 degrees.\n", 371 | "153.93804002589985\n" 372 | ] 373 | } 374 | ], 375 | "source": [ 376 | "from math import pi\n", 377 | "\n", 378 | "\n", 379 | "class Shape:\n", 380 | " def __init__(self, name):\n", 381 | " self.name = name\n", 382 | "\n", 383 | " def area(self):\n", 384 | " pass\n", 385 | "\n", 386 | " def fact(self):\n", 387 | " return \"Dimention Shape\"\n", 388 | "\n", 389 | " def __str__(self):\n", 390 | " return self.name\n", 391 | "\n", 392 | "\n", 393 | "class Square(Shape):\n", 394 | " def __init__(self, length):\n", 395 | " super().__init__(\"Square\")\n", 396 | " self.length = length\n", 397 | "\n", 398 | " def area(self):\n", 399 | " return self.length**2\n", 400 | "\n", 401 | " def fact(self):\n", 402 | " return \"Squares have 4 angle\"\n", 403 | " \n", 404 | "class Circle(Shape):\n", 405 | " def __init__(self, radius):\n", 406 | " super().__init__(\"Circle\")\n", 407 | " self.radius = radius\n", 408 | "\n", 409 | " def area(self):\n", 410 | " return pi*self.radius**2\n", 411 | "\n", 412 | "\n", 413 | "a = Square(4)\n", 414 | "b = Circle(7)\n", 415 | "print(b)\n", 416 | "print(b.fact())\n", 417 | "print(a.fact())\n", 418 | "print(b.area())" 419 | ] 420 | }, 421 | { 422 | "cell_type": "markdown", 423 | "id": "b6a1dccc", 424 | "metadata": {}, 425 | "source": [ 426 | "# Method Overloading" 427 | ] 428 | }, 429 | { 430 | "cell_type": "code", 431 | "execution_count": 9, 432 | "id": "d7157664", 433 | "metadata": {}, 434 | "outputs": [ 435 | { 436 | "name": "stdout", 437 | "output_type": "stream", 438 | "text": [ 439 | "3\n", 440 | "BIT For\n", 441 | "12\n", 442 | "BITBITBITBIT\n" 443 | ] 444 | } 445 | ], 446 | "source": [ 447 | "print(1 + 2)\n", 448 | " \n", 449 | "# concatenate two strings\n", 450 | "print(\"BIT\"+ \" \" +\"For\") \n", 451 | " \n", 452 | "# Product two numbers\n", 453 | "print(3 * 4)\n", 454 | " \n", 455 | "# Repeat the String\n", 456 | "print(\"BIT\"*4)" 457 | ] 458 | }, 459 | { 460 | "cell_type": "code", 461 | "execution_count": 10, 462 | "id": "1c52cfa8", 463 | "metadata": {}, 464 | "outputs": [ 465 | { 466 | "name": "stdout", 467 | "output_type": "stream", 468 | "text": [ 469 | "3\n", 470 | "jalpaDs\n" 471 | ] 472 | } 473 | ], 474 | "source": [ 475 | "class A:\n", 476 | " def __init__(self, a):\n", 477 | " self.a = a\n", 478 | " \n", 479 | " # adding two objects \n", 480 | " def __add__(self, b):\n", 481 | " return self.a + b.a \n", 482 | "\n", 483 | "ob1 = A(1)\n", 484 | "ob2 = A(2)\n", 485 | "ob3 = A(\"jalpa\")\n", 486 | "ob4 = A(\"Ds\")\n", 487 | " \n", 488 | "print(ob1 + ob2)\n", 489 | "print(ob3 + ob4)" 490 | ] 491 | }, 492 | { 493 | "cell_type": "code", 494 | "execution_count": 45, 495 | "id": "15e7f7e2", 496 | "metadata": {}, 497 | "outputs": [ 498 | { 499 | "name": "stdout", 500 | "output_type": "stream", 501 | "text": [ 502 | "(3, 5)\n" 503 | ] 504 | } 505 | ], 506 | "source": [ 507 | "class complex:\n", 508 | " def __init__(self, a, b):\n", 509 | " self.a = a\n", 510 | " self.b = b\n", 511 | " \n", 512 | " # adding two objects \n", 513 | " def __add__(self, other):\n", 514 | " return self.a + other.a, self.b + other.b\n", 515 | " \n", 516 | "Ob1 = complex(1, 2)\n", 517 | "Ob2 = complex(2, 3)\n", 518 | "Ob3 = Ob1 + Ob2\n", 519 | "print(Ob3)" 520 | ] 521 | }, 522 | { 523 | "cell_type": "code", 524 | "execution_count": 46, 525 | "id": "f70e0bd5", 526 | "metadata": {}, 527 | "outputs": [ 528 | { 529 | "name": "stdout", 530 | "output_type": "stream", 531 | "text": [ 532 | "ob2 is greater than ob1\n" 533 | ] 534 | } 535 | ], 536 | "source": [ 537 | "class A:\n", 538 | " def __init__(self, a):\n", 539 | " self.a = a\n", 540 | " def __gt__(self, other):\n", 541 | " if(self.a>other.a):\n", 542 | " return True\n", 543 | " else:\n", 544 | " return False\n", 545 | "ob1 = A(2)\n", 546 | "ob2 = A(3)\n", 547 | "if(ob1>ob2):\n", 548 | " print(\"ob1 is greater than ob2\")\n", 549 | "else:\n", 550 | " print(\"ob2 is greater than ob1\")" 551 | ] 552 | } 553 | ], 554 | "metadata": { 555 | "kernelspec": { 556 | "display_name": "Python 3 (ipykernel)", 557 | "language": "python", 558 | "name": "python3" 559 | }, 560 | "language_info": { 561 | "codemirror_mode": { 562 | "name": "ipython", 563 | "version": 3 564 | }, 565 | "file_extension": ".py", 566 | "mimetype": "text/x-python", 567 | "name": "python", 568 | "nbconvert_exporter": "python", 569 | "pygments_lexer": "ipython3", 570 | "version": "3.9.12" 571 | } 572 | }, 573 | "nbformat": 4, 574 | "nbformat_minor": 5 575 | } 576 | -------------------------------------------------------------------------------- /Python Networking Programming/Python Network Programming.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "raw", 5 | "id": "9053eabb", 6 | "metadata": {}, 7 | "source": [ 8 | "Python provides two levels of access to network programming. \n", 9 | "These are: \n", 10 | "\n", 11 | "Low-Level Access: At the low level, you can access the basic socket support of the operating system. You can implement client and server for both connection-oriented and connectionless protocols.\n", 12 | "High-Level Access: At the high level allows to implement protocols like HTTP, FTP, etc." 13 | ] 14 | }, 15 | { 16 | "cell_type": "raw", 17 | "id": "8bb4b190", 18 | "metadata": {}, 19 | "source": [ 20 | "What are Sockets? # 0 - 65535\n", 21 | "Consider a bidirectional communication channel, the sockets are the endpoints of this communication channel. These sockets (endpoints) can communicate within a process, between processes on the same machine, or between the processes on the different machines. Sockets use different protocols for determining the connection type for port-to-port communication between clients and servers." 22 | ] 23 | }, 24 | { 25 | "cell_type": "raw", 26 | "id": "777a3325", 27 | "metadata": {}, 28 | "source": [ 29 | "What is Socket Programming?\n", 30 | "Socket programming is a way of connecting two nodes on a network to communicate with each other. One socket(node) listens on a particular port at an IP, while the other socket reaches out to the other to form a connection. The server forms the listener socket while the client reaches out to the server. They are the real backbones behind web browsing. In simpler terms, there is a server and a client. We can use the socket module for socket programming. \n", 31 | "For this, \n", 32 | "we have to include the socket module " 33 | ] 34 | }, 35 | { 36 | "cell_type": "code", 37 | "execution_count": 1, 38 | "id": "49b09743", 39 | "metadata": {}, 40 | "outputs": [], 41 | "source": [ 42 | "import socket" 43 | ] 44 | }, 45 | { 46 | "cell_type": "code", 47 | "execution_count": 2, 48 | "id": "5c3c9371", 49 | "metadata": {}, 50 | "outputs": [ 51 | { 52 | "ename": "NameError", 53 | "evalue": "name 'socket_family' is not defined", 54 | "output_type": "error", 55 | "traceback": [ 56 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 57 | "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", 58 | "Input \u001b[1;32mIn [2]\u001b[0m, in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;66;03m## to create a socket we have to use the socket.socket() method. \u001b[39;00m\n\u001b[1;32m----> 2\u001b[0m socket\u001b[38;5;241m.\u001b[39msocket(\u001b[43msocket_family\u001b[49m, socket_type, protocol\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m0\u001b[39m)\n", 59 | "\u001b[1;31mNameError\u001b[0m: name 'socket_family' is not defined" 60 | ] 61 | } 62 | ], 63 | "source": [ 64 | "## to create a socket we have to use the socket.socket() method. \n", 65 | "## socket_family: Either AF_UNIX or AF_INET\n", 66 | "## socket_type: Either SOCK_STREAM or SOCK_DGRAM\n", 67 | "## protocol: Usually left out, defaulting to 0\n", 68 | "socket.socket(socket_family, socket_type, protocol=0)" 69 | ] 70 | }, 71 | { 72 | "cell_type": "code", 73 | "execution_count": 3, 74 | "id": "03b1f154", 75 | "metadata": {}, 76 | "outputs": [ 77 | { 78 | "name": "stdout", 79 | "output_type": "stream", 80 | "text": [ 81 | "\n" 82 | ] 83 | } 84 | ], 85 | "source": [ 86 | "import socket\n", 87 | " \n", 88 | " \n", 89 | "s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n", 90 | "print(s)" 91 | ] 92 | }, 93 | { 94 | "cell_type": "code", 95 | "execution_count": null, 96 | "id": "2496db53", 97 | "metadata": {}, 98 | "outputs": [], 99 | "source": [ 100 | "import socket\n", 101 | "\n", 102 | "soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n", 103 | "soc.bind((socket.gethostname(), 1042))\n", 104 | "soc.listen(5)\n", 105 | "\n", 106 | "while True:\n", 107 | " clt, addr = soc.accept() ## address \n", 108 | " print(f\"connection establish to address {addr}\")\n", 109 | " clt.send(bytes(\"network programmimg using python\",\"utf-8\"))\n", 110 | " #msg = \"message received and the message is \"\n", 111 | " #clt.send(bytes(msg+\" network programming using python\", \"utf-8\"))" 112 | ] 113 | }, 114 | { 115 | "cell_type": "code", 116 | "execution_count": null, 117 | "id": "51a989b8", 118 | "metadata": {}, 119 | "outputs": [], 120 | "source": [ 121 | "import socket\n", 122 | "\n", 123 | "soc = socket.socket(socket.AF_INET,socket.SOCK_STREM)\n", 124 | "soc.connet((socket.gethostname(),1042)) ## connection is established\n", 125 | "msg = soc.recv(100)\n", 126 | "print(msg.decode(\"utf-8\"))\n", 127 | " \n", 128 | "#while True:\n", 129 | " #msg = soc.recv(8)\n", 130 | " #print(msg.decode(\"utf-8\"))" 131 | ] 132 | } 133 | ], 134 | "metadata": { 135 | "kernelspec": { 136 | "display_name": "Python 3 (ipykernel)", 137 | "language": "python", 138 | "name": "python3" 139 | }, 140 | "language_info": { 141 | "codemirror_mode": { 142 | "name": "ipython", 143 | "version": 3 144 | }, 145 | "file_extension": ".py", 146 | "mimetype": "text/x-python", 147 | "name": "python", 148 | "nbconvert_exporter": "python", 149 | "pygments_lexer": "ipython3", 150 | "version": "3.9.12" 151 | } 152 | }, 153 | "nbformat": 4, 154 | "nbformat_minor": 5 155 | } 156 | -------------------------------------------------------------------------------- /Python Networking Programming/client.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "id": "72549265", 7 | "metadata": {}, 8 | "outputs": [ 9 | { 10 | "name": "stdout", 11 | "output_type": "stream", 12 | "text": [ 13 | "Enter your namejalpa\n", 14 | "welcome to BIT\n" 15 | ] 16 | } 17 | ], 18 | "source": [ 19 | "import socket\n", 20 | "c = socket.socket()\n", 21 | "\n", 22 | "c.connect(('localhost', 9999))\n", 23 | "\n", 24 | "name = input(\"Enter your name\")\n", 25 | "c.send(bytes(name,'utf-8'))\n", 26 | "\n", 27 | "\n", 28 | "print(c.recv(1024).decode())" 29 | ] 30 | }, 31 | { 32 | "cell_type": "code", 33 | "execution_count": null, 34 | "id": "5eb86beb", 35 | "metadata": {}, 36 | "outputs": [], 37 | "source": [] 38 | } 39 | ], 40 | "metadata": { 41 | "kernelspec": { 42 | "display_name": "Python 3 (ipykernel)", 43 | "language": "python", 44 | "name": "python3" 45 | }, 46 | "language_info": { 47 | "codemirror_mode": { 48 | "name": "ipython", 49 | "version": 3 50 | }, 51 | "file_extension": ".py", 52 | "mimetype": "text/x-python", 53 | "name": "python", 54 | "nbconvert_exporter": "python", 55 | "pygments_lexer": "ipython3", 56 | "version": "3.9.12" 57 | } 58 | }, 59 | "nbformat": 4, 60 | "nbformat_minor": 5 61 | } 62 | -------------------------------------------------------------------------------- /Python Networking Programming/server.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "id": "7e5d997b", 7 | "metadata": {}, 8 | "outputs": [ 9 | { 10 | "name": "stdout", 11 | "output_type": "stream", 12 | "text": [ 13 | "socket created\n", 14 | "weighting for connection\n", 15 | "connected with ('127.0.0.1', 52514) jalpa\n" 16 | ] 17 | } 18 | ], 19 | "source": [ 20 | "import socket\n", 21 | "s = socket.socket()\n", 22 | "print(\"socket created\")\n", 23 | "\n", 24 | "s.bind(('localhost', 9999))\n", 25 | "\n", 26 | "s.listen(3)\n", 27 | "\n", 28 | "print('weighting for connection')\n", 29 | "\n", 30 | "while True:\n", 31 | " c, addr = s.accept() # connection from client\n", 32 | " name = c.recv(1024).decode()\n", 33 | " print(\"connected with\", addr,name)\n", 34 | "\n", 35 | " c.send(bytes('welcome to BIT', 'utf-8'))\n", 36 | "\n", 37 | " c.close()" 38 | ] 39 | }, 40 | { 41 | "cell_type": "code", 42 | "execution_count": null, 43 | "id": "e4b3a382", 44 | "metadata": {}, 45 | "outputs": [], 46 | "source": [] 47 | } 48 | ], 49 | "metadata": { 50 | "kernelspec": { 51 | "display_name": "Python 3 (ipykernel)", 52 | "language": "python", 53 | "name": "python3" 54 | }, 55 | "language_info": { 56 | "codemirror_mode": { 57 | "name": "ipython", 58 | "version": 3 59 | }, 60 | "file_extension": ".py", 61 | "mimetype": "text/x-python", 62 | "name": "python", 63 | "nbconvert_exporter": "python", 64 | "pygments_lexer": "ipython3", 65 | "version": "3.9.12" 66 | } 67 | }, 68 | "nbformat": 4, 69 | "nbformat_minor": 5 70 | } 71 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Python Introduction 2 | Python is a high-level, versatile programming language known for its simplicity and readability, making it ideal for beginners and professionals alike. Created by Guido van Rossum and first released in 1991, Python emphasizes code readability with its clean syntax and use of indentation. It supports multiple programming paradigms, including procedural, object-oriented, and functional programming, allowing developers to tackle various tasks efficiently. 3 | 4 | Python's vast standard library and a rich ecosystem of third-party packages make it powerful for diverse applications—from web development and data science to artificial intelligence, automation, and more. Its extensive libraries like NumPy, pandas, and TensorFlow enable data analysis and machine learning, while Django and Flask are popular for web development. Python is open-source, has a large, supportive community, and runs on multiple platforms, making it a preferred choice for rapid development, prototyping, and complex, scalable projects across industries. 5 | 6 | To set up **Anaconda Navigator** with **Visual Studio Code (VS Code)** as your code editor, follow these steps: 7 | 8 | ### Step 1: Install Anaconda and VS Code 9 | 1. **Install Anaconda**: 10 | - Download and install Anaconda from the [Anaconda website](https://www.anaconda.com/products/distribution). 11 | - Follow the installation instructions for your operating system. 12 | 13 | 2. **Install Visual Studio Code**: 14 | - Download and install VS Code from the [VS Code website](https://code.visualstudio.com/). 15 | - During installation, check the box to **Add to PATH** if prompted (this allows you to open VS Code from the command line). 16 | 17 | ### Step 2: Link VS Code to Anaconda Navigator 18 | 1. **Open Anaconda Navigator**: 19 | - Launch Anaconda Navigator from your Applications folder (macOS), Start Menu (Windows), or command line (`anaconda-navigator`). 20 | 21 | 2. **Install VS Code Extension in Anaconda Navigator**: 22 | - In the **Home** tab of Anaconda Navigator, you should see an option for **Visual Studio Code**. 23 | - If it says "Install" under VS Code, click **Install**. Anaconda will set up VS Code to work with your Anaconda environments. 24 | - If it says "Launch," VS Code is already set up with Anaconda. 25 | 26 | 3. **Create or Select an Anaconda Environment** (optional but recommended): 27 | - Go to the **Environments** tab. 28 | - You can create a new environment if you want to work with a specific Python version or isolate packages. 29 | - Click **Create**, name your environment, and choose the Python version, then click **Create**. 30 | 31 | ### Step 3: Launch VS Code from Anaconda Navigator 32 | 1. **Open VS Code**: 33 | - In the **Home** tab of Anaconda Navigator, click **Launch** under **Visual Studio Code**. 34 | - This will open VS Code with the Anaconda environment activated. 35 | 36 | 2. **Select the Python Interpreter in VS Code**: 37 | - In VS Code, press `Ctrl+Shift+P` (Windows/Linux) or `Cmd+Shift+P` (macOS) to open the Command Palette. 38 | - Type **Python: Select Interpreter** and choose the Python interpreter associated with your Anaconda environment. 39 | - This ensures that any code you run will use the Python version and packages installed in your Anaconda environment. 40 | 41 | ### Step 4: Install Additional Extensions (Optional) 42 | - To enhance Python development in VS Code, you may want to install the **Python** extension by Microsoft, which adds features like IntelliSense, debugging, and Jupyter Notebook support. 43 | - Open the **Extensions** view in VS Code (click on the square icon in the sidebar), search for "Python," and install it. 44 | 45 | ### Step 5: Run and Test Python Code 46 | 1. Open a new file in VS Code, save it with a `.py` extension, and start writing your Python code. 47 | 2. Run the code by selecting **Run Python File** in the VS Code menu or by pressing `F5` (with debugging support). 48 | 49 | By setting up VS Code with Anaconda, you can seamlessly develop, debug, and run code with the benefits of virtual environments and package management. 50 | -------------------------------------------------------------------------------- /Regular Expressions .ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "1caaa318", 6 | "metadata": {}, 7 | "source": [ 8 | "# Regular expressions (RegEx)" 9 | ] 10 | }, 11 | { 12 | "cell_type": "code", 13 | "execution_count": 2, 14 | "id": "e635e453", 15 | "metadata": {}, 16 | "outputs": [ 17 | { 18 | "name": "stdout", 19 | "output_type": "stream", 20 | "text": [ 21 | "Search successful.\n" 22 | ] 23 | } 24 | ], 25 | "source": [ 26 | "# A Regular Expression (RegEx) is a sequence of characters that defines a search pattern.\n", 27 | "import re\n", 28 | "\n", 29 | "pattern = '^a...s$'\n", 30 | "test_string = 'abyss'\n", 31 | "result = re.match(pattern, test_string)\n", 32 | "\n", 33 | "if result:\n", 34 | " print(\"Search successful.\")\n", 35 | "else:\n", 36 | " print(\"Search unsuccessful.\")" 37 | ] 38 | }, 39 | { 40 | "cell_type": "code", 41 | "execution_count": 3, 42 | "id": "b1103d95", 43 | "metadata": {}, 44 | "outputs": [ 45 | { 46 | "name": "stdout", 47 | "output_type": "stream", 48 | "text": [ 49 | "['12', '89', '34']\n" 50 | ] 51 | } 52 | ], 53 | "source": [ 54 | "# Program to extract numbers from a string\n", 55 | "# re.findall()\n", 56 | "import re\n", 57 | "\n", 58 | "string = 'hello 12 hi 89. Howdy 34'\n", 59 | "pattern = '\\d+'\n", 60 | "\n", 61 | "result = re.findall(pattern, string) \n", 62 | "print(result)" 63 | ] 64 | }, 65 | { 66 | "cell_type": "code", 67 | "execution_count": 5, 68 | "id": "1f0da34b", 69 | "metadata": {}, 70 | "outputs": [ 71 | { 72 | "name": "stdout", 73 | "output_type": "stream", 74 | "text": [ 75 | "['Seven:', ' Eighty nine:', '.']\n" 76 | ] 77 | } 78 | ], 79 | "source": [ 80 | "# re.split()\n", 81 | "\n", 82 | "import re\n", 83 | "\n", 84 | "string = 'Seven:7 Eighty nine:89.'\n", 85 | "pattern = '\\d+'\n", 86 | "\n", 87 | "result = re.split(pattern, string) \n", 88 | "print(result)\n", 89 | "\n", 90 | "# Output: ['Twelve:', ' Eighty nine:', '.']" 91 | ] 92 | }, 93 | { 94 | "cell_type": "code", 95 | "execution_count": 6, 96 | "id": "47259d13", 97 | "metadata": {}, 98 | "outputs": [ 99 | { 100 | "name": "stdout", 101 | "output_type": "stream", 102 | "text": [ 103 | "abc12de23f456\n" 104 | ] 105 | } 106 | ], 107 | "source": [ 108 | "# re.sub()\n", 109 | "# Program to remove all whitespaces\n", 110 | "import re\n", 111 | "\n", 112 | "# multiline string\n", 113 | "string = 'abc 12\\\n", 114 | "de 23 \\n f45 6'\n", 115 | "\n", 116 | "# matches all whitespace characters\n", 117 | "pattern = '\\s+'\n", 118 | "\n", 119 | "# empty string\n", 120 | "replace = ''\n", 121 | "\n", 122 | "new_string = re.sub(pattern, replace, string) \n", 123 | "print(new_string)\n", 124 | "\n", 125 | "# Output: abc12de23f456" 126 | ] 127 | }, 128 | { 129 | "cell_type": "code", 130 | "execution_count": null, 131 | "id": "5bfc9318", 132 | "metadata": {}, 133 | "outputs": [], 134 | "source": [] 135 | }, 136 | { 137 | "cell_type": "raw", 138 | "id": "84634901", 139 | "metadata": {}, 140 | "source": [ 141 | "# The re.subn() is similar to re.sub() except it returns a tuple of 2 items containing the new string and the number of substitutions made." 142 | ] 143 | }, 144 | { 145 | "cell_type": "code", 146 | "execution_count": 7, 147 | "id": "27189738", 148 | "metadata": {}, 149 | "outputs": [ 150 | { 151 | "name": "stdout", 152 | "output_type": "stream", 153 | "text": [ 154 | "('abc12de23f456', 4)\n" 155 | ] 156 | } 157 | ], 158 | "source": [ 159 | "\n", 160 | "# Program to remove all whitespaces\n", 161 | "import re\n", 162 | "\n", 163 | "# multiline string\n", 164 | "string = 'abc 12\\\n", 165 | "de 23 \\n f45 6'\n", 166 | "\n", 167 | "# matches all whitespace characters\n", 168 | "pattern = '\\s+'\n", 169 | "\n", 170 | "# empty string\n", 171 | "replace = ''\n", 172 | "\n", 173 | "new_string = re.subn(pattern, replace, string) \n", 174 | "print(new_string)" 175 | ] 176 | }, 177 | { 178 | "cell_type": "code", 179 | "execution_count": 12, 180 | "id": "1737872d", 181 | "metadata": {}, 182 | "outputs": [ 183 | { 184 | "name": "stdout", 185 | "output_type": "stream", 186 | "text": [ 187 | "Pattern found inside the string\n" 188 | ] 189 | } 190 | ], 191 | "source": [ 192 | "import re\n", 193 | "\n", 194 | "string = \"Python is fun\"\n", 195 | "\n", 196 | "# check if 'Python' is at the beginning\n", 197 | "match = re.search('\\APython', string)\n", 198 | "\n", 199 | "if match:\n", 200 | " print(\"Pattern found inside the string\")\n", 201 | "else:\n", 202 | " print(\"Pattern not found\")" 203 | ] 204 | }, 205 | { 206 | "cell_type": "code", 207 | "execution_count": 18, 208 | "id": "075ad417", 209 | "metadata": {}, 210 | "outputs": [ 211 | { 212 | "name": "stdout", 213 | "output_type": "stream", 214 | "text": [ 215 | "801 35\n" 216 | ] 217 | } 218 | ], 219 | "source": [ 220 | "import re\n", 221 | "\n", 222 | "string = '39801 356, 2102 1111'\n", 223 | "\n", 224 | "# Three digit number followed by space followed by two digit number\n", 225 | "pattern = '(\\d{3}) (\\d{2})'\n", 226 | "\n", 227 | "# match variable contains a Match object.\n", 228 | "match = re.search(pattern, string) \n", 229 | "\n", 230 | "if match:\n", 231 | " print(match.group())\n", 232 | "else:\n", 233 | " print(\"pattern not found\")" 234 | ] 235 | }, 236 | { 237 | "cell_type": "code", 238 | "execution_count": 19, 239 | "id": "d44f8098", 240 | "metadata": {}, 241 | "outputs": [], 242 | "source": [ 243 | "import re # finditer" 244 | ] 245 | }, 246 | { 247 | "cell_type": "code", 248 | "execution_count": 21, 249 | "id": "47a54ea7", 250 | "metadata": {}, 251 | "outputs": [ 252 | { 253 | "name": "stdout", 254 | "output_type": "stream", 255 | "text": [ 256 | "There is inform\n" 257 | ] 258 | } 259 | ], 260 | "source": [ 261 | "if re.search(\"inform\", \" we need to inform him with latest information\"):\n", 262 | " print(\"There is inform\")" 263 | ] 264 | }, 265 | { 266 | "cell_type": "code", 267 | "execution_count": 22, 268 | "id": "027dea9a", 269 | "metadata": {}, 270 | "outputs": [ 271 | { 272 | "name": "stdout", 273 | "output_type": "stream", 274 | "text": [ 275 | "inform\n", 276 | "inform\n" 277 | ] 278 | } 279 | ], 280 | "source": [ 281 | "allinform = re.findall(\"inform\", \" we need to inform him with latest information\")\n", 282 | "for i in allinform:\n", 283 | " print(i)" 284 | ] 285 | }, 286 | { 287 | "cell_type": "code", 288 | "execution_count": 23, 289 | "id": "f101742b", 290 | "metadata": {}, 291 | "outputs": [ 292 | { 293 | "name": "stdout", 294 | "output_type": "stream", 295 | "text": [ 296 | "(11, 17)\n", 297 | "(38, 44)\n" 298 | ] 299 | } 300 | ], 301 | "source": [ 302 | "str = \"we need to inform him with the latest information\"\n", 303 | "for i in re.finditer(\"inform\",str):\n", 304 | " loctup = i.span()\n", 305 | " print(loctup)" 306 | ] 307 | }, 308 | { 309 | "cell_type": "code", 310 | "execution_count": 27, 311 | "id": "8a4c22a4", 312 | "metadata": {}, 313 | "outputs": [ 314 | { 315 | "name": "stdout", 316 | "output_type": "stream", 317 | "text": [ 318 | "sat\n", 319 | "hat\n", 320 | "mat\n" 321 | ] 322 | } 323 | ], 324 | "source": [ 325 | "str = \"sat, hat, mat, bat, pat\"\n", 326 | "allstr = re.findall(\"[shm]at\",str)\n", 327 | "for i in allstr:\n", 328 | " print(i)" 329 | ] 330 | }, 331 | { 332 | "cell_type": "code", 333 | "execution_count": 30, 334 | "id": "745bc995", 335 | "metadata": {}, 336 | "outputs": [ 337 | { 338 | "name": "stdout", 339 | "output_type": "stream", 340 | "text": [ 341 | "sat\n", 342 | "bat\n", 343 | "pat\n" 344 | ] 345 | } 346 | ], 347 | "source": [ 348 | "str = \"sat, hat, mat, bat, pat\"\n", 349 | "allstr = re.findall(\"[^h-m]at\",str) # [h-m]\n", 350 | "\n", 351 | "for i in allstr:\n", 352 | " print(i)" 353 | ] 354 | }, 355 | { 356 | "cell_type": "code", 357 | "execution_count": 32, 358 | "id": "a8d3f8d8", 359 | "metadata": {}, 360 | "outputs": [ 361 | { 362 | "name": "stdout", 363 | "output_type": "stream", 364 | "text": [ 365 | "hat food mat pat\n" 366 | ] 367 | } 368 | ], 369 | "source": [ 370 | "food = \"hat rat mat pat\"\n", 371 | "\n", 372 | "reg = re.compile(\"[r]at\")\n", 373 | "\n", 374 | "food = reg.sub(\"food\", food)\n", 375 | "\n", 376 | "print(food)" 377 | ] 378 | }, 379 | { 380 | "cell_type": "code", 381 | "execution_count": 37, 382 | "id": "eaf5b9c1", 383 | "metadata": {}, 384 | "outputs": [ 385 | { 386 | "name": "stdout", 387 | "output_type": "stream", 388 | "text": [ 389 | "\n" 390 | ] 391 | } 392 | ], 393 | "source": [ 394 | "randstr = \"here is \\\\jalpa\"\n", 395 | "#print(randstr)\n", 396 | "print(re.search(r\"\\\\jalpa\", randstr))" 397 | ] 398 | }, 399 | { 400 | "cell_type": "code", 401 | "execution_count": 40, 402 | "id": "12fdc0ce", 403 | "metadata": {}, 404 | "outputs": [ 405 | { 406 | "name": "stdout", 407 | "output_type": "stream", 408 | "text": [ 409 | "Keep the blue \n", 410 | "flag flying high \n", 411 | "chelsea\n", 412 | "Keep the blue flag flying high chelsea\n" 413 | ] 414 | } 415 | ], 416 | "source": [ 417 | "randstr = '''Keep the blue \n", 418 | "flag flying high \n", 419 | "chelsea'''\n", 420 | "print(randstr)\n", 421 | "\n", 422 | "reg = re.compile(\"\\n\") # \\b bachspace /f formfeed \\r carrige return \\t tab \\v vertical\n", 423 | "\n", 424 | "randstr = reg.sub(\" \", randstr)\n", 425 | "\n", 426 | "print(randstr)" 427 | ] 428 | }, 429 | { 430 | "cell_type": "code", 431 | "execution_count": 51, 432 | "id": "90c94c10", 433 | "metadata": {}, 434 | "outputs": [ 435 | { 436 | "name": "stdout", 437 | "output_type": "stream", 438 | "text": [ 439 | "Matches : 5\n" 440 | ] 441 | } 442 | ], 443 | "source": [ 444 | "randstr = \"12345\"\n", 445 | "print(\"Matches : \", len(re.findall(\"\\d{1}\", randstr))) #D" 446 | ] 447 | }, 448 | { 449 | "cell_type": "code", 450 | "execution_count": 53, 451 | "id": "319ef8b7", 452 | "metadata": {}, 453 | "outputs": [ 454 | { 455 | "name": "stdout", 456 | "output_type": "stream", 457 | "text": [ 458 | "Matches: 3\n" 459 | ] 460 | } 461 | ], 462 | "source": [ 463 | "num = \" 123 1234 12345 123456 1234567\"\n", 464 | "print(\"Matches: \", len(re.findall(\"\\d{5,7}\",num)))" 465 | ] 466 | }, 467 | { 468 | "cell_type": "code", 469 | "execution_count": 5, 470 | "id": "ed4102b2", 471 | "metadata": {}, 472 | "outputs": [], 473 | "source": [ 474 | "# \\w [a-zA-Z0-9_]\n", 475 | "#\\W [^a-zA-Z0-9]\n", 476 | "import re\n", 477 | "\n", 478 | "phn = \"412-555-1234\"\n", 479 | "\n", 480 | "if re.search(\"\\w{3}-\\w{3}-w{4}\",phn): # also use \\d\n", 481 | " \n", 482 | " print(\"It is a phone number\")" 483 | ] 484 | }, 485 | { 486 | "cell_type": "code", 487 | "execution_count": 2, 488 | "id": "8482e6dd", 489 | "metadata": {}, 490 | "outputs": [ 491 | { 492 | "name": "stdout", 493 | "output_type": "stream", 494 | "text": [ 495 | "(257) 563-7401\n", 496 | "(372) 587-2335\n", 497 | "(786) 713-8616\n", 498 | "(793) 151-6230\n", 499 | "(492) 709-6392\n", 500 | "(654) 393-5734\n", 501 | "(404) 960-3807\n", 502 | "(314) 244-6306\n", 503 | "(947) 278-5929\n", 504 | "(684) 579-1879\n", 505 | "(389) 737-2852\n", 506 | "(660) 663-4518\n", 507 | "(608) 265-2215\n", 508 | "(959) 119-8364\n", 509 | "(468) 353-2641\n", 510 | "(248) 675-4007\n", 511 | "(939) 353-1107\n", 512 | "(570) 873-7090\n", 513 | "(302) 259-2375\n", 514 | "(717) 450-4729\n", 515 | "(453) 391-4650\n", 516 | "(559) 104-5475\n", 517 | "(387) 142-9434\n", 518 | "(516) 745-4496\n", 519 | "(326) 677-3419\n", 520 | "(746) 679-2470\n", 521 | "(455) 430-0989\n", 522 | "(490) 936-4694\n", 523 | "(985) 834-8285\n", 524 | "(662) 661-1446\n", 525 | "(802) 668-8240\n", 526 | "(477) 768-9247\n", 527 | "(791) 239-9057\n", 528 | "(832) 109-0213\n", 529 | "(837) 196-3274\n", 530 | "(268) 442-2428\n", 531 | "(850) 676-5117\n", 532 | "(861) 546-5032\n", 533 | "(176) 805-4108\n", 534 | "(715) 912-6931\n", 535 | "(993) 554-0563\n", 536 | "(357) 616-5411\n", 537 | "(121) 347-0086\n", 538 | "(304) 506-6314\n", 539 | "(425) 288-2332\n", 540 | "(145) 987-4962\n", 541 | "(187) 582-9707\n", 542 | "(750) 558-3965\n", 543 | "(492) 467-3131\n", 544 | "(774) 914-2510\n", 545 | "(888) 106-8550\n", 546 | "(539) 567-3573\n", 547 | "(693) 337-2849\n", 548 | "(545) 604-9386\n", 549 | "(221) 156-5026\n", 550 | "(414) 876-0865\n", 551 | "(932) 726-8645\n", 552 | "(726) 710-9826\n", 553 | "(622) 594-1662\n", 554 | "(948) 600-8503\n", 555 | "(605) 900-7508\n", 556 | "(716) 977-5775\n", 557 | "(368) 239-8275\n", 558 | "(725) 342-0650\n", 559 | "(711) 993-5187\n", 560 | "(882) 399-5084\n", 561 | "(287) 755-9948\n", 562 | "(659) 551-3389\n", 563 | "(275) 730-6868\n", 564 | "(725) 757-4047\n", 565 | "(314) 882-1496\n", 566 | "(639) 360-7590\n", 567 | "(168) 222-1592\n", 568 | "(896) 303-1164\n", 569 | "(203) 982-6130\n", 570 | "(906) 217-1470\n", 571 | "(614) 514-1269\n", 572 | "(763) 409-5446\n", 573 | "(836) 292-5324\n", 574 | "(926) 709-3295\n", 575 | "(963) 356-9268\n", 576 | "(736) 522-8584\n", 577 | "(410) 483-0352\n", 578 | "(252) 204-1434\n", 579 | "(874) 886-4174\n", 580 | "(581) 379-7573\n", 581 | "(983) 632-8597\n", 582 | "(295) 983-3476\n", 583 | "(873) 392-8802\n", 584 | "(360) 669-3923\n", 585 | "(840) 987-9449\n", 586 | "(422) 517-6053\n", 587 | "(126) 940-2753\n", 588 | "(427) 930-5255\n", 589 | "(689) 721-5145\n", 590 | "(676) 334-2174\n", 591 | "(437) 994-5270\n", 592 | "(564) 908-6970\n", 593 | "(577) 333-6244\n", 594 | "(655) 840-6139\n" 595 | ] 596 | } 597 | ], 598 | "source": [ 599 | "import urllib.request\n", 600 | "from re import findall\n", 601 | "\n", 602 | "url = \"http://www.summet.com/dmsi/html/codesamples/addresses.html\"\n", 603 | "\n", 604 | "response = urllib.request.urlopen(url)\n", 605 | "\n", 606 | "html = response.read()\n", 607 | "\n", 608 | "htmlstr = html.decode()\n", 609 | "\n", 610 | "data = findall(\"\\(\\d{3}\\) \\d{3}-\\d{4}\",htmlstr)\n", 611 | "\n", 612 | "for i in data:\n", 613 | " print(i)" 614 | ] 615 | }, 616 | { 617 | "cell_type": "code", 618 | "execution_count": null, 619 | "id": "4ae9646a", 620 | "metadata": {}, 621 | "outputs": [], 622 | "source": [] 623 | } 624 | ], 625 | "metadata": { 626 | "kernelspec": { 627 | "display_name": "Python 3 (ipykernel)", 628 | "language": "python", 629 | "name": "python3" 630 | }, 631 | "language_info": { 632 | "codemirror_mode": { 633 | "name": "ipython", 634 | "version": 3 635 | }, 636 | "file_extension": ".py", 637 | "mimetype": "text/x-python", 638 | "name": "python", 639 | "nbconvert_exporter": "python", 640 | "pygments_lexer": "ipython3", 641 | "version": "3.9.12" 642 | } 643 | }, 644 | "nbformat": 4, 645 | "nbformat_minor": 5 646 | } 647 | -------------------------------------------------------------------------------- /Shallow Copy Vs deep copy.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "id": "a901dc86", 7 | "metadata": {}, 8 | "outputs": [], 9 | "source": [ 10 | "## = , copy(), deepcopy()" 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": 2, 16 | "id": "c05562c8", 17 | "metadata": {}, 18 | "outputs": [], 19 | "source": [ 20 | "list1 = [1,2,3,4]\n", 21 | "list2 = list1 ## = mean same memmory location" 22 | ] 23 | }, 24 | { 25 | "cell_type": "code", 26 | "execution_count": 3, 27 | "id": "644a1e55", 28 | "metadata": {}, 29 | "outputs": [ 30 | { 31 | "data": { 32 | "text/plain": [ 33 | "([1, 2, 3, 4], [1, 2, 3, 4])" 34 | ] 35 | }, 36 | "execution_count": 3, 37 | "metadata": {}, 38 | "output_type": "execute_result" 39 | } 40 | ], 41 | "source": [ 42 | "list1, list2" 43 | ] 44 | }, 45 | { 46 | "cell_type": "code", 47 | "execution_count": 4, 48 | "id": "0937f7cc", 49 | "metadata": {}, 50 | "outputs": [], 51 | "source": [ 52 | "list2[1] = 100" 53 | ] 54 | }, 55 | { 56 | "cell_type": "code", 57 | "execution_count": 5, 58 | "id": "deab0ade", 59 | "metadata": {}, 60 | "outputs": [ 61 | { 62 | "name": "stdout", 63 | "output_type": "stream", 64 | "text": [ 65 | "2137159712000\n" 66 | ] 67 | } 68 | ], 69 | "source": [ 70 | "print(id(list2))" 71 | ] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "execution_count": 6, 76 | "id": "6db8bcbc", 77 | "metadata": {}, 78 | "outputs": [ 79 | { 80 | "name": "stdout", 81 | "output_type": "stream", 82 | "text": [ 83 | "2137159712000\n" 84 | ] 85 | } 86 | ], 87 | "source": [ 88 | "print(id(list1))" 89 | ] 90 | }, 91 | { 92 | "cell_type": "code", 93 | "execution_count": 1, 94 | "id": "6cda6cdb", 95 | "metadata": {}, 96 | "outputs": [ 97 | { 98 | "name": "stdout", 99 | "output_type": "stream", 100 | "text": [ 101 | "[[1, 2, 3], [4, 5, 6], [7, 8, 9]]\n", 102 | "2418355079488\n", 103 | "New List: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]\n", 104 | "ID of New List: 2418355079488\n" 105 | ] 106 | } 107 | ], 108 | "source": [ 109 | "old_list = [[1, 2, 3], [4, 5, 6], [7, 8, 'a']]\n", 110 | "new_list = old_list\n", 111 | "\n", 112 | "new_list[2][2] = 9\n", 113 | "\n", 114 | "print(old_list)\n", 115 | "print(id(old_list))\n", 116 | "\n", 117 | "print('New List:', new_list)\n", 118 | "print('ID of New List:', id(new_list))" 119 | ] 120 | }, 121 | { 122 | "cell_type": "code", 123 | "execution_count": 17, 124 | "id": "23b0c7fb", 125 | "metadata": {}, 126 | "outputs": [], 127 | "source": [ 128 | "## copy\n", 129 | "## shallow copy\n", 130 | "list1 = [1,2,3,4]\n", 131 | "list2 = list1.copy() ## make a different memmory location" 132 | ] 133 | }, 134 | { 135 | "cell_type": "code", 136 | "execution_count": 14, 137 | "id": "f5573861", 138 | "metadata": {}, 139 | "outputs": [], 140 | "source": [ 141 | "#list2[1] = 100" 142 | ] 143 | }, 144 | { 145 | "cell_type": "code", 146 | "execution_count": 18, 147 | "id": "ac5ed35e", 148 | "metadata": {}, 149 | "outputs": [ 150 | { 151 | "data": { 152 | "text/plain": [ 153 | "[1, 2, 3, 4]" 154 | ] 155 | }, 156 | "execution_count": 18, 157 | "metadata": {}, 158 | "output_type": "execute_result" 159 | } 160 | ], 161 | "source": [ 162 | "list2" 163 | ] 164 | }, 165 | { 166 | "cell_type": "code", 167 | "execution_count": 19, 168 | "id": "4cb342cf", 169 | "metadata": {}, 170 | "outputs": [ 171 | { 172 | "data": { 173 | "text/plain": [ 174 | "[1, 2, 3, 4]" 175 | ] 176 | }, 177 | "execution_count": 19, 178 | "metadata": {}, 179 | "output_type": "execute_result" 180 | } 181 | ], 182 | "source": [ 183 | "list1" 184 | ] 185 | }, 186 | { 187 | "cell_type": "code", 188 | "execution_count": 20, 189 | "id": "099e1a97", 190 | "metadata": {}, 191 | "outputs": [ 192 | { 193 | "name": "stdout", 194 | "output_type": "stream", 195 | "text": [ 196 | "2137148240320\n", 197 | "2137130794496\n" 198 | ] 199 | } 200 | ], 201 | "source": [ 202 | "print(id(list1))\n", 203 | "print(id(list2))" 204 | ] 205 | }, 206 | { 207 | "cell_type": "code", 208 | "execution_count": null, 209 | "id": "4a210580", 210 | "metadata": {}, 211 | "outputs": [], 212 | "source": [ 213 | "## Shallow copy nested list" 214 | ] 215 | }, 216 | { 217 | "cell_type": "code", 218 | "execution_count": 21, 219 | "id": "315b78f1", 220 | "metadata": {}, 221 | "outputs": [], 222 | "source": [ 223 | "list1 = [[1,2,3,4],[3,43,5,6]]\n", 224 | "list2 = list1.copy()" 225 | ] 226 | }, 227 | { 228 | "cell_type": "code", 229 | "execution_count": 22, 230 | "id": "b8de2c0c", 231 | "metadata": {}, 232 | "outputs": [ 233 | { 234 | "data": { 235 | "text/plain": [ 236 | "[[1, 2, 3, 4], [3, 43, 5, 6]]" 237 | ] 238 | }, 239 | "execution_count": 22, 240 | "metadata": {}, 241 | "output_type": "execute_result" 242 | } 243 | ], 244 | "source": [ 245 | "list1" 246 | ] 247 | }, 248 | { 249 | "cell_type": "code", 250 | "execution_count": 23, 251 | "id": "f8e45c59", 252 | "metadata": {}, 253 | "outputs": [ 254 | { 255 | "data": { 256 | "text/plain": [ 257 | "[[1, 2, 3, 4], [3, 43, 5, 6]]" 258 | ] 259 | }, 260 | "execution_count": 23, 261 | "metadata": {}, 262 | "output_type": "execute_result" 263 | } 264 | ], 265 | "source": [ 266 | "list2" 267 | ] 268 | }, 269 | { 270 | "cell_type": "code", 271 | "execution_count": 24, 272 | "id": "a3e8f49f", 273 | "metadata": {}, 274 | "outputs": [], 275 | "source": [ 276 | "list1[1][0] = 100" 277 | ] 278 | }, 279 | { 280 | "cell_type": "code", 281 | "execution_count": 25, 282 | "id": "3dade8b6", 283 | "metadata": {}, 284 | "outputs": [ 285 | { 286 | "data": { 287 | "text/plain": [ 288 | "[[1, 2, 3, 4], [100, 43, 5, 6]]" 289 | ] 290 | }, 291 | "execution_count": 25, 292 | "metadata": {}, 293 | "output_type": "execute_result" 294 | } 295 | ], 296 | "source": [ 297 | "list1" 298 | ] 299 | }, 300 | { 301 | "cell_type": "code", 302 | "execution_count": 26, 303 | "id": "37114208", 304 | "metadata": {}, 305 | "outputs": [ 306 | { 307 | "data": { 308 | "text/plain": [ 309 | "[[1, 2, 3, 4], [100, 43, 5, 6]]" 310 | ] 311 | }, 312 | "execution_count": 26, 313 | "metadata": {}, 314 | "output_type": "execute_result" 315 | } 316 | ], 317 | "source": [ 318 | "list2" 319 | ] 320 | }, 321 | { 322 | "cell_type": "code", 323 | "execution_count": 27, 324 | "id": "44462aa6", 325 | "metadata": {}, 326 | "outputs": [ 327 | { 328 | "name": "stdout", 329 | "output_type": "stream", 330 | "text": [ 331 | "2137159669696\n", 332 | "2137159711296\n" 333 | ] 334 | } 335 | ], 336 | "source": [ 337 | "print(id(list1))\n", 338 | "print(id(list2))" 339 | ] 340 | }, 341 | { 342 | "cell_type": "code", 343 | "execution_count": 28, 344 | "id": "6ab6c5ad", 345 | "metadata": {}, 346 | "outputs": [ 347 | { 348 | "name": "stdout", 349 | "output_type": "stream", 350 | "text": [ 351 | "Old list: 2137159830080\n", 352 | "New list: 2137159613888\n" 353 | ] 354 | } 355 | ], 356 | "source": [ 357 | "## deep copy\n", 358 | "import copy\n", 359 | "\n", 360 | "old_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]\n", 361 | "new_list = copy.deepcopy(old_list)\n", 362 | "\n", 363 | "print(\"Old list:\", id(old_list))\n", 364 | "print(\"New list:\", id(new_list))" 365 | ] 366 | }, 367 | { 368 | "cell_type": "code", 369 | "execution_count": 29, 370 | "id": "e2578c3e", 371 | "metadata": {}, 372 | "outputs": [ 373 | { 374 | "name": "stdout", 375 | "output_type": "stream", 376 | "text": [ 377 | "Old list: [[1, 2, 3], [4, 5, 6], [7, 8, 100]]\n", 378 | "New list: [[1, 2, 3], [4, 5, 6], [7, 8, 100]]\n", 379 | "Old list: 2137159637696\n", 380 | "New list: 2137159636352\n" 381 | ] 382 | } 383 | ], 384 | "source": [ 385 | "import copy\n", 386 | "\n", 387 | "old_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]\n", 388 | "new_list = copy.copy(old_list)\n", 389 | "new_list[2][2] = 100\n", 390 | "print(\"Old list:\", old_list)\n", 391 | "print(\"New list:\", new_list)\n", 392 | "print(\"Old list:\", id(old_list))\n", 393 | "print(\"New list:\", id(new_list))" 394 | ] 395 | } 396 | ], 397 | "metadata": { 398 | "kernelspec": { 399 | "display_name": "Python 3 (ipykernel)", 400 | "language": "python", 401 | "name": "python3" 402 | }, 403 | "language_info": { 404 | "codemirror_mode": { 405 | "name": "ipython", 406 | "version": 3 407 | }, 408 | "file_extension": ".py", 409 | "mimetype": "text/x-python", 410 | "name": "python", 411 | "nbconvert_exporter": "python", 412 | "pygments_lexer": "ipython3", 413 | "version": "3.9.12" 414 | } 415 | }, 416 | "nbformat": 4, 417 | "nbformat_minor": 5 418 | } 419 | -------------------------------------------------------------------------------- /Try and Exception Handling.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 18, 6 | "id": "0a2ad058", 7 | "metadata": {}, 8 | "outputs": [ 9 | { 10 | "ename": "SyntaxError", 11 | "evalue": "invalid syntax (Temp/ipykernel_11756/1921869338.py, line 4)", 12 | "output_type": "error", 13 | "traceback": [ 14 | "\u001b[1;36m File \u001b[1;32m\"C:\\Users\\bitsj\\AppData\\Local\\Temp/ipykernel_11756/1921869338.py\"\u001b[1;36m, line \u001b[1;32m4\u001b[0m\n\u001b[1;33m print(\"hello world\")\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n" 15 | ] 16 | } 17 | ], 18 | "source": [ 19 | "try:\n", 20 | " a=8\n", 21 | " a/0\n", 22 | " print(\"hello world\")\n", 23 | "except Exception as e:\n", 24 | " print(e)" 25 | ] 26 | }, 27 | { 28 | "cell_type": "code", 29 | "execution_count": 16, 30 | "id": "3739d6cc", 31 | "metadata": {}, 32 | "outputs": [ 33 | { 34 | "ename": "ZeroDivisionError", 35 | "evalue": "division by zero", 36 | "output_type": "error", 37 | "traceback": [ 38 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 39 | "\u001b[1;31mZeroDivisionError\u001b[0m Traceback (most recent call last)", 40 | "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_11756/3777648117.py\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0ma\u001b[0m\u001b[1;33m/\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 2\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"hello world\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", 41 | "\u001b[1;31mZeroDivisionError\u001b[0m: division by zero" 42 | ] 43 | } 44 | ], 45 | "source": [ 46 | "a/0\n", 47 | "print(\"hello world\")" 48 | ] 49 | }, 50 | { 51 | "cell_type": "code", 52 | "execution_count": 2, 53 | "id": "aa23daa5", 54 | "metadata": {}, 55 | "outputs": [ 56 | { 57 | "name": "stdout", 58 | "output_type": "stream", 59 | "text": [ 60 | "hsja\n" 61 | ] 62 | }, 63 | { 64 | "ename": "ValueError", 65 | "evalue": "invalid literal for int() with base 10: 'hsja'", 66 | "output_type": "error", 67 | "traceback": [ 68 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 69 | "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", 70 | "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_11756/2305843072.py\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0ma\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0minput\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 71 | "\u001b[1;31mValueError\u001b[0m: invalid literal for int() with base 10: 'hsja'" 72 | ] 73 | } 74 | ], 75 | "source": [ 76 | "a = int(input())" 77 | ] 78 | }, 79 | { 80 | "cell_type": "code", 81 | "execution_count": 14, 82 | "id": "fe5e3597", 83 | "metadata": {}, 84 | "outputs": [ 85 | { 86 | "name": "stdout", 87 | "output_type": "stream", 88 | "text": [ 89 | "hh\n", 90 | "invalid literal for int() with base 10: 'hh'\n" 91 | ] 92 | } 93 | ], 94 | "source": [ 95 | "try:\n", 96 | " a = int(input())\n", 97 | "except Exception as e:\n", 98 | " print(e)" 99 | ] 100 | }, 101 | { 102 | "cell_type": "code", 103 | "execution_count": 12, 104 | "id": "872efbe4", 105 | "metadata": {}, 106 | "outputs": [ 107 | { 108 | "name": "stdout", 109 | "output_type": "stream", 110 | "text": [ 111 | "can only concatenate list (not \"int\") to list\n" 112 | ] 113 | } 114 | ], 115 | "source": [ 116 | "try:\n", 117 | " l = [2,3,4,5,6]\n", 118 | " for i in range(len(l+3)):\n", 119 | " print(l)\n", 120 | "except Exception as e:\n", 121 | " print(e)" 122 | ] 123 | }, 124 | { 125 | "cell_type": "code", 126 | "execution_count": 24, 127 | "id": "3099fb36", 128 | "metadata": {}, 129 | "outputs": [ 130 | { 131 | "name": "stdout", 132 | "output_type": "stream", 133 | "text": [ 134 | "invalid mode: ' rb+'\n" 135 | ] 136 | } 137 | ], 138 | "source": [ 139 | "try:\n", 140 | " f = open('test.txt' , \" rb+\")\n", 141 | " f.write(\"fdsfds\")\n", 142 | "except Exception as e:\n", 143 | " print(e)" 144 | ] 145 | }, 146 | { 147 | "cell_type": "code", 148 | "execution_count": 25, 149 | "id": "d924b33e", 150 | "metadata": {}, 151 | "outputs": [ 152 | { 153 | "name": "stdout", 154 | "output_type": "stream", 155 | "text": [ 156 | "4\n", 157 | "5\n", 158 | "6\n", 159 | "7\n", 160 | "8\n", 161 | "8\n", 162 | "9\n", 163 | "0\n", 164 | "list index out of range\n", 165 | "fdsfdsdf dsf sfsd \n" 166 | ] 167 | } 168 | ], 169 | "source": [ 170 | "l = [4,5,6,7,8,8,9,0]\n", 171 | "try:\n", 172 | " for i in range(len(l)+1) : \n", 173 | " print(l[i])\n", 174 | "except Exception as e :\n", 175 | " print( e)\n", 176 | "print(\"fdsfdsdf dsf sfsd \")" 177 | ] 178 | }, 179 | { 180 | "cell_type": "code", 181 | "execution_count": 26, 182 | "id": "474e6fab", 183 | "metadata": {}, 184 | "outputs": [ 185 | { 186 | "name": "stdout", 187 | "output_type": "stream", 188 | "text": [ 189 | "this is my handler\n", 190 | "1\n", 191 | "2\n", 192 | "3\n", 193 | "4\n", 194 | "5\n" 195 | ] 196 | } 197 | ], 198 | "source": [ 199 | "try :\n", 200 | " f = open(\"test1\" , 'r')\n", 201 | " f.write(\"this is my code with expceptoi handling\")\n", 202 | " print(\"this is my code after write ops\")\n", 203 | "except : \n", 204 | " print(\"this is my handler\")\n", 205 | "try :\n", 206 | " l = [1,2,3,4,5]\n", 207 | " for i in l :\n", 208 | " print(i)\n", 209 | "except:\n", 210 | " print(\"this is a handler for for loop\")" 211 | ] 212 | }, 213 | { 214 | "cell_type": "code", 215 | "execution_count": 27, 216 | "id": "d40e02b9", 217 | "metadata": {}, 218 | "outputs": [ 219 | { 220 | "name": "stdout", 221 | "output_type": "stream", 222 | "text": [ 223 | "kkl\n", 224 | "invalid literal for int() with base 10: 'kkl'\n" 225 | ] 226 | } 227 | ], 228 | "source": [ 229 | "try :\n", 230 | " d= {\"key1\" : \"sudh\" , \"key2\" : [1,2,3,4,5] , \"key3\" : (4,5,6,7,78)}\n", 231 | " d[\"key4\"] = int(input())\n", 232 | "except Exception as jak :\n", 233 | " print(jak)" 234 | ] 235 | }, 236 | { 237 | "cell_type": "code", 238 | "execution_count": 29, 239 | "id": "b630ae53", 240 | "metadata": {}, 241 | "outputs": [ 242 | { 243 | "name": "stdout", 244 | "output_type": "stream", 245 | "text": [ 246 | "8\n", 247 | "[Errno 2] No such file or directory: 'test2'\n" 248 | ] 249 | } 250 | ], 251 | "source": [ 252 | "try :\n", 253 | " d= {\"key1\" : \"sudh\" , \"key2\" : [1,2,3,4,5] , \"key3\" : (4,5,6,7,78)}\n", 254 | " d[\"key4\"] = int(input())\n", 255 | " f = open(\"test2\" , \"r\")\n", 256 | "\n", 257 | "except ValueError as xyz :\n", 258 | " print(xyz)\n", 259 | "except FileNotFoundError as e :\n", 260 | " print(e)\n", 261 | "except Exception as ee :\n", 262 | " print(\"this is my exceptoin class \" , ee )" 263 | ] 264 | }, 265 | { 266 | "cell_type": "code", 267 | "execution_count": 30, 268 | "id": "1a0bdd26", 269 | "metadata": {}, 270 | "outputs": [ 271 | { 272 | "name": "stdout", 273 | "output_type": "stream", 274 | "text": [ 275 | "this will handle an error [Errno 2] No such file or directory: 'test5.txt'\n" 276 | ] 277 | } 278 | ], 279 | "source": [ 280 | "try :\n", 281 | " f = open(\"test5.txt\" , \"r\")\n", 282 | " f.write(\"this is my code in try \")\n", 283 | "except Exception as e :\n", 284 | " print(\"this will handle an error \" ,e) \n", 285 | "else : \n", 286 | " print(\"this will execute once my try block will be executed succesfully\")\n", 287 | " f.close()" 288 | ] 289 | }, 290 | { 291 | "cell_type": "code", 292 | "execution_count": 31, 293 | "id": "8fc5f09b", 294 | "metadata": {}, 295 | "outputs": [ 296 | { 297 | "name": "stdout", 298 | "output_type": "stream", 299 | "text": [ 300 | "do this on succesfull eectuion of try block \n", 301 | "do this for sure\n", 302 | "handle this \n", 303 | "it will come to this block for sure \n" 304 | ] 305 | } 306 | ], 307 | "source": [ 308 | "try :\n", 309 | " f = open(\"test43.txt\" , 'w')\n", 310 | "except Exception as e : \n", 311 | " print(\"sdfsdfsfsdfsfds\" , e)\n", 312 | "else:\n", 313 | " print(\"do this on succesfull eectuion of try block \")\n", 314 | "finally : \n", 315 | " print(\"do this for sure\")\n", 316 | " try : \n", 317 | " \n", 318 | " f = open(\"dfsfssdf\" , 'r')\n", 319 | " except : \n", 320 | " print(\"handle this \")\n", 321 | " finally:\n", 322 | " print(\"it will come to this block for sure \")" 323 | ] 324 | }, 325 | { 326 | "cell_type": "code", 327 | "execution_count": 38, 328 | "id": "4428fa55", 329 | "metadata": {}, 330 | "outputs": [], 331 | "source": [ 332 | "def askint():\n", 333 | " try :\n", 334 | " a = int(input())\n", 335 | " return a \n", 336 | " except Exception as e :\n", 337 | " print(e )" 338 | ] 339 | }, 340 | { 341 | "cell_type": "code", 342 | "execution_count": 43, 343 | "id": "a80efb3e", 344 | "metadata": {}, 345 | "outputs": [ 346 | { 347 | "name": "stdout", 348 | "output_type": "stream", 349 | "text": [ 350 | "kjkl\n", 351 | "invalid literal for int() with base 10: 'kjkl'\n" 352 | ] 353 | } 354 | ], 355 | "source": [ 356 | "askint()" 357 | ] 358 | }, 359 | { 360 | "cell_type": "code", 361 | "execution_count": 54, 362 | "id": "c683633c", 363 | "metadata": {}, 364 | "outputs": [], 365 | "source": [ 366 | "def fun1():\n", 367 | " \"\"\" in this program interger value is added\"\"\"\n", 368 | " flag=True\n", 369 | " while flag:\n", 370 | " try:\n", 371 | " a=int(input(\"enter the interger:\"))\n", 372 | " if type(a)==int:\n", 373 | " return \"yes, you have entered an integer\"\n", 374 | " flag=False\n", 375 | " \n", 376 | " \n", 377 | " except Exception as e:\n", 378 | " print(\"Nah, you did not enter an integer, please enter an integer. ERROR:\",e)" 379 | ] 380 | }, 381 | { 382 | "cell_type": "code", 383 | "execution_count": 55, 384 | "id": "0b777bda", 385 | "metadata": {}, 386 | "outputs": [ 387 | { 388 | "name": "stdout", 389 | "output_type": "stream", 390 | "text": [ 391 | "enter the interger:6\n" 392 | ] 393 | }, 394 | { 395 | "data": { 396 | "text/plain": [ 397 | "'yes, you have entered an integer'" 398 | ] 399 | }, 400 | "execution_count": 55, 401 | "metadata": {}, 402 | "output_type": "execute_result" 403 | } 404 | ], 405 | "source": [ 406 | "fun1()" 407 | ] 408 | }, 409 | { 410 | "cell_type": "code", 411 | "execution_count": 52, 412 | "id": "03414836", 413 | "metadata": {}, 414 | "outputs": [], 415 | "source": [ 416 | "def integer():\n", 417 | " \"\"\"Function asks for an integer unless provided otherwise\"\"\"\n", 418 | " while True:\n", 419 | " \n", 420 | " try:\n", 421 | " x = int(input())\n", 422 | " break\n", 423 | " except ValueError as e:\n", 424 | " print(\"enter the input again\",e)" 425 | ] 426 | }, 427 | { 428 | "cell_type": "code", 429 | "execution_count": 53, 430 | "id": "5de009b5", 431 | "metadata": {}, 432 | "outputs": [ 433 | { 434 | "name": "stdout", 435 | "output_type": "stream", 436 | "text": [ 437 | "7\n" 438 | ] 439 | } 440 | ], 441 | "source": [ 442 | "integer()" 443 | ] 444 | }, 445 | { 446 | "cell_type": "code", 447 | "execution_count": 69, 448 | "id": "c8fa4914", 449 | "metadata": {}, 450 | "outputs": [], 451 | "source": [ 452 | "def getint(p):\n", 453 | " while True:\n", 454 | " try:\n", 455 | " value = int(input(p))\n", 456 | " except ValueError:\n", 457 | " print(\"Sorry, I didn't understand that.\")\n", 458 | " continue\n", 459 | "\n", 460 | " if value < 0:\n", 461 | " print(\"Sorry, your response must not be negative.\")\n", 462 | " continue\n", 463 | " else:\n", 464 | " break\n", 465 | " return value" 466 | ] 467 | }, 468 | { 469 | "cell_type": "code", 470 | "execution_count": 70, 471 | "id": "8b4412e1", 472 | "metadata": {}, 473 | "outputs": [ 474 | { 475 | "name": "stdout", 476 | "output_type": "stream", 477 | "text": [ 478 | "-1-2\n", 479 | "Sorry, your response must not be negative.\n" 480 | ] 481 | }, 482 | { 483 | "ename": "KeyboardInterrupt", 484 | "evalue": "Interrupted by user", 485 | "output_type": "error", 486 | "traceback": [ 487 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 488 | "\u001b[1;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)", 489 | "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_11756/3662852128.py\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mgetint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m-\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 490 | "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_11756/29585090.py\u001b[0m in \u001b[0;36mgetint\u001b[1;34m(p)\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[1;32mwhile\u001b[0m \u001b[1;32mTrue\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 4\u001b[1;33m \u001b[0mvalue\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0minput\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mp\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 5\u001b[0m \u001b[1;32mexcept\u001b[0m \u001b[0mValueError\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 6\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Sorry, I didn't understand that.\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", 491 | "\u001b[1;32mC:\\ProgramData\\Anaconda3\\lib\\site-packages\\ipykernel\\kernelbase.py\u001b[0m in \u001b[0;36mraw_input\u001b[1;34m(self, prompt)\u001b[0m\n\u001b[0;32m 1004\u001b[0m \u001b[1;34m\"raw_input was called, but this frontend does not support input requests.\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 1005\u001b[0m )\n\u001b[1;32m-> 1006\u001b[1;33m return self._input_request(\n\u001b[0m\u001b[0;32m 1007\u001b[0m \u001b[0mstr\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mprompt\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 1008\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_parent_ident\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m\"shell\"\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", 492 | "\u001b[1;32mC:\\ProgramData\\Anaconda3\\lib\\site-packages\\ipykernel\\kernelbase.py\u001b[0m in \u001b[0;36m_input_request\u001b[1;34m(self, prompt, ident, parent, password)\u001b[0m\n\u001b[0;32m 1049\u001b[0m \u001b[1;32mexcept\u001b[0m \u001b[0mKeyboardInterrupt\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 1050\u001b[0m \u001b[1;31m# re-raise KeyboardInterrupt, to truncate traceback\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1051\u001b[1;33m \u001b[1;32mraise\u001b[0m \u001b[0mKeyboardInterrupt\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Interrupted by user\"\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32mfrom\u001b[0m \u001b[1;32mNone\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 1052\u001b[0m \u001b[1;32mexcept\u001b[0m \u001b[0mException\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 1053\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mlog\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mwarning\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Invalid Message:\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mexc_info\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mTrue\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", 493 | "\u001b[1;31mKeyboardInterrupt\u001b[0m: Interrupted by user" 494 | ] 495 | } 496 | ], 497 | "source": [ 498 | "getint(-1)" 499 | ] 500 | }, 501 | { 502 | "cell_type": "code", 503 | "execution_count": 4, 504 | "id": "8e45da35", 505 | "metadata": {}, 506 | "outputs": [], 507 | "source": [ 508 | "def test(a) :\n", 509 | " if a == \"sjs\" :\n", 510 | " raise ZeroDivisionError(\"you have entered a negative value dfsa adsfdsasaf afd\" , a)\n", 511 | " return a " 512 | ] 513 | }, 514 | { 515 | "cell_type": "code", 516 | "execution_count": 5, 517 | "id": "aafc44ec", 518 | "metadata": {}, 519 | "outputs": [ 520 | { 521 | "ename": "ZeroDivisionError", 522 | "evalue": "('you have entered a negative value dfsa adsfdsasaf afd', 'sjs')", 523 | "output_type": "error", 524 | "traceback": [ 525 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 526 | "\u001b[1;31mZeroDivisionError\u001b[0m Traceback (most recent call last)", 527 | "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_4092/487591596.py\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mtest\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'sjs'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 528 | "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_4092/1746551771.py\u001b[0m in \u001b[0;36mtest\u001b[1;34m(a)\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mtest\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0ma\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;34m\"sjs\"\u001b[0m \u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[1;32mraise\u001b[0m \u001b[0mZeroDivisionError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"you have entered a negative value dfsa adsfdsasaf afd\"\u001b[0m \u001b[1;33m,\u001b[0m \u001b[0ma\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 4\u001b[0m \u001b[1;32mreturn\u001b[0m \u001b[0ma\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", 529 | "\u001b[1;31mZeroDivisionError\u001b[0m: ('you have entered a negative value dfsa adsfdsasaf afd', 'sjs')" 530 | ] 531 | } 532 | ], 533 | "source": [ 534 | "test('sjs')" 535 | ] 536 | }, 537 | { 538 | "cell_type": "code", 539 | "execution_count": 11, 540 | "id": "219b4dd7", 541 | "metadata": {}, 542 | "outputs": [ 543 | { 544 | "name": "stdout", 545 | "output_type": "stream", 546 | "text": [ 547 | "jalpa\n", 548 | "callinig my raised exceptoin invalid literal for int() with base 10: 'jalpa'\n" 549 | ] 550 | } 551 | ], 552 | "source": [ 553 | "try :\n", 554 | " a = int(input())\n", 555 | " test(a)\n", 556 | "except Exception as e : \n", 557 | " print(\"callinig my raised exceptoin\",e)" 558 | ] 559 | }, 560 | { 561 | "cell_type": "code", 562 | "execution_count": 9, 563 | "id": "b60dd4e8", 564 | "metadata": {}, 565 | "outputs": [ 566 | { 567 | "data": { 568 | "text/plain": [ 569 | "-2" 570 | ] 571 | }, 572 | "execution_count": 9, 573 | "metadata": {}, 574 | "output_type": "execute_result" 575 | } 576 | ], 577 | "source": [ 578 | "test(-2)" 579 | ] 580 | }, 581 | { 582 | "cell_type": "code", 583 | "execution_count": null, 584 | "id": "ba231d12", 585 | "metadata": {}, 586 | "outputs": [], 587 | "source": [] 588 | } 589 | ], 590 | "metadata": { 591 | "kernelspec": { 592 | "display_name": "Python 3 (ipykernel)", 593 | "language": "python", 594 | "name": "python3" 595 | }, 596 | "language_info": { 597 | "codemirror_mode": { 598 | "name": "ipython", 599 | "version": 3 600 | }, 601 | "file_extension": ".py", 602 | "mimetype": "text/x-python", 603 | "name": "python", 604 | "nbconvert_exporter": "python", 605 | "pygments_lexer": "ipython3", 606 | "version": "3.9.7" 607 | } 608 | }, 609 | "nbformat": 4, 610 | "nbformat_minor": 5 611 | } 612 | -------------------------------------------------------------------------------- /Zip file _Unzip file.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 2, 6 | "id": "c3443bc2", 7 | "metadata": {}, 8 | "outputs": [], 9 | "source": [ 10 | "import zipfile\n", 11 | "import os" 12 | ] 13 | }, 14 | { 15 | "cell_type": "markdown", 16 | "id": "23ef1ce7", 17 | "metadata": {}, 18 | "source": [ 19 | "# Create Zip File" 20 | ] 21 | }, 22 | { 23 | "cell_type": "code", 24 | "execution_count": 3, 25 | "id": "0f87b0e0", 26 | "metadata": {}, 27 | "outputs": [], 28 | "source": [ 29 | "os.chdir('D:\\Jalpa Desai')\n", 30 | "my_zip = zipfile.ZipFile('files1.zip','w')\n", 31 | "my_zip.write('Advanced Python Task.pdf')\n", 32 | "my_zip.write('Python Programming Course.pdf')\n", 33 | "my_zip.close()" 34 | ] 35 | }, 36 | { 37 | "cell_type": "markdown", 38 | "id": "9ed51860", 39 | "metadata": {}, 40 | "source": [ 41 | "# Create Unzip File" 42 | ] 43 | }, 44 | { 45 | "cell_type": "code", 46 | "execution_count": 6, 47 | "id": "48b69684", 48 | "metadata": {}, 49 | "outputs": [ 50 | { 51 | "name": "stdout", 52 | "output_type": "stream", 53 | "text": [ 54 | "File Name Modified Size\n", 55 | "Python_Library-Implementation-main/ 2022-09-01 03:43:38 0\n", 56 | "Python_Library-Implementation-main/.gitignore 2022-09-01 03:43:38 1799\n", 57 | "Python_Library-Implementation-main/Data Analysis_Numpy.ipynb 2022-09-01 03:43:38 60296\n", 58 | "Python_Library-Implementation-main/Data Visualization_Matplotlib_Plotly (1).ipynb 2022-09-01 03:43:38 1558179\n", 59 | "Python_Library-Implementation-main/Data Visualization_Plotly.ipynb 2022-09-01 03:43:38 1113590\n", 60 | "Python_Library-Implementation-main/DataManupulation_Titanic_Database.ipynb 2022-09-01 03:43:38 266761\n", 61 | "Python_Library-Implementation-main/LICENSE 2022-09-01 03:43:38 1068\n", 62 | "Python_Library-Implementation-main/Pandas Data Visualisation.ipynb 2022-09-01 03:43:38 1083662\n", 63 | "Python_Library-Implementation-main/Pandas with HTML.ipynb 2022-09-01 03:43:38 46860\n", 64 | "Python_Library-Implementation-main/Pandas1.ipynb 2022-09-01 03:43:38 47541\n", 65 | "Python_Library-Implementation-main/Pandas_Bank_Dataset.ipynb 2022-09-01 03:43:38 33097\n", 66 | "Python_Library-Implementation-main/Pandas_Implementation.ipynb 2022-09-01 03:43:38 51546\n", 67 | "Python_Library-Implementation-main/README.md 2022-09-01 03:43:38 31\n", 68 | "Python_Library-Implementation-main/Seaborn Data Visualization-1.ipynb 2022-09-01 03:43:38 533095\n", 69 | "Python_Library-Implementation-main/seaborn_tips_Dataset.ipynb 2022-09-01 03:43:38 505127\n", 70 | "processing ......\n", 71 | "Process completed\n" 72 | ] 73 | } 74 | ], 75 | "source": [ 76 | "from zipfile import ZipFile\n", 77 | "#file = r\"C:\\Users\\vaibh\\Downloads\\Pro-Kabaddi-Data-Analysis-master.zip\"\n", 78 | "file = r\"C:\\Users\\vaibh\\Downloads\\Python_Library-Implementation-main.zip\"\n", 79 | "with ZipFile(file,'r') as zip:\n", 80 | " zip.printdir()\n", 81 | " print(\"processing ......\")\n", 82 | " zip.extractall()\n", 83 | " print(\"Process completed\")" 84 | ] 85 | }, 86 | { 87 | "cell_type": "code", 88 | "execution_count": null, 89 | "id": "ce6cb537", 90 | "metadata": {}, 91 | "outputs": [], 92 | "source": [] 93 | } 94 | ], 95 | "metadata": { 96 | "kernelspec": { 97 | "display_name": "Python 3 (ipykernel)", 98 | "language": "python", 99 | "name": "python3" 100 | }, 101 | "language_info": { 102 | "codemirror_mode": { 103 | "name": "ipython", 104 | "version": 3 105 | }, 106 | "file_extension": ".py", 107 | "mimetype": "text/x-python", 108 | "name": "python", 109 | "nbconvert_exporter": "python", 110 | "pygments_lexer": "ipython3", 111 | "version": "3.9.12" 112 | } 113 | }, 114 | "nbformat": 4, 115 | "nbformat_minor": 5 116 | } 117 | -------------------------------------------------------------------------------- /lambdafunction.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "id": "d8ab7cd1", 7 | "metadata": {}, 8 | "outputs": [ 9 | { 10 | "data": { 11 | "text/plain": [ 12 | "(x)>" 13 | ] 14 | }, 15 | "execution_count": 1, 16 | "metadata": {}, 17 | "output_type": "execute_result" 18 | } 19 | ], 20 | "source": [ 21 | "#Lambda Functions\n", 22 | "lambda x: x+1" 23 | ] 24 | }, 25 | { 26 | "cell_type": "code", 27 | "execution_count": 2, 28 | "id": "58c859c9", 29 | "metadata": {}, 30 | "outputs": [], 31 | "source": [ 32 | "#Addition\n", 33 | "x=lambda x,y:x+y" 34 | ] 35 | }, 36 | { 37 | "cell_type": "code", 38 | "execution_count": 3, 39 | "id": "c5e7ddc3", 40 | "metadata": {}, 41 | "outputs": [ 42 | { 43 | "data": { 44 | "text/plain": [ 45 | "555" 46 | ] 47 | }, 48 | "execution_count": 3, 49 | "metadata": {}, 50 | "output_type": "execute_result" 51 | } 52 | ], 53 | "source": [ 54 | "x(222,333)" 55 | ] 56 | }, 57 | { 58 | "cell_type": "code", 59 | "execution_count": 4, 60 | "id": "34a7060c", 61 | "metadata": {}, 62 | "outputs": [], 63 | "source": [ 64 | "# Concatinating two Lists\n", 65 | "x=lambda x,y:x+y" 66 | ] 67 | }, 68 | { 69 | "cell_type": "code", 70 | "execution_count": 5, 71 | "id": "9a59c266", 72 | "metadata": {}, 73 | "outputs": [ 74 | { 75 | "data": { 76 | "text/plain": [ 77 | "[1, 2, 3, 4, 5, 5, 4, 3, 2, 1]" 78 | ] 79 | }, 80 | "execution_count": 5, 81 | "metadata": {}, 82 | "output_type": "execute_result" 83 | } 84 | ], 85 | "source": [ 86 | "l1=[1,2,3,4,5]\n", 87 | "l2=[5,4,3,2,1]\n", 88 | "x(l1,l2)" 89 | ] 90 | }, 91 | { 92 | "cell_type": "code", 93 | "execution_count": 6, 94 | "id": "52ec1e84", 95 | "metadata": {}, 96 | "outputs": [ 97 | { 98 | "data": { 99 | "text/plain": [ 100 | "" 101 | ] 102 | }, 103 | "execution_count": 6, 104 | "metadata": {}, 105 | "output_type": "execute_result" 106 | } 107 | ], 108 | "source": [ 109 | "#Map\n", 110 | "l=[1,2,3,4,5]\n", 111 | "map(lambda x:x+1,l)" 112 | ] 113 | }, 114 | { 115 | "cell_type": "code", 116 | "execution_count": 7, 117 | "id": "839f1637", 118 | "metadata": {}, 119 | "outputs": [ 120 | { 121 | "data": { 122 | "text/plain": [ 123 | "[2, 3, 4, 5, 6]" 124 | ] 125 | }, 126 | "execution_count": 7, 127 | "metadata": {}, 128 | "output_type": "execute_result" 129 | } 130 | ], 131 | "source": [ 132 | "list(map(lambda x:x+1,l))" 133 | ] 134 | }, 135 | { 136 | "cell_type": "code", 137 | "execution_count": 8, 138 | "id": "712e393d", 139 | "metadata": {}, 140 | "outputs": [ 141 | { 142 | "data": { 143 | "text/plain": [ 144 | "[1, 2, 3, 4, 5]" 145 | ] 146 | }, 147 | "execution_count": 8, 148 | "metadata": {}, 149 | "output_type": "execute_result" 150 | } 151 | ], 152 | "source": [ 153 | "l1" 154 | ] 155 | }, 156 | { 157 | "cell_type": "code", 158 | "execution_count": 9, 159 | "id": "c95e0607", 160 | "metadata": {}, 161 | "outputs": [ 162 | { 163 | "data": { 164 | "text/plain": [ 165 | "[5, 4, 3, 2, 1]" 166 | ] 167 | }, 168 | "execution_count": 9, 169 | "metadata": {}, 170 | "output_type": "execute_result" 171 | } 172 | ], 173 | "source": [ 174 | "l2" 175 | ] 176 | }, 177 | { 178 | "cell_type": "code", 179 | "execution_count": 10, 180 | "id": "7db90417", 181 | "metadata": {}, 182 | "outputs": [ 183 | { 184 | "data": { 185 | "text/plain": [ 186 | "[6, 6, 6, 6, 6]" 187 | ] 188 | }, 189 | "execution_count": 10, 190 | "metadata": {}, 191 | "output_type": "execute_result" 192 | } 193 | ], 194 | "source": [ 195 | "# Adding two lists\n", 196 | "list(map(lambda x,y:x+y,l1,l2))" 197 | ] 198 | }, 199 | { 200 | "cell_type": "code", 201 | "execution_count": 11, 202 | "id": "028ed6be", 203 | "metadata": {}, 204 | "outputs": [], 205 | "source": [ 206 | "#Reduce\n", 207 | "from functools import reduce\n", 208 | "\n" 209 | ] 210 | }, 211 | { 212 | "cell_type": "code", 213 | "execution_count": 12, 214 | "id": "5add91e2", 215 | "metadata": {}, 216 | "outputs": [ 217 | { 218 | "data": { 219 | "text/plain": [ 220 | "[1, 2, 3, 4, 5]" 221 | ] 222 | }, 223 | "execution_count": 12, 224 | "metadata": {}, 225 | "output_type": "execute_result" 226 | } 227 | ], 228 | "source": [ 229 | "l1\n" 230 | ] 231 | }, 232 | { 233 | "cell_type": "code", 234 | "execution_count": 13, 235 | "id": "a0c6d277", 236 | "metadata": {}, 237 | "outputs": [ 238 | { 239 | "data": { 240 | "text/plain": [ 241 | "5" 242 | ] 243 | }, 244 | "execution_count": 13, 245 | "metadata": {}, 246 | "output_type": "execute_result" 247 | } 248 | ], 249 | "source": [ 250 | "# Find biggest number in list\n", 251 | "reduce(lambda x,y:x if x>y else y,l1)" 252 | ] 253 | }, 254 | { 255 | "cell_type": "code", 256 | "execution_count": 14, 257 | "id": "2dc36259", 258 | "metadata": {}, 259 | "outputs": [ 260 | { 261 | "data": { 262 | "text/plain": [ 263 | "15" 264 | ] 265 | }, 266 | "execution_count": 14, 267 | "metadata": {}, 268 | "output_type": "execute_result" 269 | } 270 | ], 271 | "source": [ 272 | "# Add Numbers in List\n", 273 | "reduce(lambda x,y:x+y,l1)" 274 | ] 275 | }, 276 | { 277 | "cell_type": "code", 278 | "execution_count": 15, 279 | "id": "0a0e4270", 280 | "metadata": {}, 281 | "outputs": [ 282 | { 283 | "data": { 284 | "text/plain": [ 285 | "[1, 3, 5]" 286 | ] 287 | }, 288 | "execution_count": 15, 289 | "metadata": {}, 290 | "output_type": "execute_result" 291 | } 292 | ], 293 | "source": [ 294 | "#Filter\n", 295 | "# Filter odd numbers in list\n", 296 | "list(filter(lambda x:x%2!=0,l1))\n" 297 | ] 298 | }, 299 | { 300 | "cell_type": "code", 301 | "execution_count": 16, 302 | "id": "d2a87f31", 303 | "metadata": {}, 304 | "outputs": [ 305 | { 306 | "data": { 307 | "text/plain": [ 308 | "[-1, -4, -7, -3, -2, -73, -33]" 309 | ] 310 | }, 311 | "execution_count": 16, 312 | "metadata": {}, 313 | "output_type": "execute_result" 314 | } 315 | ], 316 | "source": [ 317 | "# Filter negative numbers in a list\n", 318 | "l=[-1,-4,-7,-3,7,-2,7,93,-73,7,2,625,73,-33]\n", 319 | "list(filter(lambda x:x<0,l))" 320 | ] 321 | }, 322 | { 323 | "cell_type": "code", 324 | "execution_count": null, 325 | "id": "3d1521a5", 326 | "metadata": {}, 327 | "outputs": [], 328 | "source": [] 329 | } 330 | ], 331 | "metadata": { 332 | "kernelspec": { 333 | "display_name": "Python 3 (ipykernel)", 334 | "language": "python", 335 | "name": "python3" 336 | }, 337 | "language_info": { 338 | "codemirror_mode": { 339 | "name": "ipython", 340 | "version": 3 341 | }, 342 | "file_extension": ".py", 343 | "mimetype": "text/x-python", 344 | "name": "python", 345 | "nbconvert_exporter": "python", 346 | "pygments_lexer": "ipython3", 347 | "version": "3.10.9" 348 | } 349 | }, 350 | "nbformat": 4, 351 | "nbformat_minor": 5 352 | } 353 | -------------------------------------------------------------------------------- /tkinter & turtle.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 3, 6 | "id": "829a0501-0b5a-4daf-a30a-7fb939acf570", 7 | "metadata": {}, 8 | "outputs": [], 9 | "source": [ 10 | "import tkinter as tk\n", 11 | "\n", 12 | "def button_click():\n", 13 | " label.config(text=\"Button clicked!\")\n", 14 | "\n", 15 | "# Create the main window\n", 16 | "root = tk.Tk()\n", 17 | "root.title(\"Tkinter Example\")\n", 18 | "\n", 19 | "# Create a label widget\n", 20 | "label = tk.Label(root, text=\"Hello, Tkinter!\")\n", 21 | "label.pack()\n", 22 | "\n", 23 | "# Create a button widget\n", 24 | "button = tk.Button(root, text=\"Click me\", command=button_click)\n", 25 | "button.pack()\n", 26 | "\n", 27 | "# Start the Tkinter event loop\n", 28 | "root.mainloop()\n" 29 | ] 30 | }, 31 | { 32 | "cell_type": "code", 33 | "execution_count": 8, 34 | "id": "c3db9f2c-6891-45b5-abc7-fd2f1a638e71", 35 | "metadata": {}, 36 | "outputs": [], 37 | "source": [ 38 | "#### TURTLE ####\n", 39 | "\n", 40 | "import turtle\n", 41 | "\n", 42 | "# Create a turtle object\n", 43 | "t = turtle.Turtle()\n", 44 | "\n", 45 | "# Draw a square\n", 46 | "for _ in range(4):\n", 47 | " t.forward(100)\n", 48 | " t.right(90)\n", 49 | "\n", 50 | "# Close the turtle graphics window when clicked\n", 51 | "turtle.done()\n" 52 | ] 53 | }, 54 | { 55 | "cell_type": "code", 56 | "execution_count": 1, 57 | "id": "625fe060-dcbc-4346-bb51-5b9920bc581b", 58 | "metadata": {}, 59 | "outputs": [], 60 | "source": [ 61 | "import tkinter as tk\n", 62 | "from tkinter import ttk\n", 63 | "\n", 64 | "def on_button_click():\n", 65 | " value = entry.get()\n", 66 | " label_value.config(text=f\"You entered: {value}\")\n", 67 | "\n", 68 | "def on_combo_select(event):\n", 69 | " selection = combo.get()\n", 70 | " label_combo.config(text=f\"Selected option: {selection}\")\n", 71 | "\n", 72 | "def on_radio_select():\n", 73 | " selection = radio_var.get()\n", 74 | " label_radio.config(text=f\"Selected option: {selection}\")\n", 75 | "\n", 76 | "# Create main application window\n", 77 | "root = tk.Tk()\n", 78 | "root.title(\"Tkinter Widgets Example\")\n", 79 | "\n", 80 | "# Create label widget\n", 81 | "label = tk.Label(root, text=\"Enter text:\")\n", 82 | "label.grid(row=0, column=0, padx=10, pady=10)\n", 83 | "\n", 84 | "# Create entry widget\n", 85 | "entry = tk.Entry(root)\n", 86 | "entry.grid(row=0, column=1, padx=10, pady=10)\n", 87 | "\n", 88 | "# Create button widget\n", 89 | "button = tk.Button(root, text=\"Submit\", command=on_button_click)\n", 90 | "button.grid(row=0, column=2, padx=10, pady=10)\n", 91 | "\n", 92 | "# Create combo box widget\n", 93 | "combo_values = [\"Option 1\", \"Option 2\", \"Option 3\"]\n", 94 | "combo = ttk.Combobox(root, values=combo_values)\n", 95 | "combo.current(0) # Set the default selection\n", 96 | "combo.bind(\"<>\", on_combo_select)\n", 97 | "combo.grid(row=1, column=0, padx=10, pady=10)\n", 98 | "\n", 99 | "# Create radio button widget\n", 100 | "radio_var = tk.StringVar()\n", 101 | "radio_var.set(\"Option 1\") # Set the default selection\n", 102 | "radio1 = tk.Radiobutton(root, text=\"Option 1\", variable=radio_var, value=\"Option 1\", command=on_radio_select)\n", 103 | "radio2 = tk.Radiobutton(root, text=\"Option 2\", variable=radio_var, value=\"Option 2\", command=on_radio_select)\n", 104 | "radio3 = tk.Radiobutton(root, text=\"Option 3\", variable=radio_var, value=\"Option 3\", command=on_radio_select)\n", 105 | "radio1.grid(row=1, column=1, padx=10, pady=10)\n", 106 | "radio2.grid(row=1, column=2, padx=10, pady=10)\n", 107 | "radio3.grid(row=1, column=3, padx=10, pady=10)\n", 108 | "\n", 109 | "# Create labels to display selected values\n", 110 | "label_value = tk.Label(root, text=\"\")\n", 111 | "label_value.grid(row=2, column=0, columnspan=3, padx=10, pady=10)\n", 112 | "\n", 113 | "label_combo = tk.Label(root, text=\"\")\n", 114 | "label_combo.grid(row=3, column=0, columnspan=3, padx=10, pady=10)\n", 115 | "\n", 116 | "label_radio = tk.Label(root, text=\"\")\n", 117 | "label_radio.grid(row=4, column=0, columnspan=3, padx=10, pady=10)\n", 118 | "\n", 119 | "# Start the Tkinter event loop\n", 120 | "root.mainloop()\n" 121 | ] 122 | }, 123 | { 124 | "cell_type": "code", 125 | "execution_count": 1, 126 | "id": "54b6a432-8af7-46a2-8029-fe35754d6b3c", 127 | "metadata": {}, 128 | "outputs": [], 129 | "source": [ 130 | "import tkinter as tk \n", 131 | "r = tk.Tk() \n", 132 | "r.title('Counting Seconds') \n", 133 | "button = tk.Button(r, text='Stop', width=25, command=r.destroy) \n", 134 | "button.pack() \n", 135 | "r.mainloop() " 136 | ] 137 | }, 138 | { 139 | "cell_type": "code", 140 | "execution_count": 2, 141 | "id": "2e6cfed2-dd72-44a8-ab9d-219941312edb", 142 | "metadata": {}, 143 | "outputs": [], 144 | "source": [ 145 | "import tkinter as tk\n", 146 | "r = tk.Tk()\n", 147 | "r.title('welcome')\n", 148 | "button=tk.Button(r, text='start', width=25, command=r.destroy)\n", 149 | "button.pack()\n", 150 | "r.mainloop()" 151 | ] 152 | }, 153 | { 154 | "cell_type": "code", 155 | "execution_count": 4, 156 | "id": "f7402b35-1b31-432a-8447-f753d47fb67a", 157 | "metadata": {}, 158 | "outputs": [], 159 | "source": [ 160 | "from tkinter import *\n", 161 | "master =Tk()\n", 162 | "w =Canvas(master, width=400, height=600)\n", 163 | "w.pack()\n", 164 | "canvas_height=200\n", 165 | "canvas_width=200\n", 166 | "y =int(canvas_height /2)\n", 167 | "w.create_line(0, y, canvas_width, y )\n", 168 | "mainloop()" 169 | ] 170 | }, 171 | { 172 | "cell_type": "code", 173 | "execution_count": 5, 174 | "id": "5b890771-21fe-49fb-bda3-068a9ff3b548", 175 | "metadata": {}, 176 | "outputs": [], 177 | "source": [ 178 | "import tkinter as tk\n", 179 | "\n", 180 | "def button_click():\n", 181 | " label.config(text=\"Button clicked!\")\n", 182 | "\n", 183 | "# Create the main window\n", 184 | "root = tk.Tk()\n", 185 | "root.title(\"Tkinter Example\")\n", 186 | "\n", 187 | "# Create a label widget\n", 188 | "label = tk.Label(root, text=\"Hello, Tkinter!\")\n", 189 | "label.pack()\n", 190 | "\n", 191 | "# Create a button widget\n", 192 | "button = tk.Button(root, text=\"Click me\", command=button_click)\n", 193 | "button.pack()\n", 194 | "\n", 195 | "# Start the Tkinter event loop\n", 196 | "root.mainloop()" 197 | ] 198 | }, 199 | { 200 | "cell_type": "code", 201 | "execution_count": null, 202 | "id": "f72a41d2-1ed7-45c8-852c-124c7a9f9f70", 203 | "metadata": {}, 204 | "outputs": [], 205 | "source": [ 206 | "#1. Binding Events with bind Method:" 207 | ] 208 | }, 209 | { 210 | "cell_type": "code", 211 | "execution_count": 7, 212 | "id": "259efa69-23b3-4a70-89a4-492c8a56ab4a", 213 | "metadata": {}, 214 | "outputs": [ 215 | { 216 | "name": "stdout", 217 | "output_type": "stream", 218 | "text": [ 219 | "Button clicked!\n", 220 | "Button clicked!\n", 221 | "Button clicked!\n", 222 | "Button clicked!\n" 223 | ] 224 | } 225 | ], 226 | "source": [ 227 | "import tkinter as tk\n", 228 | "\n", 229 | "def on_button_click(event):\n", 230 | " print(\"Button clicked!\")\n", 231 | "\n", 232 | "root = tk.Tk()\n", 233 | "button = tk.Button(root, text=\"Click me\")\n", 234 | "button.pack()\n", 235 | "button.bind(\"\", on_button_click) # Bind left mouse button click event\n", 236 | "root.mainloop()\n", 237 | "\n" 238 | ] 239 | }, 240 | { 241 | "cell_type": "code", 242 | "execution_count": null, 243 | "id": "4e293b90-00ba-4697-aae7-b11661216db8", 244 | "metadata": {}, 245 | "outputs": [], 246 | "source": [ 247 | "#2. Binding Events with Lambda Functions:" 248 | ] 249 | }, 250 | { 251 | "cell_type": "code", 252 | "execution_count": 8, 253 | "id": "ca1e875b-12c1-41e5-a8a8-89892d1c1c11", 254 | "metadata": {}, 255 | "outputs": [ 256 | { 257 | "name": "stdout", 258 | "output_type": "stream", 259 | "text": [ 260 | "Button clicked!\n", 261 | "Button clicked!\n", 262 | "Button clicked!\n", 263 | "Button clicked!\n" 264 | ] 265 | } 266 | ], 267 | "source": [ 268 | "def on_button_click(event, message):\n", 269 | " print(message)\n", 270 | "\n", 271 | "root = tk.Tk()\n", 272 | "button = tk.Button(root, text=\"Click me\")\n", 273 | "button.pack()\n", 274 | "button.bind(\"\", lambda event: on_button_click(event, \"Button clicked!\"))\n", 275 | "root.mainloop()\n" 276 | ] 277 | }, 278 | { 279 | "cell_type": "code", 280 | "execution_count": null, 281 | "id": "a0d1b5ee-fe32-46ad-8a32-056da85e4326", 282 | "metadata": {}, 283 | "outputs": [], 284 | "source": [ 285 | "#3. Binding Events with Class Methods:" 286 | ] 287 | }, 288 | { 289 | "cell_type": "code", 290 | "execution_count": 1, 291 | "id": "34738bca-c07d-4377-96b4-c0db2e45be8c", 292 | "metadata": {}, 293 | "outputs": [ 294 | { 295 | "name": "stdout", 296 | "output_type": "stream", 297 | "text": [ 298 | "Button clicked!\n", 299 | "Button clicked!\n", 300 | "Button clicked!\n" 301 | ] 302 | } 303 | ], 304 | "source": [ 305 | "import tkinter as tk\n", 306 | "\n", 307 | "class App(tk.Tk):\n", 308 | " def __init__(self):\n", 309 | " super().__init__()\n", 310 | " self.button = tk.Button(self, text=\"Click me\")\n", 311 | " self.button.pack()\n", 312 | " self.button.bind(\"\", self.on_button_click)\n", 313 | " \n", 314 | " def on_button_click(self, event):\n", 315 | " print(\"Button clicked!\")\n", 316 | "app = App()\n", 317 | "app.mainloop()\n" 318 | ] 319 | }, 320 | { 321 | "cell_type": "code", 322 | "execution_count": null, 323 | "id": "1bf5b270-220e-40d7-8efa-9bfc76a0e7e1", 324 | "metadata": {}, 325 | "outputs": [], 326 | "source": [ 327 | "#4. Predefined Event Sequences:" 328 | ] 329 | }, 330 | { 331 | "cell_type": "code", 332 | "execution_count": 10, 333 | "id": "68e38480-a9ed-4b06-8961-9190d1da3a9b", 334 | "metadata": {}, 335 | "outputs": [ 336 | { 337 | "name": "stdout", 338 | "output_type": "stream", 339 | "text": [ 340 | "Button clicked!\n", 341 | "Button clicked!\n", 342 | "Button clicked!\n" 343 | ] 344 | } 345 | ], 346 | "source": [ 347 | "import tkinter as tk\n", 348 | "\n", 349 | "def on_button_click():\n", 350 | " print(\"Button clicked!\")\n", 351 | "\n", 352 | "def on_enter_key(event):\n", 353 | " print(\"Enter key pressed!\")\n", 354 | "\n", 355 | "root = tk.Tk()\n", 356 | "button = tk.Button(root, text=\"Click me\")\n", 357 | "button.pack()\n", 358 | "button.bind(\"\", lambda event: on_button_click())\n", 359 | "root.bind(\"\", on_enter_key)\n", 360 | "root.mainloop()\n" 361 | ] 362 | }, 363 | { 364 | "cell_type": "code", 365 | "execution_count": 3, 366 | "id": "d2647ab8-d7de-4deb-a275-0ae3441c5508", 367 | "metadata": {}, 368 | "outputs": [], 369 | "source": [ 370 | "import tkinter as tk\n", 371 | "from tkinter import ttk\n", 372 | "\n", 373 | "def on_button_click():\n", 374 | " value = entry.get()\n", 375 | " label_value.config(text=f\"You entered: {value}\")\n", 376 | "\n", 377 | "def on_combo_select(event):\n", 378 | " selection = combo.get()\n", 379 | " label_combo.config(text=f\"Selected option: {selection}\")\n", 380 | "\n", 381 | "def on_radio_select():\n", 382 | " selection = radio_var.get()\n", 383 | " label_radio.config(text=f\"Selected option: {selection}\")\n", 384 | "\n", 385 | "# Create main application window\n", 386 | "root = tk.Tk()\n", 387 | "root.title(\"Tkinter Widgets Example\")\n", 388 | "\n", 389 | "# Create label widget\n", 390 | "label = tk.Label(root, text=\"Enter text:\")\n", 391 | "label.grid(row=0, column=0, padx=10, pady=10)\n", 392 | "\n", 393 | "# Create entry widget\n", 394 | "entry = tk.Entry(root)\n", 395 | "entry.grid(row=0, column=1, padx=10, pady=10)\n", 396 | "\n", 397 | "# Create button widget\n", 398 | "button = tk.Button(root, text=\"Submit\", command=on_button_click)\n", 399 | "button.grid(row=0, column=2, padx=10, pady=10)\n", 400 | "\n", 401 | "# Create combo box widget\n", 402 | "combo_values = [\"Option 1\", \"Option 2\", \"Option 3\"]\n", 403 | "combo = ttk.Combobox(root, values=combo_values)\n", 404 | "combo.current(0) # Set the default selection\n", 405 | "combo.bind(\"<>\", on_combo_select)\n", 406 | "combo.grid(row=1, column=0, padx=10, pady=10)\n", 407 | "\n", 408 | "# Create radio button widget\n", 409 | "radio_var = tk.StringVar()\n", 410 | "radio_var.set(\"Option 1\") # Set the default selection\n", 411 | "radio1 = tk.Radiobutton(root, text=\"Option 1\", variable=radio_var, value=\"Option 1\", command=on_radio_select)\n", 412 | "radio2 = tk.Radiobutton(root, text=\"Option 2\", variable=radio_var, value=\"Option 2\", command=on_radio_select)\n", 413 | "radio3 = tk.Radiobutton(root, text=\"Option 3\", variable=radio_var, value=\"Option 3\", command=on_radio_select)\n", 414 | "radio1.grid(row=1, column=1, padx=10, pady=10)\n", 415 | "radio2.grid(row=1, column=2, padx=10, pady=10)\n", 416 | "radio3.grid(row=1, column=3, padx=10, pady=10)\n", 417 | "\n", 418 | "# Create labels to display selected values\n", 419 | "label_value = tk.Label(root, text=\"\")\n", 420 | "label_value.grid(row=2, column=0, columnspan=3, padx=10, pady=10)\n", 421 | "\n", 422 | "label_combo = tk.Label(root, text=\"\")\n", 423 | "label_combo.grid(row=3, column=0, columnspan=3, padx=10, pady=10)\n", 424 | "\n", 425 | "label_radio = tk.Label(root, text=\"\")\n", 426 | "label_radio.grid(row=4, column=0, columnspan=3, padx=10, pady=10)\n", 427 | "\n", 428 | "# Start the Tkinter event loop\n", 429 | "root.mainloop()\n" 430 | ] 431 | }, 432 | { 433 | "cell_type": "code", 434 | "execution_count": null, 435 | "id": "9f569019-7623-4ae8-881e-a4c9dd2f114b", 436 | "metadata": {}, 437 | "outputs": [], 438 | "source": [] 439 | } 440 | ], 441 | "metadata": { 442 | "kernelspec": { 443 | "display_name": "Python 3 (ipykernel)", 444 | "language": "python", 445 | "name": "python3" 446 | }, 447 | "language_info": { 448 | "codemirror_mode": { 449 | "name": "ipython", 450 | "version": 3 451 | }, 452 | "file_extension": ".py", 453 | "mimetype": "text/x-python", 454 | "name": "python", 455 | "nbconvert_exporter": "python", 456 | "pygments_lexer": "ipython3", 457 | "version": "3.11.7" 458 | } 459 | }, 460 | "nbformat": 4, 461 | "nbformat_minor": 5 462 | } 463 | --------------------------------------------------------------------------------