├── Blog Project.zip ├── Gagan - Vegan Store Project.rar ├── GaganGowda.py.zip ├── Library Management System FC.xml ├── Practise Problems 1 - Conditional Statements - Solutions.ipynb ├── Project - Edyoda Stories Page Codes.rar ├── Project - Edyoda stories _GaganGwdaHR.rar ├── README.md ├── Session 10 - Functions.ipynb ├── Session 11 - Functions Problems.ipynb ├── Session 12 - Problems.ipynb ├── Session 14 - Functions.ipynb ├── Session 15 - Test File Operations.ipynb ├── Session 16 - Regular Expression.ipynb ├── Session 18 - Map - Filter and Lambda.ipynb ├── Session 19 - Problem solving.ipynb ├── Session 21 - Class and Object.ipynb ├── Session 22 - OOP Part2.ipynb ├── Session 23 - Part3 OOP.ipynb ├── Session 26 - Iterators Decorators and Generators.ipynb ├── Session 3 - Conditional Statements and looping Statements.ipynb ├── Session 4 - Looping Statements.ipynb ├── Session 7 - Tuple , Dict.ipynb ├── Session 9 - Problem Solving Session.ipynb ├── blog.txt ├── bs.py ├── python interview questions.txt ├── session 1 - basics.ipynb ├── session 2 - Python Basics.ipynb ├── session 5 - Str.ipynb ├── session 6 - List.ipynb └── session 8 - Sets.ipynb /Blog Project.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GaganGowdaHR/Gagan-python-django-code/2934a225fd282dc9c0b0c1436a17388a3bc7525f/Blog Project.zip -------------------------------------------------------------------------------- /Gagan - Vegan Store Project.rar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GaganGowdaHR/Gagan-python-django-code/2934a225fd282dc9c0b0c1436a17388a3bc7525f/Gagan - Vegan Store Project.rar -------------------------------------------------------------------------------- /GaganGowda.py.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GaganGowdaHR/Gagan-python-django-code/2934a225fd282dc9c0b0c1436a17388a3bc7525f/GaganGowda.py.zip -------------------------------------------------------------------------------- /Library Management System FC.xml: -------------------------------------------------------------------------------- 1 | 7Vxbc6M2GP01fnQGJC72Y27b7kzS2Umm26ZvMsiYBiOPkON4f30lEGAQOHQcW8Duvmz0cRPnfHcJT+Dt+v03ijarR+LjaAIM/30C7yYAAAO6/D8h2WcSawYyQUBDPxOZpeA5/IGl0JDSbejjpHIiIyRi4aYq9EgcY49VZIhSsquetiRR9akbFGBF8OyhSJX+FfpsJaWmMy8P/I7DYCUfPQPyhdcoP1m+SbJCPtkdiOD9BN5SQlj21/r9FkcCvByX7LovLUeLiVEcsy4XLJbf5t+X8T8P5H6K4ydi4/3TFEg2ErbP3xj7HAA5JJStSEBiFN2X0htKtrGPxW0NPirPeSBkw4UmF/6LGdtLNtGWES5asXUkj/IZ0/3f4vorOx++yNulg7v3ymgvR9lcxQRbMZCihGyph4+8eK5LiAaYHTnPKZjiKo7JGvP58OsojhAL36rzQFLXguK8kg7+h2Tk/7ADsxu/oWgrH6WDLg2wA62wW7+M4ig7lk52TMUmHsIFRekMHlHM/fFavDgwnvfJnuG1wmXJlIB9twoZft6gFJgdD2JVVtAiIdGW4WvqSeZSaTmy+DBhlLwW8QEUpLxhyvD7cVpUGOUF0JKBY1+NiLuDMDSTstVBBHKNc5mFMTZvBDrq+0yrN1JQf8TrBaZa9NoPKc+yQhKLm+KEfZKqz2qqbjXoutGg6865dN3sR+Rtiwnm5WLCrKONmPaJRiIv/UbC1HvnqmFUVQPMa5RnE5NX1VgvpnGCIsx7oQg6CNXq9WYK7Bsknr7EvBrkz4v9dCpBmLBRu8LCxelzhSoVtyj20jo/j0SSCYpSPESi8GUCnEhgsuCHnUD8VVx17Xk4ScI4GBFvNqjyZjWlaxflDThj81xZiOlSPZ4ai04DXjUYrblDMXipJhIXqCe7EnZyrGlOHixoV43SumzyANTk4Q+i6AJ3Oqzm2aIwEO7J44hz9wpvhGsKPRRdywPr0PczLcFJ+AMt0lsJ8jbiVdKXs28m9h2XRGiBoxvkvQapRt2SiND0uXCZ/jvm/mRTVd5/UrQyD3XhiAm0OsupcQXM/CGSG+mtTmTcrHrhqVW9AVkuE3yePNFWqH4gQRinEYAHACUabpM0cPqYoTBKRhQJgV0zOv2RUOWmSFw8JCh6wpwNrnRlYsm2VBy4IeSVk+OgtYA8XiSbAqRRkmXbusmyHa3BUl/z1ekYK22t3VdHbUcNPKvsirvTYkkXwl2B/XmfDLi7Pa03dvT3/JzRFUxO51VOrQWT04+CSQvwrlbgG9bSKkmrcZenp7KlU8uDjG9oL1bbksNTB+qQrHmt86Y9FXJchZ6ih5a3PtMuWkaO7KuNhgD9hYOrEjB0x9S1I+Nq3fjitFdsA1Vux+mdcqvpTlkG53VvrWmRVcdPtep4JJTor31dVe2H7m9gV3+jt6pVl/uzTUWhaA8NVL9h75akXKspoTnIXIxHFMY8jYwxT3P4kCwn6T6uQRe6oG9uJp9AxfN7hPpJDnmL+zevvibJFvvtZ4ArecCIcXoe9y3AWOBJ3lO9GiyPvbMm29URHHrQKbWtjkHFBjqDij26rUPdgTdOBL5tM1jFBOGF94LllvzL4NpOtLTuGbPV9KKaT4wj8uhvTNngowQiq9bFcLcSGQB/2SLNE3nBQJkw3b4lAZbaO78JoyiMg3KbZNmkHSbsJuwb6lD1NAMP7V09PIQ6PbwK++DL9Fnvvn3IN839dGnOybrdnLW6sxb3daG0FfajDukNwV283KmrH227E2st50t/zQLVPue1L1KECeA3NP7c+IiVu+B8HGExrDd50gyP5gnfONyu/u8sLDWp7ndO8XE1eOqOqY7+tP7Fa+YHzmZElrprIjcisQr/hNckXUMraqBhWohby7r1962t0S3Hw66bDy2ty/FQXSoe9NKvafSuorRUiA+dShGZG7+so7nLEaxM9H5UcA56euB6GlqL43Lv+k3AVDF+wSq+5/1cSrj7pGTiE4Cur/8WP9BxALTVgDM4F85AzV8u/lXa58Nsm/aHMMNLwpw/bGTqDOa1L4aMuV51dtT2xncOoT94pKd2bbWhqUa9KNSu6jkUlH+OTlL3Df7n6SlOYX23+qxGeksRrN4JfHSnM/ekXLXvMRL7hXbf7Fdt/32N38YBtlX7uTTYsI5ifw7YfFj+bGdmBuWPn8L7/wA= -------------------------------------------------------------------------------- /Practise Problems 1 - Conditional Statements - Solutions.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "1.Given two numbers , print their sum. However , if the two numbers are the same , return doublt their sum" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 2, 15 | "metadata": {}, 16 | "outputs": [ 17 | { 18 | "name": "stdout", 19 | "output_type": "stream", 20 | "text": [ 21 | "800\n" 22 | ] 23 | } 24 | ], 25 | "source": [ 26 | "num1 = 200\n", 27 | "num2 = 200\n", 28 | "\n", 29 | "if num1 == num2:\n", 30 | " print((num1 + num2) * 2)\n", 31 | "else:\n", 32 | " print(num1 + num2)\n" 33 | ] 34 | }, 35 | { 36 | "cell_type": "code", 37 | "execution_count": null, 38 | "metadata": {}, 39 | "outputs": [], 40 | "source": [ 41 | "2.Given a Cost price and Selling price of a product . Write program to calculate profit or lose" 42 | ] 43 | }, 44 | { 45 | "cell_type": "code", 46 | "execution_count": 5, 47 | "metadata": {}, 48 | "outputs": [ 49 | { 50 | "name": "stdout", 51 | "output_type": "stream", 52 | "text": [ 53 | "profit of: 700\n" 54 | ] 55 | } 56 | ], 57 | "source": [ 58 | "cost_price = 500\n", 59 | "selling_price = 1200\n", 60 | "profit = selling_price > cost_price\n", 61 | "loss = cost_price > selling_price\n", 62 | "if selling_price > cost_price:\n", 63 | " print(\"profit of:\",selling_price - cost_price)\n", 64 | "elif cost_price > selling_price:\n", 65 | " print(\"loss of:\",cost_price - selling_price)\n", 66 | "else:\n", 67 | " print(\"no profit no loss\")" 68 | ] 69 | }, 70 | { 71 | "cell_type": "code", 72 | "execution_count": null, 73 | "metadata": {}, 74 | "outputs": [], 75 | "source": [ 76 | "3.Write a program to input basic salary of an employee and calculate gross salary according to given conditions.\n", 77 | "Basic Salary <= 10000 : HRA = 20%, DA = 80%\n", 78 | "Basic Salary is between 10001 to 20000 : HRA = 25% , DA = 90% \n", 79 | "Basic Salary >= 20001 : HRA = 30% , DA = 95%" 80 | ] 81 | }, 82 | { 83 | "cell_type": "code", 84 | "execution_count": 21, 85 | "metadata": {}, 86 | "outputs": [ 87 | { 88 | "name": "stdout", 89 | "output_type": "stream", 90 | "text": [ 91 | "43000.0\n" 92 | ] 93 | } 94 | ], 95 | "source": [ 96 | "basic_salary = 20000\n", 97 | "\n", 98 | "if basic_salary <= 10000:\n", 99 | " hra = (basic_salary * 20) / 100\n", 100 | " da = (basic_salary * 80) / 100\n", 101 | "elif basic_salary >= 20000:\n", 102 | " hra = (basic_salary * 25) / 100\n", 103 | " da = (basic_salary * 90) / 100\n", 104 | "else:\n", 105 | " hra = (basic_salary * 30) / 100\n", 106 | " da = (basic_salary * 95) / 100\n", 107 | " \n", 108 | "gross_salary = basic_salary + hra + da\n", 109 | "print(gross_salary) " 110 | ] 111 | }, 112 | { 113 | "cell_type": "code", 114 | "execution_count": null, 115 | "metadata": {}, 116 | "outputs": [], 117 | "source": [ 118 | "4.Write a program to check if given year is leap year or not\n", 119 | " leap year : a year is said to be leap if the year is exactly divisible by 4 but not divisible by 100" 120 | ] 121 | }, 122 | { 123 | "cell_type": "code", 124 | "execution_count": 46, 125 | "metadata": {}, 126 | "outputs": [ 127 | { 128 | "name": "stdout", 129 | "output_type": "stream", 130 | "text": [ 131 | "leap Year\n" 132 | ] 133 | } 134 | ], 135 | "source": [ 136 | "year = 2000\n", 137 | "\n", 138 | "if year % 4 == 0 and year % 100 != 0:\n", 139 | " print(\"leap Year\")\n", 140 | "elif year % 4 == 0 and year % 100 == 0 and year % 400 == 0:\n", 141 | " print(\"leap Year\")\n", 142 | "else:\n", 143 | " print(\"not a leap year\")" 144 | ] 145 | }, 146 | { 147 | "cell_type": "code", 148 | "execution_count": null, 149 | "metadata": {}, 150 | "outputs": [], 151 | "source": [ 152 | "5.Write a program to check whether the given triangle is equilateral , scalene or isosceles. \n", 153 | "A triangle is said equilateral , if all the sides of the triangle is equal.if a, b, c are three sides of triangle.Then the \n", 154 | "triangle is equilateral only if a == b == c.\n", 155 | "A triangle is said to be isosceles, only if two sides are equal. like a == b , a == b or b == c\n", 156 | "A traingle is said to be scalene , only if no sides are equal." 157 | ] 158 | }, 159 | { 160 | "cell_type": "code", 161 | "execution_count": 52, 162 | "metadata": {}, 163 | "outputs": [ 164 | { 165 | "name": "stdout", 166 | "output_type": "stream", 167 | "text": [ 168 | "Scalene triangle\n" 169 | ] 170 | } 171 | ], 172 | "source": [ 173 | "side_a = 4\n", 174 | "side_b = 7\n", 175 | "side_c = 5\n", 176 | "\n", 177 | "if side_a == side_b == side_c:\n", 178 | " print(\"equilateral triangle\")\n", 179 | "elif side_a == side_b or side_a == side_c or side_b == side_c:\n", 180 | " print(\"isosceles triangle\")\n", 181 | "else:\n", 182 | " print(\"Scalene triangle\")" 183 | ] 184 | } 185 | ], 186 | "metadata": { 187 | "kernelspec": { 188 | "display_name": "Python 3", 189 | "language": "python", 190 | "name": "python3" 191 | }, 192 | "language_info": { 193 | "codemirror_mode": { 194 | "name": "ipython", 195 | "version": 3 196 | }, 197 | "file_extension": ".py", 198 | "mimetype": "text/x-python", 199 | "name": "python", 200 | "nbconvert_exporter": "python", 201 | "pygments_lexer": "ipython3", 202 | "version": "3.7.4" 203 | } 204 | }, 205 | "nbformat": 4, 206 | "nbformat_minor": 2 207 | } 208 | -------------------------------------------------------------------------------- /Project - Edyoda Stories Page Codes.rar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GaganGowdaHR/Gagan-python-django-code/2934a225fd282dc9c0b0c1436a17388a3bc7525f/Project - Edyoda Stories Page Codes.rar -------------------------------------------------------------------------------- /Project - Edyoda stories _GaganGwdaHR.rar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GaganGowdaHR/Gagan-python-django-code/2934a225fd282dc9c0b0c1436a17388a3bc7525f/Project - Edyoda stories _GaganGwdaHR.rar -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Gagan-python-django-code 2 | my code 3 | -------------------------------------------------------------------------------- /Session 10 - Functions.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 5, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "a = 10\n", 10 | "b = 20" 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": 6, 16 | "metadata": {}, 17 | "outputs": [ 18 | { 19 | "data": { 20 | "text/plain": [ 21 | "30" 22 | ] 23 | }, 24 | "execution_count": 6, 25 | "metadata": {}, 26 | "output_type": "execute_result" 27 | } 28 | ], 29 | "source": [ 30 | "a + b" 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": 7, 36 | "metadata": {}, 37 | "outputs": [ 38 | { 39 | "data": { 40 | "text/plain": [ 41 | "200" 42 | ] 43 | }, 44 | "execution_count": 7, 45 | "metadata": {}, 46 | "output_type": "execute_result" 47 | } 48 | ], 49 | "source": [ 50 | "a * b" 51 | ] 52 | }, 53 | { 54 | "cell_type": "code", 55 | "execution_count": 8, 56 | "metadata": {}, 57 | "outputs": [ 58 | { 59 | "data": { 60 | "text/plain": [ 61 | "0.5" 62 | ] 63 | }, 64 | "execution_count": 8, 65 | "metadata": {}, 66 | "output_type": "execute_result" 67 | } 68 | ], 69 | "source": [ 70 | "a / b" 71 | ] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "execution_count": 9, 76 | "metadata": {}, 77 | "outputs": [], 78 | "source": [ 79 | "def mysum(a,b):\n", 80 | " return a+b" 81 | ] 82 | }, 83 | { 84 | "cell_type": "code", 85 | "execution_count": 10, 86 | "metadata": {}, 87 | "outputs": [ 88 | { 89 | "data": { 90 | "text/plain": [ 91 | "30" 92 | ] 93 | }, 94 | "execution_count": 10, 95 | "metadata": {}, 96 | "output_type": "execute_result" 97 | } 98 | ], 99 | "source": [ 100 | "mysum(10,20)" 101 | ] 102 | }, 103 | { 104 | "cell_type": "code", 105 | "execution_count": 11, 106 | "metadata": {}, 107 | "outputs": [], 108 | "source": [ 109 | "def mymult(a,b):\n", 110 | " return a * b" 111 | ] 112 | }, 113 | { 114 | "cell_type": "code", 115 | "execution_count": 12, 116 | "metadata": {}, 117 | "outputs": [ 118 | { 119 | "data": { 120 | "text/plain": [ 121 | "600" 122 | ] 123 | }, 124 | "execution_count": 12, 125 | "metadata": {}, 126 | "output_type": "execute_result" 127 | } 128 | ], 129 | "source": [ 130 | "mymult(20,30)" 131 | ] 132 | }, 133 | { 134 | "cell_type": "code", 135 | "execution_count": 13, 136 | "metadata": {}, 137 | "outputs": [ 138 | { 139 | "data": { 140 | "text/plain": [ 141 | "1200" 142 | ] 143 | }, 144 | "execution_count": 13, 145 | "metadata": {}, 146 | "output_type": "execute_result" 147 | } 148 | ], 149 | "source": [ 150 | "mymult(30,40)" 151 | ] 152 | }, 153 | { 154 | "cell_type": "code", 155 | "execution_count": 14, 156 | "metadata": {}, 157 | "outputs": [], 158 | "source": [ 159 | "res = mysum(30,20)" 160 | ] 161 | }, 162 | { 163 | "cell_type": "code", 164 | "execution_count": 15, 165 | "metadata": {}, 166 | "outputs": [ 167 | { 168 | "data": { 169 | "text/plain": [ 170 | "50" 171 | ] 172 | }, 173 | "execution_count": 15, 174 | "metadata": {}, 175 | "output_type": "execute_result" 176 | } 177 | ], 178 | "source": [ 179 | "res" 180 | ] 181 | }, 182 | { 183 | "cell_type": "code", 184 | "execution_count": 16, 185 | "metadata": {}, 186 | "outputs": [], 187 | "source": [ 188 | "def mycal(a,b):\n", 189 | " mult = mymult(a,b)\n", 190 | " msum = mysum(a,b)\n", 191 | " return mult,msum" 192 | ] 193 | }, 194 | { 195 | "cell_type": "code", 196 | "execution_count": 17, 197 | "metadata": {}, 198 | "outputs": [], 199 | "source": [ 200 | "mymult, mysum = mycal(10,20)" 201 | ] 202 | }, 203 | { 204 | "cell_type": "code", 205 | "execution_count": 18, 206 | "metadata": {}, 207 | "outputs": [ 208 | { 209 | "data": { 210 | "text/plain": [ 211 | "200" 212 | ] 213 | }, 214 | "execution_count": 18, 215 | "metadata": {}, 216 | "output_type": "execute_result" 217 | } 218 | ], 219 | "source": [ 220 | "mymult" 221 | ] 222 | }, 223 | { 224 | "cell_type": "code", 225 | "execution_count": 19, 226 | "metadata": {}, 227 | "outputs": [ 228 | { 229 | "data": { 230 | "text/plain": [ 231 | "30" 232 | ] 233 | }, 234 | "execution_count": 19, 235 | "metadata": {}, 236 | "output_type": "execute_result" 237 | } 238 | ], 239 | "source": [ 240 | "mysum" 241 | ] 242 | }, 243 | { 244 | "cell_type": "code", 245 | "execution_count": 20, 246 | "metadata": {}, 247 | "outputs": [], 248 | "source": [ 249 | "def checkEvenNumber(num):\n", 250 | " if num % 2 == 0:\n", 251 | " return True\n", 252 | " else:\n", 253 | " return False" 254 | ] 255 | }, 256 | { 257 | "cell_type": "code", 258 | "execution_count": 21, 259 | "metadata": {}, 260 | "outputs": [ 261 | { 262 | "data": { 263 | "text/plain": [ 264 | "True" 265 | ] 266 | }, 267 | "execution_count": 21, 268 | "metadata": {}, 269 | "output_type": "execute_result" 270 | } 271 | ], 272 | "source": [ 273 | "checkEvenNumber(22)" 274 | ] 275 | }, 276 | { 277 | "cell_type": "code", 278 | "execution_count": 27, 279 | "metadata": {}, 280 | "outputs": [], 281 | "source": [ 282 | "Take two numbers\n", 283 | "Check if first number is even\n", 284 | "If first number is even, multiply them\n", 285 | "Else product them\n", 286 | "Return the result" 287 | ] 288 | }, 289 | { 290 | "cell_type": "code", 291 | "execution_count": 23, 292 | "metadata": {}, 293 | "outputs": [], 294 | "source": [ 295 | "def myMainCode(num1, num2):\n", 296 | " checkEven = checkEvenNumber(num1)\n", 297 | " \n", 298 | " if checkEven:\n", 299 | " res = mymult(num1, num2)\n", 300 | " else:\n", 301 | " res = mysum(num1, num2)\n", 302 | " \n", 303 | " return res" 304 | ] 305 | }, 306 | { 307 | "cell_type": "code", 308 | "execution_count": 30, 309 | "metadata": {}, 310 | "outputs": [ 311 | { 312 | "ename": "TypeError", 313 | "evalue": "'int' object is not callable", 314 | "output_type": "error", 315 | "traceback": [ 316 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 317 | "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", 318 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mmyMainCode\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m100\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m200\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 319 | "\u001b[1;32m\u001b[0m in \u001b[0;36mmyMainCode\u001b[1;34m(num1, num2)\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mcheckEven\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 5\u001b[1;33m \u001b[0mres\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mmymult\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mnum1\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mnum2\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 6\u001b[0m \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 7\u001b[0m \u001b[0mres\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mmysum\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mnum1\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mnum2\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", 320 | "\u001b[1;31mTypeError\u001b[0m: 'int' object is not callable" 321 | ] 322 | } 323 | ], 324 | "source": [ 325 | "myMainCode(100,200)" 326 | ] 327 | }, 328 | { 329 | "cell_type": "code", 330 | "execution_count": 26, 331 | "metadata": {}, 332 | "outputs": [], 333 | "source": [ 334 | "Problem Statement :\n", 335 | "\n", 336 | "Jack & Jill Game\n", 337 | "Jack increments by 2\n", 338 | "Jill decrements by 1" 339 | ] 340 | }, 341 | { 342 | "cell_type": "code", 343 | "execution_count": 48, 344 | "metadata": {}, 345 | "outputs": [], 346 | "source": [ 347 | "value = 0 \n", 348 | "\n", 349 | "def jackFunc():\n", 350 | " global value\n", 351 | " value += 2\n", 352 | " \n", 353 | "def jillFunc():\n", 354 | " global value\n", 355 | " value -= 1" 356 | ] 357 | }, 358 | { 359 | "cell_type": "code", 360 | "execution_count": 49, 361 | "metadata": {}, 362 | "outputs": [], 363 | "source": [ 364 | "i = 0\n", 365 | "while i < 10:\n", 366 | " jackFunc()\n", 367 | " i += 1" 368 | ] 369 | }, 370 | { 371 | "cell_type": "code", 372 | "execution_count": 51, 373 | "metadata": {}, 374 | "outputs": [ 375 | { 376 | "data": { 377 | "text/plain": [ 378 | "20" 379 | ] 380 | }, 381 | "execution_count": 51, 382 | "metadata": {}, 383 | "output_type": "execute_result" 384 | } 385 | ], 386 | "source": [ 387 | "value" 388 | ] 389 | }, 390 | { 391 | "cell_type": "code", 392 | "execution_count": 52, 393 | "metadata": {}, 394 | "outputs": [], 395 | "source": [ 396 | "value = 0 \n", 397 | "\n", 398 | "def jackFunc(value):\n", 399 | " value += 2\n", 400 | " return value\n", 401 | " \n", 402 | "def jillFunc(value):\n", 403 | " value -= 1\n", 404 | " return value" 405 | ] 406 | }, 407 | { 408 | "cell_type": "code", 409 | "execution_count": 53, 410 | "metadata": {}, 411 | "outputs": [], 412 | "source": [ 413 | "i = 0\n", 414 | "while i < 10:\n", 415 | " value = jillFunc(value)\n", 416 | " i += 1" 417 | ] 418 | }, 419 | { 420 | "cell_type": "code", 421 | "execution_count": 54, 422 | "metadata": {}, 423 | "outputs": [ 424 | { 425 | "data": { 426 | "text/plain": [ 427 | "-10" 428 | ] 429 | }, 430 | "execution_count": 54, 431 | "metadata": {}, 432 | "output_type": "execute_result" 433 | } 434 | ], 435 | "source": [ 436 | "value" 437 | ] 438 | }, 439 | { 440 | "cell_type": "code", 441 | "execution_count": null, 442 | "metadata": {}, 443 | "outputs": [], 444 | "source": [ 445 | "\n", 446 | "Problem Statement :\n", 447 | "\n", 448 | "1. Write a function a takes a string\n", 449 | "2. Check if the string contains a vowel ( a )\n", 450 | "3. If it does, print it once \n", 451 | "4. Or, print the word two times\n", 452 | "\n", 453 | "Solution :\n", 454 | "\n", 455 | "1. Write a function to check if the word contains vowel\n", 456 | "2. Use the output from above to print desired number of times" 457 | ] 458 | }, 459 | { 460 | "cell_type": "code", 461 | "execution_count": 55, 462 | "metadata": {}, 463 | "outputs": [], 464 | "source": [ 465 | "def checkIfWordConatins(word, checkalpha):\n", 466 | " for ch in word:\n", 467 | " if ch in checkalpha:\n", 468 | " return True\n", 469 | " return False\n" 470 | ] 471 | }, 472 | { 473 | "cell_type": "code", 474 | "execution_count": 56, 475 | "metadata": {}, 476 | "outputs": [ 477 | { 478 | "data": { 479 | "text/plain": [ 480 | "False" 481 | ] 482 | }, 483 | "execution_count": 56, 484 | "metadata": {}, 485 | "output_type": "execute_result" 486 | } 487 | ], 488 | "source": [ 489 | "checkIfWordConatins('sst','xyz')" 490 | ] 491 | }, 492 | { 493 | "cell_type": "code", 494 | "execution_count": 57, 495 | "metadata": {}, 496 | "outputs": [], 497 | "source": [ 498 | "def printWordTimes(word,times):\n", 499 | " for t in range(times):\n", 500 | " print (word)" 501 | ] 502 | }, 503 | { 504 | "cell_type": "code", 505 | "execution_count": 58, 506 | "metadata": {}, 507 | "outputs": [], 508 | "source": [ 509 | "def myUtilityCode(word, checkalpha, times):\n", 510 | " check = checkIfWordConatins(word, checkalpha)\n", 511 | " if check:\n", 512 | " print (word)\n", 513 | " else:\n", 514 | " printWordTimes(word, times)" 515 | ] 516 | }, 517 | { 518 | "cell_type": "code", 519 | "execution_count": 59, 520 | "metadata": {}, 521 | "outputs": [ 522 | { 523 | "name": "stdout", 524 | "output_type": "stream", 525 | "text": [ 526 | "Hello\n" 527 | ] 528 | } 529 | ], 530 | "source": [ 531 | "myUtilityCode('Hello','el',3)" 532 | ] 533 | }, 534 | { 535 | "cell_type": "code", 536 | "execution_count": 60, 537 | "metadata": {}, 538 | "outputs": [ 539 | { 540 | "name": "stdout", 541 | "output_type": "stream", 542 | "text": [ 543 | "Hello\n", 544 | "Hello\n", 545 | "Hello\n" 546 | ] 547 | } 548 | ], 549 | "source": [ 550 | "myUtilityCode('Hello','xy',3)" 551 | ] 552 | }, 553 | { 554 | "cell_type": "code", 555 | "execution_count": null, 556 | "metadata": {}, 557 | "outputs": [], 558 | "source": [ 559 | "Default Params" 560 | ] 561 | }, 562 | { 563 | "cell_type": "code", 564 | "execution_count": 61, 565 | "metadata": {}, 566 | "outputs": [], 567 | "source": [ 568 | "def printMyDetails(name, age=18, location='Karnataka'):\n", 569 | " print (name,age,location)" 570 | ] 571 | }, 572 | { 573 | "cell_type": "code", 574 | "execution_count": 62, 575 | "metadata": {}, 576 | "outputs": [ 577 | { 578 | "name": "stdout", 579 | "output_type": "stream", 580 | "text": [ 581 | "Edyoda 3 Bangalore\n" 582 | ] 583 | } 584 | ], 585 | "source": [ 586 | "printMyDetails('Edyoda',3,'Bangalore')" 587 | ] 588 | }, 589 | { 590 | "cell_type": "code", 591 | "execution_count": 63, 592 | "metadata": {}, 593 | "outputs": [ 594 | { 595 | "name": "stdout", 596 | "output_type": "stream", 597 | "text": [ 598 | "Edyoda abc Karnataka\n" 599 | ] 600 | } 601 | ], 602 | "source": [ 603 | "printMyDetails('Edyoda','abc')" 604 | ] 605 | }, 606 | { 607 | "cell_type": "code", 608 | "execution_count": null, 609 | "metadata": {}, 610 | "outputs": [], 611 | "source": [ 612 | "Named Parameters" 613 | ] 614 | }, 615 | { 616 | "cell_type": "code", 617 | "execution_count": 64, 618 | "metadata": {}, 619 | "outputs": [], 620 | "source": [ 621 | "def printMyDetails(name, age=18, location='Karnataka'):\n", 622 | " print (name,age)" 623 | ] 624 | }, 625 | { 626 | "cell_type": "code", 627 | "execution_count": 65, 628 | "metadata": {}, 629 | "outputs": [ 630 | { 631 | "name": "stdout", 632 | "output_type": "stream", 633 | "text": [ 634 | "abcdef 20\n" 635 | ] 636 | } 637 | ], 638 | "source": [ 639 | "printMyDetails(age=20, name='abcdef', location='Mumbai')" 640 | ] 641 | }, 642 | { 643 | "cell_type": "code", 644 | "execution_count": null, 645 | "metadata": {}, 646 | "outputs": [], 647 | "source": [ 648 | "Variable Arguments" 649 | ] 650 | }, 651 | { 652 | "cell_type": "code", 653 | "execution_count": null, 654 | "metadata": {}, 655 | "outputs": [], 656 | "source": [ 657 | "*packing" 658 | ] 659 | }, 660 | { 661 | "cell_type": "code", 662 | "execution_count": 66, 663 | "metadata": {}, 664 | "outputs": [], 665 | "source": [ 666 | "def mySum(*args):\n", 667 | " res = 0\n", 668 | " for e in args:\n", 669 | " res += e\n", 670 | " return res" 671 | ] 672 | }, 673 | { 674 | "cell_type": "code", 675 | "execution_count": 67, 676 | "metadata": {}, 677 | "outputs": [], 678 | "source": [ 679 | "def mySum(*args):\n", 680 | " return sum(args)" 681 | ] 682 | }, 683 | { 684 | "cell_type": "code", 685 | "execution_count": 68, 686 | "metadata": {}, 687 | "outputs": [ 688 | { 689 | "data": { 690 | "text/plain": [ 691 | "30" 692 | ] 693 | }, 694 | "execution_count": 68, 695 | "metadata": {}, 696 | "output_type": "execute_result" 697 | } 698 | ], 699 | "source": [ 700 | "mySum(10,20)" 701 | ] 702 | }, 703 | { 704 | "cell_type": "code", 705 | "execution_count": 69, 706 | "metadata": {}, 707 | "outputs": [ 708 | { 709 | "data": { 710 | "text/plain": [ 711 | "60" 712 | ] 713 | }, 714 | "execution_count": 69, 715 | "metadata": {}, 716 | "output_type": "execute_result" 717 | } 718 | ], 719 | "source": [ 720 | "mySum(10,20,30)" 721 | ] 722 | }, 723 | { 724 | "cell_type": "code", 725 | "execution_count": null, 726 | "metadata": {}, 727 | "outputs": [], 728 | "source": [ 729 | "* Unpacking" 730 | ] 731 | }, 732 | { 733 | "cell_type": "code", 734 | "execution_count": 70, 735 | "metadata": {}, 736 | "outputs": [], 737 | "source": [ 738 | "l = ['blore','edyoda',10]" 739 | ] 740 | }, 741 | { 742 | "cell_type": "code", 743 | "execution_count": 71, 744 | "metadata": {}, 745 | "outputs": [], 746 | "source": [ 747 | "def printDetails(loc, name, age):\n", 748 | " print (loc, name, age)" 749 | ] 750 | }, 751 | { 752 | "cell_type": "code", 753 | "execution_count": 72, 754 | "metadata": {}, 755 | "outputs": [ 756 | { 757 | "name": "stdout", 758 | "output_type": "stream", 759 | "text": [ 760 | "blore edyoda 10\n" 761 | ] 762 | } 763 | ], 764 | "source": [ 765 | "printDetails(*l)" 766 | ] 767 | }, 768 | { 769 | "cell_type": "code", 770 | "execution_count": null, 771 | "metadata": {}, 772 | "outputs": [], 773 | "source": [ 774 | "Named Packing" 775 | ] 776 | }, 777 | { 778 | "cell_type": "code", 779 | "execution_count": 73, 780 | "metadata": {}, 781 | "outputs": [], 782 | "source": [ 783 | "def myFunc(name, loc, age):\n", 784 | " if name == 'abc':\n", 785 | " print (name)\n", 786 | " else:\n", 787 | " print (loc,age)" 788 | ] 789 | }, 790 | { 791 | "cell_type": "code", 792 | "execution_count": 74, 793 | "metadata": {}, 794 | "outputs": [ 795 | { 796 | "name": "stdout", 797 | "output_type": "stream", 798 | "text": [ 799 | "abc\n" 800 | ] 801 | } 802 | ], 803 | "source": [ 804 | "myFunc(loc='mumbai',age=10,name='abc')" 805 | ] 806 | }, 807 | { 808 | "cell_type": "code", 809 | "execution_count": 75, 810 | "metadata": {}, 811 | "outputs": [], 812 | "source": [ 813 | "def myFunc(**kwargs):\n", 814 | " if kwargs['name'] == 'abc':\n", 815 | " print (kwargs['name'])\n", 816 | " else:\n", 817 | " print (kwargs['loc'], kwargs['age'])\n", 818 | " \n", 819 | " if 'name' in kwargs:\n", 820 | " print (kwargs['name'])" 821 | ] 822 | }, 823 | { 824 | "cell_type": "code", 825 | "execution_count": 76, 826 | "metadata": {}, 827 | "outputs": [ 828 | { 829 | "name": "stdout", 830 | "output_type": "stream", 831 | "text": [ 832 | "mumbai 10\n", 833 | "bcd\n" 834 | ] 835 | } 836 | ], 837 | "source": [ 838 | "myFunc(loc='mumbai',age=10,name='bcd')" 839 | ] 840 | }, 841 | { 842 | "cell_type": "code", 843 | "execution_count": 77, 844 | "metadata": {}, 845 | "outputs": [ 846 | { 847 | "name": "stdout", 848 | "output_type": "stream", 849 | "text": [ 850 | "abc\n", 851 | "abc\n" 852 | ] 853 | } 854 | ], 855 | "source": [ 856 | "myFunc(location='mumbai',age=10,name='abc')" 857 | ] 858 | }, 859 | { 860 | "cell_type": "code", 861 | "execution_count": null, 862 | "metadata": {}, 863 | "outputs": [], 864 | "source": [ 865 | "Problem :\n", 866 | "\n", 867 | "* mySumFunc(10,20,11,21,23)\n", 868 | "* mySumFunc(10,20,11,21,23,445,2323)\n", 869 | "* Multiply all the even numbers" 870 | ] 871 | }, 872 | { 873 | "cell_type": "code", 874 | "execution_count": 78, 875 | "metadata": {}, 876 | "outputs": [], 877 | "source": [ 878 | "def myMultFunc(*nums):\n", 879 | " res = 1\n", 880 | " for num in nums:\n", 881 | " if num %2 == 0:\n", 882 | " res *= num\n", 883 | " return res" 884 | ] 885 | }, 886 | { 887 | "cell_type": "code", 888 | "execution_count": 79, 889 | "metadata": {}, 890 | "outputs": [ 891 | { 892 | "data": { 893 | "text/plain": [ 894 | "4400" 895 | ] 896 | }, 897 | "execution_count": 79, 898 | "metadata": {}, 899 | "output_type": "execute_result" 900 | } 901 | ], 902 | "source": [ 903 | "myMultFunc(10,20,22,23)" 904 | ] 905 | }, 906 | { 907 | "cell_type": "code", 908 | "execution_count": null, 909 | "metadata": {}, 910 | "outputs": [], 911 | "source": [] 912 | } 913 | ], 914 | "metadata": { 915 | "kernelspec": { 916 | "display_name": "Python 3", 917 | "language": "python", 918 | "name": "python3" 919 | }, 920 | "language_info": { 921 | "codemirror_mode": { 922 | "name": "ipython", 923 | "version": 3 924 | }, 925 | "file_extension": ".py", 926 | "mimetype": "text/x-python", 927 | "name": "python", 928 | "nbconvert_exporter": "python", 929 | "pygments_lexer": "ipython3", 930 | "version": "3.7.4" 931 | } 932 | }, 933 | "nbformat": 4, 934 | "nbformat_minor": 2 935 | } 936 | -------------------------------------------------------------------------------- /Session 11 - Functions Problems.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "def vargFunc(*args):\n", 10 | " print (type(args))" 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": 2, 16 | "metadata": {}, 17 | "outputs": [ 18 | { 19 | "name": "stdout", 20 | "output_type": "stream", 21 | "text": [ 22 | "\n" 23 | ] 24 | } 25 | ], 26 | "source": [ 27 | "vargFunc(1,2,3,4)" 28 | ] 29 | }, 30 | { 31 | "cell_type": "code", 32 | "execution_count": 3, 33 | "metadata": {}, 34 | "outputs": [ 35 | { 36 | "name": "stdout", 37 | "output_type": "stream", 38 | "text": [ 39 | "\n" 40 | ] 41 | } 42 | ], 43 | "source": [ 44 | "vargFunc(4,5,6,7,78)" 45 | ] 46 | }, 47 | { 48 | "cell_type": "code", 49 | "execution_count": null, 50 | "metadata": {}, 51 | "outputs": [], 52 | "source": [ 53 | "Problems related to functions\n", 54 | "\n", 55 | "Call a function with multiple words\n", 56 | "Return a string separated by spaces containing only the words of even length" 57 | ] 58 | }, 59 | { 60 | "cell_type": "code", 61 | "execution_count": 4, 62 | "metadata": {}, 63 | "outputs": [], 64 | "source": [ 65 | "def evenWordsCollector(*args):\n", 66 | " l = []\n", 67 | " for word in args:\n", 68 | " if ifWordEven(word):\n", 69 | " l.append(word)\n", 70 | " res = ' '.join(l)\n", 71 | " return res" 72 | ] 73 | }, 74 | { 75 | "cell_type": "code", 76 | "execution_count": 5, 77 | "metadata": {}, 78 | "outputs": [], 79 | "source": [ 80 | "def ifWordEven(word):\n", 81 | " if len(word) % 2 == 0:\n", 82 | " return True\n", 83 | " else:\n", 84 | " return False" 85 | ] 86 | }, 87 | { 88 | "cell_type": "code", 89 | "execution_count": 6, 90 | "metadata": {}, 91 | "outputs": [ 92 | { 93 | "data": { 94 | "text/plain": [ 95 | "'abcd jklm'" 96 | ] 97 | }, 98 | "execution_count": 6, 99 | "metadata": {}, 100 | "output_type": "execute_result" 101 | } 102 | ], 103 | "source": [ 104 | "evenWordsCollector('abc','def','abcd','jklm')" 105 | ] 106 | }, 107 | { 108 | "cell_type": "code", 109 | "execution_count": 7, 110 | "metadata": {}, 111 | "outputs": [ 112 | { 113 | "data": { 114 | "text/plain": [ 115 | "True" 116 | ] 117 | }, 118 | "execution_count": 7, 119 | "metadata": {}, 120 | "output_type": "execute_result" 121 | } 122 | ], 123 | "source": [ 124 | "ifWordEven('Hell')" 125 | ] 126 | }, 127 | { 128 | "cell_type": "code", 129 | "execution_count": 8, 130 | "metadata": {}, 131 | "outputs": [ 132 | { 133 | "data": { 134 | "text/plain": [ 135 | "'abcd jklm hjhj'" 136 | ] 137 | }, 138 | "execution_count": 8, 139 | "metadata": {}, 140 | "output_type": "execute_result" 141 | } 142 | ], 143 | "source": [ 144 | "evenWordsCollector('abc','def','abcd','jklm','hjhj')" 145 | ] 146 | }, 147 | { 148 | "cell_type": "code", 149 | "execution_count": null, 150 | "metadata": {}, 151 | "outputs": [], 152 | "source": [ 153 | "Problem Statement\n", 154 | "\n", 155 | "Take Employee Data & put them in a list" 156 | ] 157 | }, 158 | { 159 | "cell_type": "code", 160 | "execution_count": 9, 161 | "metadata": {}, 162 | "outputs": [], 163 | "source": [ 164 | "def createEmpData(list_emps, **kwargs):\n", 165 | " list_emps.append(kwargs)" 166 | ] 167 | }, 168 | { 169 | "cell_type": "code", 170 | "execution_count": 10, 171 | "metadata": {}, 172 | "outputs": [], 173 | "source": [ 174 | "list_emps = []\n", 175 | "\n", 176 | "createEmpData(list_emps, name='abc',age=20)" 177 | ] 178 | }, 179 | { 180 | "cell_type": "code", 181 | "execution_count": 11, 182 | "metadata": {}, 183 | "outputs": [ 184 | { 185 | "data": { 186 | "text/plain": [ 187 | "[{'name': 'abc', 'age': 20}]" 188 | ] 189 | }, 190 | "execution_count": 11, 191 | "metadata": {}, 192 | "output_type": "execute_result" 193 | } 194 | ], 195 | "source": [ 196 | "list_emps" 197 | ] 198 | }, 199 | { 200 | "cell_type": "code", 201 | "execution_count": 12, 202 | "metadata": {}, 203 | "outputs": [], 204 | "source": [ 205 | "createEmpData(list_emps, name='def',age=20,location='blore')" 206 | ] 207 | }, 208 | { 209 | "cell_type": "code", 210 | "execution_count": 13, 211 | "metadata": {}, 212 | "outputs": [ 213 | { 214 | "data": { 215 | "text/plain": [ 216 | "[{'name': 'abc', 'age': 20}, {'name': 'def', 'age': 20, 'location': 'blore'}]" 217 | ] 218 | }, 219 | "execution_count": 13, 220 | "metadata": {}, 221 | "output_type": "execute_result" 222 | } 223 | ], 224 | "source": [ 225 | "list_emps" 226 | ] 227 | }, 228 | { 229 | "cell_type": "code", 230 | "execution_count": 15, 231 | "metadata": {}, 232 | "outputs": [], 233 | "source": [ 234 | "createEmpData(list_emps, name='def',age=20,salary=100000)\n", 235 | "createEmpData(list_emps, name='def',age=20,salary=500000)" 236 | ] 237 | }, 238 | { 239 | "cell_type": "code", 240 | "execution_count": 16, 241 | "metadata": {}, 242 | "outputs": [ 243 | { 244 | "data": { 245 | "text/plain": [ 246 | "[{'name': 'abc', 'age': 20},\n", 247 | " {'name': 'def', 'age': 20, 'location': 'blore'},\n", 248 | " {'name': 'def', 'age': 20, 'salary': 100000},\n", 249 | " {'name': 'def', 'age': 20, 'salary': 500000}]" 250 | ] 251 | }, 252 | "execution_count": 16, 253 | "metadata": {}, 254 | "output_type": "execute_result" 255 | } 256 | ], 257 | "source": [ 258 | "list_emps" 259 | ] 260 | }, 261 | { 262 | "cell_type": "code", 263 | "execution_count": 17, 264 | "metadata": {}, 265 | "outputs": [ 266 | { 267 | "name": "stdout", 268 | "output_type": "stream", 269 | "text": [ 270 | "{'name': 'def', 'age': 20, 'location': 'blore'}\n" 271 | ] 272 | } 273 | ], 274 | "source": [ 275 | "for emp in list_emps:\n", 276 | " if 'location' in emp:\n", 277 | " print (emp)" 278 | ] 279 | }, 280 | { 281 | "cell_type": "code", 282 | "execution_count": 18, 283 | "metadata": {}, 284 | "outputs": [], 285 | "source": [ 286 | "# Print folks whose salary is >= 100000\n", 287 | "def getPeopleSalary(list_emps,salary=50000):\n", 288 | " res = []\n", 289 | " for emp in list_emps:\n", 290 | " if 'salary' in emp:\n", 291 | " if emp['salary'] >= salary:\n", 292 | " res.append(emp)\n", 293 | " return res" 294 | ] 295 | }, 296 | { 297 | "cell_type": "code", 298 | "execution_count": 20, 299 | "metadata": {}, 300 | "outputs": [ 301 | { 302 | "data": { 303 | "text/plain": [ 304 | "[{'name': 'def', 'age': 20, 'salary': 100000},\n", 305 | " {'name': 'def', 'age': 20, 'salary': 500000}]" 306 | ] 307 | }, 308 | "execution_count": 20, 309 | "metadata": {}, 310 | "output_type": "execute_result" 311 | } 312 | ], 313 | "source": [ 314 | "getPeopleSalary(list_emps,100000)" 315 | ] 316 | }, 317 | { 318 | "cell_type": "code", 319 | "execution_count": null, 320 | "metadata": {}, 321 | "outputs": [], 322 | "source": [ 323 | "Problem\n", 324 | "\n", 325 | "Store students's marks information in a list [{'name':'abc','maths':20,'eglish':30}, {}]\n", 326 | "Find topper in class\n", 327 | "\n", 328 | "Pseudo-Code\n", 329 | "\n", 330 | "AddStudent Function\n", 331 | "Calculate marks percentage\n", 332 | "Find Topper" 333 | ] 334 | }, 335 | { 336 | "cell_type": "code", 337 | "execution_count": 21, 338 | "metadata": {}, 339 | "outputs": [], 340 | "source": [ 341 | "def createStudentData(list_students, **marks):\n", 342 | " list_students.append(marks)" 343 | ] 344 | }, 345 | { 346 | "cell_type": "code", 347 | "execution_count": 22, 348 | "metadata": {}, 349 | "outputs": [], 350 | "source": [ 351 | "list_students = []\n", 352 | "createStudentData(list_students,name='david',maths=80, science=75,english=76)\n", 353 | "createStudentData(list_students,name='rohit',maths=90, science=85,english=66)\n", 354 | "createStudentData(list_students,name='raj',maths=40, science=55,english=56)\n", 355 | "createStudentData(list_students,name='yazhini',maths=80, science=88,english=76)\n" 356 | ] 357 | }, 358 | { 359 | "cell_type": "code", 360 | "execution_count": 23, 361 | "metadata": {}, 362 | "outputs": [ 363 | { 364 | "data": { 365 | "text/plain": [ 366 | "[{'name': 'david', 'maths': 80, 'science': 75, 'english': 76},\n", 367 | " {'name': 'rohit', 'maths': 90, 'science': 85, 'english': 66},\n", 368 | " {'name': 'raj', 'maths': 40, 'science': 55, 'english': 56},\n", 369 | " {'name': 'yazhini', 'maths': 80, 'science': 88, 'english': 76}]" 370 | ] 371 | }, 372 | "execution_count": 23, 373 | "metadata": {}, 374 | "output_type": "execute_result" 375 | } 376 | ], 377 | "source": [ 378 | "list_students" 379 | ] 380 | }, 381 | { 382 | "cell_type": "code", 383 | "execution_count": 24, 384 | "metadata": {}, 385 | "outputs": [], 386 | "source": [ 387 | "def calcMarksAvg(list_students):\n", 388 | " for student in list_students:\n", 389 | " total_marks = 0\n", 390 | " count = 0\n", 391 | " for k,v in student.items():\n", 392 | " if k != 'name':\n", 393 | " total_marks += v\n", 394 | " count += 1\n", 395 | " student['avg'] = total_marks/count\n" 396 | ] 397 | }, 398 | { 399 | "cell_type": "code", 400 | "execution_count": 25, 401 | "metadata": {}, 402 | "outputs": [], 403 | "source": [ 404 | "calcMarksAvg(list_students)" 405 | ] 406 | }, 407 | { 408 | "cell_type": "code", 409 | "execution_count": 26, 410 | "metadata": {}, 411 | "outputs": [ 412 | { 413 | "data": { 414 | "text/plain": [ 415 | "[{'name': 'david', 'maths': 80, 'science': 75, 'english': 76, 'avg': 77.0},\n", 416 | " {'name': 'rohit',\n", 417 | " 'maths': 90,\n", 418 | " 'science': 85,\n", 419 | " 'english': 66,\n", 420 | " 'avg': 80.33333333333333},\n", 421 | " {'name': 'raj',\n", 422 | " 'maths': 40,\n", 423 | " 'science': 55,\n", 424 | " 'english': 56,\n", 425 | " 'avg': 50.333333333333336},\n", 426 | " {'name': 'yazhini',\n", 427 | " 'maths': 80,\n", 428 | " 'science': 88,\n", 429 | " 'english': 76,\n", 430 | " 'avg': 81.33333333333333}]" 431 | ] 432 | }, 433 | "execution_count": 26, 434 | "metadata": {}, 435 | "output_type": "execute_result" 436 | } 437 | ], 438 | "source": [ 439 | "list_students" 440 | ] 441 | }, 442 | { 443 | "cell_type": "code", 444 | "execution_count": 27, 445 | "metadata": {}, 446 | "outputs": [ 447 | { 448 | "name": "stdout", 449 | "output_type": "stream", 450 | "text": [ 451 | "Help on built-in function max in module builtins:\n", 452 | "\n", 453 | "max(...)\n", 454 | " max(iterable, *[, default=obj, key=func]) -> value\n", 455 | " max(arg1, arg2, *args, *[, key=func]) -> value\n", 456 | " \n", 457 | " With a single iterable argument, return its biggest item. The\n", 458 | " default keyword-only argument specifies an object to return if\n", 459 | " the provided iterable is empty.\n", 460 | " With two or more arguments, return the largest argument.\n", 461 | "\n" 462 | ] 463 | } 464 | ], 465 | "source": [ 466 | "help(max)" 467 | ] 468 | }, 469 | { 470 | "cell_type": "code", 471 | "execution_count": 28, 472 | "metadata": {}, 473 | "outputs": [], 474 | "source": [ 475 | "def findTopper(list_students):\n", 476 | " def f(e):\n", 477 | " return e['avg']\n", 478 | "\n", 479 | " return max(list_students,key=f)" 480 | ] 481 | }, 482 | { 483 | "cell_type": "code", 484 | "execution_count": 29, 485 | "metadata": {}, 486 | "outputs": [], 487 | "source": [ 488 | "l = [1,233,9]" 489 | ] 490 | }, 491 | { 492 | "cell_type": "code", 493 | "execution_count": 30, 494 | "metadata": {}, 495 | "outputs": [ 496 | { 497 | "data": { 498 | "text/plain": [ 499 | "233" 500 | ] 501 | }, 502 | "execution_count": 30, 503 | "metadata": {}, 504 | "output_type": "execute_result" 505 | } 506 | ], 507 | "source": [ 508 | "max(l)" 509 | ] 510 | }, 511 | { 512 | "cell_type": "code", 513 | "execution_count": 31, 514 | "metadata": {}, 515 | "outputs": [ 516 | { 517 | "data": { 518 | "text/plain": [ 519 | "{'name': 'yazhini',\n", 520 | " 'maths': 80,\n", 521 | " 'science': 88,\n", 522 | " 'english': 76,\n", 523 | " 'avg': 81.33333333333333}" 524 | ] 525 | }, 526 | "execution_count": 31, 527 | "metadata": {}, 528 | "output_type": "execute_result" 529 | } 530 | ], 531 | "source": [ 532 | "def f(e):\n", 533 | " return e['avg']\n", 534 | "max(list_students,key=f)\n" 535 | ] 536 | }, 537 | { 538 | "cell_type": "code", 539 | "execution_count": 32, 540 | "metadata": {}, 541 | "outputs": [], 542 | "source": [ 543 | "topper = findTopper(list_students)" 544 | ] 545 | }, 546 | { 547 | "cell_type": "code", 548 | "execution_count": 33, 549 | "metadata": {}, 550 | "outputs": [ 551 | { 552 | "data": { 553 | "text/plain": [ 554 | "'yazhini'" 555 | ] 556 | }, 557 | "execution_count": 33, 558 | "metadata": {}, 559 | "output_type": "execute_result" 560 | } 561 | ], 562 | "source": [ 563 | "topper['name']" 564 | ] 565 | }, 566 | { 567 | "cell_type": "code", 568 | "execution_count": 34, 569 | "metadata": {}, 570 | "outputs": [ 571 | { 572 | "data": { 573 | "text/plain": [ 574 | "[{'name': 'raj',\n", 575 | " 'maths': 40,\n", 576 | " 'science': 55,\n", 577 | " 'english': 56,\n", 578 | " 'avg': 50.333333333333336},\n", 579 | " {'name': 'david', 'maths': 80, 'science': 75, 'english': 76, 'avg': 77.0},\n", 580 | " {'name': 'rohit',\n", 581 | " 'maths': 90,\n", 582 | " 'science': 85,\n", 583 | " 'english': 66,\n", 584 | " 'avg': 80.33333333333333},\n", 585 | " {'name': 'yazhini',\n", 586 | " 'maths': 80,\n", 587 | " 'science': 88,\n", 588 | " 'english': 76,\n", 589 | " 'avg': 81.33333333333333}]" 590 | ] 591 | }, 592 | "execution_count": 34, 593 | "metadata": {}, 594 | "output_type": "execute_result" 595 | } 596 | ], 597 | "source": [ 598 | "sorted(list_students,key=f)" 599 | ] 600 | }, 601 | { 602 | "cell_type": "code", 603 | "execution_count": null, 604 | "metadata": {}, 605 | "outputs": [], 606 | "source": [] 607 | } 608 | ], 609 | "metadata": { 610 | "kernelspec": { 611 | "display_name": "Python 3", 612 | "language": "python", 613 | "name": "python3" 614 | }, 615 | "language_info": { 616 | "codemirror_mode": { 617 | "name": "ipython", 618 | "version": 3 619 | }, 620 | "file_extension": ".py", 621 | "mimetype": "text/x-python", 622 | "name": "python", 623 | "nbconvert_exporter": "python", 624 | "pygments_lexer": "ipython3", 625 | "version": "3.7.4" 626 | } 627 | }, 628 | "nbformat": 4, 629 | "nbformat_minor": 2 630 | } 631 | -------------------------------------------------------------------------------- /Session 12 - Problems.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "Reverse key value in a dictionary\n", 10 | "\n", 11 | "Input : { 'a':20, 'b':20, 'c':22 }\n", 12 | "\n", 13 | "Output : { 20 :['a','b'], 22:'c' }" 14 | ] 15 | }, 16 | { 17 | "cell_type": "code", 18 | "execution_count": 1, 19 | "metadata": {}, 20 | "outputs": [], 21 | "source": [ 22 | "db = { 'a':20, 'b':20, 'c':22 , 'd':20}" 23 | ] 24 | }, 25 | { 26 | "cell_type": "code", 27 | "execution_count": 2, 28 | "metadata": {}, 29 | "outputs": [ 30 | { 31 | "data": { 32 | "text/plain": [ 33 | "{'a': 20, 'b': 20, 'c': 22, 'd': 20}" 34 | ] 35 | }, 36 | "execution_count": 2, 37 | "metadata": {}, 38 | "output_type": "execute_result" 39 | } 40 | ], 41 | "source": [ 42 | "db" 43 | ] 44 | }, 45 | { 46 | "cell_type": "code", 47 | "execution_count": 3, 48 | "metadata": {}, 49 | "outputs": [], 50 | "source": [ 51 | "newdb = {}" 52 | ] 53 | }, 54 | { 55 | "cell_type": "code", 56 | "execution_count": 4, 57 | "metadata": {}, 58 | "outputs": [], 59 | "source": [ 60 | "for k,v in db.items():\n", 61 | " #if v is appearing first time as a key\n", 62 | " if v not in newdb:\n", 63 | " newdb[v] = k\n", 64 | " #If v is appearing not first time as a key\n", 65 | " else:\n", 66 | " #If value is not yet a list, create one\n", 67 | " if not isinstance(newdb[v], list):\n", 68 | " temp = newdb[v]\n", 69 | " newdb[v] = [temp,k]\n", 70 | " #If value is a list, append k here\n", 71 | " else:\n", 72 | " newdb[v].append(k)" 73 | ] 74 | }, 75 | { 76 | "cell_type": "code", 77 | "execution_count": 5, 78 | "metadata": {}, 79 | "outputs": [ 80 | { 81 | "data": { 82 | "text/plain": [ 83 | "{20: ['a', 'b', 'd'], 22: 'c'}" 84 | ] 85 | }, 86 | "execution_count": 5, 87 | "metadata": {}, 88 | "output_type": "execute_result" 89 | } 90 | ], 91 | "source": [ 92 | "newdb" 93 | ] 94 | }, 95 | { 96 | "cell_type": "code", 97 | "execution_count": 6, 98 | "metadata": {}, 99 | "outputs": [ 100 | { 101 | "data": { 102 | "text/plain": [ 103 | "list" 104 | ] 105 | }, 106 | "execution_count": 6, 107 | "metadata": {}, 108 | "output_type": "execute_result" 109 | } 110 | ], 111 | "source": [ 112 | "\n", 113 | "type(newdb[20])\n" 114 | ] 115 | }, 116 | { 117 | "cell_type": "code", 118 | "execution_count": 7, 119 | "metadata": {}, 120 | "outputs": [ 121 | { 122 | "data": { 123 | "text/plain": [ 124 | "True" 125 | ] 126 | }, 127 | "execution_count": 7, 128 | "metadata": {}, 129 | "output_type": "execute_result" 130 | } 131 | ], 132 | "source": [ 133 | "isinstance(newdb[20], list)" 134 | ] 135 | }, 136 | { 137 | "cell_type": "code", 138 | "execution_count": 8, 139 | "metadata": {}, 140 | "outputs": [ 141 | { 142 | "data": { 143 | "text/plain": [ 144 | "True" 145 | ] 146 | }, 147 | "execution_count": 8, 148 | "metadata": {}, 149 | "output_type": "execute_result" 150 | } 151 | ], 152 | "source": [ 153 | "isinstance({},dict)" 154 | ] 155 | }, 156 | { 157 | "cell_type": "code", 158 | "execution_count": 9, 159 | "metadata": {}, 160 | "outputs": [ 161 | { 162 | "data": { 163 | "text/plain": [ 164 | "True" 165 | ] 166 | }, 167 | "execution_count": 9, 168 | "metadata": {}, 169 | "output_type": "execute_result" 170 | } 171 | ], 172 | "source": [ 173 | "isinstance('hello',str)" 174 | ] 175 | }, 176 | { 177 | "cell_type": "code", 178 | "execution_count": 10, 179 | "metadata": {}, 180 | "outputs": [], 181 | "source": [ 182 | "data = [['abc',10], ['adf',15], ['ghi',9]]" 183 | ] 184 | }, 185 | { 186 | "cell_type": "code", 187 | "execution_count": 11, 188 | "metadata": {}, 189 | "outputs": [ 190 | { 191 | "data": { 192 | "text/plain": [ 193 | "[['abc', 10], ['adf', 15], ['ghi', 9]]" 194 | ] 195 | }, 196 | "execution_count": 11, 197 | "metadata": {}, 198 | "output_type": "execute_result" 199 | } 200 | ], 201 | "source": [ 202 | "def f(e):\n", 203 | " return e[0]\n", 204 | "\n", 205 | "sorted(data, key=f)" 206 | ] 207 | }, 208 | { 209 | "cell_type": "code", 210 | "execution_count": null, 211 | "metadata": {}, 212 | "outputs": [], 213 | "source": [] 214 | } 215 | ], 216 | "metadata": { 217 | "kernelspec": { 218 | "display_name": "Python 3", 219 | "language": "python", 220 | "name": "python3" 221 | }, 222 | "language_info": { 223 | "codemirror_mode": { 224 | "name": "ipython", 225 | "version": 3 226 | }, 227 | "file_extension": ".py", 228 | "mimetype": "text/x-python", 229 | "name": "python", 230 | "nbconvert_exporter": "python", 231 | "pygments_lexer": "ipython3", 232 | "version": "3.7.4" 233 | } 234 | }, 235 | "nbformat": 4, 236 | "nbformat_minor": 2 237 | } 238 | -------------------------------------------------------------------------------- /Session 18 - Map - Filter and Lambda.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 2, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "map,filter and lambda" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": null, 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "map(fun obj,iterable datatype) #map is builtin func which takes a func object as 1st args and then iterable datatype" 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": 3, 24 | "metadata": {}, 25 | "outputs": [ 26 | { 27 | "name": "stdout", 28 | "output_type": "stream", 29 | "text": [ 30 | "[100, 400, 900, 1600, 2500, 3600]\n" 31 | ] 32 | } 33 | ], 34 | "source": [ 35 | "l = [10,20,30,40,50,60]\n", 36 | "l2 = [value * value for value in l]\n", 37 | "print(l2)" 38 | ] 39 | }, 40 | { 41 | "cell_type": "code", 42 | "execution_count": 4, 43 | "metadata": {}, 44 | "outputs": [ 45 | { 46 | "name": "stdout", 47 | "output_type": "stream", 48 | "text": [ 49 | "[100, 400, 900, 1600, 2500, 3600]\n" 50 | ] 51 | } 52 | ], 53 | "source": [ 54 | "def sqr(num1):\n", 55 | " return num1 * num1\n", 56 | "\n", 57 | "l = [10,20,30,40,50,60]\n", 58 | "r = map(sqr,l)\n", 59 | "print(list(r))" 60 | ] 61 | }, 62 | { 63 | "cell_type": "code", 64 | "execution_count": 5, 65 | "metadata": {}, 66 | "outputs": [ 67 | { 68 | "name": "stdout", 69 | "output_type": "stream", 70 | "text": [ 71 | "\n" 72 | ] 73 | } 74 | ], 75 | "source": [ 76 | "def sqr(num1):\n", 77 | " return num1 * num1\n", 78 | "\n", 79 | "l = [10,20,30,40,50,60]\n", 80 | "r = map(sqr,l)\n", 81 | "print((r))" 82 | ] 83 | }, 84 | { 85 | "cell_type": "code", 86 | "execution_count": 6, 87 | "metadata": {}, 88 | "outputs": [ 89 | { 90 | "name": "stdout", 91 | "output_type": "stream", 92 | "text": [ 93 | "100\n", 94 | "400\n" 95 | ] 96 | } 97 | ], 98 | "source": [ 99 | "def sqr(num1):\n", 100 | " return num1 * num1\n", 101 | "\n", 102 | "l = [10,20,30,40,50,60]\n", 103 | "r = map(sqr,l)\n", 104 | "#print(r)\n", 105 | "print(next(r)) # by using next it calculate next element one by one\n", 106 | "print(next(r))" 107 | ] 108 | }, 109 | { 110 | "cell_type": "code", 111 | "execution_count": 7, 112 | "metadata": {}, 113 | "outputs": [ 114 | { 115 | "name": "stdout", 116 | "output_type": "stream", 117 | "text": [ 118 | "100\n", 119 | "400\n", 120 | "900\n", 121 | "1600\n", 122 | "2500\n", 123 | "3600\n" 124 | ] 125 | } 126 | ], 127 | "source": [ 128 | "def sqr(num1):\n", 129 | " return num1 * num1\n", 130 | "\n", 131 | "l = [10,20,30,40,50,60]\n", 132 | "r = map(sqr,l)\n", 133 | "\n", 134 | "for value in r:\n", 135 | " print(value)" 136 | ] 137 | }, 138 | { 139 | "cell_type": "code", 140 | "execution_count": 8, 141 | "metadata": {}, 142 | "outputs": [ 143 | { 144 | "name": "stdout", 145 | "output_type": "stream", 146 | "text": [ 147 | "\n", 148 | "0.00019145000000264645\n" 149 | ] 150 | } 151 | ], 152 | "source": [ 153 | "import timeit\n", 154 | "t = timeit.timeit(stmt = \"\"\"\n", 155 | "def sqr(num1):\n", 156 | " return num1 * num1\n", 157 | "\n", 158 | "r = map(sqr,range(1,10000000))\n", 159 | "print(r)\n", 160 | "\"\"\",number = 1)\n", 161 | "print(t)" 162 | ] 163 | }, 164 | { 165 | "cell_type": "code", 166 | "execution_count": 9, 167 | "metadata": {}, 168 | "outputs": [ 169 | { 170 | "name": "stdout", 171 | "output_type": "stream", 172 | "text": [ 173 | "2.0845699620000033\n" 174 | ] 175 | } 176 | ], 177 | "source": [ 178 | "import timeit\n", 179 | "t = timeit.timeit(stmt = \"\"\"\n", 180 | "def sqr(num1):\n", 181 | " return num1 * num1\n", 182 | "\n", 183 | "r = map(sqr,range(1,10000000))\n", 184 | "t = list(r) \n", 185 | "\"\"\",number = 1) #for calculating actual time\n", 186 | "print(t)" 187 | ] 188 | }, 189 | { 190 | "cell_type": "code", 191 | "execution_count": 10, 192 | "metadata": {}, 193 | "outputs": [ 194 | { 195 | "name": "stdout", 196 | "output_type": "stream", 197 | "text": [ 198 | "[4, 5, 8, 8]\n" 199 | ] 200 | } 201 | ], 202 | "source": [ 203 | "l = ['abcd','erere','yrwrrqwf','tsrrrsrs']\n", 204 | "m = map(len,l)\n", 205 | "print(list(m))" 206 | ] 207 | }, 208 | { 209 | "cell_type": "code", 210 | "execution_count": 11, 211 | "metadata": {}, 212 | "outputs": [ 213 | { 214 | "name": "stdout", 215 | "output_type": "stream", 216 | "text": [ 217 | "[111, 222, 333]\n" 218 | ] 219 | } 220 | ], 221 | "source": [ 222 | "def add(num1,num2,num3):\n", 223 | " return num1 + num2 + num3\n", 224 | "\n", 225 | "l1 = [10,20,30]\n", 226 | "l2 = [1,2,3]\n", 227 | "l3 = [100,200,300]\n", 228 | "\n", 229 | "m = map(add,l1,l2,l3)\n", 230 | "print(list(m))" 231 | ] 232 | }, 233 | { 234 | "cell_type": "code", 235 | "execution_count": 12, 236 | "metadata": {}, 237 | "outputs": [ 238 | { 239 | "data": { 240 | "text/plain": [ 241 | "filter" 242 | ] 243 | }, 244 | "execution_count": 12, 245 | "metadata": {}, 246 | "output_type": "execute_result" 247 | } 248 | ], 249 | "source": [ 250 | "filter " 251 | ] 252 | }, 253 | { 254 | "cell_type": "code", 255 | "execution_count": 13, 256 | "metadata": {}, 257 | "outputs": [ 258 | { 259 | "name": "stdout", 260 | "output_type": "stream", 261 | "text": [ 262 | "[10, 20, 30, 40]\n" 263 | ] 264 | } 265 | ], 266 | "source": [ 267 | "def even(num1):\n", 268 | " if num1 % 2 == 0:\n", 269 | " return True\n", 270 | " else:\n", 271 | " return False\n", 272 | "l = [10,20,35,30,35,40,45]\n", 273 | "f = filter(even,l) #in filter the expecting answer should br true\n", 274 | "print(list(f))" 275 | ] 276 | }, 277 | { 278 | "cell_type": "code", 279 | "execution_count": 14, 280 | "metadata": {}, 281 | "outputs": [ 282 | { 283 | "name": "stdout", 284 | "output_type": "stream", 285 | "text": [ 286 | "[10, 20, 35, 30, 35, 40, 45]\n" 287 | ] 288 | } 289 | ], 290 | "source": [ 291 | "def even(num1):\n", 292 | " if num1 % 2 == 0:\n", 293 | " return 10\n", 294 | " else:\n", 295 | " return 100\n", 296 | "l = [10,20,35,30,35,40,45]\n", 297 | "f = filter(even,l) #in filter the expecting answer should br true\n", 298 | "print(list(f))" 299 | ] 300 | }, 301 | { 302 | "cell_type": "code", 303 | "execution_count": 15, 304 | "metadata": {}, 305 | "outputs": [ 306 | { 307 | "name": "stdout", 308 | "output_type": "stream", 309 | "text": [ 310 | "[10, 20, 30, 40]\n" 311 | ] 312 | } 313 | ], 314 | "source": [ 315 | "def even(num1):\n", 316 | " if num1 % 2 == 0:\n", 317 | " return 10\n", 318 | " else:\n", 319 | " return 0\n", 320 | "l = [10,20,35,30,35,40,45]\n", 321 | "f = filter(even,l) #in filter the expecting answer should br true\n", 322 | "print(list(f))" 323 | ] 324 | }, 325 | { 326 | "cell_type": "code", 327 | "execution_count": 16, 328 | "metadata": {}, 329 | "outputs": [ 330 | { 331 | "name": "stdout", 332 | "output_type": "stream", 333 | "text": [ 334 | "[True, True, False, True, False, True, False]\n" 335 | ] 336 | } 337 | ], 338 | "source": [ 339 | "def even(num1):\n", 340 | " if num1 % 2 == 0:\n", 341 | " return True\n", 342 | " else:\n", 343 | " return False\n", 344 | "l = [10,20,35,30,35,40,45]\n", 345 | "f = map(even,l) # in map we cant filter out the elements\n", 346 | "print(list(f))" 347 | ] 348 | }, 349 | { 350 | "cell_type": "code", 351 | "execution_count": 19, 352 | "metadata": {}, 353 | "outputs": [ 354 | { 355 | "name": "stdout", 356 | "output_type": "stream", 357 | "text": [ 358 | "['ad', 'ps', 'xx', 'rr']\n" 359 | ] 360 | } 361 | ], 362 | "source": [ 363 | "def get_char(val1):\n", 364 | " return val1[0]+val1[-1]\n", 365 | "\n", 366 | "l = ['abcd','pqrs','xyzx',\"rrr\"]\n", 367 | "m = map(get_char,l)\n", 368 | "print(list(m))" 369 | ] 370 | }, 371 | { 372 | "cell_type": "code", 373 | "execution_count": null, 374 | "metadata": {}, 375 | "outputs": [], 376 | "source": [ 377 | "l = [6,45,28,12,35,89]" 378 | ] 379 | }, 380 | { 381 | "cell_type": "code", 382 | "execution_count": null, 383 | "metadata": {}, 384 | "outputs": [], 385 | "source": [ 386 | "perfect_no : 6 : 1 + 2 + 3 = 6" 387 | ] 388 | }, 389 | { 390 | "cell_type": "code", 391 | "execution_count": 27, 392 | "metadata": {}, 393 | "outputs": [ 394 | { 395 | "name": "stdout", 396 | "output_type": "stream", 397 | "text": [ 398 | "1\n", 399 | "2\n", 400 | "3\n" 401 | ] 402 | } 403 | ], 404 | "source": [ 405 | "def perfect(num1):\n", 406 | " for value in range(1,num1):\n", 407 | " if num1 % value == 0:\n", 408 | " print(value)\n", 409 | "num1 = 6 \n", 410 | "perfect(num1)" 411 | ] 412 | }, 413 | { 414 | "cell_type": "code", 415 | "execution_count": 32, 416 | "metadata": {}, 417 | "outputs": [ 418 | { 419 | "data": { 420 | "text/plain": [ 421 | "True" 422 | ] 423 | }, 424 | "execution_count": 32, 425 | "metadata": {}, 426 | "output_type": "execute_result" 427 | } 428 | ], 429 | "source": [ 430 | "def perfect(num1):\n", 431 | " sum = 0\n", 432 | " for value in range(1,num1//2 + 1):\n", 433 | " if num1 % value == 0:\n", 434 | " sum += value\n", 435 | " if sum == num1:\n", 436 | " return True\n", 437 | " else:\n", 438 | " return False\n", 439 | "num1 = 6\n", 440 | "perfect(num1)" 441 | ] 442 | }, 443 | { 444 | "cell_type": "code", 445 | "execution_count": 34, 446 | "metadata": {}, 447 | "outputs": [ 448 | { 449 | "name": "stdout", 450 | "output_type": "stream", 451 | "text": [ 452 | "[6, 28]\n" 453 | ] 454 | } 455 | ], 456 | "source": [ 457 | "def perfect(num1):\n", 458 | " sum = 0\n", 459 | " for value in range(1,num1//2 + 1):\n", 460 | " if num1 % value == 0:\n", 461 | " sum += value\n", 462 | " if sum == num1:\n", 463 | " return True\n", 464 | " else:\n", 465 | " return False\n", 466 | "\n", 467 | "l = [6,45,28,12,35,89] #perfect num from the list\n", 468 | "f = filter(perfect,l)\n", 469 | "print(list(f))" 470 | ] 471 | }, 472 | { 473 | "cell_type": "code", 474 | "execution_count": 39, 475 | "metadata": {}, 476 | "outputs": [ 477 | { 478 | "name": "stdout", 479 | "output_type": "stream", 480 | "text": [ 481 | "[6, 28, 496]\n" 482 | ] 483 | } 484 | ], 485 | "source": [ 486 | "def perfect(num1):\n", 487 | " sum = 0\n", 488 | " for value in range(1,num1//2 + 1):\n", 489 | " if num1 % value == 0:\n", 490 | " sum += value\n", 491 | " if sum == num1:\n", 492 | " return True\n", 493 | " else:\n", 494 | " return False\n", 495 | "\n", 496 | "l = [6,45,28,12,35,89] \n", 497 | "f = filter(perfect,range(1,1000)) #perfect num in the basis range\n", 498 | "print(list(f))" 499 | ] 500 | }, 501 | { 502 | "cell_type": "code", 503 | "execution_count": null, 504 | "metadata": {}, 505 | "outputs": [], 506 | "source": [ 507 | "lambda func : a single line anonymous func that executed in a line " 508 | ] 509 | }, 510 | { 511 | "cell_type": "code", 512 | "execution_count": null, 513 | "metadata": {}, 514 | "outputs": [], 515 | "source": [ 516 | "def add()\n", 517 | "\n", 518 | "add()" 519 | ] 520 | }, 521 | { 522 | "cell_type": "code", 523 | "execution_count": null, 524 | "metadata": {}, 525 | "outputs": [], 526 | "source": [ 527 | "lambda arg1,arg2,arg3:single expression" 528 | ] 529 | }, 530 | { 531 | "cell_type": "code", 532 | "execution_count": 41, 533 | "metadata": {}, 534 | "outputs": [ 535 | { 536 | "name": "stdout", 537 | "output_type": "stream", 538 | "text": [ 539 | "[100, 400, 900, 1600, 2500, 3600]\n" 540 | ] 541 | } 542 | ], 543 | "source": [ 544 | "l = [10,20,30,40,50,60]\n", 545 | "r = map(lambda num1: num1 * num1,l) # we are not transfering control every operation to the func def and comes back to the fun call after the return statement so the execution will be in line\n", 546 | "print(list(r))" 547 | ] 548 | }, 549 | { 550 | "cell_type": "code", 551 | "execution_count": 49, 552 | "metadata": {}, 553 | "outputs": [ 554 | { 555 | "name": "stdout", 556 | "output_type": "stream", 557 | "text": [ 558 | "[111, 222, 333]\n" 559 | ] 560 | } 561 | ], 562 | "source": [ 563 | "l1 = [1,2,3]\n", 564 | "l2 = [100,200,300]\n", 565 | "l3 = [10,20,30]\n", 566 | "r = map(lambda num1,num2,num3:num1 + num2 + num3 ,l1,l2,l3)\n", 567 | "print(list(r))" 568 | ] 569 | }, 570 | { 571 | "cell_type": "code", 572 | "execution_count": null, 573 | "metadata": {}, 574 | "outputs": [], 575 | "source": [ 576 | "#convert it to lambda\n", 577 | "\n", 578 | "def get_char(val1):\n", 579 | " return val1[0]+val1[-1]\n", 580 | "\n", 581 | "l = ['abcd','pqrs','xyzx',\"rrr\"]\n", 582 | "m = map(get_char,l)\n", 583 | "print(list(m))" 584 | ] 585 | }, 586 | { 587 | "cell_type": "code", 588 | "execution_count": 53, 589 | "metadata": {}, 590 | "outputs": [], 591 | "source": [ 592 | "#convert it to lambda\n", 593 | "\n", 594 | "def even(num1):\n", 595 | " if num1 % 2 == 0:\n", 596 | " return True\n", 597 | " else:\n", 598 | " return False\n", 599 | "l = [10,20,35,30,35,40,45]\n", 600 | "f = map(even,l)\n", 601 | "print(list(f))" 602 | ] 603 | }, 604 | { 605 | "cell_type": "code", 606 | "execution_count": 54, 607 | "metadata": {}, 608 | "outputs": [ 609 | { 610 | "name": "stdout", 611 | "output_type": "stream", 612 | "text": [ 613 | "[10, 20, 30, 40]\n" 614 | ] 615 | } 616 | ], 617 | "source": [ 618 | "l = [10,20,35,30,35,40,45]\n", 619 | "f = filter(lambda num1: num1%2 == 0,l)\n", 620 | "print(list(f))" 621 | ] 622 | }, 623 | { 624 | "cell_type": "code", 625 | "execution_count": null, 626 | "metadata": {}, 627 | "outputs": [], 628 | "source": [ 629 | "#convert it to lambda\n", 630 | "\n", 631 | "def perfect(num1):\n", 632 | " sum = 0\n", 633 | " for value in range(1,num1//2 + 1):\n", 634 | " if num1 % value == 0:\n", 635 | " sum += value\n", 636 | " if sum == num1:\n", 637 | " return True\n", 638 | " else:\n", 639 | " return False\n", 640 | "\n", 641 | "l = [6,45,28,12,35,89] #perfect num from the list\n", 642 | "f = filter(perfect,l)\n", 643 | "print(list(f))" 644 | ] 645 | }, 646 | { 647 | "cell_type": "code", 648 | "execution_count": 55, 649 | "metadata": {}, 650 | "outputs": [ 651 | { 652 | "name": "stdout", 653 | "output_type": "stream", 654 | "text": [ 655 | "True\n" 656 | ] 657 | } 658 | ], 659 | "source": [ 660 | "char = \"a\"\n", 661 | "f = lambda value: value in 'aeiou'\n", 662 | "print(f(char))" 663 | ] 664 | }, 665 | { 666 | "cell_type": "code", 667 | "execution_count": 57, 668 | "metadata": {}, 669 | "outputs": [ 670 | { 671 | "name": "stdout", 672 | "output_type": "stream", 673 | "text": [ 674 | "[(10, 50), (20, 40), (30, 30), (40, 20), (50, 10)]\n" 675 | ] 676 | } 677 | ], 678 | "source": [ 679 | "d = {10:50,20:40,30:30,40:20,50:10}\n", 680 | "print(sorted(d.items()))" 681 | ] 682 | }, 683 | { 684 | "cell_type": "code", 685 | "execution_count": 58, 686 | "metadata": { 687 | "scrolled": true 688 | }, 689 | "outputs": [ 690 | { 691 | "name": "stdout", 692 | "output_type": "stream", 693 | "text": [ 694 | "[(50, 10), (40, 20), (30, 30), (20, 40), (10, 50)]\n" 695 | ] 696 | } 697 | ], 698 | "source": [ 699 | "def get_val(t):\n", 700 | " return t[1]\n", 701 | "\n", 702 | "\n", 703 | "d = {10:50,20:40,30:30,40:20,50:10} # sorted on the basis of key\n", 704 | "print(sorted(d.items(),key=get_val))" 705 | ] 706 | }, 707 | { 708 | "cell_type": "code", 709 | "execution_count": 59, 710 | "metadata": {}, 711 | "outputs": [ 712 | { 713 | "name": "stdout", 714 | "output_type": "stream", 715 | "text": [ 716 | "[(50, 10), (40, 20), (30, 30), (20, 40), (10, 50)]\n" 717 | ] 718 | } 719 | ], 720 | "source": [ 721 | "\n", 722 | "d = {10:50,20:40,30:30,40:20,50:10} # by using lambda\n", 723 | "print(sorted(d.items(),key=lambda t:t[1]))" 724 | ] 725 | }, 726 | { 727 | "cell_type": "code", 728 | "execution_count": 61, 729 | "metadata": {}, 730 | "outputs": [], 731 | "source": [ 732 | "overview :\n", 733 | " \n", 734 | "map(func obj,iterable) pik one element and pass it to func\n", 735 | "r =map(func obj,iterable) => return type return me a map obj\n", 736 | "\n", 737 | "evaluate value of the map oject using\n", 738 | "1.list\n", 739 | "2.next\n", 740 | "3.for loop\n", 741 | "\n", 742 | "filter(func obj,iterable) \n", 743 | "r = filter(func obj,iterable) => filter obj\n", 744 | "\n", 745 | "lambda we specify any arguments like arg1,arg2,arg3:statements" 746 | ] 747 | }, 748 | { 749 | "cell_type": "code", 750 | "execution_count": null, 751 | "metadata": {}, 752 | "outputs": [], 753 | "source": [ 754 | ">" 755 | ] 756 | } 757 | ], 758 | "metadata": { 759 | "kernelspec": { 760 | "display_name": "Python 3", 761 | "language": "python", 762 | "name": "python3" 763 | }, 764 | "language_info": { 765 | "codemirror_mode": { 766 | "name": "ipython", 767 | "version": 3 768 | }, 769 | "file_extension": ".py", 770 | "mimetype": "text/x-python", 771 | "name": "python", 772 | "nbconvert_exporter": "python", 773 | "pygments_lexer": "ipython3", 774 | "version": "3.7.4" 775 | } 776 | }, 777 | "nbformat": 4, 778 | "nbformat_minor": 2 779 | } 780 | -------------------------------------------------------------------------------- /Session 19 - Problem solving.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 2, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "# 1.Keyword density is a foundational aspect of on-page SEO. When used correctly, it can help attract search engines and boost a page’s visibility on search engine results pages (SERPs).\n", 10 | "# Formula to calculate the keyword density :\n", 11 | "\n", 12 | "# keyword density = Number of times the target keyword appears / Number of words on a webpage = Keyword density\n", 13 | "\n", 14 | "# eg : 20 (Number of times “project management” appears on the page) / 1,000 (Number of words on the webpage) = 2% (Keyword density)\n", 15 | "\n", 16 | "# - Write a program to find out 10 words with highest keyword density from a given text file.\n", 17 | "# - Given a set of keywords calulate the keyword density for the same." 18 | ] 19 | }, 20 | { 21 | "cell_type": "code", 22 | "execution_count": 3, 23 | "metadata": {}, 24 | "outputs": [ 25 | { 26 | "name": "stdout", 27 | "output_type": "stream", 28 | "text": [ 29 | "Total numbers of words in this file 610\n", 30 | "0.00819672131147541\n" 31 | ] 32 | } 33 | ], 34 | "source": [ 35 | "import re\n", 36 | "fp = open(\"blog.txt\",\"r\",encoding = \"utf-8\")\n", 37 | "raw_content = fp.read()\n", 38 | "\n", 39 | "words = re.compile(\"[a-zA-Z]+\")\n", 40 | "all_words = re.findall(words,raw_content.lower())\n", 41 | "all_words_count = len(all_words)\n", 42 | "\n", 43 | "print(\"Total numbers of words in this file {}\".format(all_words_count))\n", 44 | "\n", 45 | "target_keyword = \"Artificial Intelligence\".lower()\n", 46 | "target_keyword_count = raw_content.count(target_keyword)\n", 47 | "keyword_density = target_keyword_count / all_words_count\n", 48 | "print(keyword_density)\n", 49 | "fp.close()" 50 | ] 51 | }, 52 | { 53 | "cell_type": "code", 54 | "execution_count": null, 55 | "metadata": {}, 56 | "outputs": [], 57 | "source": [ 58 | "2.Write a program to search word in given text file.\n", 59 | "If a word is present print the sentence containing the given word.\n", 60 | "Implement find, find next and find all functionality" 61 | ] 62 | }, 63 | { 64 | "cell_type": "code", 65 | "execution_count": 13, 66 | "metadata": {}, 67 | "outputs": [ 68 | { 69 | "name": "stdout", 70 | "output_type": "stream", 71 | "text": [ 72 | "['We’re rapidly moving towards a day and age where almost everything can be controlled by machines and one of the most revolutionary moves has been the advent of artificial intelligence.', '\\n\\n \\n\\nTypes\\n\\nNext in line with this artificial intelligence online course is understanding the various types of AI.']\n" 73 | ] 74 | } 75 | ], 76 | "source": [ 77 | "import re\n", 78 | "fp = open(\"blog.txt\",\"r\",encoding=\"utf-8\")\n", 79 | "raw_content = fp.read()\n", 80 | "sentense = re.compile(\"[a-zA-Z\\s’]+\\.\")\n", 81 | "all_sen = re.findall(sentense,raw_content)\n", 82 | "# print(all_sen)\n", 83 | "\n", 84 | "keyword = \"Artificial Intelligence\".lower()\n", 85 | "f = filter(lambda value:keyword in value,all_sen)\n", 86 | "print(list(f))" 87 | ] 88 | }, 89 | { 90 | "cell_type": "code", 91 | "execution_count": null, 92 | "metadata": {}, 93 | "outputs": [], 94 | "source": [ 95 | "3.Word Wrap - Given a sequence of words, and a limit on the number of characters that can be put in one line (line width). Put line breaks in the given sequence such that the lines are printed neatly.\n", 96 | "\n", 97 | "Assume that the length of each word is smaller than the line width." 98 | ] 99 | }, 100 | { 101 | "cell_type": "code", 102 | "execution_count": 14, 103 | "metadata": {}, 104 | "outputs": [ 105 | { 106 | "name": "stdout", 107 | "output_type": "stream", 108 | "text": [ 109 | "[6, 28]\n" 110 | ] 111 | } 112 | ], 113 | "source": [ 114 | "f = filter(lambda num1:num1 == sum([value for value in range(1,num1) if num1 % value == 0]),range(1,100))\n", 115 | "print(list(f))" 116 | ] 117 | } 118 | ], 119 | "metadata": { 120 | "kernelspec": { 121 | "display_name": "Python 3", 122 | "language": "python", 123 | "name": "python3" 124 | }, 125 | "language_info": { 126 | "codemirror_mode": { 127 | "name": "ipython", 128 | "version": 3 129 | }, 130 | "file_extension": ".py", 131 | "mimetype": "text/x-python", 132 | "name": "python", 133 | "nbconvert_exporter": "python", 134 | "pygments_lexer": "ipython3", 135 | "version": "3.7.4" 136 | } 137 | }, 138 | "nbformat": 4, 139 | "nbformat_minor": 2 140 | } 141 | -------------------------------------------------------------------------------- /Session 21 - Class and Object.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "class and objects" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": null, 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "Class :\n", 19 | " Data members / attributes / variables\n", 20 | " Methods\n", 21 | " \n", 22 | "Objects" 23 | ] 24 | }, 25 | { 26 | "cell_type": "code", 27 | "execution_count": null, 28 | "metadata": {}, 29 | "outputs": [], 30 | "source": [ 31 | "Paytm\n", 32 | "Class:\n", 33 | " contact\n", 34 | " email\n", 35 | " kyc\n", 36 | " \n", 37 | " recharge\n", 38 | " boook_tickets\n", 39 | " bill\n", 40 | " wallet\n", 41 | " \n", 42 | "user1\n", 43 | "user2\n", 44 | " " 45 | ] 46 | }, 47 | { 48 | "cell_type": "code", 49 | "execution_count": null, 50 | "metadata": {}, 51 | "outputs": [], 52 | "source": [ 53 | "User\n", 54 | " user_name\n", 55 | " name\n", 56 | " email\n", 57 | " password\n", 58 | " contact\n", 59 | " \n", 60 | " login\n", 61 | " logout\n", 62 | " \n", 63 | " add_contact\n", 64 | " add_name\n", 65 | " update_name\n", 66 | " update_email\n", 67 | " change_password\n", 68 | " change_contact" 69 | ] 70 | }, 71 | { 72 | "cell_type": "code", 73 | "execution_count": 6, 74 | "metadata": {}, 75 | "outputs": [ 76 | { 77 | "name": "stdout", 78 | "output_type": "stream", 79 | "text": [ 80 | "<__main__.user object at 0x0000021A9F178B08>\n" 81 | ] 82 | } 83 | ], 84 | "source": [ 85 | "class user:\n", 86 | " pass\n", 87 | "\n", 88 | "user1 = user()\n", 89 | "print(user1)" 90 | ] 91 | }, 92 | { 93 | "cell_type": "code", 94 | "execution_count": null, 95 | "metadata": {}, 96 | "outputs": [], 97 | "source": [ 98 | "Instance Varible : Every object will have its own copy of that variable" 99 | ] 100 | }, 101 | { 102 | "cell_type": "code", 103 | "execution_count": 9, 104 | "metadata": {}, 105 | "outputs": [], 106 | "source": [ 107 | "class user:\n", 108 | " def __init__(self,username,email,password):\n", 109 | " \n", 110 | " self.user_name = username\n", 111 | " self.email = email\n", 112 | " self.password = password\n", 113 | "\n", 114 | "user1 = user(\"abc@123\",\"abc@gmail.com\",\"password@123\")\n", 115 | "# user(user1,\"abc@123\",\"abc@gmail.com\",\"password@123\")\n", 116 | "user2 = user(\"pqr@123\",\"pqr@gmail.com\",\"password@123\")" 117 | ] 118 | }, 119 | { 120 | "cell_type": "code", 121 | "execution_count": 13, 122 | "metadata": {}, 123 | "outputs": [ 124 | { 125 | "name": "stdout", 126 | "output_type": "stream", 127 | "text": [ 128 | "abc@123 abc@gmail.com\n", 129 | "pqr@123 pqr@gmail.com\n" 130 | ] 131 | } 132 | ], 133 | "source": [ 134 | "print(user1.user_name,user1.email)\n", 135 | "print(user2.user_name,user2.email)" 136 | ] 137 | }, 138 | { 139 | "cell_type": "code", 140 | "execution_count": 14, 141 | "metadata": {}, 142 | "outputs": [ 143 | { 144 | "ename": "TypeError", 145 | "evalue": "user() takes no arguments", 146 | "output_type": "error", 147 | "traceback": [ 148 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 149 | "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", 150 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 6\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpassword\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mpassword\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 7\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 8\u001b[1;33m \u001b[0muser1\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0muser\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"abc@123\"\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m\"abc@gmail.com\"\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m\"password@123\"\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 9\u001b[0m \u001b[0muser2\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0muser\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"pqr@123\"\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m\"pqr@gmail.com\"\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m\"password@123\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", 151 | "\u001b[1;31mTypeError\u001b[0m: user() takes no arguments" 152 | ] 153 | } 154 | ], 155 | "source": [ 156 | "class User:\n", 157 | " def signup(self,username,email,password):\n", 158 | " \n", 159 | " self.user_name = username\n", 160 | " self.email = email\n", 161 | " self.password = password\n", 162 | "\n", 163 | "user1 = user(\"abc@123\",\"abc@gmail.com\",\"password@123\")\n", 164 | "user2 = user(\"pqr@123\",\"pqr@gmail.com\",\"password@123\")" 165 | ] 166 | }, 167 | { 168 | "cell_type": "code", 169 | "execution_count": 18, 170 | "metadata": {}, 171 | "outputs": [ 172 | { 173 | "name": "stdout", 174 | "output_type": "stream", 175 | "text": [ 176 | "abc@123 abc@gmail.com\n", 177 | "pqr@123 pqr@gmail.com\n" 178 | ] 179 | } 180 | ], 181 | "source": [ 182 | "class User:\n", 183 | " def signup(self,username,email,password): #without init\n", 184 | " \n", 185 | " self.user_name = username\n", 186 | " self.email = email\n", 187 | " self.password = password\n", 188 | " \n", 189 | "user1 = user()\n", 190 | "user2 = user()\n", 191 | "user1.signup(\"abc@123\",\"abc@gmail.com\",\"password@123\")\n", 192 | "user2.signup(\"pqr@123\",\"pqr@gmail.com\",\"password@123\")\n", 193 | "print(user1.user_name,user1.email)\n", 194 | "print(user2.user_name,user2.email)" 195 | ] 196 | }, 197 | { 198 | "cell_type": "code", 199 | "execution_count": 24, 200 | "metadata": {}, 201 | "outputs": [ 202 | { 203 | "name": "stdout", 204 | "output_type": "stream", 205 | "text": [ 206 | "True\n" 207 | ] 208 | } 209 | ], 210 | "source": [ 211 | "class user:\n", 212 | " def __init__(self,username,email,password):\n", 213 | " \n", 214 | " self.user_name = username\n", 215 | " self.email = email\n", 216 | " self.password = password\n", 217 | " \n", 218 | " def login(self,username,password):\n", 219 | " if self.user_name == username and self.password == password:\n", 220 | " return True\n", 221 | " else:\n", 222 | " return False\n", 223 | "\n", 224 | "user1 = user(\"abc@123\",\"abc@gmail.com\",\"password@123\")\n", 225 | "# user(user1,\"abc@123\",\"abc@gmail.com\",\"password@123\")\n", 226 | "user2 = user(\"pqr@123\",\"pqr@gmail.com\",\"password@123\")\n", 227 | "\n", 228 | "print(user1.login(\"abc@123\",\"password@123\"))" 229 | ] 230 | }, 231 | { 232 | "cell_type": "code", 233 | "execution_count": 29, 234 | "metadata": {}, 235 | "outputs": [ 236 | { 237 | "name": "stdout", 238 | "output_type": "stream", 239 | "text": [ 240 | "ABC\n", 241 | "PQR\n" 242 | ] 243 | } 244 | ], 245 | "source": [ 246 | "class user:\n", 247 | " def __init__(self,username,email,password):\n", 248 | " \n", 249 | " self.user_name = username\n", 250 | " self.email = email\n", 251 | " self.password = password\n", 252 | " \n", 253 | " def login(self,username,password):\n", 254 | " if self.user_name == username and self.password == password:\n", 255 | " return True\n", 256 | " else:\n", 257 | " return False\n", 258 | " \n", 259 | " def add_name(self,name):\n", 260 | " self.name = name\n", 261 | "\n", 262 | "user1 = user(\"abc@123\",\"abc@gmail.com\",\"password@123\")\n", 263 | "# user(user1,\"abc@123\",\"abc@gmail.com\",\"password@123\")\n", 264 | "user2 = user(\"pqr@123\",\"pqr@gmail.com\",\"password@123\")\n", 265 | "\n", 266 | "user1.add_name(\"ABC\")\n", 267 | "user2.add_name(\"PQR\")\n", 268 | "print(user1.name)\n", 269 | "print(user2.name)" 270 | ] 271 | }, 272 | { 273 | "cell_type": "code", 274 | "execution_count": 32, 275 | "metadata": {}, 276 | "outputs": [ 277 | { 278 | "name": "stdout", 279 | "output_type": "stream", 280 | "text": [ 281 | "{'user_name': 'abc@123', 'email': 'abc@gmail.com', 'password': 'password@123', 'name': 'ABC'}\n", 282 | "{'user_name': 'pqr@123', 'email': 'pqr@gmail.com', 'password': 'password@123', 'name': 'PQR'}\n" 283 | ] 284 | } 285 | ], 286 | "source": [ 287 | "print(user1.__dict__) #object values \n", 288 | "print(user2.__dict__)" 289 | ] 290 | }, 291 | { 292 | "cell_type": "code", 293 | "execution_count": 34, 294 | "metadata": {}, 295 | "outputs": [ 296 | { 297 | "name": "stdout", 298 | "output_type": "stream", 299 | "text": [ 300 | "password@123\n", 301 | "abc@gmail\n" 302 | ] 303 | } 304 | ], 305 | "source": [ 306 | "class user:\n", 307 | " def __init__(self,username,email,password):\n", 308 | " \n", 309 | " self.user_name = username\n", 310 | " self.email = email\n", 311 | " self.password = password\n", 312 | " \n", 313 | " def login(self,username,password):\n", 314 | " if self.user_name == username and self.password == password:\n", 315 | " return True\n", 316 | " else:\n", 317 | " return False\n", 318 | " \n", 319 | " def change_password(self,new_password):\n", 320 | " self.password = new_password\n", 321 | "\n", 322 | "user1 = user(\"abc@123\",\"abc@gmail.com\",\"password@123\")\n", 323 | "# user(user1,\"abc@123\",\"abc@gmail.com\",\"password@123\")\n", 324 | "user2 = user(\"pqr@123\",\"pqr@gmail.com\",\"password@123\")\n", 325 | "print(user1.password) #old password\n", 326 | "user1.change_password(\"abc@gmail\") # new password\n", 327 | "\n", 328 | "print(user1.password)" 329 | ] 330 | }, 331 | { 332 | "cell_type": "code", 333 | "execution_count": 35, 334 | "metadata": {}, 335 | "outputs": [ 336 | { 337 | "name": "stdout", 338 | "output_type": "stream", 339 | "text": [ 340 | "<__main__.user object at 0x0000021A9F177948>\n" 341 | ] 342 | } 343 | ], 344 | "source": [ 345 | "class user:\n", 346 | " def __init__(self,username,email,password):\n", 347 | " \n", 348 | " self.user_name = username\n", 349 | " self.email = email\n", 350 | " self.password = password\n", 351 | " \n", 352 | " def login(self,username,password):\n", 353 | " if self.user_name == username and self.password == password:\n", 354 | " return True\n", 355 | " else:\n", 356 | " return False\n", 357 | " \n", 358 | " def change_password(self,new_password):\n", 359 | " self.password = new_password\n", 360 | "\n", 361 | "user1 = user(\"abc@123\",\"abc@gmail.com\",\"password@123\")\n", 362 | "# user(user1,\"abc@123\",\"abc@gmail.com\",\"password@123\")\n", 363 | "user2 = user(\"pqr@123\",\"pqr@gmail.com\",\"password@123\")\n", 364 | "\n", 365 | "print(user1) #doesnt give value need to add aditional method" 366 | ] 367 | }, 368 | { 369 | "cell_type": "code", 370 | "execution_count": 37, 371 | "metadata": {}, 372 | "outputs": [ 373 | { 374 | "name": "stdout", 375 | "output_type": "stream", 376 | "text": [ 377 | "abc@123\n" 378 | ] 379 | } 380 | ], 381 | "source": [ 382 | "class user:\n", 383 | " def __init__(self,username,email,password):\n", 384 | " \n", 385 | " self.user_name = username\n", 386 | " self.email = email\n", 387 | " self.password = password\n", 388 | " \n", 389 | " def login(self,username,password):\n", 390 | " if self.user_name == username and self.password == password:\n", 391 | " return True\n", 392 | " else:\n", 393 | " return False\n", 394 | " \n", 395 | " def change_password(self,new_password):\n", 396 | " self.password = new_password\n", 397 | " \n", 398 | " def __str__(self):\n", 399 | " return self.user_name # should be only str\n", 400 | "\n", 401 | "user1 = user(\"abc@123\",\"abc@gmail.com\",\"password@123\")\n", 402 | "# user(user1,\"abc@123\",\"abc@gmail.com\",\"password@123\")\n", 403 | "user2 = user(\"pqr@123\",\"pqr@gmail.com\",\"password@123\")\n", 404 | "\n", 405 | "print(user1)\n" 406 | ] 407 | }, 408 | { 409 | "cell_type": "code", 410 | "execution_count": 39, 411 | "metadata": {}, 412 | "outputs": [ 413 | { 414 | "name": "stdout", 415 | "output_type": "stream", 416 | "text": [ 417 | "abc@123 password@123\n" 418 | ] 419 | } 420 | ], 421 | "source": [ 422 | "class user:\n", 423 | " def __init__(self,username,email,password):\n", 424 | " \n", 425 | " self.user_name = username\n", 426 | " self.email = email\n", 427 | " self.password = password\n", 428 | " \n", 429 | " def login(self,username,password):\n", 430 | " if self.user_name == username and self.password == password:\n", 431 | " return True\n", 432 | " else:\n", 433 | " return False\n", 434 | " \n", 435 | " def change_password(self,new_password):\n", 436 | " self.password = new_password\n", 437 | " \n", 438 | " def __str__(self):\n", 439 | " return \"{} {}\".format(self.user_name,self.password) # to get both name and password\n", 440 | "\n", 441 | "user1 = user(\"abc@123\",\"abc@gmail.com\",\"password@123\")\n", 442 | "# user(user1,\"abc@123\",\"abc@gmail.com\",\"password@123\")\n", 443 | "user2 = user(\"pqr@123\",\"pqr@gmail.com\",\"password@123\")\n", 444 | "\n", 445 | "print(user1)" 446 | ] 447 | }, 448 | { 449 | "cell_type": "code", 450 | "execution_count": 1, 451 | "metadata": {}, 452 | "outputs": [ 453 | { 454 | "name": "stdout", 455 | "output_type": "stream", 456 | "text": [ 457 | "abc@123 abc@gmail.com ABC\n", 458 | "pqr@123 pqr@gmail.com PQR\n", 459 | "xyz@123 xyz@gmail.com RST\n", 460 | "rst@123 rst@gmail.com XYZ\n" 461 | ] 462 | } 463 | ], 464 | "source": [ 465 | "class User:\n", 466 | " def __init__(self,username,email,password):\n", 467 | " \n", 468 | " self.user_name = username\n", 469 | " self.email = email\n", 470 | " self.password = password\n", 471 | " \n", 472 | " def login(self,username,password):\n", 473 | " if self.user_name == username and self.password == password:\n", 474 | " return True\n", 475 | " else:\n", 476 | " return False\n", 477 | " \n", 478 | " def add_name(self,name):\n", 479 | " self.name = name\n", 480 | " \n", 481 | " def change_password(self,new_password):\n", 482 | " self.password = new_password\n", 483 | " \n", 484 | " def __str__(self):\n", 485 | " return \"{} {}\".format(self.user_name,self.email)\n", 486 | " \n", 487 | " def get_details(self):\n", 488 | " return \"{} {} {}\".format(self.user_name,self.email,self.name)\n", 489 | " \n", 490 | "user1 = User(\"abc@123\",\"abc@gmail.com\",\"password@123\")\n", 491 | "# User(user1,\"abc@123\",\"abc@gmail.com\",\"password@123\")\n", 492 | "\n", 493 | "user2 = User(\"pqr@123\",\"pqr@gmail.com\",\"password@123\")\n", 494 | "user3 = User(\"xyz@123\",\"xyz@gmail.com\",\"password@123\")\n", 495 | "user4 = User(\"rst@123\",\"rst@gmail.com\",\"password@123\")\n", 496 | "\n", 497 | "user1.add_name(\"ABC\")\n", 498 | "user2.add_name(\"PQR\")\n", 499 | "user3.add_name(\"RST\")\n", 500 | "user4.add_name(\"XYZ\")\n", 501 | "\n", 502 | "users = [user1,user2,user3,user4]\n", 503 | "for user in users:\n", 504 | " print(user.get_details())\n", 505 | "# print(user1)" 506 | ] 507 | }, 508 | { 509 | "cell_type": "code", 510 | "execution_count": null, 511 | "metadata": {}, 512 | "outputs": [], 513 | "source": [ 514 | "\n", 515 | "Student \n", 516 | " name \n", 517 | " roll_no \n", 518 | " \n", 519 | " \n", 520 | " add_marks marks = []\n", 521 | " cal_result()\n", 522 | " \n", 523 | " \n", 524 | " All students who failed \n", 525 | " Topper of the class name,rollno, marks , percernatge" 526 | ] 527 | }, 528 | { 529 | "cell_type": "code", 530 | "execution_count": 2, 531 | "metadata": {}, 532 | "outputs": [ 533 | { 534 | "name": "stdout", 535 | "output_type": "stream", 536 | "text": [ 537 | "3 RST 34.6 Fail\n", 538 | "4 XYZ 39.0 Fail\n", 539 | "5 PPP 24.8 Fail\n", 540 | "[(<__main__.Student object at 0x000002617B1FDA88>, 88.2), (<__main__.Student object at 0x000002617B1FDAC8>, 71.2), (<__main__.Student object at 0x000002617B1FDFC8>, 73.0)]\n", 541 | "(<__main__.Student object at 0x000002617B1FDA88>, 88.2)\n", 542 | "Topper of the class is 1 ABC 88.2 Pass\n" 543 | ] 544 | } 545 | ], 546 | "source": [ 547 | "class Student:\n", 548 | " def __init__(self,roll_no,name):\n", 549 | " self.roll_no = roll_no\n", 550 | " self.name = name\n", 551 | " \n", 552 | " def add_marks(self,marks):\n", 553 | " self.marks = marks\n", 554 | " \n", 555 | " def cal_result(self):\n", 556 | " self.per = sum(self.marks.values())/500 * 100\n", 557 | " if self.per >= 40:\n", 558 | " self.result = \"Pass\"\n", 559 | " else:\n", 560 | " self.result = \"Fail\"\n", 561 | " \n", 562 | " def get_details(self):\n", 563 | " return \"{} {} {} {}\".format(str(self.roll_no),self.name,round(self.per,2),self.result)\n", 564 | " \n", 565 | " \n", 566 | "stud1 = Student(1,\"ABC\")\n", 567 | "stud2 = Student(2,\"PQR\")\n", 568 | "stud3 = Student(3,\"RST\")\n", 569 | "stud4 = Student(4,\"XYZ\")\n", 570 | "stud5 = Student(5,\"PPP\")\n", 571 | "stud6 = Student(6,\"TTT\")\n", 572 | "\n", 573 | "stud1.add_marks({\"eng\":78,\"maths\":88,\"his\":88,\"sci\":88,\"geo\":99})\n", 574 | "stud2.add_marks({\"eng\":80,\"maths\":77,\"his\":89,\"sci\":70,\"geo\":40})\n", 575 | "stud3.add_marks({\"eng\":50,\"maths\":33,\"his\":30,\"sci\":30,\"geo\":30})\n", 576 | "stud4.add_marks({\"eng\":30,\"maths\":44,\"his\":35,\"sci\":56,\"geo\":30})\n", 577 | "stud5.add_marks({\"eng\":20,\"maths\":20,\"his\":20,\"sci\":44,\"geo\":20})\n", 578 | "stud6.add_marks({\"eng\":35,\"maths\":66,\"his\":89,\"sci\":76,\"geo\":99})\n", 579 | "\n", 580 | "students = [stud1,stud2,stud3,stud4,stud5,stud6]\n", 581 | "for student in students:\n", 582 | " student.cal_result()\n", 583 | " \n", 584 | "\n", 585 | "#Failed Students\n", 586 | "for student in students:\n", 587 | " if student.result == \"Fail\":\n", 588 | " print(student.get_details())\n", 589 | " \n", 590 | "student_per = [(student,student.per) for student in students if student.result == \"Pass\"]\n", 591 | "print(student_per)\n", 592 | "topper = max(student_per,key = lambda t : t[1])\n", 593 | "print(topper)\n", 594 | "print(\"Topper of the class is\",topper[0].get_details())" 595 | ] 596 | }, 597 | { 598 | "cell_type": "code", 599 | "execution_count": null, 600 | "metadata": {}, 601 | "outputs": [], 602 | "source": [] 603 | } 604 | ], 605 | "metadata": { 606 | "kernelspec": { 607 | "display_name": "Python 3", 608 | "language": "python", 609 | "name": "python3" 610 | }, 611 | "language_info": { 612 | "codemirror_mode": { 613 | "name": "ipython", 614 | "version": 3 615 | }, 616 | "file_extension": ".py", 617 | "mimetype": "text/x-python", 618 | "name": "python", 619 | "nbconvert_exporter": "python", 620 | "pygments_lexer": "ipython3", 621 | "version": "3.7.4" 622 | } 623 | }, 624 | "nbformat": 4, 625 | "nbformat_minor": 2 626 | } 627 | -------------------------------------------------------------------------------- /Session 22 - OOP Part2.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "class class_name:\n", 10 | " def __init__(self):\n", 11 | " self.var_name = val\n", 12 | " \n", 13 | "obj_name = class_name" 14 | ] 15 | }, 16 | { 17 | "cell_type": "code", 18 | "execution_count": 6, 19 | "metadata": {}, 20 | "outputs": [ 21 | { 22 | "name": "stdout", 23 | "output_type": "stream", 24 | "text": [ 25 | "abc@123\n" 26 | ] 27 | } 28 | ], 29 | "source": [ 30 | "class user:\n", 31 | " def __init__(self,username,email,password):\n", 32 | " \n", 33 | " self.user_name = username\n", 34 | " self.email = email\n", 35 | " self.password = password\n", 36 | "\n", 37 | "user1 = user(\"abc@123\",\"abc@gmail.com\",\"password@123\")\n", 38 | "# user(user1,\"abc@123\",\"abc@gmail.com\",\"password@123\")\n", 39 | "user2 = user(\"pqr@123\",\"pqr@gmail.com\",\"password@123\")\n", 40 | "print(user1.user_name)" 41 | ] 42 | }, 43 | { 44 | "cell_type": "code", 45 | "execution_count": 3, 46 | "metadata": {}, 47 | "outputs": [ 48 | { 49 | "name": "stdout", 50 | "output_type": "stream", 51 | "text": [ 52 | "3 RST 34.6 Fail\n", 53 | "4 XYZ 39.0 Fail\n", 54 | "5 PPP 24.8 Fail\n", 55 | "[(<__main__.Student object at 0x000002F71436D2C8>, 88.2), (<__main__.Student object at 0x000002F71436D308>, 71.2), (<__main__.Student object at 0x000002F71436D808>, 73.0)]\n", 56 | "(<__main__.Student object at 0x000002F71436D2C8>, 88.2)\n", 57 | "Topper of the class is 1 ABC 88.2 Pass\n" 58 | ] 59 | } 60 | ], 61 | "source": [ 62 | "class Student:\n", 63 | " def __init__(self,roll_no,name):\n", 64 | " self.roll_no = roll_no\n", 65 | " self.name = name\n", 66 | " \n", 67 | " def add_marks(self,marks):\n", 68 | " self.marks = marks\n", 69 | " \n", 70 | " def cal_result(self):\n", 71 | " self.per = sum(self.marks.values())/500 * 100\n", 72 | " if self.per >= 40:\n", 73 | " self.result = \"Pass\"\n", 74 | " else:\n", 75 | " self.result = \"Fail\"\n", 76 | " \n", 77 | " def get_details(self):\n", 78 | " return \"{} {} {} {}\".format(str(self.roll_no),self.name,round(self.per,2),self.result)\n", 79 | " \n", 80 | " \n", 81 | "stud1 = Student(1,\"ABC\")\n", 82 | "stud2 = Student(2,\"PQR\")\n", 83 | "stud3 = Student(3,\"RST\")\n", 84 | "stud4 = Student(4,\"XYZ\")\n", 85 | "stud5 = Student(5,\"PPP\")\n", 86 | "stud6 = Student(6,\"TTT\")\n", 87 | "\n", 88 | "stud1.add_marks({\"eng\":78,\"maths\":88,\"his\":88,\"sci\":88,\"geo\":99})\n", 89 | "stud2.add_marks({\"eng\":80,\"maths\":77,\"his\":89,\"sci\":70,\"geo\":40})\n", 90 | "stud3.add_marks({\"eng\":50,\"maths\":33,\"his\":30,\"sci\":30,\"geo\":30})\n", 91 | "stud4.add_marks({\"eng\":30,\"maths\":44,\"his\":35,\"sci\":56,\"geo\":30})\n", 92 | "stud5.add_marks({\"eng\":20,\"maths\":20,\"his\":20,\"sci\":44,\"geo\":20})\n", 93 | "stud6.add_marks({\"eng\":35,\"maths\":66,\"his\":89,\"sci\":76,\"geo\":99})\n", 94 | "\n", 95 | "students = [stud1,stud2,stud3,stud4,stud5,stud6]\n", 96 | "for student in students:\n", 97 | " student.cal_result()\n", 98 | " \n", 99 | "\n", 100 | "#Failed Students\n", 101 | "for student in students:\n", 102 | " if student.result == \"Fail\":\n", 103 | " print(student.get_details())\n", 104 | " \n", 105 | "student_per = [(student,student.per) for student in students if student.result == \"Pass\"]\n", 106 | "print(student_per)\n", 107 | "topper = max(student_per,key = lambda t : t[1])\n", 108 | "print(topper)\n", 109 | "print(\"Topper of the class is\",topper[0].get_details())" 110 | ] 111 | }, 112 | { 113 | "cell_type": "code", 114 | "execution_count": null, 115 | "metadata": {}, 116 | "outputs": [], 117 | "source": [] 118 | } 119 | ], 120 | "metadata": { 121 | "kernelspec": { 122 | "display_name": "Python 3", 123 | "language": "python", 124 | "name": "python3" 125 | }, 126 | "language_info": { 127 | "codemirror_mode": { 128 | "name": "ipython", 129 | "version": 3 130 | }, 131 | "file_extension": ".py", 132 | "mimetype": "text/x-python", 133 | "name": "python", 134 | "nbconvert_exporter": "python", 135 | "pygments_lexer": "ipython3", 136 | "version": "3.7.4" 137 | } 138 | }, 139 | "nbformat": 4, 140 | "nbformat_minor": 2 141 | } 142 | -------------------------------------------------------------------------------- /Session 23 - Part3 OOP.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "Instance variable :" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": null, 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "class User: \n", 19 | " \n", 20 | " @classmethod \n", 21 | " def incr_count(cls):\n", 22 | " cls.count += 1 \n", 23 | " \n", 24 | " def __init__(self,id,name):\n", 25 | " self.id = id\n", 26 | " self.name = name \n", 27 | " count = 0\n", 28 | " \n", 29 | "user1 = User(1,\"ABC\")\n", 30 | "print(User.count)\n", 31 | "print(user1.__dict__)\n", 32 | "user1.count = 50" 33 | ] 34 | }, 35 | { 36 | "cell_type": "code", 37 | "execution_count": null, 38 | "metadata": {}, 39 | "outputs": [], 40 | "source": [ 41 | "__str__ : => str \n", 42 | "__add__ : obj1 + obj2" 43 | ] 44 | }, 45 | { 46 | "cell_type": "code", 47 | "execution_count": null, 48 | "metadata": {}, 49 | "outputs": [], 50 | "source": [ 51 | "Inheritance" 52 | ] 53 | }, 54 | { 55 | "cell_type": "code", 56 | "execution_count": null, 57 | "metadata": {}, 58 | "outputs": [], 59 | "source": [ 60 | "User :\n", 61 | " username\n", 62 | " first_name \n", 63 | " last_name \n", 64 | " password\n", 65 | " email\n", 66 | " \n", 67 | " login\n", 68 | " logout \n", 69 | " isauthenticated \n", 70 | " \n", 71 | "Profile\n", 72 | " Contact \n", 73 | " Github \n", 74 | " linkedin" 75 | ] 76 | }, 77 | { 78 | "cell_type": "code", 79 | "execution_count": 1, 80 | "metadata": {}, 81 | "outputs": [ 82 | { 83 | "name": "stdout", 84 | "output_type": "stream", 85 | "text": [ 86 | "AB\n", 87 | "www.github.com/a\n", 88 | "abc@123 abc@gmail.com www.github.com/a www.linkedin.com/abc\n" 89 | ] 90 | } 91 | ], 92 | "source": [ 93 | "\n", 94 | "class User:\n", 95 | " def __init__(self,username,email,password):\n", 96 | " \n", 97 | " self.user_name = username\n", 98 | " self.email = email\n", 99 | " self.password = password\n", 100 | " \n", 101 | " def login(self,username,password):\n", 102 | " if self.user_name == username and self.password == password:\n", 103 | " return True\n", 104 | " else:\n", 105 | " return False\n", 106 | " \n", 107 | " def add_name(self,name):\n", 108 | " self.name = name\n", 109 | " \n", 110 | " def change_password(self,new_password):\n", 111 | " self.password = new_password\n", 112 | " \n", 113 | " def get_details(self):\n", 114 | " return \"{} {}\".format(self.user_name,self.email)\n", 115 | " \n", 116 | "class Profile(User):\n", 117 | " def __init__(self,user,email,password,contact,github,linkedin):\n", 118 | " super().__init__(user,email,password)\n", 119 | " self.contact = contact\n", 120 | " self.github_url = github\n", 121 | " self.linkedin_url = linkedin\n", 122 | " \n", 123 | " def update_github(self,new_url):\n", 124 | " self.github_url = new_url\n", 125 | " \n", 126 | " def get_details(self):\n", 127 | " return \"{} {} {} {}\".format(self.user_name,self.email,self.github_url,self.linkedin_url)\n", 128 | "\n", 129 | "profile1 = Profile(\"abc@123\",\"abc@gmail.com\",\"password@123\",\"9123456789\",\"www.github.com/abc\",\"www.linkedin.com/abc\")\n", 130 | "\n", 131 | "profile1.add_name(\"AB\")\n", 132 | "print(profile1.name)\n", 133 | "profile1.update_github(\"www.github.com/a\")\n", 134 | "print(profile1.github_url)\n", 135 | "\n", 136 | "# user2 = User(\"pqr\",\"pqr@gmail.com\",\"password@123\")\n", 137 | "# user2.update_github(\"www.github.com/p\")\n", 138 | "\n", 139 | "print(profile1.get_details())\n" 140 | ] 141 | }, 142 | { 143 | "cell_type": "code", 144 | "execution_count": null, 145 | "metadata": {}, 146 | "outputs": [], 147 | "source": [ 148 | "Method Overiding" 149 | ] 150 | }, 151 | { 152 | "cell_type": "code", 153 | "execution_count": 3, 154 | "metadata": {}, 155 | "outputs": [ 156 | { 157 | "name": "stdout", 158 | "output_type": "stream", 159 | "text": [ 160 | "AB\n", 161 | "www.github.com/a\n", 162 | "abc@123 abc@gmail.com www.github.com/a www.linkedin.com/abc\n" 163 | ] 164 | } 165 | ], 166 | "source": [ 167 | "class User:\n", 168 | " def __init__(self,username,email,password):\n", 169 | " \n", 170 | " self.user_name = username\n", 171 | " self.email = email\n", 172 | " self.password = password\n", 173 | " \n", 174 | " def login(self,username,password):\n", 175 | " if self.user_name == username and self.password == password:\n", 176 | " return True\n", 177 | " else:\n", 178 | " return False\n", 179 | " \n", 180 | " def add_name(self,name):\n", 181 | " self.name = name\n", 182 | " \n", 183 | " def change_password(self,new_password):\n", 184 | " self.password = new_password\n", 185 | " \n", 186 | " def get_details(self):\n", 187 | " return \"{} {}\".format(self.user_name,self.email)\n", 188 | "\n", 189 | "class Admin:\n", 190 | " def get_details(self):\n", 191 | " return \"{},{}\".format(\"admin\",\"admin@xyz.com\")\n", 192 | " \n", 193 | "class Profile(Admin,User):\n", 194 | " def __init__(self,user,email,password,contact,github,linkedin):\n", 195 | " super().__init__(user,email,password)\n", 196 | " self.contact = contact\n", 197 | " self.github_url = github\n", 198 | " self.linkedin_url = linkedin\n", 199 | " \n", 200 | " def update_github(self,new_url):\n", 201 | " self.github_url = new_url\n", 202 | " \n", 203 | " def get_details(self):\n", 204 | " \n", 205 | " return \"{} {} {}\".format(User.get_details(self),self.github_url,self.linkedin_url)\n", 206 | "\n", 207 | "profile1 = Profile(\"abc@123\",\"abc@gmail.com\",\"password@123\",\"9123456789\",\"www.github.com/abc\",\"www.linkedin.com/abc\")\n", 208 | "\n", 209 | "profile1.add_name(\"AB\")\n", 210 | "print(profile1.name)\n", 211 | "profile1.update_github(\"www.github.com/a\")\n", 212 | "print(profile1.github_url)\n", 213 | "\n", 214 | "# user2 = User(\"pqr\",\"pqr@gmail.com\",\"password@123\")\n", 215 | "# user2.update_github(\"www.github.com/p\")\n", 216 | "\n", 217 | "print(profile1.get_details())" 218 | ] 219 | }, 220 | { 221 | "cell_type": "code", 222 | "execution_count": null, 223 | "metadata": {}, 224 | "outputs": [], 225 | "source": [ 226 | "=> Single Inheritance \n", 227 | " User(Parent) \n", 228 | " Profile(Child)\n", 229 | " \n", 230 | " \n", 231 | "Multilevel :\n", 232 | " Parent1\n", 233 | " \n", 234 | " parent2(parent1)\n", 235 | " \n", 236 | " child1(parent2) \n", 237 | " \n", 238 | "Multiple inheritance :\n", 239 | " \n", 240 | "User(Parent 1) Admin(Parent 2)\n", 241 | " Profile(Child )\n", 242 | " \n", 243 | "Hybrid :\n", 244 | " \n", 245 | " parent1\n", 246 | " \n", 247 | "parent2() parent3\n", 248 | "\n", 249 | " child(parent2,parent3)" 250 | ] 251 | }, 252 | { 253 | "cell_type": "code", 254 | "execution_count": 4, 255 | "metadata": {}, 256 | "outputs": [ 257 | { 258 | "name": "stdout", 259 | "output_type": "stream", 260 | "text": [ 261 | "Help on Profile in module __main__ object:\n", 262 | "\n", 263 | "class Profile(Admin, User)\n", 264 | " | Profile(user, email, password, contact, github, linkedin)\n", 265 | " | \n", 266 | " | Method resolution order:\n", 267 | " | Profile\n", 268 | " | Admin\n", 269 | " | User\n", 270 | " | builtins.object\n", 271 | " | \n", 272 | " | Methods defined here:\n", 273 | " | \n", 274 | " | __init__(self, user, email, password, contact, github, linkedin)\n", 275 | " | Initialize self. See help(type(self)) for accurate signature.\n", 276 | " | \n", 277 | " | get_details(self)\n", 278 | " | \n", 279 | " | update_github(self, new_url)\n", 280 | " | \n", 281 | " | ----------------------------------------------------------------------\n", 282 | " | Data descriptors inherited from Admin:\n", 283 | " | \n", 284 | " | __dict__\n", 285 | " | dictionary for instance variables (if defined)\n", 286 | " | \n", 287 | " | __weakref__\n", 288 | " | list of weak references to the object (if defined)\n", 289 | " | \n", 290 | " | ----------------------------------------------------------------------\n", 291 | " | Methods inherited from User:\n", 292 | " | \n", 293 | " | add_name(self, name)\n", 294 | " | \n", 295 | " | change_password(self, new_password)\n", 296 | " | \n", 297 | " | login(self, username, password)\n", 298 | "\n" 299 | ] 300 | } 301 | ], 302 | "source": [ 303 | "help(profile1)" 304 | ] 305 | }, 306 | { 307 | "cell_type": "code", 308 | "execution_count": null, 309 | "metadata": {}, 310 | "outputs": [], 311 | "source": [ 312 | "Depth first left to right algo" 313 | ] 314 | }, 315 | { 316 | "cell_type": "code", 317 | "execution_count": 7, 318 | "metadata": {}, 319 | "outputs": [ 320 | { 321 | "name": "stdout", 322 | "output_type": "stream", 323 | "text": [ 324 | "abc@123 abc@gmail.com www.github.com/abc www.linkedin.com/abc\n" 325 | ] 326 | } 327 | ], 328 | "source": [ 329 | "class User:\n", 330 | " def __init__(self,username,email,password):\n", 331 | " \n", 332 | " self._user_name = username\n", 333 | " self.email = email\n", 334 | " self.__password = password\n", 335 | " \n", 336 | " def get_user_name(self):\n", 337 | " return self.__user_name \n", 338 | " \n", 339 | " \n", 340 | " def login(self,username,password):\n", 341 | " if self._user_name == username and self.__password == password:\n", 342 | " return True\n", 343 | " else:\n", 344 | " return False\n", 345 | " \n", 346 | " def set_name(self,new_name):\n", 347 | " self.__name = new_name\n", 348 | " \n", 349 | " def get_name(self):\n", 350 | " return self.__name\n", 351 | " \n", 352 | " def change_password(self,new_password):\n", 353 | " self.__password = new_password\n", 354 | " \n", 355 | " def get_details(self):\n", 356 | " return \"{} {}\".format(self._user_name,self.email)\n", 357 | "\n", 358 | "class Admin:\n", 359 | " def get_details(self):\n", 360 | " return \"{},{}\".format(\"admin\",\"admin@xyz.com\")\n", 361 | " \n", 362 | "class Profile(Admin,User):\n", 363 | " def __init__(self,user,email,password,contact,github,linkedin):\n", 364 | " super().__init__(user,email,password)\n", 365 | " self.contact = contact\n", 366 | " self.github_url = github\n", 367 | " self.linkedin_url = linkedin\n", 368 | " \n", 369 | " def update_github(self,new_url):\n", 370 | " self.github_url = new_url\n", 371 | " \n", 372 | " def get_details(self):\n", 373 | " \n", 374 | " return \"{} {} {} {}\".format(self._user_name,self.email,self.github_url,self.linkedin_url)\n", 375 | "\n", 376 | "profile1 = Profile(\"abc@123\",\"abc@gmail.com\",\"password@123\",\"9123456789\",\"www.github.com/abc\",\"www.linkedin.com/abc\")\n", 377 | "\n", 378 | "profile1.set_name(\"ABC\")\n", 379 | "# print(profile1.name)\n", 380 | "\n", 381 | "# profile1.update_github(\"www.github.com/a\")\n", 382 | "# print(profile1.github_url)\n", 383 | "\n", 384 | "print(profile1.get_details())\n", 385 | "\n", 386 | "# print(profile1.get_name())\n", 387 | "# print(profile1.get_user_name())" 388 | ] 389 | }, 390 | { 391 | "cell_type": "code", 392 | "execution_count": null, 393 | "metadata": {}, 394 | "outputs": [], 395 | "source": [ 396 | "Public\n", 397 | "Private \n", 398 | "Protected" 399 | ] 400 | }, 401 | { 402 | "cell_type": "code", 403 | "execution_count": null, 404 | "metadata": {}, 405 | "outputs": [], 406 | "source": [ 407 | "getters and setters" 408 | ] 409 | }, 410 | { 411 | "cell_type": "code", 412 | "execution_count": 8, 413 | "metadata": {}, 414 | "outputs": [ 415 | { 416 | "name": "stdout", 417 | "output_type": "stream", 418 | "text": [ 419 | "abc@123\n" 420 | ] 421 | } 422 | ], 423 | "source": [ 424 | "\n", 425 | "class User:\n", 426 | " def __init__(self,username,email,password):\n", 427 | " \n", 428 | " self._user_name = username\n", 429 | " self.email = email\n", 430 | " self.__password = password\n", 431 | " \n", 432 | " def get_user_name(self):\n", 433 | " return self.__user_name \n", 434 | " \n", 435 | " \n", 436 | " def login(self,username,password):\n", 437 | " if self._user_name == username and self.__password == password:\n", 438 | " return True\n", 439 | " else:\n", 440 | " return False\n", 441 | " \n", 442 | " def set_name(self,new_name):\n", 443 | " self.__name = new_name\n", 444 | " \n", 445 | " def get_name(self):\n", 446 | " return self.__name\n", 447 | " \n", 448 | " def change_password(self,new_password):\n", 449 | " self.__password = new_password\n", 450 | " \n", 451 | " def get_details(self):\n", 452 | " return \"{} {}\".format(self._user_name,self.email)\n", 453 | "\n", 454 | "class Admin:\n", 455 | " def get_details(self):\n", 456 | " return \"{},{}\".format(\"admin\",\"admin@xyz.com\")\n", 457 | " \n", 458 | "class Profile(Admin,User):\n", 459 | " def __init__(self,user,email,password,contact,github,linkedin):\n", 460 | " super().__init__(user,email,password)\n", 461 | " self.contact = contact\n", 462 | " self.github_url = github\n", 463 | " self.linkedin_url = linkedin\n", 464 | " \n", 465 | " def update_github(self,new_url):\n", 466 | " self.github_url = new_url\n", 467 | " \n", 468 | " def get_details(self):\n", 469 | " \n", 470 | " return \"{} {} {} {}\".format(self._user_name,self.email,self.github_url,self.linkedin_url)\n", 471 | "\n", 472 | "profile1 = Profile(\"abc@123\",\"abc@gmail.com\",\"password@123\",\"9123456789\",\"www.github.com/abc\",\"www.linkedin.com/abc\")\n", 473 | "# print(profile1._User__password)\n", 474 | "\n", 475 | "print(profile1._user_name)\n", 476 | "# profile1.set_name(\"ABC\")\n", 477 | "# print(profile1.name)\n", 478 | "\n", 479 | "# profile1.update_github(\"www.github.com/a\")\n", 480 | "# print(profile1.github_url)\n", 481 | "\n", 482 | "# print(profile1.get_details())\n", 483 | "\n", 484 | "# print(profile1.get_name())\n", 485 | "# print(profile1.get_user_name())" 486 | ] 487 | }, 488 | { 489 | "cell_type": "code", 490 | "execution_count": null, 491 | "metadata": {}, 492 | "outputs": [], 493 | "source": [] 494 | } 495 | ], 496 | "metadata": { 497 | "kernelspec": { 498 | "display_name": "Python 3", 499 | "language": "python", 500 | "name": "python3" 501 | }, 502 | "language_info": { 503 | "codemirror_mode": { 504 | "name": "ipython", 505 | "version": 3 506 | }, 507 | "file_extension": ".py", 508 | "mimetype": "text/x-python", 509 | "name": "python", 510 | "nbconvert_exporter": "python", 511 | "pygments_lexer": "ipython3", 512 | "version": "3.7.4" 513 | } 514 | }, 515 | "nbformat": 4, 516 | "nbformat_minor": 2 517 | } 518 | -------------------------------------------------------------------------------- /Session 26 - Iterators Decorators and Generators.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "[100, 400, 900, 1600, 2500, 3600]\n" 13 | ] 14 | } 15 | ], 16 | "source": [ 17 | "l = [10,20,30,40,50,60]\n", 18 | "m = map(lambda num1:num1 * num1,l)\n", 19 | "# print(next(m))\n", 20 | "# print(next(m))\n", 21 | "print(list(m))" 22 | ] 23 | }, 24 | { 25 | "cell_type": "code", 26 | "execution_count": null, 27 | "metadata": {}, 28 | "outputs": [], 29 | "source": [ 30 | "Iterator :\n", 31 | " \n", 32 | "__iter__\n", 33 | "__next__\n" 34 | ] 35 | }, 36 | { 37 | "cell_type": "code", 38 | "execution_count": 2, 39 | "metadata": {}, 40 | "outputs": [ 41 | { 42 | "name": "stdout", 43 | "output_type": "stream", 44 | "text": [ 45 | "['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']\n" 46 | ] 47 | } 48 | ], 49 | "source": [ 50 | "print(dir(list))" 51 | ] 52 | }, 53 | { 54 | "cell_type": "code", 55 | "execution_count": 3, 56 | "metadata": {}, 57 | "outputs": [ 58 | { 59 | "name": "stdout", 60 | "output_type": "stream", 61 | "text": [ 62 | "\n" 63 | ] 64 | } 65 | ], 66 | "source": [ 67 | "l = [10,20,30,40,50] \n", 68 | "\n", 69 | "i = iter(l)\n", 70 | "print(i)\n", 71 | "# print(next(i))\n", 72 | "# print(next(i))\n", 73 | "# print(next(i))\n", 74 | "# print(next(i))\n", 75 | "# print(next(i))\n", 76 | "# print(next(i))\n", 77 | "\n", 78 | "# for value in i:\n", 79 | "# print(value)\n", 80 | " \n", 81 | "# for value in i:\n", 82 | "# print(value)\n", 83 | "\n", 84 | "# for index in range(5):\n", 85 | "# print(next(i))\n", 86 | " \n", 87 | "# for value in i:\n", 88 | "# print(value)" 89 | ] 90 | }, 91 | { 92 | "cell_type": "code", 93 | "execution_count": 4, 94 | "metadata": {}, 95 | "outputs": [], 96 | "source": [ 97 | "import itertools" 98 | ] 99 | }, 100 | { 101 | "cell_type": "code", 102 | "execution_count": 5, 103 | "metadata": {}, 104 | "outputs": [ 105 | { 106 | "name": "stdout", 107 | "output_type": "stream", 108 | "text": [ 109 | "10\n", 110 | "20\n", 111 | "30\n", 112 | "40\n", 113 | "50\n", 114 | "P\n", 115 | "y\n", 116 | "t\n", 117 | "h\n", 118 | "o\n", 119 | "n\n", 120 | ",\n", 121 | "J\n", 122 | "a\n", 123 | "v\n", 124 | "a\n", 125 | ",\n", 126 | "C\n", 127 | "10\n", 128 | "20\n", 129 | "30\n", 130 | "40\n", 131 | "50\n", 132 | "60\n", 133 | "0\n", 134 | "1\n", 135 | "2\n", 136 | "3\n", 137 | "4\n" 138 | ] 139 | } 140 | ], 141 | "source": [ 142 | "l1 = [10,20,30,40,50]\n", 143 | "s1 = \"Python,Java,C\"\n", 144 | "t1 = (10,20,30,40,50,60)\n", 145 | "r1 = range(5)\n", 146 | "\n", 147 | "i = itertools.chain(l1,s1,t1,r1)\n", 148 | "print(next(i))\n", 149 | "print(next(i))\n", 150 | "for value in i:\n", 151 | " print(value)\n", 152 | " \n", 153 | "for value in i:\n", 154 | " print(value)" 155 | ] 156 | }, 157 | { 158 | "cell_type": "code", 159 | "execution_count": 6, 160 | "metadata": {}, 161 | "outputs": [ 162 | { 163 | "name": "stdout", 164 | "output_type": "stream", 165 | "text": [ 166 | "30\n", 167 | "40\n", 168 | "50\n", 169 | "P\n" 170 | ] 171 | } 172 | ], 173 | "source": [ 174 | "l1 = [10,20,30,40,50]\n", 175 | "s1 = \"Python,Java,C\"\n", 176 | "t1 = (10,20,30,40,50,60)\n", 177 | "r1 = range(5)\n", 178 | "\n", 179 | "i = itertools.chain(l1,s1,t1,r1)\n", 180 | "\n", 181 | "for value in itertools.islice(i,2,6):\n", 182 | " print(value)" 183 | ] 184 | }, 185 | { 186 | "cell_type": "code", 187 | "execution_count": 7, 188 | "metadata": {}, 189 | "outputs": [ 190 | { 191 | "name": "stdout", 192 | "output_type": "stream", 193 | "text": [ 194 | "10\n", 195 | "20\n", 196 | "30\n", 197 | "40\n", 198 | "50\n", 199 | "P\n", 200 | "y\n", 201 | "t\n", 202 | "h\n", 203 | "o\n", 204 | "n\n", 205 | ",\n", 206 | "J\n", 207 | "a\n", 208 | "v\n", 209 | "a\n", 210 | ",\n", 211 | "C\n", 212 | "10\n", 213 | "20\n", 214 | "30\n", 215 | "40\n", 216 | "50\n", 217 | "60\n", 218 | "0\n", 219 | "1\n", 220 | "2\n", 221 | "3\n", 222 | "4\n", 223 | "10\n", 224 | "20\n", 225 | "30\n", 226 | "40\n", 227 | "50\n", 228 | "P\n", 229 | "y\n", 230 | "t\n", 231 | "h\n", 232 | "o\n", 233 | "n\n", 234 | ",\n", 235 | "J\n", 236 | "a\n", 237 | "v\n", 238 | "a\n", 239 | ",\n", 240 | "C\n", 241 | "10\n", 242 | "20\n", 243 | "30\n", 244 | "40\n", 245 | "50\n", 246 | "60\n", 247 | "0\n", 248 | "1\n", 249 | "2\n", 250 | "3\n", 251 | "4\n" 252 | ] 253 | } 254 | ], 255 | "source": [ 256 | "l1 = [10,20,30,40,50]\n", 257 | "s1 = \"Python,Java,C\"\n", 258 | "t1 = (10,20,30,40,50,60)\n", 259 | "r1 = range(5)\n", 260 | "\n", 261 | "i = itertools.chain(l1,s1,t1,r1)\n", 262 | "print(next(i))\n", 263 | "print(next(i))\n", 264 | "for value in i:\n", 265 | " print(value)\n", 266 | " \n", 267 | "j = itertools.chain(l1,s1,t1,r1)\n", 268 | "for value in j:\n", 269 | " print(value)" 270 | ] 271 | }, 272 | { 273 | "cell_type": "code", 274 | "execution_count": 8, 275 | "metadata": {}, 276 | "outputs": [ 277 | { 278 | "name": "stdout", 279 | "output_type": "stream", 280 | "text": [ 281 | "(, , , , )\n", 282 | "10\n", 283 | "20\n", 284 | "30\n", 285 | "40\n", 286 | "50\n", 287 | "P\n", 288 | "y\n", 289 | "t\n", 290 | "h\n", 291 | "o\n", 292 | "n\n", 293 | ",\n", 294 | "J\n", 295 | "a\n", 296 | "v\n", 297 | "a\n", 298 | ",\n", 299 | "C\n", 300 | "10\n", 301 | "20\n", 302 | "30\n", 303 | "40\n", 304 | "50\n", 305 | "60\n", 306 | "0\n", 307 | "1\n", 308 | "2\n", 309 | "3\n", 310 | "4\n", 311 | "10\n", 312 | "20\n", 313 | "30\n", 314 | "40\n", 315 | "50\n", 316 | "P\n", 317 | "y\n", 318 | "t\n", 319 | "h\n", 320 | "o\n", 321 | "n\n", 322 | ",\n", 323 | "J\n", 324 | "a\n", 325 | "v\n", 326 | "a\n", 327 | ",\n", 328 | "C\n", 329 | "10\n", 330 | "20\n", 331 | "30\n", 332 | "40\n", 333 | "50\n", 334 | "60\n", 335 | "0\n", 336 | "1\n", 337 | "2\n", 338 | "3\n", 339 | "4\n" 340 | ] 341 | } 342 | ], 343 | "source": [ 344 | "l1 = [10,20,30,40,50]\n", 345 | "s1 = \"Python,Java,C\"\n", 346 | "t1 = (10,20,30,40,50,60)\n", 347 | "r1 = range(5)\n", 348 | "\n", 349 | "i = itertools.chain(l1,s1,t1,r1)\n", 350 | "t = itertools.tee(i,5)\n", 351 | "print(t)\n", 352 | "\n", 353 | "for value in t[0]:\n", 354 | " print(value)\n", 355 | " \n", 356 | "for value in t[1]:\n", 357 | " print(value)" 358 | ] 359 | }, 360 | { 361 | "cell_type": "code", 362 | "execution_count": 9, 363 | "metadata": {}, 364 | "outputs": [ 365 | { 366 | "name": "stdout", 367 | "output_type": "stream", 368 | "text": [ 369 | "(10, 20)\n", 370 | "(10, 30)\n", 371 | "(10, 40)\n", 372 | "(10, 50)\n", 373 | "(20, 30)\n", 374 | "(20, 40)\n", 375 | "(20, 50)\n", 376 | "(30, 40)\n", 377 | "(30, 50)\n", 378 | "(40, 50)\n" 379 | ] 380 | } 381 | ], 382 | "source": [ 383 | "l = [10,20,30,40,50]\n", 384 | "i = itertools.combinations(l,2)\n", 385 | "for value in i:\n", 386 | " print(value)" 387 | ] 388 | }, 389 | { 390 | "cell_type": "code", 391 | "execution_count": 10, 392 | "metadata": {}, 393 | "outputs": [ 394 | { 395 | "name": "stdout", 396 | "output_type": "stream", 397 | "text": [ 398 | "\n", 399 | "(10, 20, 30)\n" 400 | ] 401 | } 402 | ], 403 | "source": [ 404 | "l = [10,20,30,40,50]\n", 405 | "i = itertools.permutations(l,3)\n", 406 | "print(i)\n", 407 | "print(next(i))\n", 408 | "# for value in i:\n", 409 | "# print(value)" 410 | ] 411 | }, 412 | { 413 | "cell_type": "code", 414 | "execution_count": 11, 415 | "metadata": {}, 416 | "outputs": [ 417 | { 418 | "name": "stdout", 419 | "output_type": "stream", 420 | "text": [ 421 | "10\n", 422 | "20\n", 423 | "30\n", 424 | "40\n", 425 | "50\n", 426 | "10\n", 427 | "20\n", 428 | "30\n", 429 | "210\n" 430 | ] 431 | } 432 | ], 433 | "source": [ 434 | "l = [10,20,30,40,50]\n", 435 | "i = itertools.cycle(l)\n", 436 | "sum = 0 \n", 437 | "for value in i:\n", 438 | " print(value)\n", 439 | " sum = sum + value\n", 440 | " if sum > 200:\n", 441 | " print(sum)\n", 442 | " break" 443 | ] 444 | }, 445 | { 446 | "cell_type": "code", 447 | "execution_count": 12, 448 | "metadata": {}, 449 | "outputs": [ 450 | { 451 | "name": "stdout", 452 | "output_type": "stream", 453 | "text": [ 454 | "[10, 20, 30, 40, 50]\n", 455 | "[10, 20, 30, 40, 50]\n", 456 | "[10, 20, 30, 40, 50]\n", 457 | "[10, 20, 30, 40, 50]\n", 458 | "[10, 20, 30, 40, 50]\n", 459 | "[10, 20, 30, 40, 50]\n", 460 | "[10, 20, 30, 40, 50]\n", 461 | "[10, 20, 30, 40, 50]\n", 462 | "[10, 20, 30, 40, 50]\n", 463 | "[10, 20, 30, 40, 50]\n", 464 | "[10, 20, 30, 40, 50]\n", 465 | "[10, 20, 30, 40, 50]\n", 466 | "[10, 20, 30, 40, 50]\n", 467 | "[10, 20, 30, 40, 50]\n", 468 | "[10, 20, 30, 40, 50]\n", 469 | "[10, 20, 30, 40, 50]\n", 470 | "[10, 20, 30, 40, 50]\n", 471 | "[10, 20, 30, 40, 50]\n", 472 | "[10, 20, 30, 40, 50]\n", 473 | "[10, 20, 30, 40, 50]\n", 474 | "[10, 20, 30, 40, 50]\n", 475 | "21\n" 476 | ] 477 | } 478 | ], 479 | "source": [ 480 | "l = [10,20,30,40,50]\n", 481 | "i = itertools.repeat(l)\n", 482 | "count = 0 \n", 483 | "for value in i:\n", 484 | " print(value)\n", 485 | " count+=1\n", 486 | " \n", 487 | " if count > 20:\n", 488 | " print(count)\n", 489 | " break" 490 | ] 491 | }, 492 | { 493 | "cell_type": "code", 494 | "execution_count": 13, 495 | "metadata": {}, 496 | "outputs": [ 497 | { 498 | "name": "stdout", 499 | "output_type": "stream", 500 | "text": [ 501 | "5\n", 502 | "7\n", 503 | "9\n", 504 | "11\n", 505 | "13\n", 506 | "15\n", 507 | "17\n", 508 | "19\n", 509 | "21\n", 510 | "23\n", 511 | "25\n", 512 | "27\n", 513 | "29\n", 514 | "31\n", 515 | "33\n", 516 | "35\n", 517 | "37\n", 518 | "39\n", 519 | "41\n", 520 | "43\n", 521 | "45\n" 522 | ] 523 | } 524 | ], 525 | "source": [ 526 | "import itertools\n", 527 | "count = 0\n", 528 | "for value in itertools.count(5,2):\n", 529 | " print(value)\n", 530 | " count+=1\n", 531 | " if count > 20:\n", 532 | " break" 533 | ] 534 | }, 535 | { 536 | "cell_type": "code", 537 | "execution_count": null, 538 | "metadata": {}, 539 | "outputs": [], 540 | "source": [ 541 | "list()\n", 542 | "for\n", 543 | "next()" 544 | ] 545 | }, 546 | { 547 | "cell_type": "code", 548 | "execution_count": null, 549 | "metadata": {}, 550 | "outputs": [], 551 | "source": [ 552 | "chain \n", 553 | "islice \n", 554 | "repeat \n", 555 | "cycle \n", 556 | "permutations\n", 557 | "combinations\n", 558 | "count" 559 | ] 560 | }, 561 | { 562 | "cell_type": "code", 563 | "execution_count": 29, 564 | "metadata": {}, 565 | "outputs": [ 566 | { 567 | "name": "stdout", 568 | "output_type": "stream", 569 | "text": [ 570 | "60\n" 571 | ] 572 | } 573 | ], 574 | "source": [ 575 | "def add(num1,num2):\n", 576 | " num3 = 100\n", 577 | " num1 = num1 * 2\n", 578 | " num2 = num2 * 2\n", 579 | " return num1 + num2\n", 580 | "\n", 581 | "print(add(10,20))\n", 582 | "#print(num1)\n", 583 | "#print(num2)" 584 | ] 585 | }, 586 | { 587 | "cell_type": "code", 588 | "execution_count": null, 589 | "metadata": {}, 590 | "outputs": [], 591 | "source": [ 592 | "generators : func iterators" 593 | ] 594 | }, 595 | { 596 | "cell_type": "code", 597 | "execution_count": null, 598 | "metadata": {}, 599 | "outputs": [], 600 | "source": [ 601 | "first_num = 0\n", 602 | "sec_num = 1 \n", 603 | "next_val = first_num + sec_num\n", 604 | "\n", 605 | "0,1 => 0+1 =1\n", 606 | "\n", 607 | "first_num = 1 \n", 608 | "sec_num = 1 \n", 609 | "next_val = 2" 610 | ] 611 | }, 612 | { 613 | "cell_type": "code", 614 | "execution_count": 15, 615 | "metadata": {}, 616 | "outputs": [ 617 | { 618 | "name": "stdout", 619 | "output_type": "stream", 620 | "text": [ 621 | "\n", 622 | "0\n", 623 | "1\n", 624 | "1\n", 625 | "2\n" 626 | ] 627 | } 628 | ], 629 | "source": [ 630 | "def fibo():\n", 631 | " num1 = 0\n", 632 | " yield num1\n", 633 | " num2 = 1\n", 634 | " yield num2\n", 635 | " \n", 636 | " while True:\n", 637 | " next_num = num1 + num2\n", 638 | " yield next_num\n", 639 | " num1,num2 = num2,next_num\n", 640 | " \n", 641 | "g = fibo()\n", 642 | "print(g)\n", 643 | "print(next(g)) \n", 644 | "print(next(g)) \n", 645 | "print(next(g)) \n", 646 | "print(next(g))" 647 | ] 648 | }, 649 | { 650 | "cell_type": "code", 651 | "execution_count": 16, 652 | "metadata": {}, 653 | "outputs": [ 654 | { 655 | "name": "stdout", 656 | "output_type": "stream", 657 | "text": [ 658 | "\n", 659 | "[0, 1, 1, 2, 3]\n", 660 | "[5, 8, 13, 21, 34]\n" 661 | ] 662 | } 663 | ], 664 | "source": [ 665 | "def fibo():\n", 666 | " l = []\n", 667 | " num1 = 0\n", 668 | " num2 = 1\n", 669 | " \n", 670 | " l.append(num1)\n", 671 | " l.append(num2)\n", 672 | " \n", 673 | " count = 2\n", 674 | " while True:\n", 675 | " next_num = num1 + num2\n", 676 | " l.append(next_num)\n", 677 | " count+=1\n", 678 | " \n", 679 | " if count == 5:\n", 680 | " yield l\n", 681 | " l = []\n", 682 | " count = 0\n", 683 | " num1,num2 = num2,next_num\n", 684 | " \n", 685 | "g = fibo()\n", 686 | "print(g)\n", 687 | "print(next(g)) \n", 688 | "print(next(g))" 689 | ] 690 | }, 691 | { 692 | "cell_type": "code", 693 | "execution_count": 17, 694 | "metadata": {}, 695 | "outputs": [ 696 | { 697 | "name": "stdout", 698 | "output_type": "stream", 699 | "text": [ 700 | "\n", 701 | "Sample program line1\n", 702 | "Sample program line2\n", 703 | "Sample program line3\n", 704 | "Sample program line4\n", 705 | "sample1.txt\n" 706 | ] 707 | } 708 | ], 709 | "source": [ 710 | "def get_file_name():\n", 711 | " files = [\"sample.txt\",\"sample1.txt\",\"sample3.txt\"]\n", 712 | " for file in files:\n", 713 | " yield file\n", 714 | "\n", 715 | "def read_file(filename):\n", 716 | " fp = open(filename,\"r\")\n", 717 | " print(fp.read())\n", 718 | "g = get_file_name()\n", 719 | "print(g)\n", 720 | "read_file(next(g))\n", 721 | "print(next(g))" 722 | ] 723 | }, 724 | { 725 | "cell_type": "code", 726 | "execution_count": 18, 727 | "metadata": {}, 728 | "outputs": [ 729 | { 730 | "name": "stdout", 731 | "output_type": "stream", 732 | "text": [ 733 | "100\n" 734 | ] 735 | } 736 | ], 737 | "source": [ 738 | "l = [10,20,30,40,50,60]\n", 739 | "l2 = (value * value for value in l)\n", 740 | "\n", 741 | "# print(list(l2))\n", 742 | "print(next(l2))\n", 743 | "# print(next(l2))\n", 744 | "# for value in l2:\n", 745 | "# print(value)" 746 | ] 747 | }, 748 | { 749 | "cell_type": "code", 750 | "execution_count": null, 751 | "metadata": {}, 752 | "outputs": [], 753 | "source": [ 754 | "Decorators : a func which takes a func as an arg and return a new func" 755 | ] 756 | }, 757 | { 758 | "cell_type": "code", 759 | "execution_count": null, 760 | "metadata": {}, 761 | "outputs": [], 762 | "source": [] 763 | }, 764 | { 765 | "cell_type": "code", 766 | "execution_count": null, 767 | "metadata": {}, 768 | "outputs": [], 769 | "source": [ 770 | "@classmethod" 771 | ] 772 | }, 773 | { 774 | "cell_type": "code", 775 | "execution_count": 19, 776 | "metadata": {}, 777 | "outputs": [ 778 | { 779 | "name": "stdout", 780 | "output_type": "stream", 781 | "text": [ 782 | "30\n", 783 | "PythonJava\n", 784 | "10Python\n" 785 | ] 786 | } 787 | ], 788 | "source": [ 789 | "def add_deco(func):\n", 790 | " def new_func(val1,val2):\n", 791 | " if type(val1) == type(val2):\n", 792 | " return func(val1,val2)\n", 793 | " else:\n", 794 | " return func(str(val1),str(val2)) \n", 795 | " \n", 796 | " \n", 797 | " return new_func\n", 798 | "\n", 799 | "@add_deco\n", 800 | "def add(val1,val2):\n", 801 | " return val1 + val2\n", 802 | "\n", 803 | "print(add(10,20))\n", 804 | "print(add(\"Python\",\"Java\"))\n", 805 | "print(add(10,\"Python\"))" 806 | ] 807 | }, 808 | { 809 | "cell_type": "code", 810 | "execution_count": null, 811 | "metadata": {}, 812 | "outputs": [], 813 | "source": [ 814 | "print(add(10,20)) => Deco \n", 815 | " def add_deco(add)\n", 816 | " \n", 817 | " def new_func(val1,val2):\n", 818 | " \n", 819 | " return new_func => 2" 820 | ] 821 | }, 822 | { 823 | "cell_type": "code", 824 | "execution_count": 20, 825 | "metadata": {}, 826 | "outputs": [ 827 | { 828 | "name": "stdout", 829 | "output_type": "stream", 830 | "text": [ 831 | "Unauthorised user\n" 832 | ] 833 | } 834 | ], 835 | "source": [ 836 | "def login(func):\n", 837 | " \n", 838 | " def new_func(username,password,val): \n", 839 | " if username == \"abc@123\" and password == \"password\":\n", 840 | " return func(username,password,val)\n", 841 | " else:\n", 842 | " return \"Unauthorised user\"\n", 843 | " \n", 844 | " return new_func\n", 845 | " \n", 846 | "@login\n", 847 | "def edit_name(username,password,new_name):\n", 848 | " print(\"getting in edit name\")\n", 849 | " return \"Name updated\"\n", 850 | "\n", 851 | "@login\n", 852 | "def edit_email(username,password,new_name):\n", 853 | " print(\"getting in edit email\")\n", 854 | " return \"email updated\"\n", 855 | "\n", 856 | " \n", 857 | "print(edit_name(\"abc@1234\",\"password\",\"ABZ\"))\n", 858 | "# print(edit_email(\"abc@123\",\"password\",\"ABZ@gmail.com\"))" 859 | ] 860 | } 861 | ], 862 | "metadata": { 863 | "kernelspec": { 864 | "display_name": "Python 3", 865 | "language": "python", 866 | "name": "python3" 867 | }, 868 | "language_info": { 869 | "codemirror_mode": { 870 | "name": "ipython", 871 | "version": 3 872 | }, 873 | "file_extension": ".py", 874 | "mimetype": "text/x-python", 875 | "name": "python", 876 | "nbconvert_exporter": "python", 877 | "pygments_lexer": "ipython3", 878 | "version": "3.7.4" 879 | } 880 | }, 881 | "nbformat": 4, 882 | "nbformat_minor": 2 883 | } 884 | -------------------------------------------------------------------------------- /Session 3 - Conditional Statements and looping Statements.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 5, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "ename": "SyntaxError", 10 | "evalue": "invalid syntax (, line 1)", 11 | "output_type": "error", 12 | "traceback": [ 13 | "\u001b[1;36m File \u001b[1;32m\"\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m = => assignment\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n" 14 | ] 15 | }, 16 | { 17 | "ename": "SyntaxError", 18 | "evalue": "invalid syntax (, line 1)", 19 | "output_type": "error", 20 | "traceback": [ 21 | "\u001b[1;36m File \u001b[1;32m\"\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m = => assignment\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n" 22 | ] 23 | } 24 | ], 25 | "source": [ 26 | "= => assignment\n", 27 | "== => comparison" 28 | ] 29 | }, 30 | { 31 | "cell_type": "code", 32 | "execution_count": null, 33 | "metadata": {}, 34 | "outputs": [], 35 | "source": [ 36 | "type()\n", 37 | "id()" 38 | ] 39 | }, 40 | { 41 | "cell_type": "code", 42 | "execution_count": null, 43 | "metadata": {}, 44 | "outputs": [], 45 | "source": [ 46 | "Immutable : int , float , str , tuple\n", 47 | "Mutable : list , dict , set" 48 | ] 49 | }, 50 | { 51 | "cell_type": "code", 52 | "execution_count": null, 53 | "metadata": {}, 54 | "outputs": [], 55 | "source": [ 56 | "Conditional Statements if elif else" 57 | ] 58 | }, 59 | { 60 | "cell_type": "code", 61 | "execution_count": 6, 62 | "metadata": {}, 63 | "outputs": [ 64 | { 65 | "ename": "SyntaxError", 66 | "evalue": "invalid syntax (, line 4)", 67 | "output_type": "error", 68 | "traceback": [ 69 | "\u001b[1;36m File \u001b[1;32m\"\"\u001b[1;36m, line \u001b[1;32m4\u001b[0m\n\u001b[1;33m Posibilities :\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n" 70 | ] 71 | }, 72 | { 73 | "ename": "SyntaxError", 74 | "evalue": "invalid syntax (, line 4)", 75 | "output_type": "error", 76 | "traceback": [ 77 | "\u001b[1;36m File \u001b[1;32m\"\"\u001b[1;36m, line \u001b[1;32m4\u001b[0m\n\u001b[1;33m Posibilities :\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n" 78 | ] 79 | } 80 | ], 81 | "source": [ 82 | "num1 = 100\n", 83 | "num2 = 200\n", 84 | "\n", 85 | "Posibilities :\n", 86 | " \n", 87 | "# num1 is greater\n", 88 | "# num2 is greater\n", 89 | "# both are equal\n", 90 | "\n", 91 | "Syntax :\n", 92 | " \n", 93 | "if[condition] => True/False:\n", 94 | " print(\"num1 is greater\")\n" 95 | ] 96 | }, 97 | { 98 | "cell_type": "code", 99 | "execution_count": null, 100 | "metadata": {}, 101 | "outputs": [], 102 | "source": [ 103 | "num1 = 200\n", 104 | "num2 = 200\n", 105 | "\n", 106 | "if num1 > num2:\n", 107 | " print(\"num1 is greater here\")\n", 108 | "elif num2 > num1:\n", 109 | " print(\"num2 is greater here\")\n", 110 | "else:\n", 111 | " print(\"Both are equal\")" 112 | ] 113 | }, 114 | { 115 | "cell_type": "code", 116 | "execution_count": null, 117 | "metadata": {}, 118 | "outputs": [], 119 | "source": [ 120 | "True/False\n", 121 | "logical operatotrs\n", 122 | "comparision\n", 123 | "membership\n", 124 | "identity" 125 | ] 126 | }, 127 | { 128 | "cell_type": "code", 129 | "execution_count": null, 130 | "metadata": {}, 131 | "outputs": [], 132 | "source": [ 133 | "char = 'a'\n", 134 | "# \"Vowel\"/\"Consonent\"\n", 135 | "if char in 'aeiou':\n", 136 | " print(\"Vowel\")\n", 137 | "else:\n", 138 | " print(\"Consonent\")" 139 | ] 140 | }, 141 | { 142 | "cell_type": "code", 143 | "execution_count": null, 144 | "metadata": {}, 145 | "outputs": [], 146 | "source": [ 147 | "if [condition]:\n", 148 | " statements\n", 149 | "elif [condition]:\n", 150 | " statement\n", 151 | "else:" 152 | ] 153 | }, 154 | { 155 | "cell_type": "code", 156 | "execution_count": null, 157 | "metadata": {}, 158 | "outputs": [], 159 | "source": [ 160 | "PEP 8" 161 | ] 162 | }, 163 | { 164 | "cell_type": "code", 165 | "execution_count": 1, 166 | "metadata": {}, 167 | "outputs": [ 168 | { 169 | "name": "stdout", 170 | "output_type": "stream", 171 | "text": [ 172 | "Python\n" 173 | ] 174 | } 175 | ], 176 | "source": [ 177 | "if 0:\n", 178 | " print(\"Hii\")\n", 179 | "else:\n", 180 | " print(\"Python\")" 181 | ] 182 | }, 183 | { 184 | "cell_type": "code", 185 | "execution_count": null, 186 | "metadata": {}, 187 | "outputs": [], 188 | "source": [ 189 | "Any non zero value is True in Python\n", 190 | "Any zero value is False in Python like 0 , None , [] , () , {}" 191 | ] 192 | }, 193 | { 194 | "cell_type": "code", 195 | "execution_count": 2, 196 | "metadata": {}, 197 | "outputs": [ 198 | { 199 | "name": "stdout", 200 | "output_type": "stream", 201 | "text": [ 202 | "List is not empty\n" 203 | ] 204 | } 205 | ], 206 | "source": [ 207 | "l = [10,20,30]\n", 208 | "if l:\n", 209 | " print(\"List is not empty\")\n", 210 | "else:\n", 211 | " print(\"List is empty\")" 212 | ] 213 | }, 214 | { 215 | "cell_type": "code", 216 | "execution_count": null, 217 | "metadata": {}, 218 | "outputs": [], 219 | "source": [ 220 | "PEP 8 => To get the length of the list use len()\n", 221 | " cntl + / will comment all" 222 | ] 223 | }, 224 | { 225 | "cell_type": "code", 226 | "execution_count": 1, 227 | "metadata": {}, 228 | "outputs": [ 229 | { 230 | "name": "stdout", 231 | "output_type": "stream", 232 | "text": [ 233 | "3\n" 234 | ] 235 | } 236 | ], 237 | "source": [ 238 | "l = [10,20,30]\n", 239 | "print(len(l))" 240 | ] 241 | }, 242 | { 243 | "cell_type": "code", 244 | "execution_count": 2, 245 | "metadata": {}, 246 | "outputs": [ 247 | { 248 | "name": "stdout", 249 | "output_type": "stream", 250 | "text": [ 251 | "a is present\n" 252 | ] 253 | } 254 | ], 255 | "source": [ 256 | "s = \"Python sample string\"\n", 257 | "if 'a' in s:\n", 258 | " print(\"a is present\")\n", 259 | "else:\n", 260 | " print(\"a is not present\")" 261 | ] 262 | }, 263 | { 264 | "cell_type": "code", 265 | "execution_count": null, 266 | "metadata": {}, 267 | "outputs": [], 268 | "source": [ 269 | "marks_obtained = 380\n", 270 | "total_marks = 500\n", 271 | "# Pass/Fail\n", 272 | "# 40% => Pass else Fail\n", 273 | "percentage = (marks_obtained/total_marks)*100\n", 274 | "\n", 275 | "if percentage > passing_per:\n", 276 | " print(\"Pass\")\n", 277 | "else\n", 278 | " print(\"Fail\")" 279 | ] 280 | }, 281 | { 282 | "cell_type": "code", 283 | "execution_count": null, 284 | "metadata": {}, 285 | "outputs": [], 286 | "source": [ 287 | "cntl + / => Multiline comments" 288 | ] 289 | }, 290 | { 291 | "cell_type": "code", 292 | "execution_count": 4, 293 | "metadata": {}, 294 | "outputs": [ 295 | { 296 | "name": "stdout", 297 | "output_type": "stream", 298 | "text": [ 299 | "Odd\n" 300 | ] 301 | } 302 | ], 303 | "source": [ 304 | "num1 = 45\n", 305 | "if num1 % 2 == 0:\n", 306 | " print(\"Even\")\n", 307 | "else:\n", 308 | " print(\"Odd\")" 309 | ] 310 | }, 311 | { 312 | "cell_type": "code", 313 | "execution_count": null, 314 | "metadata": {}, 315 | "outputs": [], 316 | "source": [ 317 | "currencies = 2000 , 500 , 100 , 50 , 10\n", 318 | "amount = 5560" 319 | ] 320 | }, 321 | { 322 | "cell_type": "code", 323 | "execution_count": 2, 324 | "metadata": {}, 325 | "outputs": [ 326 | { 327 | "name": "stdout", 328 | "output_type": "stream", 329 | "text": [ 330 | "Notes of 2000 are 2\n" 331 | ] 332 | } 333 | ], 334 | "source": [ 335 | "amount = 5560\n", 336 | "if amount >= 2000:\n", 337 | " print(\"Notes of 2000 are\", amount // 2000)\n", 338 | " " 339 | ] 340 | }, 341 | { 342 | "cell_type": "code", 343 | "execution_count": 5, 344 | "metadata": {}, 345 | "outputs": [ 346 | { 347 | "name": "stdout", 348 | "output_type": "stream", 349 | "text": [ 350 | "Can not issue notes of 2000\n" 351 | ] 352 | } 353 | ], 354 | "source": [ 355 | "amount = 1999 \n", 356 | "if amount >= 2000:\n", 357 | " print(\"Notes of 2000 are\", amount // 2000)\n", 358 | " amount = amount %n2000\n", 359 | "else:\n", 360 | " print(\"Can not issue notes of 2000\")" 361 | ] 362 | }, 363 | { 364 | "cell_type": "code", 365 | "execution_count": 6, 366 | "metadata": {}, 367 | "outputs": [ 368 | { 369 | "name": "stdout", 370 | "output_type": "stream", 371 | "text": [ 372 | "Can not issue notes of 2000\n", 373 | "Notes of 500 are 3\n", 374 | "Notes of 100 are 4\n", 375 | "Notes of 50 are 1\n", 376 | "Notes of 10 are 4\n" 377 | ] 378 | } 379 | ], 380 | "source": [ 381 | "amount = 1999\n", 382 | "if amount >= 2000:\n", 383 | " print(\"Notes of 2000 are\", amount // 2000)\n", 384 | " amount = amount % 2000\n", 385 | "else:\n", 386 | " print(\"Can not issue notes of 2000\")\n", 387 | " \n", 388 | "if amount >= 500:\n", 389 | " print(\"Notes of 500 are\", amount // 500)\n", 390 | " amount = amount % 500\n", 391 | "else:\n", 392 | " print(\"Can not issue notes of 500\")\n", 393 | " \n", 394 | "if amount >= 100:\n", 395 | " print(\"Notes of 100 are\", amount // 100)\n", 396 | " amount = amount % 100\n", 397 | "else:\n", 398 | " print(\"Can not issue notes of 100\")\n", 399 | " \n", 400 | "if amount >= 50:\n", 401 | " print(\"Notes of 50 are\", amount // 50)\n", 402 | " amount = amount % 50\n", 403 | "else:\n", 404 | " print(\"Can not issue notes of 50\")\n", 405 | " \n", 406 | "if amount >= 10:\n", 407 | " print(\"Notes of 10 are\", amount // 10)\n", 408 | " amount = amount % 10\n", 409 | "else:\n", 410 | " print(\"Can not issue notes of 10\")" 411 | ] 412 | }, 413 | { 414 | "cell_type": "code", 415 | "execution_count": null, 416 | "metadata": {}, 417 | "outputs": [], 418 | "source": [ 419 | "For note_count" 420 | ] 421 | }, 422 | { 423 | "cell_type": "code", 424 | "execution_count": 11, 425 | "metadata": {}, 426 | "outputs": [ 427 | { 428 | "name": "stdout", 429 | "output_type": "stream", 430 | "text": [ 431 | "Notes of 2000 are 2\n", 432 | "Notes of 500 are 3\n", 433 | "Can not issue notes of 100\n", 434 | "Notes of 50 are 1\n", 435 | "Notes of 10 are 1\n", 436 | "Total no of notes are 7\n" 437 | ] 438 | } 439 | ], 440 | "source": [ 441 | "amount = 5560\n", 442 | "note_count = 0\n", 443 | "if amount >= 2000:\n", 444 | " print(\"Notes of 2000 are\", amount // 2000)\n", 445 | " note_count+=amount // 2000\n", 446 | " amount = amount % 2000\n", 447 | "else:\n", 448 | " print(\"Can not issue notes of 2000\")\n", 449 | " \n", 450 | "if amount >= 500:\n", 451 | " print(\"Notes of 500 are\", amount // 500)\n", 452 | " note_count+=amount // 500\n", 453 | " amount = amount % 500\n", 454 | "else:\n", 455 | " print(\"Can not issue notes of 500\")\n", 456 | " \n", 457 | "if amount >= 100:\n", 458 | " print(\"Notes of 100 are\", amount // 100)\n", 459 | " note_count+=amount // 100\n", 460 | " amount = amount % 100\n", 461 | "else:\n", 462 | " print(\"Can not issue notes of 100\")\n", 463 | " \n", 464 | "if amount >= 50:\n", 465 | " print(\"Notes of 50 are\", amount // 50)\n", 466 | " note_count+=amount // 50\n", 467 | " amount = amount % 50\n", 468 | "else:\n", 469 | " print(\"Can not issue notes of 50\")\n", 470 | " \n", 471 | "if amount >= 10:\n", 472 | " print(\"Notes of 10 are\", amount // 10)\n", 473 | " note_count+=amount // 10\n", 474 | " amount = amount % 10\n", 475 | "else:\n", 476 | " print(\"Can not issue notes of 10\")\n", 477 | " \n", 478 | "print(\"Total no of notes are\",note_count)" 479 | ] 480 | }, 481 | { 482 | "cell_type": "code", 483 | "execution_count": null, 484 | "metadata": {}, 485 | "outputs": [], 486 | "source": [ 487 | "rating = (1 - 5)\n", 488 | "tip % of total bill\n", 489 | " 1 10\n", 490 | " 2 20 \n", 491 | " 3 30\n", 492 | " 4 40\n", 493 | " 5 50" 494 | ] 495 | }, 496 | { 497 | "cell_type": "code", 498 | "execution_count": 11, 499 | "metadata": {}, 500 | "outputs": [ 501 | { 502 | "name": "stdout", 503 | "output_type": "stream", 504 | "text": [ 505 | "1300.0\n" 506 | ] 507 | } 508 | ], 509 | "source": [ 510 | "rating = 3\n", 511 | "bill_amt = 1000\n", 512 | "tip = bill_amt * (rating * 10) / 100\n", 513 | "final_amt = bill_amt + tip\n", 514 | "print(final_amt)" 515 | ] 516 | }, 517 | { 518 | "cell_type": "code", 519 | "execution_count": null, 520 | "metadata": {}, 521 | "outputs": [], 522 | "source": [ 523 | "overview" 524 | ] 525 | }, 526 | { 527 | "cell_type": "code", 528 | "execution_count": 18, 529 | "metadata": {}, 530 | "outputs": [ 531 | { 532 | "name": "stdout", 533 | "output_type": "stream", 534 | "text": [ 535 | "Java\n" 536 | ] 537 | } 538 | ], 539 | "source": [ 540 | "if \"Python\" :\n", 541 | " print(\"Java\")\n", 542 | "else:\n", 543 | " print(\"Python\")" 544 | ] 545 | }, 546 | { 547 | "cell_type": "code", 548 | "execution_count": null, 549 | "metadata": {}, 550 | "outputs": [], 551 | "source": [ 552 | "0 False None \"\" [] {} ()" 553 | ] 554 | }, 555 | { 556 | "cell_type": "code", 557 | "execution_count": null, 558 | "metadata": {}, 559 | "outputs": [], 560 | "source": [ 561 | "if condition:\n", 562 | " statements\n", 563 | "elif condition:\n", 564 | " statements\n", 565 | "else:\n", 566 | " statements" 567 | ] 568 | } 569 | ], 570 | "metadata": { 571 | "kernelspec": { 572 | "display_name": "Python 3", 573 | "language": "python", 574 | "name": "python3" 575 | }, 576 | "language_info": { 577 | "codemirror_mode": { 578 | "name": "ipython", 579 | "version": 3 580 | }, 581 | "file_extension": ".py", 582 | "mimetype": "text/x-python", 583 | "name": "python", 584 | "nbconvert_exporter": "python", 585 | "pygments_lexer": "ipython3", 586 | "version": "3.7.4" 587 | } 588 | }, 589 | "nbformat": 4, 590 | "nbformat_minor": 2 591 | } 592 | -------------------------------------------------------------------------------- /Session 4 - Looping Statements.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "Mutable :\n", 10 | " list\n", 11 | " set\n", 12 | " dict\n", 13 | "Immutable:\n", 14 | " int \n", 15 | " float\n", 16 | " str\n", 17 | " tuple" 18 | ] 19 | }, 20 | { 21 | "cell_type": "code", 22 | "execution_count": null, 23 | "metadata": {}, 24 | "outputs": [], 25 | "source": [ 26 | "id()\n", 27 | "type()" 28 | ] 29 | }, 30 | { 31 | "cell_type": "code", 32 | "execution_count": null, 33 | "metadata": {}, 34 | "outputs": [], 35 | "source": [ 36 | "None , 0 , False , () , [] , {}" 37 | ] 38 | }, 39 | { 40 | "cell_type": "code", 41 | "execution_count": 2, 42 | "metadata": {}, 43 | "outputs": [ 44 | { 45 | "name": "stdout", 46 | "output_type": "stream", 47 | "text": [ 48 | "hello\n" 49 | ] 50 | } 51 | ], 52 | "source": [ 53 | "if None:\n", 54 | " print(\"hi\")\n", 55 | "else:\n", 56 | " print(\"hello\")" 57 | ] 58 | }, 59 | { 60 | "cell_type": "code", 61 | "execution_count": null, 62 | "metadata": {}, 63 | "outputs": [], 64 | "source": [ 65 | "Looping Statements : For Loop\n", 66 | " While Loop" 67 | ] 68 | }, 69 | { 70 | "cell_type": "code", 71 | "execution_count": null, 72 | "metadata": {}, 73 | "outputs": [], 74 | "source": [ 75 | "marks_obtained = 380\n", 76 | "total_marks = 500\n", 77 | "# Pass/Fail\n", 78 | "# 40% => Pass else Fail\n", 79 | "percentage = (marks_obtained/total_marks)*100\n", 80 | "\n", 81 | "if percentage > passing_per:\n", 82 | " print(\"Pass\")\n", 83 | "else\n", 84 | " print(\"Fail\")" 85 | ] 86 | }, 87 | { 88 | "cell_type": "code", 89 | "execution_count": null, 90 | "metadata": {}, 91 | "outputs": [], 92 | "source": [ 93 | "l = [380,400,450,110,490]" 94 | ] 95 | }, 96 | { 97 | "cell_type": "code", 98 | "execution_count": null, 99 | "metadata": {}, 100 | "outputs": [], 101 | "source": [ 102 | "iterables datatypes :\n", 103 | " str\n", 104 | " list\n", 105 | " tuple\n", 106 | " dict\n", 107 | "Non iterables datatypes :\n", 108 | " int \n", 109 | " float" 110 | ] 111 | }, 112 | { 113 | "cell_type": "code", 114 | "execution_count": null, 115 | "metadata": {}, 116 | "outputs": [], 117 | "source": [ 118 | "l = [10,20,30,40,50]\n", 119 | "for [userdefined_variable] in [iterable_datatype]:\n", 120 | " [statements]\n", 121 | "[out of for loop]" 122 | ] 123 | }, 124 | { 125 | "cell_type": "code", 126 | "execution_count": 4, 127 | "metadata": {}, 128 | "outputs": [ 129 | { 130 | "name": "stdout", 131 | "output_type": "stream", 132 | "text": [ 133 | "10\n", 134 | "20\n", 135 | "30\n", 136 | "40\n", 137 | "50\n" 138 | ] 139 | } 140 | ], 141 | "source": [ 142 | "l = [10,20,30,40,50]\n", 143 | "\n", 144 | "for value in l:\n", 145 | " print(value)" 146 | ] 147 | }, 148 | { 149 | "cell_type": "code", 150 | "execution_count": 5, 151 | "metadata": {}, 152 | "outputs": [ 153 | { 154 | "name": "stdout", 155 | "output_type": "stream", 156 | "text": [ 157 | "p\n", 158 | "y\n", 159 | "t\n", 160 | "h\n", 161 | "o\n", 162 | "n\n", 163 | " \n", 164 | "i\n", 165 | "s\n", 166 | " \n", 167 | "e\n", 168 | "a\n", 169 | "s\n", 170 | "y\n", 171 | " \n", 172 | "t\n", 173 | "o\n", 174 | " \n", 175 | "l\n", 176 | "e\n", 177 | "a\n", 178 | "r\n", 179 | "n\n" 180 | ] 181 | } 182 | ], 183 | "source": [ 184 | "str = \"python is easy to learn\"\n", 185 | "\n", 186 | "for value in str:\n", 187 | " print(value)" 188 | ] 189 | }, 190 | { 191 | "cell_type": "code", 192 | "execution_count": 11, 193 | "metadata": {}, 194 | "outputs": [ 195 | { 196 | "name": "stdout", 197 | "output_type": "stream", 198 | "text": [ 199 | "email\n", 200 | "name\n", 201 | "id\n" 202 | ] 203 | } 204 | ], 205 | "source": [ 206 | "dict = {\"email\":\"abc@gmail.com\",\"name\":\"abc\",\"id\":\"100\"}\n", 207 | "\n", 208 | "for x in dict:\n", 209 | " print(x)" 210 | ] 211 | }, 212 | { 213 | "cell_type": "code", 214 | "execution_count": 23, 215 | "metadata": {}, 216 | "outputs": [ 217 | { 218 | "name": "stdout", 219 | "output_type": "stream", 220 | "text": [ 221 | "150\n" 222 | ] 223 | } 224 | ], 225 | "source": [ 226 | "l = [10,20,30,40,50]\n", 227 | "# sum = 0\n", 228 | "# num1 = 10\n", 229 | "# sum = 0 + 10 = 10\n", 230 | "\n", 231 | "# num2 = 20\n", 232 | "# sum = 10 + 20 = 30\n", 233 | "\n", 234 | "# num3 = 30\n", 235 | "# sum = 30 + 30 = 60\n", 236 | "\n", 237 | "# num4 = 40 \n", 238 | "# sum = 60 + 40 = 100\n", 239 | "\n", 240 | "# num5 = 50 \n", 241 | "# sum = 100 + 50 = 150\n", 242 | "\n", 243 | "sum=0\n", 244 | "for value in l:\n", 245 | " sum = sum + value\n", 246 | "print(sum)" 247 | ] 248 | }, 249 | { 250 | "cell_type": "code", 251 | "execution_count": null, 252 | "metadata": {}, 253 | "outputs": [], 254 | "source": [ 255 | "range function\n", 256 | "range(10) 0.....9\n", 257 | "range(10,25) 10...24\n", 258 | "range(10,50,5) 10.....45" 259 | ] 260 | }, 261 | { 262 | "cell_type": "code", 263 | "execution_count": 24, 264 | "metadata": {}, 265 | "outputs": [ 266 | { 267 | "name": "stdout", 268 | "output_type": "stream", 269 | "text": [ 270 | "0\n", 271 | "1\n", 272 | "2\n", 273 | "3\n", 274 | "4\n", 275 | "5\n", 276 | "6\n", 277 | "7\n", 278 | "8\n", 279 | "9\n" 280 | ] 281 | } 282 | ], 283 | "source": [ 284 | "for value in range(10):\n", 285 | " print(value)" 286 | ] 287 | }, 288 | { 289 | "cell_type": "code", 290 | "execution_count": 25, 291 | "metadata": {}, 292 | "outputs": [ 293 | { 294 | "name": "stdout", 295 | "output_type": "stream", 296 | "text": [ 297 | "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49]\n" 298 | ] 299 | } 300 | ], 301 | "source": [ 302 | "print(list(range(50)))" 303 | ] 304 | }, 305 | { 306 | "cell_type": "code", 307 | "execution_count": 26, 308 | "metadata": {}, 309 | "outputs": [ 310 | { 311 | "name": "stdout", 312 | "output_type": "stream", 313 | "text": [ 314 | "0\n", 315 | "1\n", 316 | "2\n", 317 | "3\n", 318 | "4\n", 319 | "5\n", 320 | "6\n", 321 | "7\n", 322 | "8\n", 323 | "9\n" 324 | ] 325 | } 326 | ], 327 | "source": [ 328 | "num1 = range(10)\n", 329 | "\n", 330 | "for value in num1:\n", 331 | " print(value)" 332 | ] 333 | }, 334 | { 335 | "cell_type": "code", 336 | "execution_count": null, 337 | "metadata": {}, 338 | "outputs": [], 339 | "source": [ 340 | "pass : do nothing\n", 341 | "continue : will jump to next itiration and not executable\n", 342 | "break : helps to come out or break the loop based on certain conditions" 343 | ] 344 | }, 345 | { 346 | "cell_type": "code", 347 | "execution_count": 35, 348 | "metadata": {}, 349 | "outputs": [ 350 | { 351 | "name": "stdout", 352 | "output_type": "stream", 353 | "text": [ 354 | "4 14\n" 355 | ] 356 | } 357 | ], 358 | "source": [ 359 | "s = \"Python Sample String\"\n", 360 | "vowel_count = 0\n", 361 | "consonent_count = 0\n", 362 | "\n", 363 | "for value in s:\n", 364 | " if value in \"AEIOUaeiou\":\n", 365 | " vowel_count+=1\n", 366 | " elif value == \" \":\n", 367 | " continue\n", 368 | " print(\"lets check if i see this in output\")\n", 369 | " else:\n", 370 | " consonent_count+= 1\n", 371 | " \n", 372 | "print(vowel_count,consonent_count)" 373 | ] 374 | }, 375 | { 376 | "cell_type": "code", 377 | "execution_count": 36, 378 | "metadata": {}, 379 | "outputs": [ 380 | { 381 | "name": "stdout", 382 | "output_type": "stream", 383 | "text": [ 384 | "lets check if i see this in output\n", 385 | "lets check if i see this in output\n", 386 | "4 14\n" 387 | ] 388 | } 389 | ], 390 | "source": [ 391 | "s = \"Python Sample String\"\n", 392 | "vowel_count = 0\n", 393 | "consonent_count = 0\n", 394 | "\n", 395 | "for value in s:\n", 396 | " if value in \"AEIOUaeiou\":\n", 397 | " vowel_count+=1\n", 398 | " elif value == \" \":\n", 399 | " pass\n", 400 | " print(\"lets check if i see this in output\")\n", 401 | " else:\n", 402 | " consonent_count+= 1\n", 403 | " \n", 404 | "print(vowel_count,consonent_count)" 405 | ] 406 | }, 407 | { 408 | "cell_type": "code", 409 | "execution_count": 37, 410 | "metadata": {}, 411 | "outputs": [ 412 | { 413 | "ename": "SyntaxError", 414 | "evalue": "invalid syntax (, line 1)", 415 | "output_type": "error", 416 | "traceback": [ 417 | "\u001b[1;36m File \u001b[1;32m\"\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m if 100\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n" 418 | ] 419 | } 420 | ], 421 | "source": [ 422 | "if 100\n", 423 | " print(\"hi\")\n", 424 | "else:\n", 425 | " print(\"hello\")" 426 | ] 427 | }, 428 | { 429 | "cell_type": "code", 430 | "execution_count": 40, 431 | "metadata": {}, 432 | "outputs": [ 433 | { 434 | "name": "stdout", 435 | "output_type": "stream", 436 | "text": [ 437 | "hi\n", 438 | "hello\n" 439 | ] 440 | } 441 | ], 442 | "source": [ 443 | "if 100:\n", 444 | " print(\"hi\")\n", 445 | "else:\n", 446 | " pass\n", 447 | "print(\"hello\")" 448 | ] 449 | }, 450 | { 451 | "cell_type": "code", 452 | "execution_count": 47, 453 | "metadata": {}, 454 | "outputs": [ 455 | { 456 | "name": "stdout", 457 | "output_type": "stream", 458 | "text": [ 459 | "10\n", 460 | "20\n", 461 | "30\n", 462 | "Number is Negative -1\n", 463 | "100\n", 464 | "20\n", 465 | "40\n", 466 | "60\n" 467 | ] 468 | } 469 | ], 470 | "source": [ 471 | "l = [10,20,30,-1,100,20,40,60]\n", 472 | "for value in l:\n", 473 | " if value < 0:\n", 474 | " print(\"Number is Negative\",value)\n", 475 | " else:\n", 476 | " print(value)" 477 | ] 478 | }, 479 | { 480 | "cell_type": "code", 481 | "execution_count": 48, 482 | "metadata": {}, 483 | "outputs": [ 484 | { 485 | "name": "stdout", 486 | "output_type": "stream", 487 | "text": [ 488 | "10\n", 489 | "20\n", 490 | "30\n", 491 | "Number is Negative -1\n" 492 | ] 493 | } 494 | ], 495 | "source": [ 496 | "l = [10,20,30,-1,100,20,40,60]\n", 497 | "for value in l:\n", 498 | " if value < 0:\n", 499 | " print(\"Number is Negative\",value)\n", 500 | " break\n", 501 | " else:\n", 502 | " print(value)" 503 | ] 504 | }, 505 | { 506 | "cell_type": "code", 507 | "execution_count": 49, 508 | "metadata": {}, 509 | "outputs": [ 510 | { 511 | "name": "stdout", 512 | "output_type": "stream", 513 | "text": [ 514 | "True\n" 515 | ] 516 | } 517 | ], 518 | "source": [ 519 | "l = [1,2,3,4,5]\n", 520 | "print(5 in l)" 521 | ] 522 | }, 523 | { 524 | "cell_type": "code", 525 | "execution_count": 61, 526 | "metadata": {}, 527 | "outputs": [ 528 | { 529 | "name": "stdout", 530 | "output_type": "stream", 531 | "text": [ 532 | "key is not present\n" 533 | ] 534 | } 535 | ], 536 | "source": [ 537 | "nums = [10,20,30,40,50,60,70,80]\n", 538 | "key = 500\n", 539 | "flag = False\n", 540 | "for value in nums:\n", 541 | " if value == key:\n", 542 | " flag = True\n", 543 | " break\n", 544 | " \n", 545 | "if flag == True:\n", 546 | " print(\"key is present\")\n", 547 | "else:\n", 548 | " print(\"key is not present\")" 549 | ] 550 | }, 551 | { 552 | "cell_type": "code", 553 | "execution_count": null, 554 | "metadata": {}, 555 | "outputs": [], 556 | "source": [ 557 | "flag variable is used to check element exist ot not" 558 | ] 559 | }, 560 | { 561 | "cell_type": "code", 562 | "execution_count": 72, 563 | "metadata": {}, 564 | "outputs": [ 565 | { 566 | "name": "stdout", 567 | "output_type": "stream", 568 | "text": [ 569 | "key is present\n" 570 | ] 571 | } 572 | ], 573 | "source": [ 574 | "nums = [10,20,30,40,50,60,70,80]\n", 575 | "key = 80\n", 576 | "\n", 577 | "for value in nums:\n", 578 | " if value == key:\n", 579 | " print(\"key is present\")\n", 580 | " break\n", 581 | "else:\n", 582 | " print(\"key is not present\")\n", 583 | " " 584 | ] 585 | }, 586 | { 587 | "cell_type": "code", 588 | "execution_count": null, 589 | "metadata": {}, 590 | "outputs": [], 591 | "source": [ 592 | "enumerate => gives us index , value" 593 | ] 594 | }, 595 | { 596 | "cell_type": "code", 597 | "execution_count": 73, 598 | "metadata": {}, 599 | "outputs": [ 600 | { 601 | "name": "stdout", 602 | "output_type": "stream", 603 | "text": [ 604 | "0 10\n", 605 | "1 20\n", 606 | "2 30\n", 607 | "3 40\n", 608 | "4 50\n", 609 | "5 60\n", 610 | "6 70\n", 611 | "7 80\n" 612 | ] 613 | } 614 | ], 615 | "source": [ 616 | "l = [10,20,30,40,50,60,70,80]\n", 617 | "for index,value in enumerate(l):\n", 618 | " print(index,value)" 619 | ] 620 | }, 621 | { 622 | "cell_type": "code", 623 | "execution_count": 75, 624 | "metadata": {}, 625 | "outputs": [ 626 | { 627 | "name": "stdout", 628 | "output_type": "stream", 629 | "text": [ 630 | "key is present at index: 7\n" 631 | ] 632 | } 633 | ], 634 | "source": [ 635 | "nums = [10,20,30,40,50,60,70,80]\n", 636 | "key = 80\n", 637 | "\n", 638 | "for index,value in enumerate(nums):\n", 639 | " if value == key:\n", 640 | " print(\"key is present at index:\",index)\n", 641 | " break\n", 642 | "else:\n", 643 | " print(\"key is not present\")" 644 | ] 645 | }, 646 | { 647 | "cell_type": "code", 648 | "execution_count": null, 649 | "metadata": {}, 650 | "outputs": [], 651 | "source": [ 652 | "num1 = 6\n", 653 | "# perfect no or not\n", 654 | "# 1 + 2 + 3 = 6\n", 655 | "\n", 656 | "Possible divisors = 1 2 3 4 5 \n", 657 | "Divisors = 1 2 3 \n", 658 | "sum = \n" 659 | ] 660 | }, 661 | { 662 | "cell_type": "code", 663 | "execution_count": 2, 664 | "metadata": {}, 665 | "outputs": [ 666 | { 667 | "name": "stdout", 668 | "output_type": "stream", 669 | "text": [ 670 | "1\n", 671 | "2\n", 672 | "3\n", 673 | "4\n", 674 | "5\n" 675 | ] 676 | } 677 | ], 678 | "source": [ 679 | "num1 = 6\n", 680 | "\n", 681 | "for value in range(1,num1):\n", 682 | " print(value)" 683 | ] 684 | }, 685 | { 686 | "cell_type": "code", 687 | "execution_count": 3, 688 | "metadata": {}, 689 | "outputs": [ 690 | { 691 | "name": "stdout", 692 | "output_type": "stream", 693 | "text": [ 694 | "1\n", 695 | "2\n", 696 | "3\n" 697 | ] 698 | } 699 | ], 700 | "source": [ 701 | "num1 = 6\n", 702 | "\n", 703 | "for value in range(1,num1):\n", 704 | " if num1 % value == 0:\n", 705 | " print(value)" 706 | ] 707 | }, 708 | { 709 | "cell_type": "code", 710 | "execution_count": 4, 711 | "metadata": {}, 712 | "outputs": [ 713 | { 714 | "name": "stdout", 715 | "output_type": "stream", 716 | "text": [ 717 | "Perfect number\n" 718 | ] 719 | } 720 | ], 721 | "source": [ 722 | "num1 = 6\n", 723 | "sum = 0\n", 724 | "for value in range(1,num1):\n", 725 | " if num1 % value == 0:\n", 726 | " sum = sum + value\n", 727 | "if sum == num1:\n", 728 | " print(\"Perfect number\")\n", 729 | "else:\n", 730 | " print(\"Not a Perfect number\")\n", 731 | " " 732 | ] 733 | }, 734 | { 735 | "cell_type": "code", 736 | "execution_count": 7, 737 | "metadata": {}, 738 | "outputs": [ 739 | { 740 | "name": "stdout", 741 | "output_type": "stream", 742 | "text": [ 743 | "1\n", 744 | "2\n", 745 | "3\n", 746 | "4\n", 747 | "5\n", 748 | "6\n", 749 | "7\n", 750 | "8\n", 751 | "9\n", 752 | "10\n", 753 | "11\n", 754 | "12\n", 755 | "13\n", 756 | "14\n", 757 | "Perfect number\n" 758 | ] 759 | } 760 | ], 761 | "source": [ 762 | "num1 = 28\n", 763 | "sum = 0\n", 764 | "for value in range(1,num1 // 2 + 1):\n", 765 | " print(value)\n", 766 | " if num1 % value == 0:\n", 767 | " sum = sum + value\n", 768 | "if sum == num1:\n", 769 | " print(\"Perfect number\")\n", 770 | "else:\n", 771 | " print(\"Not a Perfect number\")\n", 772 | " " 773 | ] 774 | }, 775 | { 776 | "cell_type": "code", 777 | "execution_count": 9, 778 | "metadata": {}, 779 | "outputs": [ 780 | { 781 | "name": "stdout", 782 | "output_type": "stream", 783 | "text": [ 784 | "1 2 3 4 5 6 7 8 9 10 2 4 6 8 10 12 14 16 18 20 3 6 9 12 15 18 21 24 27 30 4 8 12 16 20 24 28 32 36 40 5 10 15 20 25 30 35 40 45 50 " 785 | ] 786 | } 787 | ], 788 | "source": [ 789 | "for value1 in range(1,6):\n", 790 | " for value2 in range(1,11):\n", 791 | " print(value1 * value2,end = \" \")" 792 | ] 793 | }, 794 | { 795 | "cell_type": "code", 796 | "execution_count": 11, 797 | "metadata": {}, 798 | "outputs": [ 799 | { 800 | "name": "stdout", 801 | "output_type": "stream", 802 | "text": [ 803 | "1 2 3 4 5 6 7 8 9 10 \n", 804 | "\n", 805 | "2 4 6 8 10 12 14 16 18 20 \n", 806 | "\n", 807 | "3 6 9 12 15 18 21 24 27 30 \n", 808 | "\n", 809 | "4 8 12 16 20 24 28 32 36 40 \n", 810 | "\n", 811 | "5 10 15 20 25 30 35 40 45 50 \n", 812 | "\n" 813 | ] 814 | } 815 | ], 816 | "source": [ 817 | "for value1 in range(1,6):\n", 818 | " for value2 in range(1,11):\n", 819 | " print(value1 * value2,end = \" \")\n", 820 | " print(\"\\n\")" 821 | ] 822 | }, 823 | { 824 | "cell_type": "code", 825 | "execution_count": null, 826 | "metadata": {}, 827 | "outputs": [], 828 | "source": [ 829 | "Overview:" 830 | ] 831 | }, 832 | { 833 | "cell_type": "code", 834 | "execution_count": null, 835 | "metadata": {}, 836 | "outputs": [], 837 | "source": [ 838 | "for loop syntax\n", 839 | "for userdefined_variables" 840 | ] 841 | }, 842 | { 843 | "cell_type": "code", 844 | "execution_count": 12, 845 | "metadata": {}, 846 | "outputs": [ 847 | { 848 | "name": "stdout", 849 | "output_type": "stream", 850 | "text": [ 851 | "10 20 Python\n" 852 | ] 853 | } 854 | ], 855 | "source": [ 856 | "print(10,20,\"Python\")" 857 | ] 858 | }, 859 | { 860 | "cell_type": "code", 861 | "execution_count": 14, 862 | "metadata": {}, 863 | "outputs": [ 864 | { 865 | "name": "stdout", 866 | "output_type": "stream", 867 | "text": [ 868 | "10,20,Python\n", 869 | "Hi\n" 870 | ] 871 | } 872 | ], 873 | "source": [ 874 | "print(10,20,\"Python\",sep = \",\")\n", 875 | "print(\"Hi\")" 876 | ] 877 | }, 878 | { 879 | "cell_type": "code", 880 | "execution_count": 16, 881 | "metadata": {}, 882 | "outputs": [ 883 | { 884 | "name": "stdout", 885 | "output_type": "stream", 886 | "text": [ 887 | "10,20,Python Hi Java\n" 888 | ] 889 | } 890 | ], 891 | "source": [ 892 | "print(10,20,\"Python\",sep = \",\",end = \" \")\n", 893 | "print(\"Hi\",end = \" \")\n", 894 | "print(\"Java\")" 895 | ] 896 | }, 897 | { 898 | "cell_type": "code", 899 | "execution_count": null, 900 | "metadata": {}, 901 | "outputs": [], 902 | "source": [ 903 | "while => True/False:\n", 904 | " statements" 905 | ] 906 | }, 907 | { 908 | "cell_type": "code", 909 | "execution_count": 18, 910 | "metadata": {}, 911 | "outputs": [ 912 | { 913 | "name": "stdout", 914 | "output_type": "stream", 915 | "text": [ 916 | "120\n" 917 | ] 918 | } 919 | ], 920 | "source": [ 921 | "num1 = 5\n", 922 | "result = 1\n", 923 | "while num1 > 1:\n", 924 | " result = result * num1\n", 925 | " num1 = num1 - 1\n", 926 | "print(result)" 927 | ] 928 | }, 929 | { 930 | "cell_type": "code", 931 | "execution_count": null, 932 | "metadata": {}, 933 | "outputs": [], 934 | "source": [ 935 | "while 100:\n", 936 | " print(100)" 937 | ] 938 | }, 939 | { 940 | "cell_type": "code", 941 | "execution_count": null, 942 | "metadata": {}, 943 | "outputs": [], 944 | "source": [] 945 | } 946 | ], 947 | "metadata": { 948 | "kernelspec": { 949 | "display_name": "Python 3", 950 | "language": "python", 951 | "name": "python3" 952 | }, 953 | "language_info": { 954 | "codemirror_mode": { 955 | "name": "ipython", 956 | "version": 3 957 | }, 958 | "file_extension": ".py", 959 | "mimetype": "text/x-python", 960 | "name": "python", 961 | "nbconvert_exporter": "python", 962 | "pygments_lexer": "ipython3", 963 | "version": "3.7.4" 964 | } 965 | }, 966 | "nbformat": 4, 967 | "nbformat_minor": 2 968 | } 969 | -------------------------------------------------------------------------------- /Session 9 - Problem Solving Session.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "ename": "SyntaxError", 10 | "evalue": "invalid syntax (, line 1)", 11 | "output_type": "error", 12 | "traceback": [ 13 | "\u001b[1;36m File \u001b[1;32m\"\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m True/False :\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n" 14 | ] 15 | } 16 | ], 17 | "source": [ 18 | "True/False :\n", 19 | " - Comparision ==, != ,< , >\n", 20 | " - Identity is is not\n", 21 | " - Membership in not in \n", 22 | " - Logical operators and or not\n", 23 | " \n", 24 | "== => values\n", 25 | "is => memory allocation\n", 26 | "\n", 27 | "False values : None \"\" 0 [] () {}\n", 28 | " \n", 29 | "enumerate\n", 30 | "for index,value in enumerate(iterable):\n", 31 | " \n", 32 | "keys, values => items()\n", 33 | "\n", 34 | "for \n", 35 | "else:\n", 36 | " \n", 37 | "[start:end:stride]\n", 38 | "2 => every seco char\n", 39 | "-1 => \n", 40 | "1 => left to right\n", 41 | "\n", 42 | "s[100] => Indexerror #if 100 not exist\n", 43 | "\n", 44 | "Lists:\n", 45 | " add :\n", 46 | " append extend insert\n", 47 | " \n", 48 | " update :\n", 49 | " l[index]=new_value\n", 50 | " \n", 51 | " delete :\n", 52 | " pop => index\n", 53 | " remove => value\n", 54 | " clear => empty list\n", 55 | " del => del reference\n", 56 | " \n", 57 | " l1 + l2 => it will perform same operation as extend or concatinate two list\n", 58 | " l1 * l2 => repeat same list two times\n", 59 | " \n", 60 | "Dict :\n", 61 | " Add :\n", 62 | " d[key] = value\n", 63 | " setdefault(key,default_value) => it return the existing value if key exist and if key does not exist\n", 64 | " it set the value defaultly\n", 65 | " \n", 66 | " Upadate :\n", 67 | " d[key] = new_value\n", 68 | " \n", 69 | " delete :\n", 70 | " pop => deletes key\n", 71 | " pop.item => del last key and value\n", 72 | " clear => empty dict\n", 73 | " del => del ref\n", 74 | " \n", 75 | "rules of dict\n", 76 | "- mutable\n", 77 | "- unordered\n", 78 | "- all the keys should be unique\n", 79 | "- all the keys should be immutable => int,str,float,tuple\n", 80 | "\n", 81 | "\n", 82 | "Sets :\n", 83 | " - mutable \n", 84 | " - unordered\n", 85 | " - all the elements should be unique\n", 86 | " - all the elements should be immutable\n", 87 | " \n", 88 | "Iterable :\n", 89 | " list tuple str dict set\n", 90 | "Not iterable :\n", 91 | " int float\n", 92 | " \n", 93 | " \n", 94 | "print(dir(list))\n", 95 | "\n", 96 | "help(list)" 97 | ] 98 | }, 99 | { 100 | "cell_type": "code", 101 | "execution_count": null, 102 | "metadata": {}, 103 | "outputs": [], 104 | "source": [ 105 | "1.Find the missing number in given integer array of 1 to 10\n", 106 | "l = [1,2,3,4,5,6,7,7,8,9,10]\n", 107 | "\n", 108 | "=> Range(1,11)\n", 109 | "\n", 110 | "\n", 111 | "=> Sum of n natural nos : n (n+1) / 2\n", 112 | " sum => 1 to 10\n", 113 | " sum_list\n", 114 | " missing_element = sum - sum_list\n", 115 | " \n", 116 | "=> sets only unique elements" 117 | ] 118 | }, 119 | { 120 | "cell_type": "code", 121 | "execution_count": 2, 122 | "metadata": {}, 123 | "outputs": [ 124 | { 125 | "name": "stdout", 126 | "output_type": "stream", 127 | "text": [ 128 | "[8]\n" 129 | ] 130 | } 131 | ], 132 | "source": [ 133 | "input_nums = [1,2,3,4,5,6,7,9,10]\n", 134 | "all_nums = list(range(1,11))\n", 135 | "missing_element = []\n", 136 | "\n", 137 | "for value in all_nums:\n", 138 | " if value not in input_nums:\n", 139 | " missing_element.append(value)\n", 140 | "print(missing_element)" 141 | ] 142 | }, 143 | { 144 | "cell_type": "code", 145 | "execution_count": 3, 146 | "metadata": {}, 147 | "outputs": [ 148 | { 149 | "name": "stdout", 150 | "output_type": "stream", 151 | "text": [ 152 | "55.0\n" 153 | ] 154 | } 155 | ], 156 | "source": [ 157 | "input_nums = [1,2,3,4,5,6,7,9,10]\n", 158 | "n = 10\n", 159 | "expected_sum = n * (n + 1) / 2\n", 160 | "print(expected_sum)\n" 161 | ] 162 | }, 163 | { 164 | "cell_type": "code", 165 | "execution_count": 4, 166 | "metadata": {}, 167 | "outputs": [ 168 | { 169 | "name": "stdout", 170 | "output_type": "stream", 171 | "text": [ 172 | "[8]\n" 173 | ] 174 | } 175 | ], 176 | "source": [ 177 | "input_nums = [1,2,3,4,5,6,7,9,10]#sum of all elements\n", 178 | "n = 10\n", 179 | "expected_sum = n * (n + 1) / 2\n", 180 | "actual_sum = sum(input_nums)\n", 181 | "missing_nums = expected_sum - actual_sum\n", 182 | "print(missing_element)" 183 | ] 184 | }, 185 | { 186 | "cell_type": "code", 187 | "execution_count": null, 188 | "metadata": {}, 189 | "outputs": [], 190 | "source": [ 191 | "2.Find an element in array such that sum of left array is equal to right array.\n", 192 | "\n", 193 | "l = [1,2,3,1,1,1]\n", 194 | "\n", 195 | "1 2 left side 1 rs => 6\n", 196 | " 3 left side right side => 3" 197 | ] 198 | }, 199 | { 200 | "cell_type": "code", 201 | "execution_count": 9, 202 | "metadata": {}, 203 | "outputs": [ 204 | { 205 | "name": "stdout", 206 | "output_type": "stream", 207 | "text": [ 208 | "eqi element is present at index 2 and value is 3\n" 209 | ] 210 | } 211 | ], 212 | "source": [ 213 | "l = [1,2,3,1,1,1]\n", 214 | "\n", 215 | "for index,value in enumerate(l):\n", 216 | " if sum(l[:index]) == sum(l[index + 1:]):\n", 217 | " print(\"eqi element is present at index {} and value is {}\".format(index,l[index]))\n", 218 | " break" 219 | ] 220 | }, 221 | { 222 | "cell_type": "code", 223 | "execution_count": 10, 224 | "metadata": {}, 225 | "outputs": [ 226 | { 227 | "name": "stdout", 228 | "output_type": "stream", 229 | "text": [ 230 | "eqi element is present at index 4 and value is 5\n" 231 | ] 232 | } 233 | ], 234 | "source": [ 235 | "l = [1,2,1,1,5,1,4]\n", 236 | "for index,value in enumerate(l):\n", 237 | " if sum(l[:index]) == sum(l[index + 1:]):\n", 238 | " print(\"eqi element is present at index {} and value is {}\".format(index,l[index]))\n", 239 | " break" 240 | ] 241 | }, 242 | { 243 | "cell_type": "code", 244 | "execution_count": null, 245 | "metadata": {}, 246 | "outputs": [], 247 | "source": [ 248 | "3.Given string: first, second and third. Third string is said to be a shuffle of first\n", 249 | " and second if it can be formed byinterleaving the chracters of first and second string\n", 250 | " in a way that maintain the left to right ordering of the characters from each string.\n", 251 | " \n", 252 | "For exmaple, given first = \"abc\" second = \"def\" and third = \"dabecf\" is valid shuffle since it preserves \n", 253 | "the character ordering of the two strings. So,given these string write a program that detects whether the \n", 254 | "third string is valid shuffle of first and second string." 255 | ] 256 | }, 257 | { 258 | "cell_type": "code", 259 | "execution_count": 17, 260 | "metadata": {}, 261 | "outputs": [ 262 | { 263 | "name": "stdout", 264 | "output_type": "stream", 265 | "text": [ 266 | "valid shuffle\n" 267 | ] 268 | } 269 | ], 270 | "source": [ 271 | "s1 = \"abc\"\n", 272 | "s2 = \"def\"\n", 273 | "\n", 274 | "s3 = \"dabecf\"\n", 275 | "\n", 276 | "# 1. Iterate over s3\n", 277 | "# 2.Check if char is present in S1 or S2\n", 278 | "# 3. s1_pointer = l s2_pointer = 1\n", 279 | "\n", 280 | "s1_pointer = s2_pointer = 0\n", 281 | "s3_index = 0\n", 282 | "while s3_index < len(s3):\n", 283 | " \n", 284 | " if s3[s3_index] in s1 and s1[s1_pointer] == s3[s3_index]:\n", 285 | " s1_pointer+=1\n", 286 | " \n", 287 | " elif s3[s3_index] in s2 and s2[s2_pointer] == s3[s3_index]:\n", 288 | " s2_pointer+=1\n", 289 | " else:\n", 290 | " print(\"Not a valid shuffle\")\n", 291 | " break\n", 292 | " s3_index+=1\n", 293 | "else:\n", 294 | " print(\"valid shuffle\")" 295 | ] 296 | }, 297 | { 298 | "cell_type": "code", 299 | "execution_count": null, 300 | "metadata": {}, 301 | "outputs": [], 302 | "source": [] 303 | } 304 | ], 305 | "metadata": { 306 | "kernelspec": { 307 | "display_name": "Python 3", 308 | "language": "python", 309 | "name": "python3" 310 | }, 311 | "language_info": { 312 | "codemirror_mode": { 313 | "name": "ipython", 314 | "version": 3 315 | }, 316 | "file_extension": ".py", 317 | "mimetype": "text/x-python", 318 | "name": "python", 319 | "nbconvert_exporter": "python", 320 | "pygments_lexer": "ipython3", 321 | "version": "3.7.4" 322 | } 323 | }, 324 | "nbformat": 4, 325 | "nbformat_minor": 2 326 | } 327 | -------------------------------------------------------------------------------- /blog.txt: -------------------------------------------------------------------------------- 1 | We’re rapidly moving towards a day and age where almost everything can be controlled by machines and one of the most revolutionary moves has been the advent of artificial intelligence. In this artificial intelligence course, we will talk about how to get started with AI and what exactly it is. While it began as a part of smartphones and other smart devices, AI has been increasingly taking over in other fields too. To begin with, one must understand what Artificial Intelligence is. It is an area of computer science focusing on the creation of intelligent machines that work and react like humans. 2 | 3 | Well, of course, there’s a lot more to this simple-seeming definition and this phenomenon is surely headed to change the future. 4 | 5 | 6 | 7 | Goals 8 | 9 | Without us realizing, it has become amply clear that AI has become a part of a lot of industries in ways which now seem inseparable. If you are trying to learn artificial intelligence it is firstly, important to understand the goals of AI. 10 | 11 | 12 | 13 | - Primary goals: deduction and reasoning, knowledge representation, planning, Natural Language Processing (NLP), learning, perception and the ability to move and manipulate objects. 14 | 15 | 16 | 17 | - Long-term goals: To achieve creativity, social intelligence, and human level intelligence. 18 | 19 | 20 | 21 | It is clear as day, that a lot of these goals have been achieved while a lot are on the way of being fulfilled. Moreover, the goals that have been achieved are surely being refined every day. 22 | 23 | 24 | 25 | Types 26 | 27 | Next in line with this artificial intelligence online course is understanding the various types of AI. There are a lot of categories that fall into AI but it can be divided into three basic concepts based on AI’s capabilities: 28 | 29 | 30 | 31 | - Weak: Weak AI or Narrow AI, perform only one task. The AI has to be conditioned with a set of certain answers and roles and it does not go beyond that, weak AI is thus not self-sufficient. An example of this is iOs’ Siri. 32 | 33 | 34 | 35 | - Strong: Strong AI can basically do everything that a human being can do. Imagine delivering food to people’s doorsteps. A lot of research is going into this and engineers have made enough headway into it, but there’s still a long way to go. 36 | 37 | 38 | 39 | - Superintelligence: Artificial superintelligence goes beyond strong AI. It can actually exceed human beings when it comes to creativity, general wisdom and even social skills. 40 | 41 | 42 | 43 | Core Concepts 44 | 45 | The core concepts that are currently guiding AI are: 46 | 47 | 48 | 49 | - Machine Learning: Under machine learning, a machine is fed data which it learns to utilize without actually being explicitly programmed. The machine automatically recognizes patterns and makes decisions. 50 | 51 | 52 | 53 | - Deep Learning: Deep learning is a subfield of machine learning and aims to simulate human thinking. The algorithms of this can automatically determine the accuracy of a prediction. 54 | 55 | 56 | 57 | - Supervised Learning: Under supervised learning a machine is fed data with labels and categorizations, leading the machine to churn out only the same and right answers. 58 | 59 | 60 | 61 | - Unsupervised Learning: With unsupervised learning, on the other hand, the machine is fed data without any labels or categories yet the machine manages to find patterns and create categories 62 | 63 | 64 | 65 | - Reinforcement Learning: The algorithm works on trial and error and has to be conditioned using reinforcement much like the human mind. 66 | 67 | 68 | 69 | Getting Started 70 | 71 | Now that you know, the basic concepts and the foundation of AI, it’s time to invest your time and resources in a programming languages course. There are a lot of languages you can start with but many tend to prefer Python. 72 | 73 | 74 | 75 | You can learn Python and take a comprehensive artificial intelligence course only on Edyoda, that too for free. 76 | 77 | 78 | 79 | For more, head down to https://www.edyoda.com/ -------------------------------------------------------------------------------- /bs.py: -------------------------------------------------------------------------------- 1 | def binary_search(l,key): 2 | mid = len(l) // 2 3 | if not l: 4 | return False 5 | 6 | elif l[mid] == key: 7 | return True 8 | elif key < l[mid]: 9 | return(binary_search(l[:mid],key)) 10 | 11 | else: 12 | return(binary_search(l[mid+1:],key)) 13 | 14 | if __name__ == "__main__": 15 | l = [100,200,300,400,500,600,700] 16 | key = 600 17 | 18 | print(binary_search(l,key)) -------------------------------------------------------------------------------- /session 1 - basics.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 2, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "data": { 10 | "text/plain": [ 11 | "17" 12 | ] 13 | }, 14 | "execution_count": 2, 15 | "metadata": {}, 16 | "output_type": "execute_result" 17 | } 18 | ], 19 | "source": [ 20 | "5+12" 21 | ] 22 | }, 23 | { 24 | "cell_type": "code", 25 | "execution_count": 2, 26 | "metadata": {}, 27 | "outputs": [ 28 | { 29 | "data": { 30 | "text/plain": [ 31 | "36" 32 | ] 33 | }, 34 | "execution_count": 2, 35 | "metadata": {}, 36 | "output_type": "execute_result" 37 | } 38 | ], 39 | "source": [ 40 | "6*6" 41 | ] 42 | }, 43 | { 44 | "cell_type": "code", 45 | "execution_count": 4, 46 | "metadata": {}, 47 | "outputs": [ 48 | { 49 | "name": "stdout", 50 | "output_type": "stream", 51 | "text": [ 52 | "300\n" 53 | ] 54 | } 55 | ], 56 | "source": [ 57 | "print(300)" 58 | ] 59 | }, 60 | { 61 | "cell_type": "code", 62 | "execution_count": 3, 63 | "metadata": {}, 64 | "outputs": [ 65 | { 66 | "name": "stdout", 67 | "output_type": "stream", 68 | "text": [ 69 | "hii\n" 70 | ] 71 | } 72 | ], 73 | "source": [ 74 | "print(\"hii\")" 75 | ] 76 | }, 77 | { 78 | "cell_type": "code", 79 | "execution_count": 7, 80 | "metadata": {}, 81 | "outputs": [ 82 | { 83 | "name": "stdout", 84 | "output_type": "stream", 85 | "text": [ 86 | "0.4\n" 87 | ] 88 | } 89 | ], 90 | "source": [ 91 | "num1 = 200\n", 92 | "num2 = 500\n", 93 | "num3 = num1 / num2\n", 94 | "print(num3)" 95 | ] 96 | }, 97 | { 98 | "cell_type": "code", 99 | "execution_count": 14, 100 | "metadata": {}, 101 | "outputs": [ 102 | { 103 | "name": "stdout", 104 | "output_type": "stream", 105 | "text": [ 106 | "\n" 107 | ] 108 | } 109 | ], 110 | "source": [ 111 | "num = {1,2,3,5}\n", 112 | "print(type(num))" 113 | ] 114 | }, 115 | { 116 | "cell_type": "code", 117 | "execution_count": 16, 118 | "metadata": {}, 119 | "outputs": [ 120 | { 121 | "name": "stdout", 122 | "output_type": "stream", 123 | "text": [ 124 | "2022814945584\n", 125 | "2022887424752\n" 126 | ] 127 | } 128 | ], 129 | "source": [ 130 | "num = \"abc\"\n", 131 | "num1 = \"bca\"\n", 132 | "print(id(num))\n", 133 | "print(id(num1))" 134 | ] 135 | } 136 | ], 137 | "metadata": { 138 | "kernelspec": { 139 | "display_name": "Python 3", 140 | "language": "python", 141 | "name": "python3" 142 | }, 143 | "language_info": { 144 | "codemirror_mode": { 145 | "name": "ipython", 146 | "version": 3 147 | }, 148 | "file_extension": ".py", 149 | "mimetype": "text/x-python", 150 | "name": "python", 151 | "nbconvert_exporter": "python", 152 | "pygments_lexer": "ipython3", 153 | "version": "3.7.4" 154 | } 155 | }, 156 | "nbformat": 4, 157 | "nbformat_minor": 2 158 | } 159 | -------------------------------------------------------------------------------- /session 2 - Python Basics.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "hi\n" 13 | ] 14 | } 15 | ], 16 | "source": [ 17 | "print(\"hi\")" 18 | ] 19 | }, 20 | { 21 | "cell_type": "code", 22 | "execution_count": null, 23 | "metadata": {}, 24 | "outputs": [], 25 | "source": [ 26 | "execution :\n", 27 | " ctrl + enter\n", 28 | " shift + enter\n", 29 | " add line up : Esc + A\n", 30 | " add line bottom : Esc + B" 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": null, 36 | "metadata": {}, 37 | "outputs": [], 38 | "source": [ 39 | "Datatypes :\n", 40 | " 1. int 10 20 30\n", 41 | " 2. float 10.235 27.367\n", 42 | " 3. str 'hi' \"python\" \"\"\"welcome to python\"\"\" : It is the collection of Characters\n", 43 | " 4. list [10,20,30,40][10,20,30,\"python\",\"java\"[10,20,3,3,0]] : collection of elements , str or integers\n", 44 | " 5. tuple (10,20,30,40,\"python\",\"java\") : collection of elements and elements can be anything\n", 45 | " 6. dict {\"email\":\"gagangowdru8506@gmail.com\",\"contact\":\"1234567890\",\"age\":\"76\"} : collection of key and value\n", 46 | " 7. set {10,20,30,40} : collection of elements\n", 47 | " 8. bool True False\n", 48 | " None\n", 49 | " " 50 | ] 51 | }, 52 | { 53 | "cell_type": "code", 54 | "execution_count": null, 55 | "metadata": {}, 56 | "outputs": [], 57 | "source": [ 58 | "everything in python is object" 59 | ] 60 | }, 61 | { 62 | "cell_type": "code", 63 | "execution_count": 7, 64 | "metadata": {}, 65 | "outputs": [ 66 | { 67 | "name": "stdout", 68 | "output_type": "stream", 69 | "text": [ 70 | "300\n" 71 | ] 72 | } 73 | ], 74 | "source": [ 75 | "num1 = 100\n", 76 | "num2 = 200\n", 77 | "num3 = num1 + num2\n", 78 | "print(num3)" 79 | ] 80 | }, 81 | { 82 | "cell_type": "code", 83 | "execution_count": null, 84 | "metadata": {}, 85 | "outputs": [], 86 | "source": [ 87 | "PEP 8 => guidelines about how ur python code should look (python extension proposal)" 88 | ] 89 | }, 90 | { 91 | "cell_type": "code", 92 | "execution_count": null, 93 | "metadata": {}, 94 | "outputs": [], 95 | "source": [ 96 | "Python allows only one special character i.e, _(under_score)" 97 | ] 98 | }, 99 | { 100 | "cell_type": "code", 101 | "execution_count": null, 102 | "metadata": {}, 103 | "outputs": [], 104 | "source": [ 105 | "num_one = 100 (Variable)means which holds the data or value\n", 106 | "FILE_LOCATION = \"\" (Constant)means it is a value cant be change or altered by the program during execution" 107 | ] 108 | }, 109 | { 110 | "cell_type": "code", 111 | "execution_count": null, 112 | "metadata": {}, 113 | "outputs": [], 114 | "source": [ 115 | "FIRST_LOCATION = \"Gagan\"" 116 | ] 117 | }, 118 | { 119 | "cell_type": "code", 120 | "execution_count": null, 121 | "metadata": {}, 122 | "outputs": [], 123 | "source": [ 124 | "* Always give a meaningful variable names\n", 125 | "1. underscore first_name\n", 126 | "2. for variable name it should be with lower_case alphabet\n", 127 | "3. for constant all CAPS\n", 128 | "4. digits are allowed but not at the start , only special character allowed is _" 129 | ] 130 | }, 131 | { 132 | "cell_type": "code", 133 | "execution_count": 8, 134 | "metadata": {}, 135 | "outputs": [ 136 | { 137 | "name": "stdout", 138 | "output_type": "stream", 139 | "text": [ 140 | "\n" 141 | ] 142 | } 143 | ], 144 | "source": [ 145 | "name = \"ABC\"\n", 146 | "print(type(name))" 147 | ] 148 | }, 149 | { 150 | "cell_type": "code", 151 | "execution_count": 10, 152 | "metadata": {}, 153 | "outputs": [ 154 | { 155 | "name": "stdout", 156 | "output_type": "stream", 157 | "text": [ 158 | "10 20 30\n" 159 | ] 160 | } 161 | ], 162 | "source": [ 163 | "print(10,20,30)" 164 | ] 165 | }, 166 | { 167 | "cell_type": "code", 168 | "execution_count": 13, 169 | "metadata": {}, 170 | "outputs": [ 171 | { 172 | "name": "stdout", 173 | "output_type": "stream", 174 | "text": [ 175 | "\n" 176 | ] 177 | } 178 | ], 179 | "source": [ 180 | "d = {1:2 , 3:4}\n", 181 | "print(type(d))" 182 | ] 183 | }, 184 | { 185 | "cell_type": "code", 186 | "execution_count": null, 187 | "metadata": {}, 188 | "outputs": [], 189 | "source": [ 190 | "Imutable vs Mutable" 191 | ] 192 | }, 193 | { 194 | "cell_type": "code", 195 | "execution_count": null, 196 | "metadata": {}, 197 | "outputs": [], 198 | "source": [ 199 | "id() : memory location" 200 | ] 201 | }, 202 | { 203 | "cell_type": "code", 204 | "execution_count": 15, 205 | "metadata": {}, 206 | "outputs": [ 207 | { 208 | "name": "stdout", 209 | "output_type": "stream", 210 | "text": [ 211 | "100 140720611175920\n", 212 | "200 140720611179120\n" 213 | ] 214 | } 215 | ], 216 | "source": [ 217 | "num1 = 100\n", 218 | "print(num1,id(num1))\n", 219 | "\n", 220 | "num1 = 200\n", 221 | "print(num1,id(num1))" 222 | ] 223 | }, 224 | { 225 | "cell_type": "code", 226 | "execution_count": null, 227 | "metadata": {}, 228 | "outputs": [], 229 | "source": [ 230 | "Memory Loaction :\n", 231 | " 1. Immutable : at same memory lopcation I can not change the value\n", 232 | " 2. Mutable : I can change the value at same memory location" 233 | ] 234 | }, 235 | { 236 | "cell_type": "code", 237 | "execution_count": 17, 238 | "metadata": {}, 239 | "outputs": [ 240 | { 241 | "name": "stdout", 242 | "output_type": "stream", 243 | "text": [ 244 | "1650789152048\n", 245 | "1650796588336\n" 246 | ] 247 | } 248 | ], 249 | "source": [ 250 | "s = \"Python\"\n", 251 | "print(id(s))\n", 252 | "s = \"Java\"\n", 253 | "print(id(s))" 254 | ] 255 | }, 256 | { 257 | "cell_type": "code", 258 | "execution_count": 18, 259 | "metadata": {}, 260 | "outputs": [ 261 | { 262 | "name": "stdout", 263 | "output_type": "stream", 264 | "text": [ 265 | "1650832105160\n", 266 | "1650832105160\n" 267 | ] 268 | } 269 | ], 270 | "source": [ 271 | "l = [10,20,30,40]\n", 272 | "print(id(l))\n", 273 | "l.append(50)\n", 274 | "print(id(l))" 275 | ] 276 | }, 277 | { 278 | "cell_type": "code", 279 | "execution_count": null, 280 | "metadata": {}, 281 | "outputs": [], 282 | "source": [ 283 | "Mutable : Add , Update and Delete operations\n", 284 | "Immutable : Can not perform add , update and delete " 285 | ] 286 | }, 287 | { 288 | "cell_type": "code", 289 | "execution_count": null, 290 | "metadata": {}, 291 | "outputs": [], 292 | "source": [ 293 | "Immutable :\n", 294 | " int\n", 295 | " float\n", 296 | " str\n", 297 | " tuple\n", 298 | " \n", 299 | "Mutable : (Add,Update and Delete)\n", 300 | " list\n", 301 | " dict\n", 302 | " set" 303 | ] 304 | }, 305 | { 306 | "cell_type": "code", 307 | "execution_count": 22, 308 | "metadata": {}, 309 | "outputs": [ 310 | { 311 | "name": "stdout", 312 | "output_type": "stream", 313 | "text": [ 314 | " {40, 10, 20, 30}\n", 315 | "\n" 316 | ] 317 | } 318 | ], 319 | "source": [ 320 | "s = {10,20,20,30,30,40}\n", 321 | "print(type(s),s)\n", 322 | "s = {10,20,30,40}\n", 323 | "print(type(s))" 324 | ] 325 | }, 326 | { 327 | "cell_type": "code", 328 | "execution_count": null, 329 | "metadata": {}, 330 | "outputs": [], 331 | "source": [ 332 | "type() : to find data types\n", 333 | "id() : to find memory location" 334 | ] 335 | }, 336 | { 337 | "cell_type": "code", 338 | "execution_count": 24, 339 | "metadata": {}, 340 | "outputs": [ 341 | { 342 | "name": "stdout", 343 | "output_type": "stream", 344 | "text": [ 345 | "140720611175920 140720611175920\n", 346 | "1650832466384 1650832466800\n" 347 | ] 348 | } 349 | ], 350 | "source": [ 351 | "num1 = 100\n", 352 | "num2 = 100\n", 353 | "print(id(num1),id(num2))\n", 354 | "num1 = 1000\n", 355 | "num2 = 1000\n", 356 | "print(id(num1),id(num2))" 357 | ] 358 | }, 359 | { 360 | "cell_type": "code", 361 | "execution_count": null, 362 | "metadata": {}, 363 | "outputs": [], 364 | "source": [ 365 | "Python allows in default the numbers only in the range of 0 - 256\n", 366 | "0 - 256 => object intering " 367 | ] 368 | }, 369 | { 370 | "cell_type": "code", 371 | "execution_count": 26, 372 | "metadata": {}, 373 | "outputs": [ 374 | { 375 | "name": "stdout", 376 | "output_type": "stream", 377 | "text": [ 378 | "PythonJava\n" 379 | ] 380 | }, 381 | { 382 | "ename": "TypeError", 383 | "evalue": "unsupported operand type(s) for -: 'str' and 'str'", 384 | "output_type": "error", 385 | "traceback": [ 386 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 387 | "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", 388 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[0ms1\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m\"Python\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[0ms2\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m\"java\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 6\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ms1\u001b[0m \u001b[1;33m-\u001b[0m \u001b[0ms2\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 389 | "\u001b[1;31mTypeError\u001b[0m: unsupported operand type(s) for -: 'str' and 'str'" 390 | ] 391 | } 392 | ], 393 | "source": [ 394 | "s1 = \"Python\"\n", 395 | "s2 = \"Java\"\n", 396 | "print(s1 + s2)\n", 397 | "s1 = \"Python\"\n", 398 | "s2 = \"java\"\n", 399 | "print(s1 - s2)" 400 | ] 401 | }, 402 | { 403 | "cell_type": "code", 404 | "execution_count": null, 405 | "metadata": {}, 406 | "outputs": [], 407 | "source": [ 408 | "Operators :\n", 409 | " 1. Arthmetic Operators :\n", 410 | " + => Addition\n", 411 | " - => Substraction\n", 412 | " * => Multiplication\n", 413 | " / => true division (because it gives true value with decimals)\n", 414 | " // => floor division (it gives values without decimals)\n", 415 | " % => Modules (gives remainder)\n", 416 | " ** => powers\n", 417 | " 2. Comparision Operators : It always gives us the answer is True or False\n", 418 | " == , < , > , <= , >= , !=\n", 419 | " 3. Membership Operators : in not in (This is only used in Iterable and cant be used in not Iterable) \n", 420 | " incase of dictionary in/not in will only check with keys , will not check with the values\n", 421 | " 4.Identity Operator : is is not\n", 422 | " 5.Assignment Operator : = , += , -= , *= , /=\n", 423 | " 6.Logical Operator : and or not\n", 424 | " " 425 | ] 426 | }, 427 | { 428 | "cell_type": "code", 429 | "execution_count": 29, 430 | "metadata": {}, 431 | "outputs": [ 432 | { 433 | "name": "stdout", 434 | "output_type": "stream", 435 | "text": [ 436 | "3.3333333333333335\n", 437 | "3\n", 438 | "1\n", 439 | "30\n", 440 | "13\n", 441 | "7\n", 442 | "1000\n" 443 | ] 444 | } 445 | ], 446 | "source": [ 447 | "print(10 / 3)\n", 448 | "print(10 // 3)\n", 449 | "print(10 % 3)\n", 450 | "print(10 * 3)\n", 451 | "print(10 + 3)\n", 452 | "print(10 - 3)\n", 453 | "print(10 ** 3)" 454 | ] 455 | }, 456 | { 457 | "cell_type": "code", 458 | "execution_count": 32, 459 | "metadata": {}, 460 | "outputs": [ 461 | { 462 | "name": "stdout", 463 | "output_type": "stream", 464 | "text": [ 465 | "False\n", 466 | "True\n", 467 | "False\n", 468 | "True\n", 469 | "False\n" 470 | ] 471 | } 472 | ], 473 | "source": [ 474 | "s1 = \"Python\"\n", 475 | "s2 = \"python\"\n", 476 | "print(s1 == s2)\n", 477 | "s1 = 10\n", 478 | "s2 = 5\n", 479 | "print(s1 > s2)\n", 480 | "print(s1 < s2)\n", 481 | "print(s1 >= s2)\n", 482 | "print(s1 <= s2)" 483 | ] 484 | }, 485 | { 486 | "cell_type": "code", 487 | "execution_count": 37, 488 | "metadata": {}, 489 | "outputs": [ 490 | { 491 | "name": "stdout", 492 | "output_type": "stream", 493 | "text": [ 494 | "False\n", 495 | "True\n", 496 | "False\n" 497 | ] 498 | }, 499 | { 500 | "ename": "TypeError", 501 | "evalue": "argument of type 'int' is not iterable", 502 | "output_type": "error", 503 | "traceback": [ 504 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 505 | "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", 506 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[0mb\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m\"Java\"\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\"a\"\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mb\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 7\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m5\u001b[0m \u001b[1;32min\u001b[0m \u001b[1;36m500\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 507 | "\u001b[1;31mTypeError\u001b[0m: argument of type 'int' is not iterable" 508 | ] 509 | } 510 | ], 511 | "source": [ 512 | "l = [100,200,300,400,500]\n", 513 | "print(4000 in l)\n", 514 | "a = \"python\"\n", 515 | "print(\"n\" in a)\n", 516 | "b = \"Java\"\n", 517 | "print(\"a\" not in b)\n", 518 | "print(5 in 500)" 519 | ] 520 | }, 521 | { 522 | "cell_type": "code", 523 | "execution_count": null, 524 | "metadata": {}, 525 | "outputs": [], 526 | "source": [ 527 | "Not Iterable :\n", 528 | " int , float\n", 529 | "Iterable : Means collection values\n", 530 | " str , tuple , list , dict , set" 531 | ] 532 | }, 533 | { 534 | "cell_type": "code", 535 | "execution_count": 41, 536 | "metadata": {}, 537 | "outputs": [ 538 | { 539 | "name": "stdout", 540 | "output_type": "stream", 541 | "text": [ 542 | "True\n", 543 | "False\n" 544 | ] 545 | } 546 | ], 547 | "source": [ 548 | "d = {1:100 , 2:200 , 3:300}\n", 549 | "print(3 in d)\n", 550 | "print(300 in d)" 551 | ] 552 | }, 553 | { 554 | "cell_type": "code", 555 | "execution_count": 43, 556 | "metadata": {}, 557 | "outputs": [ 558 | { 559 | "name": "stdout", 560 | "output_type": "stream", 561 | "text": [ 562 | "True\n", 563 | "True\n" 564 | ] 565 | } 566 | ], 567 | "source": [ 568 | "num1 = 100\n", 569 | "num2 = 100\n", 570 | "print(num1 == num2)\n", 571 | "print(num1 is num2)" 572 | ] 573 | }, 574 | { 575 | "cell_type": "code", 576 | "execution_count": 48, 577 | "metadata": {}, 578 | "outputs": [ 579 | { 580 | "name": "stdout", 581 | "output_type": "stream", 582 | "text": [ 583 | "True\n", 584 | "False\n" 585 | ] 586 | } 587 | ], 588 | "source": [ 589 | "l1 = [10,20,30]\n", 590 | "l2 = [10,20,30]\n", 591 | "\n", 592 | "print(l1 == l2)\n", 593 | "print(l1 is l2)" 594 | ] 595 | }, 596 | { 597 | "cell_type": "code", 598 | "execution_count": null, 599 | "metadata": {}, 600 | "outputs": [], 601 | "source": [ 602 | "== => values\n", 603 | "is => memory allocation" 604 | ] 605 | }, 606 | { 607 | "cell_type": "code", 608 | "execution_count": 49, 609 | "metadata": {}, 610 | "outputs": [ 611 | { 612 | "name": "stdout", 613 | "output_type": "stream", 614 | "text": [ 615 | "100\n", 616 | "110\n", 617 | "105\n" 618 | ] 619 | } 620 | ], 621 | "source": [ 622 | "num1 = 100\n", 623 | "print(num1)\n", 624 | "\n", 625 | "num1 += 10\n", 626 | "print(num1)\n", 627 | "\n", 628 | "num1 -= 5\n", 629 | "print(num1)" 630 | ] 631 | }, 632 | { 633 | "cell_type": "code", 634 | "execution_count": null, 635 | "metadata": {}, 636 | "outputs": [], 637 | "source": [ 638 | "Logical Operator :" 639 | ] 640 | }, 641 | { 642 | "cell_type": "code", 643 | "execution_count": 50, 644 | "metadata": {}, 645 | "outputs": [ 646 | { 647 | "name": "stdout", 648 | "output_type": "stream", 649 | "text": [ 650 | "False\n" 651 | ] 652 | } 653 | ], 654 | "source": [ 655 | "print(100 == 100 and 100 != 100)" 656 | ] 657 | }, 658 | { 659 | "cell_type": "code", 660 | "execution_count": 51, 661 | "metadata": {}, 662 | "outputs": [ 663 | { 664 | "name": "stdout", 665 | "output_type": "stream", 666 | "text": [ 667 | "False\n" 668 | ] 669 | } 670 | ], 671 | "source": [ 672 | "print(100 != 100 and 100 == 100)" 673 | ] 674 | }, 675 | { 676 | "cell_type": "code", 677 | "execution_count": 52, 678 | "metadata": {}, 679 | "outputs": [ 680 | { 681 | "name": "stdout", 682 | "output_type": "stream", 683 | "text": [ 684 | "True\n" 685 | ] 686 | } 687 | ], 688 | "source": [ 689 | "print(100 == 100 or 100 != 100)" 690 | ] 691 | }, 692 | { 693 | "cell_type": "code", 694 | "execution_count": null, 695 | "metadata": {}, 696 | "outputs": [], 697 | "source": [ 698 | "True/False :\n", 699 | " Comparsion\n", 700 | " Membership\n", 701 | " Logical\n", 702 | " Identity" 703 | ] 704 | }, 705 | { 706 | "cell_type": "code", 707 | "execution_count": null, 708 | "metadata": {}, 709 | "outputs": [], 710 | "source": [ 711 | "Overview" 712 | ] 713 | }, 714 | { 715 | "cell_type": "code", 716 | "execution_count": null, 717 | "metadata": {}, 718 | "outputs": [], 719 | "source": [ 720 | "type()\n", 721 | "id()" 722 | ] 723 | }, 724 | { 725 | "cell_type": "code", 726 | "execution_count": 53, 727 | "metadata": {}, 728 | "outputs": [ 729 | { 730 | "name": "stdout", 731 | "output_type": "stream", 732 | "text": [ 733 | "140720611173520\n", 734 | "140720611173520\n" 735 | ] 736 | } 737 | ], 738 | "source": [ 739 | "num1 = 25\n", 740 | "num2 = 25\n", 741 | "print(id(num1))\n", 742 | "print(id(num2))\n" 743 | ] 744 | }, 745 | { 746 | "cell_type": "code", 747 | "execution_count": 54, 748 | "metadata": {}, 749 | "outputs": [ 750 | { 751 | "name": "stdout", 752 | "output_type": "stream", 753 | "text": [ 754 | "1650832206856\n", 755 | "1650829794376\n" 756 | ] 757 | } 758 | ], 759 | "source": [ 760 | "l1 = [10,20,30]\n", 761 | "l2 = [10,20,30]\n", 762 | "print(id(l1))\n", 763 | "print(id(l2))" 764 | ] 765 | }, 766 | { 767 | "cell_type": "code", 768 | "execution_count": null, 769 | "metadata": {}, 770 | "outputs": [], 771 | "source": [ 772 | "immutable : cant change the value with different memory location\n", 773 | " int\n", 774 | " float\n", 775 | " tuple\n", 776 | "mutable :\n", 777 | " list dict set" 778 | ] 779 | }, 780 | { 781 | "cell_type": "code", 782 | "execution_count": null, 783 | "metadata": {}, 784 | "outputs": [], 785 | "source": [ 786 | "Iterable :\n", 787 | " str list dict set tuple\n", 788 | "not iterable :\n", 789 | " int float" 790 | ] 791 | }, 792 | { 793 | "cell_type": "code", 794 | "execution_count": null, 795 | "metadata": {}, 796 | "outputs": [], 797 | "source": [ 798 | "in operator not work with int , float and works with dict only in terms of keys not values" 799 | ] 800 | }, 801 | { 802 | "cell_type": "code", 803 | "execution_count": null, 804 | "metadata": {}, 805 | "outputs": [], 806 | "source": [ 807 | "== checks the values\n", 808 | "is checks the memory location" 809 | ] 810 | } 811 | ], 812 | "metadata": { 813 | "kernelspec": { 814 | "display_name": "Python 3", 815 | "language": "python", 816 | "name": "python3" 817 | }, 818 | "language_info": { 819 | "codemirror_mode": { 820 | "name": "ipython", 821 | "version": 3 822 | }, 823 | "file_extension": ".py", 824 | "mimetype": "text/x-python", 825 | "name": "python", 826 | "nbconvert_exporter": "python", 827 | "pygments_lexer": "ipython3", 828 | "version": "3.7.4" 829 | } 830 | }, 831 | "nbformat": 4, 832 | "nbformat_minor": 2 833 | } 834 | --------------------------------------------------------------------------------