├── 1. Intro to Python ├── 1.jpeg └── Intro.ipynb ├── 10. Threads └── multi_threading.ipynb ├── 11. Exception Handling └── exception_handling.ipynb ├── 12. File Handling ├── file_handling.ipynb └── test.txt ├── 13. Regular Expressions └── reg_exp.ipynb ├── 14. Databases └── database.ipynb ├── 15. GUI-1 ├── Login.py ├── creds.txt ├── demo_1.py ├── demo_2.py ├── demo_3.py ├── demo_4.py ├── demo_5.py ├── demo_6.py ├── demo_7.py ├── demo_8.py └── login_page.py ├── 16. GUI-2 ├── backend.py ├── demo_1.py ├── demo_2.py ├── demo_3.py ├── demo_4.py ├── demo_5.py ├── demo_6.py ├── demo_7.py ├── demo_8.py ├── demo_9.py ├── frontend.py └── search.py ├── 17. Numpy └── numpy.ipynb ├── 18. Pandas ├── 1.jpg ├── Iris.csv └── Pandas.ipynb ├── 2. Data Types - 1 └── data_type_1.ipynb ├── 3. Data Types - 2 ├── 1.jpeg └── data_type_2.ipynb ├── 4. Decision Statements ├── 1.jpg ├── 2.jpg ├── 3.jpg └── decision_control_statement.ipynb ├── 5. Loops ├── 1.jpg ├── 2.jpg └── loops.ipynb ├── 6. Functions └── functions.ipynb ├── 7. Modules └── modules.ipynb ├── 8. OOPs - 1 └── OOPs_1.ipynb ├── 9. OOPs - 2 └── OOPs_2.ipynb ├── Assignments ├── 1 - Intro to Python.docx ├── 10 - Threads and Processes.docx ├── 11 - Web Basics and API.docx ├── 12 - Exception Handling.docx ├── 13 - File Handling.docx ├── 14 - Regular Expressions.docx ├── 15 - Databses in Python.docx ├── 16 - GUI - I.docx ├── 17 - GUI - II.docx ├── 18 - Pandas.docx ├── 2 - Data Types I.docx ├── 3 - Data Types II.docx ├── 4 - Decision Control System.docx ├── 5 - Loops.docx ├── 6 - Functions.docx ├── 7 - Common Modules in Python.docx ├── 8 - Classes and Modules I.docx └── 9 - Classes and Modules II.docx ├── Helicopter ├── Helicopter.png └── helicopter_game.py ├── LICENSE ├── Library Management System ├── backend.py └── frontend.py ├── README.md ├── RaceCar ├── Helicopter.png ├── racecar.png └── racecar.py └── car-game-master ├── README.md ├── __pycache__ └── pygame_car.cpython-36.pyc ├── images ├── car.png ├── car1.png ├── carcrash.png └── trees.jpg ├── music ├── atlanta.wav ├── coffee_stains.wav └── crash.wav ├── pygame_car.py └── setup.py /1. Intro to Python/1.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Singh-K01/Python-tutorials-Kanav-Bansal/05065cb71ab0a5b9ff17b5befe485294c8395d47/1. Intro to Python/1.jpeg -------------------------------------------------------------------------------- /1. Intro to Python/Intro.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Python Programming Introduction" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": { 13 | "ExecuteTime": { 14 | "end_time": "2018-05-21T17:50:13.054524Z", 15 | "start_time": "2018-05-21T17:50:13.038897Z" 16 | } 17 | }, 18 | "source": [ 19 | "" 20 | ] 21 | }, 22 | { 23 | "cell_type": "markdown", 24 | "metadata": {}, 25 | "source": [ 26 | "Father of Python - GUIDO VAN ROSSUM" 27 | ] 28 | }, 29 | { 30 | "cell_type": "markdown", 31 | "metadata": {}, 32 | "source": [ 33 | "# Where can we use Python?\n", 34 | "1. Desktop Applications (Calculator, Notepad, etc..)\n", 35 | "2. Web Applications (Youtube, Dropbox, Google, Instagram, Quora, Spotify)\n", 36 | "3. Scientific and Numeric Computing (Scipy, Numpy)\n", 37 | "4. Database Applications (Library Management Systems, Pharmaceutical)\n", 38 | "5. Network Applications (Network Sniffer, Network Parameter extraction, Routing Algo's like OSPF)\n", 39 | "6. Developing Games (Battlefield, Sims 4)\n", 40 | "7. Data Science (Pandas, Matplotlib, Seaborn, etc)\n", 41 | "8. Machine Learning (scikit)\n", 42 | "9. AI (AIMA - AI: A Modern Approach)\n", 43 | "10. IOT (Raspberry pi, Phillips(works great with low level code in c++))\n" 44 | ] 45 | }, 46 | { 47 | "cell_type": "markdown", 48 | "metadata": {}, 49 | "source": [ 50 | "# Features of Python:\n", 51 | "1. Simple and easy to learn\n", 52 | "2. Free and open source.\n", 53 | "3. General Purpose and high level programming language.\n", 54 | "4. Platform independent.\n", 55 | "5. Case Sensitive.\n", 56 | "6. Interpreted Language.\n", 57 | "7. Dynamically typed.\n", 58 | "8. Rich Library.\n", 59 | "9. We can write concise code using python." 60 | ] 61 | }, 62 | { 63 | "cell_type": "markdown", 64 | "metadata": {}, 65 | "source": [ 66 | "# Input & Output" 67 | ] 68 | }, 69 | { 70 | "cell_type": "code", 71 | "execution_count": 23, 72 | "metadata": { 73 | "ExecuteTime": { 74 | "end_time": "2018-05-22T03:28:54.379907Z", 75 | "start_time": "2018-05-22T03:28:54.362392Z" 76 | } 77 | }, 78 | "outputs": [ 79 | { 80 | "data": { 81 | "text/plain": [ 82 | "\"\\nnum = input()\\nprint(num)\\n\\nnum = input()\\nprint('%d' % (num))\\n\"" 83 | ] 84 | }, 85 | "execution_count": 23, 86 | "metadata": {}, 87 | "output_type": "execute_result" 88 | } 89 | ], 90 | "source": [ 91 | "\"\"\"\n", 92 | "num = input()\n", 93 | "print(num)\n", 94 | "\n", 95 | "\n", 96 | "num = input()\n", 97 | "print('%d' % (num))\n", 98 | "\"\"\"\n", 99 | "\n", 100 | "# be aware of the error that you would encounter in above code\n", 101 | "# try using %s instead of %d" 102 | ] 103 | }, 104 | { 105 | "cell_type": "code", 106 | "execution_count": 6, 107 | "metadata": { 108 | "ExecuteTime": { 109 | "end_time": "2018-06-05T11:41:37.729710Z", 110 | "start_time": "2018-06-05T11:41:37.712664Z" 111 | } 112 | }, 113 | "outputs": [ 114 | { 115 | "name": "stdout", 116 | "output_type": "stream", 117 | "text": [ 118 | "['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']\n" 119 | ] 120 | }, 121 | { 122 | "data": { 123 | "text/plain": [ 124 | "33" 125 | ] 126 | }, 127 | "execution_count": 6, 128 | "metadata": {}, 129 | "output_type": "execute_result" 130 | } 131 | ], 132 | "source": [ 133 | "import keyword\n", 134 | "print(keyword.kwlist)\n", 135 | "len(keyword.kwlist)" 136 | ] 137 | }, 138 | { 139 | "cell_type": "code", 140 | "execution_count": 25, 141 | "metadata": { 142 | "ExecuteTime": { 143 | "end_time": "2018-05-22T03:29:06.004059Z", 144 | "start_time": "2018-05-22T03:29:05.984740Z" 145 | } 146 | }, 147 | "outputs": [ 148 | { 149 | "data": { 150 | "text/plain": [ 151 | "\"\\nnum2 = input('Please enter a number: ')\\nprint('User has entered', num2)\\n\\nnum2 = input('Please enter a number: ')\\nprint('User has entered %d as input' % (num2))\\n\"" 152 | ] 153 | }, 154 | "execution_count": 25, 155 | "metadata": {}, 156 | "output_type": "execute_result" 157 | } 158 | ], 159 | "source": [ 160 | "'''\n", 161 | "num2 = input('Please enter a number: ')\n", 162 | "print('User has entered', num2)\n", 163 | "\n", 164 | "num2 = input('Please enter a number: ')\n", 165 | "print('User has entered %d as input' % (num2))\n", 166 | "'''\n", 167 | "\n", 168 | "# be aware of the error that you would encounter in above code\n", 169 | "# try using %s instead of %d\n", 170 | "# another way is to typecaste(coded below)" 171 | ] 172 | }, 173 | { 174 | "cell_type": "code", 175 | "execution_count": 12, 176 | "metadata": { 177 | "ExecuteTime": { 178 | "end_time": "2018-06-05T11:50:38.084330Z", 179 | "start_time": "2018-06-05T11:50:34.035809Z" 180 | } 181 | }, 182 | "outputs": [ 183 | { 184 | "name": "stdout", 185 | "output_type": "stream", 186 | "text": [ 187 | "Please enter a number: 545\n", 188 | "User has entered 545 as input\n" 189 | ] 190 | } 191 | ], 192 | "source": [ 193 | "num2 = int(input('Please enter a number: '))\n", 194 | "print('User has entered %d as input' % (num2))" 195 | ] 196 | }, 197 | { 198 | "cell_type": "code", 199 | "execution_count": 26, 200 | "metadata": { 201 | "ExecuteTime": { 202 | "end_time": "2018-05-22T03:32:05.264900Z", 203 | "start_time": "2018-05-22T03:32:05.259387Z" 204 | } 205 | }, 206 | "outputs": [], 207 | "source": [ 208 | "#This entire statement won't be displayed\n", 209 | "#print('5'*6)\n", 210 | "#print(5,6,7)\n", 211 | "#print('Python', 1, 2)\n", 212 | "#print(1,2,3, sep=',')\n", 213 | "#print(1,2,3, sep=',', end='!!\\n')" 214 | ] 215 | }, 216 | { 217 | "cell_type": "code", 218 | "execution_count": 25, 219 | "metadata": { 220 | "ExecuteTime": { 221 | "end_time": "2018-06-05T12:01:16.512330Z", 222 | "start_time": "2018-06-05T12:01:16.506312Z" 223 | } 224 | }, 225 | "outputs": [ 226 | { 227 | "name": "stdout", 228 | "output_type": "stream", 229 | "text": [ 230 | "555555\n", 231 | "5 6 7\n", 232 | "Python 1 2\n", 233 | "1,2,3\n", 234 | "1,2,3!!\n" 235 | ] 236 | } 237 | ], 238 | "source": [ 239 | "print('5'*6)\n", 240 | "print(5,6,7)\n", 241 | "print('Python', 1, 2)\n", 242 | "print(1,2,3, sep=',')\n", 243 | "print(1,2,3, sep=',', end='!!\\n')" 244 | ] 245 | }, 246 | { 247 | "cell_type": "code", 248 | "execution_count": null, 249 | "metadata": {}, 250 | "outputs": [], 251 | "source": [] 252 | } 253 | ], 254 | "metadata": { 255 | "kernelspec": { 256 | "display_name": "Python 3", 257 | "language": "python", 258 | "name": "python3" 259 | }, 260 | "language_info": { 261 | "codemirror_mode": { 262 | "name": "ipython", 263 | "version": 3 264 | }, 265 | "file_extension": ".py", 266 | "mimetype": "text/x-python", 267 | "name": "python", 268 | "nbconvert_exporter": "python", 269 | "pygments_lexer": "ipython3", 270 | "version": "3.6.5" 271 | }, 272 | "toc": { 273 | "nav_menu": {}, 274 | "number_sections": true, 275 | "sideBar": true, 276 | "skip_h1_title": false, 277 | "title_cell": "Table of Contents", 278 | "title_sidebar": "Contents", 279 | "toc_cell": false, 280 | "toc_position": {}, 281 | "toc_section_display": true, 282 | "toc_window_display": false 283 | }, 284 | "varInspector": { 285 | "cols": { 286 | "lenName": 16, 287 | "lenType": 16, 288 | "lenVar": 40 289 | }, 290 | "kernels_config": { 291 | "python": { 292 | "delete_cmd_postfix": "", 293 | "delete_cmd_prefix": "del ", 294 | "library": "var_list.py", 295 | "varRefreshCmd": "print(var_dic_list())" 296 | }, 297 | "r": { 298 | "delete_cmd_postfix": ") ", 299 | "delete_cmd_prefix": "rm(", 300 | "library": "var_list.r", 301 | "varRefreshCmd": "cat(var_dic_list()) " 302 | } 303 | }, 304 | "types_to_exclude": [ 305 | "module", 306 | "function", 307 | "builtin_function_or_method", 308 | "instance", 309 | "_Feature" 310 | ], 311 | "window_display": false 312 | } 313 | }, 314 | "nbformat": 4, 315 | "nbformat_minor": 2 316 | } 317 | -------------------------------------------------------------------------------- /10. Threads/multi_threading.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Threading" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 1, 13 | "metadata": { 14 | "ExecuteTime": { 15 | "end_time": "2018-06-04T04:45:09.916660Z", 16 | "start_time": "2018-06-04T04:45:09.911647Z" 17 | }, 18 | "scrolled": true 19 | }, 20 | "outputs": [ 21 | { 22 | "name": "stdout", 23 | "output_type": "stream", 24 | "text": [ 25 | "Current executing Thread: <_MainThread(MainThread, started 10348)>\n", 26 | "Name of the Thread MainThread\n", 27 | "Identification Number: 10348\n" 28 | ] 29 | } 30 | ], 31 | "source": [ 32 | "import threading\n", 33 | "\n", 34 | "print('Current executing Thread: ', threading.current_thread())\n", 35 | "\n", 36 | "print('Name of the Thread ', threading.current_thread().getName())\n", 37 | "\n", 38 | "print('Identification Number: ', threading.current_thread().ident)\n", 39 | "\n", 40 | "# current_thread() returns current thread object\n", 41 | "# We can see the name of current thread object using getName() method" 42 | ] 43 | }, 44 | { 45 | "cell_type": "code", 46 | "execution_count": 2, 47 | "metadata": { 48 | "ExecuteTime": { 49 | "end_time": "2018-06-04T04:48:25.889553Z", 50 | "start_time": "2018-06-04T04:48:25.884537Z" 51 | } 52 | }, 53 | "outputs": [ 54 | { 55 | "name": "stdout", 56 | "output_type": "stream", 57 | "text": [ 58 | "Current executing Thread: MainThread\n", 59 | "Current executing Thread: my_thread\n" 60 | ] 61 | } 62 | ], 63 | "source": [ 64 | "from threading import *\n", 65 | "\n", 66 | "print('Current executing Thread: ', current_thread().getName())\n", 67 | "\n", 68 | "current_thread().setName('my_thread')\n", 69 | "\n", 70 | "print('Current executing Thread: ', current_thread().getName())\n" 71 | ] 72 | }, 73 | { 74 | "cell_type": "markdown", 75 | "metadata": {}, 76 | "source": [ 77 | "## Creating the Thread (Functional Programming way)" 78 | ] 79 | }, 80 | { 81 | "cell_type": "code", 82 | "execution_count": 3, 83 | "metadata": { 84 | "ExecuteTime": { 85 | "end_time": "2018-06-04T04:54:56.229836Z", 86 | "start_time": "2018-06-04T04:54:56.213773Z" 87 | } 88 | }, 89 | "outputs": [ 90 | { 91 | "name": "stdout", 92 | "output_type": "stream", 93 | "text": [ 94 | "Child executing thread: Main executing thread: Thread-6\n", 95 | " my_thread\n" 96 | ] 97 | } 98 | ], 99 | "source": [ 100 | "from threading import *\n", 101 | "\n", 102 | "def display():\n", 103 | " print('Child executing thread: ', current_thread().getName())\n", 104 | " \n", 105 | "# Lets execute display() as seperate thread\n", 106 | "\n", 107 | "t = Thread(target = display)\n", 108 | "# MainThread creates child thread object\n", 109 | "\n", 110 | "t.start()\n", 111 | "\n", 112 | "print('Main executing thread: ', current_thread().getName())" 113 | ] 114 | }, 115 | { 116 | "cell_type": "code", 117 | "execution_count": 4, 118 | "metadata": { 119 | "ExecuteTime": { 120 | "end_time": "2018-06-04T05:00:18.304785Z", 121 | "start_time": "2018-06-04T05:00:18.293757Z" 122 | }, 123 | "scrolled": false 124 | }, 125 | "outputs": [ 126 | { 127 | "name": "stdout", 128 | "output_type": "stream", 129 | "text": [ 130 | "Child ThreadMain Thread\n", 131 | "Main Thread\n", 132 | "Main Thread\n", 133 | "Main Thread\n", 134 | "Main Thread\n", 135 | "Main Thread\n", 136 | "Main Thread\n", 137 | "Main Thread\n", 138 | "Main Thread\n", 139 | "Main Thread\n", 140 | "\n", 141 | "Child Thread\n", 142 | "Child Thread\n", 143 | "Child Thread\n", 144 | "Child Thread\n", 145 | "Child Thread\n", 146 | "Child Thread\n", 147 | "Child Thread\n", 148 | "Child Thread\n", 149 | "Child Thread\n" 150 | ] 151 | } 152 | ], 153 | "source": [ 154 | "from threading import *\n", 155 | "\n", 156 | "def display():\n", 157 | " for i in range(10):\n", 158 | " print('Child Thread')\n", 159 | " \n", 160 | "t = Thread(target = display)\n", 161 | "\n", 162 | "t.start()\n", 163 | "\n", 164 | "for i in range(10):\n", 165 | " print('Main Thread')" 166 | ] 167 | }, 168 | { 169 | "cell_type": "markdown", 170 | "metadata": {}, 171 | "source": [ 172 | "## Creating the Thread (OOP way)" 173 | ] 174 | }, 175 | { 176 | "cell_type": "code", 177 | "execution_count": 5, 178 | "metadata": { 179 | "ExecuteTime": { 180 | "end_time": "2018-06-04T05:10:08.516969Z", 181 | "start_time": "2018-06-04T05:10:08.501930Z" 182 | } 183 | }, 184 | "outputs": [ 185 | { 186 | "name": "stdout", 187 | "output_type": "stream", 188 | "text": [ 189 | "Child executing thread: Main executing thread: my_thread\n", 190 | " Thread-8\n" 191 | ] 192 | } 193 | ], 194 | "source": [ 195 | "from threading import *\n", 196 | "\n", 197 | "class MyThread(Thread):\n", 198 | " def run(self):\n", 199 | " print('Child executing thread: ', current_thread().getName())\n", 200 | " \n", 201 | "# Lets execute display() as seperate thread\n", 202 | "\n", 203 | "t = MyThread()\n", 204 | "# MainThread creates child thread object\n", 205 | "\n", 206 | "t.start()\n", 207 | "\n", 208 | "print('Main executing thread: ', current_thread().getName())" 209 | ] 210 | }, 211 | { 212 | "cell_type": "code", 213 | "execution_count": 25, 214 | "metadata": { 215 | "ExecuteTime": { 216 | "end_time": "2018-06-02T12:20:47.908265Z", 217 | "start_time": "2018-06-02T12:20:47.898226Z" 218 | }, 219 | "scrolled": false 220 | }, 221 | "outputs": [ 222 | { 223 | "name": "stdout", 224 | "output_type": "stream", 225 | "text": [ 226 | "Child Thread\n", 227 | "Child Thread\n", 228 | "Child Thread\n", 229 | "Child Thread\n", 230 | "Child Thread\n", 231 | "Child Thread\n", 232 | "Child Thread\n", 233 | "Child ThreadMain Thread\n", 234 | "Main Thread\n", 235 | "Main Thread\n", 236 | "Main Thread\n", 237 | "Main Thread\n", 238 | "Main Thread\n", 239 | "Main Thread\n", 240 | "Main Thread\n", 241 | "Main Thread\n", 242 | "Main Thread\n", 243 | "\n", 244 | "Child Thread\n", 245 | "Child Thread\n" 246 | ] 247 | } 248 | ], 249 | "source": [ 250 | "from threading import *\n", 251 | "\n", 252 | "class MyThread(Thread):\n", 253 | " def run(self):\n", 254 | " for i in range(10):\n", 255 | " print('Child Thread')\n", 256 | " \n", 257 | "t = MyThread()\n", 258 | "\n", 259 | "t.start()\n", 260 | "\n", 261 | "for i in range(10):\n", 262 | " print('Main Thread')" 263 | ] 264 | }, 265 | { 266 | "cell_type": "markdown", 267 | "metadata": {}, 268 | "source": [ 269 | "## threading Module" 270 | ] 271 | }, 272 | { 273 | "cell_type": "code", 274 | "execution_count": 8, 275 | "metadata": { 276 | "ExecuteTime": { 277 | "end_time": "2018-06-04T02:57:54.006731Z", 278 | "start_time": "2018-06-04T02:57:48.967065Z" 279 | }, 280 | "scrolled": true 281 | }, 282 | "outputs": [ 283 | { 284 | "name": "stdout", 285 | "output_type": "stream", 286 | "text": [ 287 | "Active Threads: 5\n", 288 | "Starting Starting 1st Thread\n", 289 | "2nd Thread\n", 290 | "StartingActive Threads: 3rd Thread\n", 291 | " 8\n", 292 | "EndingEndingEnding 2nd Thread\n", 293 | " 3rd Thread\n", 294 | " 1st Thread\n", 295 | "Active Threads: 5\n" 296 | ] 297 | } 298 | ], 299 | "source": [ 300 | "# current_thread()\n", 301 | "\n", 302 | "# getName()\n", 303 | "\n", 304 | "# setName()\n", 305 | "\n", 306 | "# start()\n", 307 | "\n", 308 | "# run()\n", 309 | "\n", 310 | "# activeCount()\n", 311 | "\n", 312 | "# enumerate() -> returns the list of all active threads currently running\n", 313 | "\n", 314 | "# isAlive() -> used to check whether a thread is still executing\n", 315 | "\n", 316 | "# join()\n", 317 | "\n", 318 | "from threading import *\n", 319 | "import time\n", 320 | "\n", 321 | "def display():\n", 322 | " print('Starting', current_thread().getName())\n", 323 | " time.sleep(3)\n", 324 | " print('Ending', current_thread().getName())\n", 325 | "\n", 326 | "print('Active Threads: ', active_count())\n", 327 | "\n", 328 | "t1 = Thread(target = display, name = '1st Thread')\n", 329 | "\n", 330 | "t2 = Thread(target = display, name = '2nd Thread')\n", 331 | "\n", 332 | "t3 = Thread(target = display, name = '3rd Thread')\n", 333 | "\n", 334 | "t1.start()\n", 335 | "\n", 336 | "t2.start()\n", 337 | "\n", 338 | "t3.start()\n", 339 | "\n", 340 | "print(t1.name, 'is', t1.isAlive())\n", 341 | "\n", 342 | "enum = enumerate()\n", 343 | "\n", 344 | "for t in enum:\n", 345 | " print('Thread Name from enum', t.name)\n", 346 | "\n", 347 | "print('Active Threads: ', active_count())\n", 348 | "\n", 349 | "time.sleep(5)\n", 350 | "\n", 351 | "print('Active Threads: ', active_count())\n", 352 | "\n", 353 | "print(t1.name, 'is', t1.isAlive())" 354 | ] 355 | }, 356 | { 357 | "cell_type": "markdown", 358 | "metadata": {}, 359 | "source": [ 360 | "## Without Multi-threading" 361 | ] 362 | }, 363 | { 364 | "cell_type": "code", 365 | "execution_count": 6, 366 | "metadata": { 367 | "ExecuteTime": { 368 | "end_time": "2018-06-04T05:44:12.675416Z", 369 | "start_time": "2018-06-04T05:44:00.659361Z" 370 | }, 371 | "scrolled": true 372 | }, 373 | "outputs": [ 374 | { 375 | "name": "stdout", 376 | "output_type": "stream", 377 | "text": [ 378 | "Double: 2\n", 379 | "Double: 4\n", 380 | "Double: 6\n", 381 | "Double: 8\n", 382 | "Double: 10\n", 383 | "Double: 12\n", 384 | "Square: 1\n", 385 | "Square: 4\n", 386 | "Square: 9\n", 387 | "Square: 16\n", 388 | "Square: 25\n", 389 | "Square: 36\n", 390 | "Time taken: 12.008029460906982\n" 391 | ] 392 | } 393 | ], 394 | "source": [ 395 | "import time\n", 396 | "\n", 397 | "def double(num):\n", 398 | " for n in num:\n", 399 | " time.sleep(1)\n", 400 | " print('Double: ', 2*n)\n", 401 | " \n", 402 | "def square(num):\n", 403 | " for n in num:\n", 404 | " time.sleep(1)\n", 405 | " print('Square: ', n*n)\n", 406 | " \n", 407 | "num = [1, 2, 3, 4, 5, 6]\n", 408 | "\n", 409 | "begintime = time.time()\n", 410 | "\n", 411 | "double(num)\n", 412 | "\n", 413 | "square(num)\n", 414 | "\n", 415 | "endtime = time.time()\n", 416 | "print('Time taken: ', endtime - begintime)" 417 | ] 418 | }, 419 | { 420 | "cell_type": "markdown", 421 | "metadata": {}, 422 | "source": [ 423 | "## With Multi-threading" 424 | ] 425 | }, 426 | { 427 | "cell_type": "code", 428 | "execution_count": 7, 429 | "metadata": { 430 | "ExecuteTime": { 431 | "end_time": "2018-06-04T05:46:29.578025Z", 432 | "start_time": "2018-06-04T05:46:23.560687Z" 433 | }, 434 | "scrolled": true 435 | }, 436 | "outputs": [ 437 | { 438 | "name": "stdout", 439 | "output_type": "stream", 440 | "text": [ 441 | "Double: Square: 1\n", 442 | " 2\n", 443 | "Square: Double: 4\n", 444 | " 4\n", 445 | "Double: 6\n", 446 | "Square: 9\n", 447 | "Double: 8Square: \n", 448 | " 16\n", 449 | "Double: 10\n", 450 | "Square: 25\n", 451 | "Double: 12\n", 452 | "Square: 36\n", 453 | "Time taken: 6.010820150375366\n" 454 | ] 455 | } 456 | ], 457 | "source": [ 458 | "from threading import *\n", 459 | "import time\n", 460 | "\n", 461 | "def double(num):\n", 462 | " for n in num:\n", 463 | " time.sleep(1)\n", 464 | " print('Double: ', 2*n)\n", 465 | "def square(num):\n", 466 | " for n in num:\n", 467 | " time.sleep(1)\n", 468 | " print('Square: ', n*n)\n", 469 | " \n", 470 | "num = [1, 2, 3, 4, 5, 6]\n", 471 | "\n", 472 | "begintime = time.time()\n", 473 | "\n", 474 | "# double(num)\n", 475 | "t1 = Thread(target = double, args = (num,))\n", 476 | "\n", 477 | "# square(num)\n", 478 | "t2 = Thread(target = square, args = (num,))\n", 479 | "\n", 480 | "t1.start()\n", 481 | "t2.start()\n", 482 | "\n", 483 | "t1.join()\n", 484 | "t2.join()\n", 485 | "\n", 486 | "endtime = time.time()\n", 487 | "print('Time taken: ', endtime - begintime)" 488 | ] 489 | }, 490 | { 491 | "cell_type": "markdown", 492 | "metadata": {}, 493 | "source": [ 494 | "## Synchronization" 495 | ] 496 | }, 497 | { 498 | "cell_type": "code", 499 | "execution_count": 8, 500 | "metadata": { 501 | "ExecuteTime": { 502 | "end_time": "2018-06-04T05:47:59.042609Z", 503 | "start_time": "2018-06-04T05:47:54.005307Z" 504 | } 505 | }, 506 | "outputs": [ 507 | { 508 | "name": "stdout", 509 | "output_type": "stream", 510 | "text": [ 511 | "Good Morning: Good Morning: Good Morning: Good Morning: abc\n", 512 | "xyz\n", 513 | "pqrlmn\n", 514 | "\n" 515 | ] 516 | } 517 | ], 518 | "source": [ 519 | "from threading import *\n", 520 | "import time\n", 521 | "\n", 522 | "def greet(name):\n", 523 | " print('Good Morning: ', end = ' ')\n", 524 | " time.sleep(1)\n", 525 | " print(name)\n", 526 | " \n", 527 | " \n", 528 | "t1 = Thread(target = greet, args=('abc',))\n", 529 | "\n", 530 | "t2 = Thread(target = greet, args=('xyz',))\n", 531 | "\n", 532 | "t3 = Thread(target = greet, args=('lmn',))\n", 533 | "\n", 534 | "t4 = Thread(target = greet, args=('pqr',))\n", 535 | "\n", 536 | "t1.start()\n", 537 | "\n", 538 | "\n", 539 | "t2.start()\n", 540 | "\n", 541 | "t3.start()\n", 542 | "\n", 543 | "t4.start()\n", 544 | "\n", 545 | "time.sleep(5)" 546 | ] 547 | }, 548 | { 549 | "cell_type": "code", 550 | "execution_count": 9, 551 | "metadata": { 552 | "ExecuteTime": { 553 | "end_time": "2018-06-04T05:50:07.357106Z", 554 | "start_time": "2018-06-04T05:50:02.331625Z" 555 | } 556 | }, 557 | "outputs": [ 558 | { 559 | "name": "stdout", 560 | "output_type": "stream", 561 | "text": [ 562 | "Good Morning: abc\n", 563 | "Good Morning: xyz\n", 564 | "Good Morning: lmn\n", 565 | "Good Morning: pqr\n" 566 | ] 567 | } 568 | ], 569 | "source": [ 570 | "from threading import *\n", 571 | "import time\n", 572 | "\n", 573 | "l = Lock()\n", 574 | "\n", 575 | "def greet(name):\n", 576 | " l.acquire() \n", 577 | " print('Good Morning: ', end = ' ')\n", 578 | " time.sleep(1)\n", 579 | " print(name)\n", 580 | " l.release() \n", 581 | " \n", 582 | "t1 = Thread(target = greet, args=('abc',))\n", 583 | "\n", 584 | "t2 = Thread(target = greet, args=('xyz',))\n", 585 | "\n", 586 | "t3 = Thread(target = greet, args=('lmn',))\n", 587 | "\n", 588 | "t4 = Thread(target = greet, args=('pqr',))\n", 589 | "\n", 590 | "t1.start()\n", 591 | "\n", 592 | "t2.start()\n", 593 | "\n", 594 | "t3.start()\n", 595 | "\n", 596 | "t4.start()\n", 597 | "\n", 598 | "time.sleep(5)" 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.6.5" 626 | }, 627 | "toc": { 628 | "nav_menu": {}, 629 | "number_sections": true, 630 | "sideBar": true, 631 | "skip_h1_title": false, 632 | "title_cell": "Table of Contents", 633 | "title_sidebar": "Contents", 634 | "toc_cell": false, 635 | "toc_position": {}, 636 | "toc_section_display": true, 637 | "toc_window_display": false 638 | }, 639 | "varInspector": { 640 | "cols": { 641 | "lenName": 16, 642 | "lenType": 16, 643 | "lenVar": 40 644 | }, 645 | "kernels_config": { 646 | "python": { 647 | "delete_cmd_postfix": "", 648 | "delete_cmd_prefix": "del ", 649 | "library": "var_list.py", 650 | "varRefreshCmd": "print(var_dic_list())" 651 | }, 652 | "r": { 653 | "delete_cmd_postfix": ") ", 654 | "delete_cmd_prefix": "rm(", 655 | "library": "var_list.r", 656 | "varRefreshCmd": "cat(var_dic_list()) " 657 | } 658 | }, 659 | "types_to_exclude": [ 660 | "module", 661 | "function", 662 | "builtin_function_or_method", 663 | "instance", 664 | "_Feature" 665 | ], 666 | "window_display": false 667 | } 668 | }, 669 | "nbformat": 4, 670 | "nbformat_minor": 2 671 | } 672 | -------------------------------------------------------------------------------- /12. File Handling/file_handling.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Opening & Closing a File" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 6, 13 | "metadata": { 14 | "ExecuteTime": { 15 | "end_time": "2018-06-06T04:37:28.052812Z", 16 | "start_time": "2018-06-06T04:37:28.033261Z" 17 | } 18 | }, 19 | "outputs": [ 20 | { 21 | "ename": "FileNotFoundError", 22 | "evalue": "[Errno 2] No such file or directory: 'test.txt'", 23 | "output_type": "error", 24 | "traceback": [ 25 | "\u001b[1;31m------------------------------------------------------------------\u001b[0m", 26 | "\u001b[1;31mFileNotFoundError\u001b[0m Traceback (most recent call last)", 27 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mf\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mopen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'test.txt'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 28 | "\u001b[1;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: 'test.txt'" 29 | ] 30 | } 31 | ], 32 | "source": [ 33 | "f = open('test.txt')" 34 | ] 35 | }, 36 | { 37 | "cell_type": "code", 38 | "execution_count": 3, 39 | "metadata": { 40 | "ExecuteTime": { 41 | "end_time": "2018-06-06T04:25:29.032904Z", 42 | "start_time": "2018-06-06T04:25:29.030397Z" 43 | } 44 | }, 45 | "outputs": [], 46 | "source": [ 47 | "f.close()" 48 | ] 49 | }, 50 | { 51 | "cell_type": "markdown", 52 | "metadata": {}, 53 | "source": [ 54 | "## File Object Properties" 55 | ] 56 | }, 57 | { 58 | "cell_type": "code", 59 | "execution_count": 4, 60 | "metadata": { 61 | "ExecuteTime": { 62 | "end_time": "2018-06-06T04:29:39.179147Z", 63 | "start_time": "2018-06-06T04:29:39.171126Z" 64 | } 65 | }, 66 | "outputs": [ 67 | { 68 | "name": "stdout", 69 | "output_type": "stream", 70 | "text": [ 71 | "File Name: test.txt\n", 72 | "File Mode: w\n", 73 | "Is file readable: False\n", 74 | "Is file Writable: True\n", 75 | "Is file closed: False\n", 76 | "Is file closed: True\n" 77 | ] 78 | } 79 | ], 80 | "source": [ 81 | "f = open(\"test.txt\", 'w')\n", 82 | "\n", 83 | "print('File Name: ', f.name)\n", 84 | "\n", 85 | "print('File Mode: ', f.mode)\n", 86 | "\n", 87 | "print('Is file readable: ', f.readable())\n", 88 | "\n", 89 | "print('Is file Writable: ', f.writable())\n", 90 | "\n", 91 | "print('Is file closed: ', f.closed)\n", 92 | "\n", 93 | "f.close()\n", 94 | "\n", 95 | "print('Is file closed: ', f.closed)\n" 96 | ] 97 | }, 98 | { 99 | "cell_type": "markdown", 100 | "metadata": {}, 101 | "source": [ 102 | "## Read Data from text files" 103 | ] 104 | }, 105 | { 106 | "cell_type": "code", 107 | "execution_count": 8, 108 | "metadata": { 109 | "ExecuteTime": { 110 | "end_time": "2018-06-06T04:39:26.673103Z", 111 | "start_time": "2018-06-06T04:39:26.668088Z" 112 | } 113 | }, 114 | "outputs": [ 115 | { 116 | "name": "stdout", 117 | "output_type": "stream", 118 | "text": [ 119 | "\n", 120 | "sdf\n", 121 | "sdf\n", 122 | "sd\n", 123 | "fsd\n", 124 | "f\n", 125 | "sdf\n", 126 | "sf\n", 127 | "sdf\n", 128 | "sdf\n", 129 | "sd\n", 130 | "f\n" 131 | ] 132 | } 133 | ], 134 | "source": [ 135 | "# read() -> read all the data\n", 136 | "\n", 137 | "\n", 138 | "f = open('test.txt','r')\n", 139 | "\n", 140 | "data = f.read()\n", 141 | "\n", 142 | "print(data)\n", 143 | "\n", 144 | "f.close()" 145 | ] 146 | }, 147 | { 148 | "cell_type": "code", 149 | "execution_count": 9, 150 | "metadata": { 151 | "ExecuteTime": { 152 | "end_time": "2018-06-06T04:41:27.625547Z", 153 | "start_time": "2018-06-06T04:41:27.619031Z" 154 | } 155 | }, 156 | "outputs": [ 157 | { 158 | "name": "stdout", 159 | "output_type": "stream", 160 | "text": [ 161 | "\n", 162 | "sdf\n", 163 | "sdf\n", 164 | "s\n" 165 | ] 166 | } 167 | ], 168 | "source": [ 169 | "# read(n) -> read first n characters\n", 170 | "\n", 171 | "f = open('test.txt','r')\n", 172 | "\n", 173 | "data = f.read(10)\n", 174 | "\n", 175 | "print(data)\n", 176 | "\n", 177 | "f.close()" 178 | ] 179 | }, 180 | { 181 | "cell_type": "code", 182 | "execution_count": 10, 183 | "metadata": { 184 | "ExecuteTime": { 185 | "end_time": "2018-06-06T04:44:46.691521Z", 186 | "start_time": "2018-06-06T04:44:46.679490Z" 187 | } 188 | }, 189 | "outputs": [ 190 | { 191 | "name": "stdout", 192 | "output_type": "stream", 193 | "text": [ 194 | "\n", 195 | "\n", 196 | "sdf\n", 197 | "\n", 198 | "sdf\n", 199 | "\n" 200 | ] 201 | } 202 | ], 203 | "source": [ 204 | "# readline() -> read only first line\n", 205 | "\n", 206 | "\n", 207 | "f = open('test.txt','r')\n", 208 | "\n", 209 | "line1 = f.readline()\n", 210 | "\n", 211 | "print(line1)\n", 212 | "\n", 213 | "line2 = f.readline()\n", 214 | "\n", 215 | "print(line2)\n", 216 | "\n", 217 | "line3 = f.readline()\n", 218 | "\n", 219 | "print(line3)\n", 220 | "\n", 221 | "f.close()" 222 | ] 223 | }, 224 | { 225 | "cell_type": "code", 226 | "execution_count": 11, 227 | "metadata": { 228 | "ExecuteTime": { 229 | "end_time": "2018-06-06T04:47:45.754270Z", 230 | "start_time": "2018-06-06T04:47:45.749254Z" 231 | } 232 | }, 233 | "outputs": [ 234 | { 235 | "name": "stdout", 236 | "output_type": "stream", 237 | "text": [ 238 | "\n", 239 | "\n", 240 | "\n", 241 | "sdf\n", 242 | "\n", 243 | "sdf\n", 244 | "\n", 245 | "sd\n", 246 | "\n", 247 | "fsd\n", 248 | "\n", 249 | "f\n", 250 | "\n", 251 | "sdf\n", 252 | "\n", 253 | "sf\n", 254 | "\n", 255 | "sdf\n", 256 | "\n", 257 | "sdf\n", 258 | "\n", 259 | "sd\n", 260 | "\n", 261 | "f\n" 262 | ] 263 | } 264 | ], 265 | "source": [ 266 | "# readlines() -> read all the lines into list\n", 267 | "\n", 268 | "\n", 269 | "f = open('test.txt', 'r')\n", 270 | "\n", 271 | "lines = f.readlines()\n", 272 | "\n", 273 | "print(type(lines))\n", 274 | "\n", 275 | "for l in lines:\n", 276 | " print(l)\n", 277 | " \n", 278 | "f.close()" 279 | ] 280 | }, 281 | { 282 | "cell_type": "markdown", 283 | "metadata": {}, 284 | "source": [ 285 | "## Write Data to text files" 286 | ] 287 | }, 288 | { 289 | "cell_type": "code", 290 | "execution_count": 13, 291 | "metadata": { 292 | "ExecuteTime": { 293 | "end_time": "2018-06-06T04:51:32.495535Z", 294 | "start_time": "2018-06-06T04:51:32.463449Z" 295 | } 296 | }, 297 | "outputs": [], 298 | "source": [ 299 | "f = open('test.txt', 'a')\n", 300 | "\n", 301 | "f.write(\"abc\")\n", 302 | "\n", 303 | "f.write(\"xyz\\n\")\n", 304 | "\n", 305 | "f.write(\"lmn\")\n", 306 | "\n", 307 | "f.close()\n", 308 | "\n", 309 | "# f = open('test.txt', 'a')" 310 | ] 311 | }, 312 | { 313 | "cell_type": "code", 314 | "execution_count": 24, 315 | "metadata": { 316 | "ExecuteTime": { 317 | "end_time": "2018-06-06T04:57:51.097713Z", 318 | "start_time": "2018-06-06T04:57:51.065125Z" 319 | } 320 | }, 321 | "outputs": [], 322 | "source": [ 323 | "# writeline takes a sequence\n", 324 | "\n", 325 | "f = open('test.txt', 'w')\n", 326 | "\n", 327 | "lines = ['line-1', 'line-2', 'line-3']\n", 328 | "\n", 329 | "f.writelines(lines)\n", 330 | "\n", 331 | "f.close()" 332 | ] 333 | }, 334 | { 335 | "cell_type": "markdown", 336 | "metadata": {}, 337 | "source": [ 338 | "## r+ Mode" 339 | ] 340 | }, 341 | { 342 | "cell_type": "code", 343 | "execution_count": 25, 344 | "metadata": { 345 | "ExecuteTime": { 346 | "end_time": "2018-06-06T04:58:29.790108Z", 347 | "start_time": "2018-06-06T04:58:29.779079Z" 348 | } 349 | }, 350 | "outputs": [ 351 | { 352 | "name": "stdout", 353 | "output_type": "stream", 354 | "text": [ 355 | "line-1line-2line-3\n" 356 | ] 357 | } 358 | ], 359 | "source": [ 360 | "f = open('test.txt','r+')\n", 361 | "\n", 362 | "data = f.read()\n", 363 | "\n", 364 | "print(data)\n", 365 | "\n", 366 | "f.write('asdfghjkl')\n", 367 | "\n", 368 | "f.close()" 369 | ] 370 | }, 371 | { 372 | "cell_type": "code", 373 | "execution_count": 26, 374 | "metadata": { 375 | "ExecuteTime": { 376 | "end_time": "2018-06-06T04:58:33.086207Z", 377 | "start_time": "2018-06-06T04:58:33.080191Z" 378 | } 379 | }, 380 | "outputs": [ 381 | { 382 | "name": "stdout", 383 | "output_type": "stream", 384 | "text": [ 385 | "line-1line-2line-3asdfghjkl\n" 386 | ] 387 | } 388 | ], 389 | "source": [ 390 | "f = open('test.txt')\n", 391 | "\n", 392 | "data = f.read()\n", 393 | "\n", 394 | "print(data)" 395 | ] 396 | }, 397 | { 398 | "cell_type": "code", 399 | "execution_count": 27, 400 | "metadata": { 401 | "ExecuteTime": { 402 | "end_time": "2018-06-06T05:06:20.634323Z", 403 | "start_time": "2018-06-06T05:06:20.616778Z" 404 | } 405 | }, 406 | "outputs": [], 407 | "source": [ 408 | "f = open('test.txt','r+')\n", 409 | "\n", 410 | "f.write('\\nqwertyuiop\\n')\n", 411 | "\n", 412 | "f.close()" 413 | ] 414 | }, 415 | { 416 | "cell_type": "markdown", 417 | "metadata": {}, 418 | "source": [ 419 | "## 'with' Statement" 420 | ] 421 | }, 422 | { 423 | "cell_type": "code", 424 | "execution_count": 28, 425 | "metadata": { 426 | "ExecuteTime": { 427 | "end_time": "2018-06-06T05:11:47.426468Z", 428 | "start_time": "2018-06-06T05:11:47.407402Z" 429 | }, 430 | "scrolled": false 431 | }, 432 | "outputs": [ 433 | { 434 | "name": "stdout", 435 | "output_type": "stream", 436 | "text": [ 437 | "Is file closed: False\n", 438 | "Is file closed: True\n" 439 | ] 440 | } 441 | ], 442 | "source": [ 443 | "with open('test.txt', 'a') as f:\n", 444 | " f.write('\\nabc\\n')\n", 445 | " f.write('xyz\\n')\n", 446 | " print('Is file closed: ', f.closed)\n", 447 | "print('Is file closed: ', f.closed)" 448 | ] 449 | }, 450 | { 451 | "cell_type": "markdown", 452 | "metadata": {}, 453 | "source": [ 454 | "## seek() and tell()" 455 | ] 456 | }, 457 | { 458 | "cell_type": "code", 459 | "execution_count": 29, 460 | "metadata": { 461 | "ExecuteTime": { 462 | "end_time": "2018-06-06T05:15:53.119649Z", 463 | "start_time": "2018-06-06T05:15:53.102596Z" 464 | } 465 | }, 466 | "outputs": [ 467 | { 468 | "name": "stdout", 469 | "output_type": "stream", 470 | "text": [ 471 | "0\n", 472 | "abc\n", 473 | "\n", 474 | "5\n", 475 | "def\n", 476 | "g\n", 477 | "11\n" 478 | ] 479 | } 480 | ], 481 | "source": [ 482 | "f = open('test.txt', 'r')\n", 483 | "\n", 484 | "print(f.tell()) # Current Position of the cursor. Print Index\n", 485 | "\n", 486 | "print(f.read(4))\n", 487 | "\n", 488 | "print(f.tell()) # Current Position of the cursor. Print Index\n", 489 | "\n", 490 | "print(f.read(5))\n", 491 | "\n", 492 | "print(f.tell()) # Current Position of the cursor. Print Index\n", 493 | "\n", 494 | "f.close()" 495 | ] 496 | }, 497 | { 498 | "cell_type": "code", 499 | "execution_count": 30, 500 | "metadata": { 501 | "ExecuteTime": { 502 | "end_time": "2018-06-06T05:20:13.017479Z", 503 | "start_time": "2018-06-06T05:20:13.002439Z" 504 | } 505 | }, 506 | "outputs": [ 507 | { 508 | "name": "stdout", 509 | "output_type": "stream", 510 | "text": [ 511 | "The current file pointer position: 0\n", 512 | "The current file pointer position: 28\n", 513 | "The current file pointer position: 2\n", 514 | "The current file pointer position: 10\n" 515 | ] 516 | } 517 | ], 518 | "source": [ 519 | "with open('test.txt', 'r+') as f:\n", 520 | " print('The current file pointer position: ', f.tell())\n", 521 | " \n", 522 | " text = f.read()\n", 523 | " \n", 524 | " print('The current file pointer position: ', f.tell())\n", 525 | " \n", 526 | " f.seek(2)\n", 527 | " \n", 528 | " print('The current file pointer position: ', f.tell())\n", 529 | " \n", 530 | " f.write('HELL NO!')\n", 531 | " \n", 532 | " print('The current file pointer position: ', f.tell())" 533 | ] 534 | }, 535 | { 536 | "cell_type": "markdown", 537 | "metadata": {}, 538 | "source": [ 539 | "## Renaming & Deleting files" 540 | ] 541 | }, 542 | { 543 | "cell_type": "code", 544 | "execution_count": 31, 545 | "metadata": { 546 | "ExecuteTime": { 547 | "end_time": "2018-06-06T05:22:56.579668Z", 548 | "start_time": "2018-06-06T05:22:56.573653Z" 549 | } 550 | }, 551 | "outputs": [], 552 | "source": [ 553 | "import os\n", 554 | "\n", 555 | "os.rename('test.txt', 'test-1.txt')" 556 | ] 557 | }, 558 | { 559 | "cell_type": "code", 560 | "execution_count": 32, 561 | "metadata": { 562 | "ExecuteTime": { 563 | "end_time": "2018-06-06T05:23:04.326993Z", 564 | "start_time": "2018-06-06T05:23:04.323484Z" 565 | } 566 | }, 567 | "outputs": [], 568 | "source": [ 569 | "os.remove('test-1.txt')" 570 | ] 571 | }, 572 | { 573 | "cell_type": "markdown", 574 | "metadata": {}, 575 | "source": [ 576 | "## Splitting Lines" 577 | ] 578 | }, 579 | { 580 | "cell_type": "code", 581 | "execution_count": 33, 582 | "metadata": { 583 | "ExecuteTime": { 584 | "end_time": "2018-06-06T05:26:09.278300Z", 585 | "start_time": "2018-06-06T05:26:09.270279Z" 586 | } 587 | }, 588 | "outputs": [ 589 | { 590 | "name": "stdout", 591 | "output_type": "stream", 592 | "text": [ 593 | "['dsas', 'das', 'dsd', 'fsdf']\n", 594 | "['sdf', 'sdf', 'df', 'dfg']\n", 595 | "[]\n", 596 | "['dfgdf', 'dfg', 'g', 'gh']\n", 597 | "[]\n", 598 | "['dg']\n", 599 | "['gh']\n", 600 | "['fghfghfgh', 'fgh', 'fgh']\n" 601 | ] 602 | } 603 | ], 604 | "source": [ 605 | "with open('test.txt', 'r') as f:\n", 606 | " lines = f.readlines()\n", 607 | " for l in lines:\n", 608 | " words = l.split()\n", 609 | " print(words)" 610 | ] 611 | }, 612 | { 613 | "cell_type": "markdown", 614 | "metadata": {}, 615 | "source": [ 616 | "## Assignments" 617 | ] 618 | }, 619 | { 620 | "cell_type": "markdown", 621 | "metadata": {}, 622 | "source": [ 623 | " Q.1- Write a Python program to read last n lines of a file\n", 624 | " \n", 625 | " Q.3- Write a Python program to copy the contents of a file to another file \n", 626 | " \n", 627 | " Q.4- Write a Python program to combine each line from first file with the corresponding line in second file. \n", 628 | " \n", 629 | " Q.5- Write a Python program to write 10 random numbers into a file. Read the file and then sort the numbers and then store it to another file." 630 | ] 631 | }, 632 | { 633 | "cell_type": "code", 634 | "execution_count": 10, 635 | "metadata": { 636 | "ExecuteTime": { 637 | "end_time": "2018-06-06T02:00:49.708293Z", 638 | "start_time": "2018-06-06T02:00:49.688347Z" 639 | } 640 | }, 641 | "outputs": [], 642 | "source": [ 643 | "with open('test-1.txt', 'r') as f1:\n", 644 | " with open('test-2.txt', 'w') as f2:\n", 645 | " for line in f1:\n", 646 | " f2.write(line)" 647 | ] 648 | }, 649 | { 650 | "cell_type": "code", 651 | "execution_count": 11, 652 | "metadata": { 653 | "ExecuteTime": { 654 | "end_time": "2018-06-06T02:01:59.482337Z", 655 | "start_time": "2018-06-06T02:01:59.470459Z" 656 | } 657 | }, 658 | "outputs": [], 659 | "source": [ 660 | "with open('test-1.txt', 'r') as f1, open('test-2.txt', 'w') as f2:\n", 661 | " for line in f1:\n", 662 | " f2.write(line)" 663 | ] 664 | }, 665 | { 666 | "cell_type": "code", 667 | "execution_count": null, 668 | "metadata": {}, 669 | "outputs": [], 670 | "source": [] 671 | } 672 | ], 673 | "metadata": { 674 | "kernelspec": { 675 | "display_name": "Python 3", 676 | "language": "python", 677 | "name": "python3" 678 | }, 679 | "language_info": { 680 | "codemirror_mode": { 681 | "name": "ipython", 682 | "version": 3 683 | }, 684 | "file_extension": ".py", 685 | "mimetype": "text/x-python", 686 | "name": "python", 687 | "nbconvert_exporter": "python", 688 | "pygments_lexer": "ipython3", 689 | "version": "3.6.5" 690 | }, 691 | "toc": { 692 | "nav_menu": {}, 693 | "number_sections": true, 694 | "sideBar": true, 695 | "skip_h1_title": false, 696 | "title_cell": "Table of Contents", 697 | "title_sidebar": "Contents", 698 | "toc_cell": false, 699 | "toc_position": {}, 700 | "toc_section_display": true, 701 | "toc_window_display": false 702 | }, 703 | "varInspector": { 704 | "cols": { 705 | "lenName": 16, 706 | "lenType": 16, 707 | "lenVar": 40 708 | }, 709 | "kernels_config": { 710 | "python": { 711 | "delete_cmd_postfix": "", 712 | "delete_cmd_prefix": "del ", 713 | "library": "var_list.py", 714 | "varRefreshCmd": "print(var_dic_list())" 715 | }, 716 | "r": { 717 | "delete_cmd_postfix": ") ", 718 | "delete_cmd_prefix": "rm(", 719 | "library": "var_list.r", 720 | "varRefreshCmd": "cat(var_dic_list()) " 721 | } 722 | }, 723 | "types_to_exclude": [ 724 | "module", 725 | "function", 726 | "builtin_function_or_method", 727 | "instance", 728 | "_Feature" 729 | ], 730 | "window_display": false 731 | } 732 | }, 733 | "nbformat": 4, 734 | "nbformat_minor": 2 735 | } 736 | -------------------------------------------------------------------------------- /12. File Handling/test.txt: -------------------------------------------------------------------------------- 1 | dsas das dsd fsdf 2 | sdf sdf df dfg 3 | 4 | dfgdf dfg g gh 5 | 6 | dg 7 | gh 8 | fghfghfgh fgh fgh -------------------------------------------------------------------------------- /14. Databases/database.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## MySQLdb vs pymysql vs mysql.connector" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "First install mysql-community software(refer the link mentioned below)
\n", 15 | "https://www.youtube.com/watch?v=WuBcTJnIuzo\n", 16 | "\n", 17 | "After that
pip install pymysql\n", 18 | "\n", 19 | "or\n", 20 | "\n", 21 | "pip install mysql.connector" 22 | ] 23 | }, 24 | { 25 | "cell_type": "code", 26 | "execution_count": 1, 27 | "metadata": { 28 | "ExecuteTime": { 29 | "end_time": "2018-06-09T07:34:50.318453Z", 30 | "start_time": "2018-06-09T07:34:49.864937Z" 31 | } 32 | }, 33 | "outputs": [], 34 | "source": [ 35 | "import pymysql" 36 | ] 37 | }, 38 | { 39 | "cell_type": "markdown", 40 | "metadata": {}, 41 | "source": [ 42 | "Before moving forward, lets open MySQL Command Line Client and perform basic database operations:

\n", 43 | "show databases;

\n", 44 | "create database mydb;

\n", 45 | "use mydb;

\n", 46 | "show tables;

\n", 47 | "create table employees(eno int(5) primary key, ename varchar(10), eage int(3), eincome double(10,2));

\n", 48 | "desc employees;

\n", 49 | "select * from employees;

\n" 50 | ] 51 | }, 52 | { 53 | "cell_type": "markdown", 54 | "metadata": { 55 | "ExecuteTime": { 56 | "end_time": "2018-06-09T07:54:40.331235Z", 57 | "start_time": "2018-06-09T07:54:40.326237Z" 58 | } 59 | }, 60 | "source": [ 61 | "## How to connect with database in Python?" 62 | ] 63 | }, 64 | { 65 | "cell_type": "markdown", 66 | "metadata": {}, 67 | "source": [ 68 | "1. Import database module

\n", 69 | "import pymysql as pm

or

pip install mysql.connector as pm

\n", 70 | "\n", 71 | "2. Establish connection between python program and db

\n", 72 | "con = pm.connect(host, database, user, password)

\n", 73 | "\n", 74 | "3. To execute mysql query and hold result cursor is required

\n", 75 | "cursor = con.cursor()

\n", 76 | "\n", 77 | "4. Execute MySql query with the help of cursor object

\n", 78 | "cursor.execute(query)

\n", 79 | "cursor.executemany()

\n", 80 | "\n", 81 | "5. fetch the result from cursor object in case of select query

\n", 82 | "cursor.fetchone()

\n", 83 | "cursor.fetchall()

\n", 84 | "cursor.fetchmany(n)

\n", 85 | "\n", 86 | "6. commit or rollback changes based on your requirement

\n", 87 | "con.commit()

\n", 88 | "con.rollback()

\n", 89 | "\n", 90 | "7. close the resources and disconnect database

\n", 91 | "cursor.close()

\n", 92 | "con.close()

" 93 | ] 94 | }, 95 | { 96 | "cell_type": "code", 97 | "execution_count": 1, 98 | "metadata": { 99 | "ExecuteTime": { 100 | "end_time": "2018-06-11T04:59:34.951262Z", 101 | "start_time": "2018-06-11T04:59:34.460573Z" 102 | } 103 | }, 104 | "outputs": [ 105 | { 106 | "name": "stdout", 107 | "output_type": "stream", 108 | "text": [ 109 | "\n", 110 | "DONE!!\n" 111 | ] 112 | } 113 | ], 114 | "source": [ 115 | "import pymysql as pm\n", 116 | "\n", 117 | "try:\n", 118 | " con = pm.connect(host='localhost', database='acadviewdb',\\\n", 119 | " user='root', password='root')\n", 120 | " print(con)\n", 121 | " \n", 122 | "finally:\n", 123 | " con.close()\n", 124 | " print('DONE!!')" 125 | ] 126 | }, 127 | { 128 | "cell_type": "markdown", 129 | "metadata": { 130 | "ExecuteTime": { 131 | "end_time": "2018-06-09T07:54:53.266358Z", 132 | "start_time": "2018-06-09T07:54:53.256400Z" 133 | } 134 | }, 135 | "source": [ 136 | "## Creating Database Table" 137 | ] 138 | }, 139 | { 140 | "cell_type": "code", 141 | "execution_count": 2, 142 | "metadata": { 143 | "ExecuteTime": { 144 | "end_time": "2018-06-11T05:04:02.228942Z", 145 | "start_time": "2018-06-11T05:04:01.817014Z" 146 | } 147 | }, 148 | "outputs": [ 149 | { 150 | "name": "stdout", 151 | "output_type": "stream", 152 | "text": [ 153 | "Table created successfully!!\n", 154 | "DONE!!\n" 155 | ] 156 | } 157 | ], 158 | "source": [ 159 | "import pymysql as pm\n", 160 | "\n", 161 | "try:\n", 162 | " con = pm.connect(host='localhost', database='acadviewdb',\\\n", 163 | " user='root', password='root')\n", 164 | " \n", 165 | " cursor = con.cursor()\n", 166 | " \n", 167 | " query = 'create table employees1(eno int(5) primary key, \\\n", 168 | " ename varchar(10), eage int(3), eincome double(10,2))'\n", 169 | " \n", 170 | " cursor.execute(query)\n", 171 | " \n", 172 | " print('Table created successfully!!')\n", 173 | " \n", 174 | "except pm.DatabaseError as e:\n", 175 | " if con:\n", 176 | " con.rollback()\n", 177 | " print('Problem occured: ', e)\n", 178 | " \n", 179 | "finally:\n", 180 | " if cursor:\n", 181 | " cursor.close()\n", 182 | " if con:\n", 183 | " con.close()\n", 184 | " print('DONE!!')" 185 | ] 186 | }, 187 | { 188 | "cell_type": "markdown", 189 | "metadata": { 190 | "ExecuteTime": { 191 | "end_time": "2018-06-09T07:55:03.089437Z", 192 | "start_time": "2018-06-09T07:55:03.084439Z" 193 | } 194 | }, 195 | "source": [ 196 | "## Insert Operation" 197 | ] 198 | }, 199 | { 200 | "cell_type": "code", 201 | "execution_count": 1, 202 | "metadata": { 203 | "ExecuteTime": { 204 | "end_time": "2018-06-12T16:03:47.643024Z", 205 | "start_time": "2018-06-12T16:03:47.375146Z" 206 | } 207 | }, 208 | "outputs": [ 209 | { 210 | "name": "stdout", 211 | "output_type": "stream", 212 | "text": [ 213 | "Problem occured: (1062, \"Duplicate entry '2' for key 'PRIMARY'\")\n", 214 | "DONE!!\n" 215 | ] 216 | } 217 | ], 218 | "source": [ 219 | "import pymysql as pm\n", 220 | "\n", 221 | "try:\n", 222 | " con = pm.connect(host='localhost', database='acadviewdb',\\\n", 223 | " user='root', password='root')\n", 224 | " \n", 225 | " cursor = con.cursor()\n", 226 | " \n", 227 | " query = \"insert into employees1(eno, ename, eage, eincome) \\\n", 228 | " values(%s, %s, %s, %s)\"\n", 229 | " \n", 230 | " records = [(1, 'abc', 23, 23000),\n", 231 | " (2, 'def', 24, 20000),\n", 232 | " (3, 'ghi', 50, 100000)]\n", 233 | " \n", 234 | " cursor.executemany(query, records)\n", 235 | " \n", 236 | " con.commit()\n", 237 | " \n", 238 | "except pm.DatabaseError as e:\n", 239 | " if con:\n", 240 | " con.rollback()\n", 241 | " print('Problem occured: ', e)\n", 242 | " \n", 243 | "finally:\n", 244 | " if cursor:\n", 245 | " cursor.close()\n", 246 | " if con:\n", 247 | " con.close()\n", 248 | " print('DONE!!')" 249 | ] 250 | }, 251 | { 252 | "cell_type": "markdown", 253 | "metadata": { 254 | "ExecuteTime": { 255 | "end_time": "2018-06-09T07:55:13.283293Z", 256 | "start_time": "2018-06-09T07:55:13.273300Z" 257 | } 258 | }, 259 | "source": [ 260 | "## Read Operation" 261 | ] 262 | }, 263 | { 264 | "cell_type": "code", 265 | "execution_count": 5, 266 | "metadata": { 267 | "ExecuteTime": { 268 | "end_time": "2018-06-11T05:14:31.366302Z", 269 | "start_time": "2018-06-11T05:14:31.336380Z" 270 | } 271 | }, 272 | "outputs": [ 273 | { 274 | "name": "stdout", 275 | "output_type": "stream", 276 | "text": [ 277 | "Eno: 1, Ename: abc, Eage: 23, Esal: 23000.0\n", 278 | "Eno: 2, Ename: def, Eage: 24, Esal: 20000.0\n", 279 | "Eno: 3, Ename: ghi, Eage: 50, Esal: 100000.0\n", 280 | "DONE!!\n" 281 | ] 282 | } 283 | ], 284 | "source": [ 285 | "import pymysql as pm\n", 286 | "\n", 287 | "try:\n", 288 | " con = pm.connect(host='localhost', database='acadviewdb',\\\n", 289 | " user='root', password='root')\n", 290 | " \n", 291 | " cursor = con.cursor()\n", 292 | " \n", 293 | " query = 'select * from employees1'\n", 294 | " \n", 295 | " cursor.execute(query)\n", 296 | " \n", 297 | " data = cursor.fetchall()\n", 298 | " \n", 299 | " for row in data:\n", 300 | " print('Eno: {}, Ename: {}, Eage: {}, Esal: {}'\\\n", 301 | " .format(row[0], row[1], row[2], row[3]))\n", 302 | " \n", 303 | "except pm.DatabaseError as e:\n", 304 | " if con:\n", 305 | " con.rollback()\n", 306 | " print('Problem occured: ', e)\n", 307 | " \n", 308 | "finally:\n", 309 | " if cursor:\n", 310 | " cursor.close()\n", 311 | " if con:\n", 312 | " con.close()\n", 313 | " print('DONE!!')" 314 | ] 315 | }, 316 | { 317 | "cell_type": "markdown", 318 | "metadata": { 319 | "ExecuteTime": { 320 | "end_time": "2018-06-09T07:55:25.493274Z", 321 | "start_time": "2018-06-09T07:55:25.483243Z" 322 | } 323 | }, 324 | "source": [ 325 | "## Update Operation" 326 | ] 327 | }, 328 | { 329 | "cell_type": "code", 330 | "execution_count": 6, 331 | "metadata": { 332 | "ExecuteTime": { 333 | "end_time": "2018-06-11T05:16:03.394703Z", 334 | "start_time": "2018-06-11T05:16:03.241093Z" 335 | }, 336 | "scrolled": false 337 | }, 338 | "outputs": [ 339 | { 340 | "name": "stdout", 341 | "output_type": "stream", 342 | "text": [ 343 | "DONE!!\n" 344 | ] 345 | } 346 | ], 347 | "source": [ 348 | "import pymysql as pm\n", 349 | "\n", 350 | "try:\n", 351 | " con = pm.connect(host='localhost', database='acadviewdb',\\\n", 352 | " user='root', password='root')\n", 353 | " \n", 354 | " cursor = con.cursor()\n", 355 | " \n", 356 | " query = \"update employees1 set eage=eage+1 where ename = 'def'\"\n", 357 | " \n", 358 | " cursor.execute(query)\n", 359 | " \n", 360 | " con.commit()\n", 361 | " \n", 362 | "except pm.DatabaseError as e:\n", 363 | " if con:\n", 364 | " con.rollback()\n", 365 | " print('Problem occured: ', e)\n", 366 | " \n", 367 | "finally:\n", 368 | " if cursor:\n", 369 | " cursor.close()\n", 370 | " if con:\n", 371 | " con.close()\n", 372 | " print('DONE!!')" 373 | ] 374 | }, 375 | { 376 | "cell_type": "markdown", 377 | "metadata": { 378 | "ExecuteTime": { 379 | "end_time": "2018-06-09T07:55:36.169792Z", 380 | "start_time": "2018-06-09T07:55:36.164804Z" 381 | } 382 | }, 383 | "source": [ 384 | "## Delete Operation" 385 | ] 386 | }, 387 | { 388 | "cell_type": "code", 389 | "execution_count": 10, 390 | "metadata": { 391 | "ExecuteTime": { 392 | "end_time": "2018-06-11T05:24:07.063442Z", 393 | "start_time": "2018-06-11T05:24:01.564789Z" 394 | }, 395 | "scrolled": true 396 | }, 397 | "outputs": [ 398 | { 399 | "name": "stdout", 400 | "output_type": "stream", 401 | "text": [ 402 | "Enter age: 50\n", 403 | "DONE!!\n" 404 | ] 405 | } 406 | ], 407 | "source": [ 408 | "import pymysql as pm\n", 409 | "\n", 410 | "try:\n", 411 | " con = pm.connect(host='localhost', database='acadviewdb',\\\n", 412 | " user='root', password='root')\n", 413 | " \n", 414 | " cursor = con.cursor()\n", 415 | " \n", 416 | " age = input('Enter age: ')\n", 417 | " \n", 418 | " query = \"delete from employees1 where eage='%s'\"%(age)\n", 419 | " \n", 420 | " cursor.execute(query)\n", 421 | " \n", 422 | " con.commit()\n", 423 | " \n", 424 | "except pm.DatabaseError as e:\n", 425 | " if con:\n", 426 | " con.rollback()\n", 427 | " print('Problem occured: ', e)\n", 428 | " \n", 429 | "finally:\n", 430 | " if cursor:\n", 431 | " cursor.close()\n", 432 | " if con:\n", 433 | " con.close()\n", 434 | " print('DONE!!')" 435 | ] 436 | }, 437 | { 438 | "cell_type": "code", 439 | "execution_count": null, 440 | "metadata": {}, 441 | "outputs": [], 442 | "source": [] 443 | } 444 | ], 445 | "metadata": { 446 | "kernelspec": { 447 | "display_name": "Python 3", 448 | "language": "python", 449 | "name": "python3" 450 | }, 451 | "language_info": { 452 | "codemirror_mode": { 453 | "name": "ipython", 454 | "version": 3 455 | }, 456 | "file_extension": ".py", 457 | "mimetype": "text/x-python", 458 | "name": "python", 459 | "nbconvert_exporter": "python", 460 | "pygments_lexer": "ipython3", 461 | "version": "3.6.5" 462 | }, 463 | "toc": { 464 | "nav_menu": {}, 465 | "number_sections": true, 466 | "sideBar": true, 467 | "skip_h1_title": false, 468 | "title_cell": "Table of Contents", 469 | "title_sidebar": "Contents", 470 | "toc_cell": false, 471 | "toc_position": {}, 472 | "toc_section_display": true, 473 | "toc_window_display": false 474 | }, 475 | "varInspector": { 476 | "cols": { 477 | "lenName": 16, 478 | "lenType": 16, 479 | "lenVar": 40 480 | }, 481 | "kernels_config": { 482 | "python": { 483 | "delete_cmd_postfix": "", 484 | "delete_cmd_prefix": "del ", 485 | "library": "var_list.py", 486 | "varRefreshCmd": "print(var_dic_list())" 487 | }, 488 | "r": { 489 | "delete_cmd_postfix": ") ", 490 | "delete_cmd_prefix": "rm(", 491 | "library": "var_list.r", 492 | "varRefreshCmd": "cat(var_dic_list()) " 493 | } 494 | }, 495 | "types_to_exclude": [ 496 | "module", 497 | "function", 498 | "builtin_function_or_method", 499 | "instance", 500 | "_Feature" 501 | ], 502 | "window_display": false 503 | } 504 | }, 505 | "nbformat": 4, 506 | "nbformat_minor": 2 507 | } 508 | -------------------------------------------------------------------------------- /15. GUI-1/Login.py: -------------------------------------------------------------------------------- 1 | ''' 2 | All the credits for this code goes to: 3 | Github-> https://gist.github.com/SirRobo/c503014bbb03088bec37a61231036461 4 | 5 | Youtube-> https://www.youtube.com/watch?v=iCK8adSeG7A&list=PL_iZ8cQZDSSBe3UJg99ioPe2TfzXOxYZY&index=4&t=0s 6 | ''' 7 | 8 | from tkinter import * 9 | import os 10 | 11 | 12 | def Signup(): # This is the signup definition, 13 | global pwordE # These globals just make the variables global to the entire script, meaning any definition can use them 14 | global nameE 15 | global roots 16 | 17 | roots = Tk() # This creates the window, just a blank one. 18 | roots.title('Signup') # This renames the title of said window to 'signup' 19 | intruction = Label(roots, 20 | text='Please Enter new Credentials\n') # This puts a label, so just a piece of text saying 'please enter blah' 21 | intruction.grid(row=0, column=0, 22 | sticky=E) # This just puts it in the window, on row 0, col 0. If you want to learn more look up a tkinter tutorial :) 23 | 24 | nameL = Label(roots, text='New Username: ') # This just does the same as above, instead with the text new username. 25 | pwordL = Label(roots, text='New Password: ') # ^^ 26 | nameL.grid(row=1, column=0, 27 | sticky=W) # Same thing as the instruction var just on different rows. :) Tkinter is like that. 28 | pwordL.grid(row=2, column=0, sticky=W) # ^^ 29 | 30 | nameE = Entry(roots) # This now puts a text box waiting for input. 31 | pwordE = Entry(roots, 32 | show='*') # Same as above, yet 'show="*"' What this does is replace the text with *, like a password box :D 33 | nameE.grid(row=1, column=1) # You know what this does now :D 34 | pwordE.grid(row=2, column=1) # ^^ 35 | 36 | signupButton = Button(roots, text='Signup', 37 | command=FSSignup) # This creates the button with the text 'signup', when you click it, the command 'fssignup' will run. which is the def 38 | signupButton.grid(columnspan=2, sticky=W) 39 | roots.mainloop() # This just makes the window keep open, we will destroy it soon 40 | 41 | 42 | def FSSignup(): 43 | with open('creds.txt', 'w') as f: # Creates a document using the variable we made at the top. 44 | f.write( 45 | nameE.get()) # nameE is the variable we were storing the input to. Tkinter makes us use .get() to get the actual string. 46 | f.write('\n') # Splits the line so both variables are on different lines. 47 | f.write(pwordE.get()) # Same as nameE just with pword var 48 | f.close() # Closes the file 49 | 50 | roots.destroy() # This will destroy the signup window. :) 51 | Login() # This will move us onto the login definition :D 52 | 53 | 54 | def Login(): 55 | global nameEL 56 | global pwordEL # More globals :D 57 | global rootA 58 | 59 | rootA = Tk() # This now makes a new window. 60 | rootA.title('Login') # This makes the window title 'login' 61 | 62 | intruction = Label(rootA, text='Please Login\n') # More labels to tell us what they do 63 | intruction.grid(sticky=E) # Blahdy Blah 64 | 65 | nameL = Label(rootA, text='Username: ') # More labels 66 | pwordL = Label(rootA, text='Password: ') # ^ 67 | nameL.grid(row=1, sticky=W) 68 | pwordL.grid(row=2, sticky=W) 69 | 70 | nameEL = Entry(rootA) # The entry input 71 | pwordEL = Entry(rootA, show='*') 72 | nameEL.grid(row=1, column=1) 73 | pwordEL.grid(row=2, column=1) 74 | 75 | loginB = Button(rootA, text='Login', 76 | command=CheckLogin) # This makes the login button, which will go to the CheckLogin def. 77 | loginB.grid(columnspan=2, sticky=W) 78 | 79 | rmuser = Button(rootA, text='Delete User', fg='red', 80 | command=DelUser) # This makes the deluser button. blah go to the deluser def. 81 | rmuser.grid(columnspan=2, sticky=W) 82 | rootA.mainloop() 83 | 84 | 85 | def CheckLogin(): 86 | with open('creds.txt') as f: 87 | data = f.readlines() # This takes the entire document we put the info into and puts it into the data variable 88 | uname = data[0].rstrip() # Data[0], 0 is the first line, 1 is the second and so on. 89 | pword = data[1].rstrip() # Using .rstrip() will remove the \n (new line) word from before when we input it 90 | 91 | if nameEL.get() == uname and pwordEL.get() == pword: # Checks to see if you entered the correct data. 92 | r = Tk() # Opens new window 93 | r.title(':D') 94 | r.geometry('150x50') # Makes the window a certain size 95 | rlbl = Label(r, text='\n[+] Logged In') # "logged in" label 96 | rlbl.pack() # Pack is like .grid(), just different 97 | r.mainloop() 98 | else: 99 | r = Tk() 100 | r.title('D:') 101 | r.geometry('150x50') 102 | rlbl = Label(r, text='\n[!] Invalid Login') 103 | rlbl.pack() 104 | r.mainloop() 105 | 106 | 107 | def DelUser(): 108 | os.remove('creds.txt') # Removes the file 109 | rootA.destroy() # Destroys the login window 110 | Signup() # And goes back to the start! 111 | 112 | 113 | if os.path.isfile('creds.txt'): 114 | Login() 115 | else: # This if else statement checks to see if the file exists. If it does it will go to Login, if not it will go to Signup :) 116 | Signup() -------------------------------------------------------------------------------- /15. GUI-1/creds.txt: -------------------------------------------------------------------------------- 1 | kanav 2 | 1234 -------------------------------------------------------------------------------- /15. GUI-1/demo_1.py: -------------------------------------------------------------------------------- 1 | from tkinter import * 2 | 3 | root = Tk() 4 | 5 | hwL = Label(root, text='Hello World!!') 6 | 7 | hwL.pack() 8 | 9 | root.mainloop() 10 | 11 | -------------------------------------------------------------------------------- /15. GUI-1/demo_2.py: -------------------------------------------------------------------------------- 1 | # Customizing root and Label + geometry 2 | 3 | from tkinter import * 4 | 5 | root = Tk() 6 | 7 | root.title('My App') 8 | 9 | root.geometry('300x300') 10 | 11 | root.configure(background='blue') 12 | 13 | hwL = Label(root) 14 | 15 | hwL.configure(text='Hello World!!', bg='blue',\ 16 | font='Times 25 bold underline') 17 | 18 | hwL.pack() 19 | 20 | root.mainloop() 21 | 22 | 23 | # *************************************************************** 24 | # geometry('widhtxheight') -> 'x' is lower case, dont put spaces in between 25 | 26 | # *************************************************************** -------------------------------------------------------------------------------- /15. GUI-1/demo_3.py: -------------------------------------------------------------------------------- 1 | # Playing with geometry(), pack() and grid() 2 | 3 | from tkinter import * 4 | 5 | root = Tk() 6 | 7 | root.title('My App') 8 | 9 | root.configure(background='blue') 10 | 11 | hwL = Label(root, text='Hello World!!', bg='yellow', \ 12 | font='Times 25 bold underline') 13 | 14 | randomL = Label(root) 15 | 16 | randomL.configure(text='We are learning tkinter') 17 | 18 | nextL = Label(root, text='Where?', bg='red') 19 | 20 | # hwL.pack(side=LEFT, fill=Y) 21 | # 22 | # randomL.pack(fill=X) 23 | # 24 | # nextL.pack(side=BOTTOM) 25 | 26 | hwL.grid(row=0, column=0) 27 | randomL.grid(row=0, column=1) 28 | nextL.grid(row=1, column=1, sticky=W) 29 | 30 | root.mainloop() 31 | 32 | 33 | # *************************************************************** 34 | # pack() 35 | 36 | # hwL.pack(side=LEFT) 37 | # 38 | # randomL.pack(side=LEFT) 39 | # side->TOP(default), BOTTOM, LEFT, RIGHT 40 | # 41 | # hwL.pack(fill=X) -> 'X' is upper case 42 | 43 | # *************************************************************** 44 | # grid() 45 | 46 | # hwL.grid(row=0, column=0, sticky=E) 47 | # sticky->E,W,N,S 48 | # 49 | # randomL.grid(row=1, column=1) 50 | 51 | 52 | -------------------------------------------------------------------------------- /15. GUI-1/demo_4.py: -------------------------------------------------------------------------------- 1 | # Entry(), Button(), CheckButton(), Listbox() 2 | 3 | from tkinter import * 4 | 5 | root = Tk() 6 | root.title('My App') 7 | root.configure(background='blue') 8 | 9 | userL = Label(root, text='User Name: ') 10 | passL = Label(root, text='Password: ') 11 | userL.pack() 12 | passL.pack() 13 | 14 | userE = Entry(root) 15 | passE = Entry(root, show='*') 16 | userE.pack() 17 | passE.pack() 18 | 19 | countryLb = Listbox(root) 20 | countryLb.insert(1, 'India') 21 | countryLb.insert(2, 'Sri Lanka') 22 | countryLb.insert(3, 'Bangladesh') 23 | countryLb.insert(4, 'Nepal') 24 | countryLb.insert(5, 'Bhutan') 25 | countryLb.insert(6, 'Pakistan') 26 | countryLb.insert(7, 'China') 27 | countryLb.pack() 28 | 29 | rememberCb = Checkbutton(root, text='Remember this login') 30 | rememberCb.pack() 31 | 32 | submitB = Button(root, text='Submit', bg='green',\ 33 | activebackground='yellow', \ 34 | activeforeground='white') 35 | exitB = Button(root, text='exit', width=25, \ 36 | command=root.destroy) 37 | submitB.pack() 38 | exitB.pack() 39 | 40 | root.mainloop() 41 | -------------------------------------------------------------------------------- /15. GUI-1/demo_5.py: -------------------------------------------------------------------------------- 1 | # login layout 2 | 3 | from tkinter import * 4 | 5 | root = Tk() 6 | root.title('My App') 7 | root.configure(background='#EEEEEE') 8 | 9 | userL = Label(root, text='User Name: ') 10 | passL = Label(root, text='Password: ') 11 | userL.grid(row=1, column=0, sticky=E) 12 | passL.grid(row=2, column=0, sticky=E) 13 | 14 | userE = Entry(root) 15 | passE = Entry(root, show='*') 16 | userE.grid(row=1, column=1) 17 | passE.grid(row=2, column=1) 18 | 19 | rememberCb = Checkbutton(root, text='Remember this login') 20 | rememberCb.grid(row=3, columnspan=2) 21 | 22 | submitB = Button(root, text='Submit', bg='green',\ 23 | activebackground='yellow', \ 24 | activeforeground='white') 25 | exitB = Button(root, text='Log Out', \ 26 | command=root.destroy) 27 | submitB.grid(row=4, column=1, sticky=W) 28 | exitB.grid(row=4, column=1, sticky=E) 29 | 30 | root.mainloop() 31 | -------------------------------------------------------------------------------- /15. GUI-1/demo_6.py: -------------------------------------------------------------------------------- 1 | from tkinter import * 2 | 3 | root = Tk() 4 | 5 | C = Canvas(root, bg='yellow') 6 | 7 | coord = 10, 50, 240, 210 8 | arc = C.create_arc(coord, start=0, extent=300, fill="red") 9 | 10 | C.pack() 11 | root.mainloop() 12 | 13 | # create_arc 14 | # create_image 15 | # create_line 16 | # create_oval 17 | # create_polygon 18 | -------------------------------------------------------------------------------- /15. GUI-1/demo_7.py: -------------------------------------------------------------------------------- 1 | # Understanding the layout 2 | 3 | from tkinter import * 4 | 5 | root = Tk() 6 | 7 | root.title('My App') 8 | 9 | root.configure(background='blue') 10 | 11 | lbl_1L = Label(root, text='This is Label 1', bg='red', \ 12 | font='none 30') 13 | 14 | lbl_1L.pack(fill=Y, side=LEFT) 15 | 16 | lbl_2L = Label(root, text='This is Label 2', bg='white', \ 17 | font='none 30') 18 | 19 | lbl_2L.pack(fill=X) 20 | 21 | root.mainloop() 22 | 23 | # try lbl_1 side=LEFT, lbl_2 side=BOTTOM 24 | -------------------------------------------------------------------------------- /15. GUI-1/demo_8.py: -------------------------------------------------------------------------------- 1 | # Working with Frames 2 | 3 | from tkinter import * 4 | 5 | root = Tk() 6 | 7 | root.title('My App') 8 | 9 | root.configure(background='blue') 10 | 11 | frame_1 = Frame(root, bg='black') 12 | 13 | frame_1.pack(fill=X) 14 | 15 | frame_2 = Frame(root, bg='brown') 16 | 17 | frame_2.pack(fill=X, side=BOTTOM) 18 | 19 | lbl_1L = Label(frame_1, text='This is Label 1', bg='red') 20 | 21 | lbl_1L.pack() 22 | 23 | lbl_2L = Label(frame_2, text='This is Label 2', bg='white') 24 | 25 | lbl_2L.pack() 26 | 27 | 28 | root.mainloop() 29 | 30 | # Change label container to root 31 | -------------------------------------------------------------------------------- /15. GUI-1/login_page.py: -------------------------------------------------------------------------------- 1 | from tkinter import * 2 | 3 | 4 | def CheckLogin(): 5 | with open('creds.txt') as f: 6 | data = f.readlines() 7 | uname = data[0].rstrip() 8 | pword = data[1].rstrip() 9 | 10 | if userE.get() == uname and passE.get() == pword: 11 | r = Tk() 12 | r.title(':D') 13 | r.geometry('150x50') 14 | rlbl = Label(r, text='\n[+] Logged In') 15 | rlbl.pack() 16 | r.mainloop() 17 | else: 18 | r = Tk() 19 | r.title('D:') 20 | r.geometry('150x50') 21 | rlbl = Label(r, text='\n[!] Invalid Login') 22 | rlbl.pack() 23 | r.mainloop() 24 | 25 | 26 | root = Tk() 27 | root.title('Login Page') 28 | root.configure(background='#EEEEEE') 29 | 30 | userL = Label(root, text='User Name: ') 31 | passL = Label(root, text='Password: ') 32 | userL.grid(row=0, column=0) 33 | passL.grid(row=1, column=0) 34 | 35 | userE = Entry(root) 36 | passE = Entry(root, show='*') 37 | userE.grid(row=0, column=1) 38 | passE.grid(row=1, column=1) 39 | 40 | loginB = Button(root, text='Login', command=CheckLogin) 41 | loginB.grid(row=2, column=0) 42 | 43 | root.mainloop() 44 | 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /16. GUI-2/backend.py: -------------------------------------------------------------------------------- 1 | import pymysql as pm 2 | 3 | def connectdb(): 4 | try: 5 | con = pm.connect(host='localhost', database='books', \ 6 | user='root', password='root') 7 | cursor = con.cursor() 8 | query = 'create table if not exist book(id int(5) primary key, \ 9 | title varchar(10), author varchar(10), year int(4), isbn int(20))' 10 | cursor.execute(query) 11 | con.commit() 12 | 13 | except pm.DatabaseError as e: 14 | if con: 15 | con.rollback() 16 | print('Problem occured: ', e) 17 | 18 | finally: 19 | if cursor: 20 | cursor.close() 21 | if con: 22 | con.close() 23 | 24 | def insert(title, author, year, isbn): 25 | try: 26 | con = pm.connect(host='localhost', database='books', \ 27 | user='root', password='root') 28 | cursor = con.cursor() 29 | query = 'insert into book values (NULL, ?, ?, ?, ?)',(title, author, year, isbn) 30 | cursor.execute(query) 31 | con.commit() 32 | 33 | except pm.DatabaseError as e: 34 | if con: 35 | con.rollback() 36 | print('Problem occured: ', e) 37 | 38 | finally: 39 | if cursor: 40 | cursor.close() 41 | if con: 42 | con.close() 43 | 44 | def view(): 45 | try: 46 | con = pm.connect(host='localhost', database='books', \ 47 | user='root', password='root') 48 | cursor = con.cursor() 49 | query = 'select * from book' 50 | cursor.execute(query) 51 | data = cursor.fetchall() 52 | return data 53 | except pm.DatabaseError as e: 54 | if con: 55 | con.rollback() 56 | print('Problem occured: ', e) 57 | 58 | finally: 59 | if cursor: 60 | cursor.close() 61 | if con: 62 | con.close() 63 | 64 | def search(title='', author='', year='', isbn=''): 65 | try: 66 | con = pm.connect(host='localhost', database='books', \ 67 | user='root', password='root') 68 | cursor = con.cursor() 69 | query = 'select * from book where title=? OR author=? OR year=? OR isbn=?',(title, author, year, isbn) 70 | cursor.execute(query) 71 | data = cursor.fetchall() 72 | return data 73 | except pm.DatabaseError as e: 74 | if con: 75 | con.rollback() 76 | print('Problem occured: ', e) 77 | 78 | finally: 79 | if cursor: 80 | cursor.close() 81 | if con: 82 | con.close() 83 | 84 | def delete(id): 85 | try: 86 | con = pm.connect(host='localhost', database='books', \ 87 | user='root', password='root') 88 | cursor = con.cursor() 89 | query = 'delete from book where id=?',(id,) 90 | cursor.execute(query) 91 | except pm.DatabaseError as e: 92 | if con: 93 | con.rollback() 94 | print('Problem occured: ', e) 95 | 96 | finally: 97 | if cursor: 98 | cursor.close() 99 | if con: 100 | con.close() 101 | 102 | def update(id, title, author, year, isbn): 103 | try: 104 | con = pm.connect(host='localhost', database='books', \ 105 | user='root', password='root') 106 | cursor = con.cursor() 107 | query = 'update book set title=?, author=?, year=?, isbn=? where id=?',(title, author, year, isbn) 108 | cursor.execute(query) 109 | except pm.DatabaseError as e: 110 | if con: 111 | con.rollback() 112 | print('Problem occured: ', e) 113 | 114 | finally: 115 | if cursor: 116 | cursor.close() 117 | if con: 118 | con.close() -------------------------------------------------------------------------------- /16. GUI-2/demo_1.py: -------------------------------------------------------------------------------- 1 | from tkinter import * 2 | 3 | root = Tk() 4 | 5 | textT = Text(root) 6 | 7 | textT.configure(height=10, width=20) 8 | 9 | textT.pack() 10 | 11 | textT.insert(END, 'hi\n') 12 | 13 | textT.insert(END, 'how are you') 14 | 15 | textT.insert(1.0, 'hello, ') 16 | 17 | root.mainloop() 18 | -------------------------------------------------------------------------------- /16. GUI-2/demo_2.py: -------------------------------------------------------------------------------- 1 | from tkinter import * 2 | 3 | root = Tk() 4 | 5 | menuM = Menu(root) 6 | root.configure(menu=menuM) 7 | 8 | fileM = Menu(menuM) 9 | menuM.add_cascade(label='File', menu=fileM) 10 | fileM.add_command(label='New') 11 | fileM.add_command(label='Open...') 12 | fileM.add_separator() 13 | fileM.add_command(label='Exit', command=root.destroy) 14 | 15 | helpM = Menu(menuM) 16 | menuM.add_cascade(label='Help', menu=helpM) 17 | helpM.add_command(label='About') 18 | 19 | root.mainloop() 20 | -------------------------------------------------------------------------------- /16. GUI-2/demo_3.py: -------------------------------------------------------------------------------- 1 | from tkinter import * 2 | 3 | root = Tk() 4 | 5 | messageM = Message(root) 6 | messageM.configure(text='I am writing this message', \ 7 | bg='lightgreen') 8 | messageM.pack() 9 | root.mainloop() 10 | -------------------------------------------------------------------------------- /16. GUI-2/demo_4.py: -------------------------------------------------------------------------------- 1 | from tkinter import * 2 | 3 | 4 | def fun(): 5 | if v.get() == 1: 6 | lblL.configure(text='You pressed radiobutton 1') 7 | else: 8 | lblL.configure(text='You pressed radiobutton 2') 9 | 10 | 11 | root = Tk() 12 | 13 | v = IntVar() 14 | Radiobutton(root, text='DIT', variable=v, \ 15 | value=1, command=fun).pack(anchor=W) 16 | Radiobutton(root, text='UIT', variable=v, \ 17 | value=2, command=fun).pack(anchor=W) 18 | 19 | lblL = Label(root) 20 | lblL.pack() 21 | 22 | root.mainloop() 23 | -------------------------------------------------------------------------------- /16. GUI-2/demo_5.py: -------------------------------------------------------------------------------- 1 | # It is used to provide a graphical slider that allows to select any value from that scale. 2 | 3 | from tkinter import * 4 | 5 | root = Tk() 6 | 7 | scale1 = Scale(root, from_=0, to=50) 8 | scale1.pack() 9 | 10 | scale2 = Scale(root, from_=0, to=50, \ 11 | orient=HORIZONTAL) 12 | scale2.pack() 13 | 14 | 15 | root.mainloop() 16 | -------------------------------------------------------------------------------- /16. GUI-2/demo_6.py: -------------------------------------------------------------------------------- 1 | from tkinter import * 2 | 3 | root = Tk() 4 | 5 | sb = Scrollbar(root) 6 | sb.pack(side=RIGHT, fill=Y) 7 | mylist = Listbox(root, yscrollcommand=sb.set) 8 | for line in range(100): 9 | mylist.insert(END, 'Hi '+str(line)) 10 | mylist.pack(side=LEFT, fill=Y) 11 | sb.configure(command=mylist.yview) 12 | 13 | root.mainloop() 14 | -------------------------------------------------------------------------------- /16. GUI-2/demo_7.py: -------------------------------------------------------------------------------- 1 | from tkinter import * 2 | 3 | root = Tk() 4 | 5 | root.title('My App') 6 | top = Toplevel(root) 7 | top.title('Python') 8 | 9 | root.mainloop() 10 | -------------------------------------------------------------------------------- /16. GUI-2/demo_8.py: -------------------------------------------------------------------------------- 1 | from tkinter import * 2 | 3 | root = Tk() 4 | 5 | w = Spinbox(root, from_=0, to=10) 6 | w.pack() 7 | 8 | root.mainloop() 9 | -------------------------------------------------------------------------------- /16. GUI-2/demo_9.py: -------------------------------------------------------------------------------- 1 | from tkinter import * 2 | 3 | dict= {} 4 | root = Tk() 5 | 6 | 7 | def fun(): 8 | k = e1.get() 9 | v = e2.get() 10 | dict[k]=v 11 | lbl3.configure(text=k) 12 | lbl4.configure(text=v) 13 | 14 | 15 | lbl1 = Label(root, text='Key') 16 | lbl1.grid(row=0, column=0) 17 | lbl2 = Label(root, text='Value') 18 | lbl2.grid(row=1, column=0) 19 | 20 | e1 = Entry(root) 21 | e1.grid(row=0, column=1) 22 | 23 | e2 = Entry(root) 24 | e2.grid(row=1, column=1) 25 | 26 | add = Button(root, text='ADD IT', command=fun) 27 | add.grid(row=2, column=1) 28 | 29 | lbl3 = Label(root) 30 | lbl3.grid(row=3, column=0) 31 | 32 | lbl4 = Label(root) 33 | lbl4.grid(row=3, column=1) 34 | root.mainloop() -------------------------------------------------------------------------------- /16. GUI-2/frontend.py: -------------------------------------------------------------------------------- 1 | from tkinter import * 2 | 3 | root = Tk() 4 | 5 | # define Labels 6 | l1 = Label(root, text='Title') 7 | l1.grid(row=0, column=0) 8 | 9 | l1 = Label(root, text='Author') 10 | l1.grid(row=0, column=2) 11 | 12 | l1 = Label(root, text='Year') 13 | l1.grid(row=1, column=0) 14 | 15 | l1 = Label(root, text='ISBN') 16 | l1.grid(row=1, column=2) 17 | 18 | # define Entries 19 | title_text=StringVar() 20 | e1=Entry(root,textvariable=title_text) 21 | e1.grid(row=0, column=1) 22 | 23 | author_text=StringVar() 24 | e2=Entry(root,textvariable=author_text) 25 | e2.grid(row=0, column=3) 26 | 27 | year_text=StringVar() 28 | e3=Entry(root,textvariable=year_text) 29 | e3.grid(row=1, column=1) 30 | 31 | isbn_text=StringVar() 32 | e4=Entry(root,textvariable=isbn_text) 33 | e4.grid(row=1, column=3) 34 | 35 | # define Listbox 36 | lstbox = Listbox(root, height=6, width=35) 37 | lstbox.grid(row=2, column=0, rowspan=6, columnspan=2) 38 | 39 | # add scrollbar to listbox 40 | sb = Scrollbar(root) 41 | sb.grid(row=2, column=2, rowspan=6) 42 | 43 | lstbox.configure(yscrollcommand=sb.set) 44 | sb.configure(command=lstbox.yview) 45 | 46 | # define Buttons 47 | b1 = Button(root, text='View All', width=12) 48 | b1.grid(row=2, column=3) 49 | 50 | b1 = Button(root, text='Search Entry', width=12) 51 | b1.grid(row=3, column=3) 52 | 53 | b1 = Button(root, text='Add Entry', width=12) 54 | b1.grid(row=4, column=3) 55 | 56 | b1 = Button(root, text='Update Selected', width=12) 57 | b1.grid(row=5, column=3) 58 | 59 | b1 = Button(root, text='Delete Selected', width=12) 60 | b1.grid(row=6, column=3) 61 | 62 | b1 = Button(root, text='Close', width=12) 63 | b1.grid(row=7, column=3) 64 | 65 | root.mainloop() 66 | -------------------------------------------------------------------------------- /16. GUI-2/search.py: -------------------------------------------------------------------------------- 1 | ''' 2 | CREDITS: https://github.com/CristiVlad25/py-scripts-other/blob/master/tkapp5.py 3 | 4 | YOUTUBE: https://www.youtube.com/playlist?list=PLonlF40eS6nyjI_OlR21se8aE1LTtNH1g 5 | ''' 6 | 7 | import tkinter as tk 8 | from tkinter import ttk 9 | import webbrowser 10 | from tkinter import * 11 | 12 | root = tk.Tk() 13 | root.title('Universal Search Bar') 14 | 15 | label1 = ttk.Label(root, text='Query') 16 | label1.grid(row=0, column=0) 17 | entry1 = ttk.Entry(root, width=50) 18 | entry1.grid(row=0, column=1) 19 | 20 | btn2 = StringVar() 21 | 22 | 23 | def callback(): 24 | if btn2.get() == 'google': 25 | webbrowser.open('http://google.com/search?q=' + entry1.get()) 26 | elif btn2.get() == 'duck': 27 | webbrowser.open('http://duckduckgo.com/?q=' + entry1.get()) 28 | 29 | 30 | # def get(event): 31 | # if btn2.get() == 'google': 32 | # webbrowser.open('http://google.com/search?q=' + entry1.get()) 33 | # elif btn2.get() == 'duck': 34 | # webbrowser.open('http://duckduckgo.com/?q=' + entry1.get()) 35 | 36 | 37 | MyButton1 = ttk.Button(root, text='Search', width=10, command=callback) 38 | MyButton1.grid(row=0, column=2) 39 | 40 | # entry1.bind('', get) 41 | 42 | MyButton2 = ttk.Radiobutton(root, text='Google', value='google', variable=btn2) 43 | MyButton2.grid(row=1, column=1, sticky=W) 44 | 45 | MyButton3 = ttk.Radiobutton(root, text='Duck', value='duck', variable=btn2) 46 | MyButton3.grid(row=1, column=1, sticky=E) 47 | 48 | entry1.focus() 49 | 50 | root.mainloop() 51 | -------------------------------------------------------------------------------- /18. Pandas/1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Singh-K01/Python-tutorials-Kanav-Bansal/05065cb71ab0a5b9ff17b5befe485294c8395d47/18. Pandas/1.jpg -------------------------------------------------------------------------------- /18. Pandas/Iris.csv: -------------------------------------------------------------------------------- 1 | Id,SepalLengthCm,SepalWidthCm,PetalLengthCm,PetalWidthCm,Species 2 | 1,5.1,3.5,1.4,0.2,Iris-setosa 3 | 2,4.9,3.0,1.4,0.2,Iris-setosa 4 | 3,4.7,3.2,1.3,0.2,Iris-setosa 5 | 4,4.6,3.1,1.5,0.2,Iris-setosa 6 | 5,5.0,3.6,1.4,0.2,Iris-setosa 7 | 6,5.4,3.9,1.7,0.4,Iris-setosa 8 | 7,4.6,3.4,1.4,0.3,Iris-setosa 9 | 8,5.0,3.4,1.5,0.2,Iris-setosa 10 | 9,4.4,2.9,1.4,0.2,Iris-setosa 11 | 10,4.9,3.1,1.5,0.1,Iris-setosa 12 | 11,5.4,3.7,1.5,0.2,Iris-setosa 13 | 12,4.8,3.4,1.6,0.2,Iris-setosa 14 | 13,4.8,3.0,1.4,0.1,Iris-setosa 15 | 14,4.3,3.0,1.1,0.1,Iris-setosa 16 | 15,5.8,4.0,1.2,0.2,Iris-setosa 17 | 16,5.7,4.4,1.5,0.4,Iris-setosa 18 | 17,5.4,3.9,1.3,0.4,Iris-setosa 19 | 18,5.1,3.5,1.4,0.3,Iris-setosa 20 | 19,5.7,3.8,1.7,0.3,Iris-setosa 21 | 20,5.1,3.8,1.5,0.3,Iris-setosa 22 | 21,5.4,3.4,1.7,0.2,Iris-setosa 23 | 22,5.1,3.7,1.5,0.4,Iris-setosa 24 | 23,4.6,3.6,1.0,0.2,Iris-setosa 25 | 24,5.1,3.3,1.7,0.5,Iris-setosa 26 | 25,4.8,3.4,1.9,0.2,Iris-setosa 27 | 26,5.0,3.0,1.6,0.2,Iris-setosa 28 | 27,5.0,3.4,1.6,0.4,Iris-setosa 29 | 28,5.2,3.5,1.5,0.2,Iris-setosa 30 | 29,5.2,3.4,1.4,0.2,Iris-setosa 31 | 30,4.7,3.2,1.6,0.2,Iris-setosa 32 | 31,4.8,3.1,1.6,0.2,Iris-setosa 33 | 32,5.4,3.4,1.5,0.4,Iris-setosa 34 | 33,5.2,4.1,1.5,0.1,Iris-setosa 35 | 34,5.5,4.2,1.4,0.2,Iris-setosa 36 | 35,4.9,3.1,1.5,0.1,Iris-setosa 37 | 36,5.0,3.2,1.2,0.2,Iris-setosa 38 | 37,5.5,3.5,1.3,0.2,Iris-setosa 39 | 38,4.9,3.1,1.5,0.1,Iris-setosa 40 | 39,4.4,3.0,1.3,0.2,Iris-setosa 41 | 40,5.1,3.4,1.5,0.2,Iris-setosa 42 | 41,5.0,3.5,1.3,0.3,Iris-setosa 43 | 42,4.5,2.3,1.3,0.3,Iris-setosa 44 | 43,4.4,3.2,1.3,0.2,Iris-setosa 45 | 44,5.0,3.5,1.6,0.6,Iris-setosa 46 | 45,5.1,3.8,1.9,0.4,Iris-setosa 47 | 46,4.8,3.0,1.4,0.3,Iris-setosa 48 | 47,5.1,3.8,1.6,0.2,Iris-setosa 49 | 48,4.6,3.2,1.4,0.2,Iris-setosa 50 | 49,5.3,3.7,1.5,0.2,Iris-setosa 51 | 50,5.0,3.3,1.4,0.2,Iris-setosa 52 | 51,7.0,3.2,4.7,1.4,Iris-versicolor 53 | 52,6.4,3.2,4.5,1.5,Iris-versicolor 54 | 53,6.9,3.1,4.9,1.5,Iris-versicolor 55 | 54,5.5,2.3,4.0,1.3,Iris-versicolor 56 | 55,6.5,2.8,4.6,1.5,Iris-versicolor 57 | 56,5.7,2.8,4.5,1.3,Iris-versicolor 58 | 57,6.3,3.3,4.7,1.6,Iris-versicolor 59 | 58,4.9,2.4,3.3,1.0,Iris-versicolor 60 | 59,6.6,2.9,4.6,1.3,Iris-versicolor 61 | 60,5.2,2.7,3.9,1.4,Iris-versicolor 62 | 61,5.0,2.0,3.5,1.0,Iris-versicolor 63 | 62,5.9,3.0,4.2,1.5,Iris-versicolor 64 | 63,6.0,2.2,4.0,1.0,Iris-versicolor 65 | 64,6.1,2.9,4.7,1.4,Iris-versicolor 66 | 65,5.6,2.9,3.6,1.3,Iris-versicolor 67 | 66,6.7,3.1,4.4,1.4,Iris-versicolor 68 | 67,5.6,3.0,4.5,1.5,Iris-versicolor 69 | 68,5.8,2.7,4.1,1.0,Iris-versicolor 70 | 69,6.2,2.2,4.5,1.5,Iris-versicolor 71 | 70,5.6,2.5,3.9,1.1,Iris-versicolor 72 | 71,5.9,3.2,4.8,1.8,Iris-versicolor 73 | 72,6.1,2.8,4.0,1.3,Iris-versicolor 74 | 73,6.3,2.5,4.9,1.5,Iris-versicolor 75 | 74,6.1,2.8,4.7,1.2,Iris-versicolor 76 | 75,6.4,2.9,4.3,1.3,Iris-versicolor 77 | 76,6.6,3.0,4.4,1.4,Iris-versicolor 78 | 77,6.8,2.8,4.8,1.4,Iris-versicolor 79 | 78,6.7,3.0,5.0,1.7,Iris-versicolor 80 | 79,6.0,2.9,4.5,1.5,Iris-versicolor 81 | 80,5.7,2.6,3.5,1.0,Iris-versicolor 82 | 81,5.5,2.4,3.8,1.1,Iris-versicolor 83 | 82,5.5,2.4,3.7,1.0,Iris-versicolor 84 | 83,5.8,2.7,3.9,1.2,Iris-versicolor 85 | 84,6.0,2.7,5.1,1.6,Iris-versicolor 86 | 85,5.4,3.0,4.5,1.5,Iris-versicolor 87 | 86,6.0,3.4,4.5,1.6,Iris-versicolor 88 | 87,6.7,3.1,4.7,1.5,Iris-versicolor 89 | 88,6.3,2.3,4.4,1.3,Iris-versicolor 90 | 89,5.6,3.0,4.1,1.3,Iris-versicolor 91 | 90,5.5,2.5,4.0,1.3,Iris-versicolor 92 | 91,5.5,2.6,4.4,1.2,Iris-versicolor 93 | 92,6.1,3.0,4.6,1.4,Iris-versicolor 94 | 93,5.8,2.6,4.0,1.2,Iris-versicolor 95 | 94,5.0,2.3,3.3,1.0,Iris-versicolor 96 | 95,5.6,2.7,4.2,1.3,Iris-versicolor 97 | 96,5.7,3.0,4.2,1.2,Iris-versicolor 98 | 97,5.7,2.9,4.2,1.3,Iris-versicolor 99 | 98,6.2,2.9,4.3,1.3,Iris-versicolor 100 | 99,5.1,2.5,3.0,1.1,Iris-versicolor 101 | 100,5.7,2.8,4.1,1.3,Iris-versicolor 102 | 101,6.3,3.3,6.0,2.5,Iris-virginica 103 | 102,5.8,2.7,5.1,1.9,Iris-virginica 104 | 103,7.1,3.0,5.9,2.1,Iris-virginica 105 | 104,6.3,2.9,5.6,1.8,Iris-virginica 106 | 105,6.5,3.0,5.8,2.2,Iris-virginica 107 | 106,7.6,3.0,6.6,2.1,Iris-virginica 108 | 107,4.9,2.5,4.5,1.7,Iris-virginica 109 | 108,7.3,2.9,6.3,1.8,Iris-virginica 110 | 109,6.7,2.5,5.8,1.8,Iris-virginica 111 | 110,7.2,3.6,6.1,2.5,Iris-virginica 112 | 111,6.5,3.2,5.1,2.0,Iris-virginica 113 | 112,6.4,2.7,5.3,1.9,Iris-virginica 114 | 113,6.8,3.0,5.5,2.1,Iris-virginica 115 | 114,5.7,2.5,5.0,2.0,Iris-virginica 116 | 115,5.8,2.8,5.1,2.4,Iris-virginica 117 | 116,6.4,3.2,5.3,2.3,Iris-virginica 118 | 117,6.5,3.0,5.5,1.8,Iris-virginica 119 | 118,7.7,3.8,6.7,2.2,Iris-virginica 120 | 119,7.7,2.6,6.9,2.3,Iris-virginica 121 | 120,6.0,2.2,5.0,1.5,Iris-virginica 122 | 121,6.9,3.2,5.7,2.3,Iris-virginica 123 | 122,5.6,2.8,4.9,2.0,Iris-virginica 124 | 123,7.7,2.8,6.7,2.0,Iris-virginica 125 | 124,6.3,2.7,4.9,1.8,Iris-virginica 126 | 125,6.7,3.3,5.7,2.1,Iris-virginica 127 | 126,7.2,3.2,6.0,1.8,Iris-virginica 128 | 127,6.2,2.8,4.8,1.8,Iris-virginica 129 | 128,6.1,3.0,4.9,1.8,Iris-virginica 130 | 129,6.4,2.8,5.6,2.1,Iris-virginica 131 | 130,7.2,3.0,5.8,1.6,Iris-virginica 132 | 131,7.4,2.8,6.1,1.9,Iris-virginica 133 | 132,7.9,3.8,6.4,2.0,Iris-virginica 134 | 133,6.4,2.8,5.6,2.2,Iris-virginica 135 | 134,6.3,2.8,5.1,1.5,Iris-virginica 136 | 135,6.1,2.6,5.6,1.4,Iris-virginica 137 | 136,7.7,3.0,6.1,2.3,Iris-virginica 138 | 137,6.3,3.4,5.6,2.4,Iris-virginica 139 | 138,6.4,3.1,5.5,1.8,Iris-virginica 140 | 139,6.0,3.0,4.8,1.8,Iris-virginica 141 | 140,6.9,3.1,5.4,2.1,Iris-virginica 142 | 141,6.7,3.1,5.6,2.4,Iris-virginica 143 | 142,6.9,3.1,5.1,2.3,Iris-virginica 144 | 143,5.8,2.7,5.1,1.9,Iris-virginica 145 | 144,6.8,3.2,5.9,2.3,Iris-virginica 146 | 145,6.7,3.3,5.7,2.5,Iris-virginica 147 | 146,6.7,3.0,5.2,2.3,Iris-virginica 148 | 147,6.3,2.5,5.0,1.9,Iris-virginica 149 | 148,6.5,3.0,5.2,2.0,Iris-virginica 150 | 149,6.2,3.4,5.4,2.3,Iris-virginica 151 | 150,5.9,3.0,5.1,1.8,Iris-virginica 152 | -------------------------------------------------------------------------------- /2. Data Types - 1/data_type_1.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Numeric Data Type" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 2, 13 | "metadata": { 14 | "ExecuteTime": { 15 | "end_time": "2018-05-23T07:02:10.569790Z", 16 | "start_time": "2018-05-23T07:02:10.565176Z" 17 | } 18 | }, 19 | "outputs": [ 20 | { 21 | "name": "stdout", 22 | "output_type": "stream", 23 | "text": [ 24 | "\n", 25 | "\n", 26 | "True\n" 27 | ] 28 | } 29 | ], 30 | "source": [ 31 | "a = 5\n", 32 | "\n", 33 | "print(type(a))\n", 34 | "\n", 35 | "print(type(5.0))\n", 36 | "\n", 37 | "print(isinstance(a, int))\n" 38 | ] 39 | }, 40 | { 41 | "cell_type": "code", 42 | "execution_count": 3, 43 | "metadata": { 44 | "ExecuteTime": { 45 | "end_time": "2018-05-23T07:03:17.763527Z", 46 | "start_time": "2018-05-23T07:03:17.759015Z" 47 | } 48 | }, 49 | "outputs": [ 50 | { 51 | "name": "stdout", 52 | "output_type": "stream", 53 | "text": [ 54 | "\n" 55 | ] 56 | } 57 | ], 58 | "source": [ 59 | "b = 10.34\n", 60 | "print(type(b))" 61 | ] 62 | }, 63 | { 64 | "cell_type": "code", 65 | "execution_count": 6, 66 | "metadata": { 67 | "ExecuteTime": { 68 | "end_time": "2018-05-23T07:06:33.991483Z", 69 | "start_time": "2018-05-23T07:06:33.987472Z" 70 | } 71 | }, 72 | "outputs": [ 73 | { 74 | "name": "stdout", 75 | "output_type": "stream", 76 | "text": [ 77 | "1.2e-37\n" 78 | ] 79 | } 80 | ], 81 | "source": [ 82 | "f = 1.2e-37\n", 83 | "# f= 1.23e34 and f= 1.23e+34 are equivalent\n", 84 | "print(f)" 85 | ] 86 | }, 87 | { 88 | "cell_type": "code", 89 | "execution_count": 11, 90 | "metadata": { 91 | "ExecuteTime": { 92 | "end_time": "2018-05-23T07:10:41.100468Z", 93 | "start_time": "2018-05-23T07:10:41.095947Z" 94 | } 95 | }, 96 | "outputs": [ 97 | { 98 | "name": "stdout", 99 | "output_type": "stream", 100 | "text": [ 101 | "\n", 102 | "(15+20j)\n", 103 | "True\n" 104 | ] 105 | } 106 | ], 107 | "source": [ 108 | "c = 10+20J\n", 109 | "\n", 110 | "print(type(c))\n", 111 | "\n", 112 | "print(c + 5)\n", 113 | "\n", 114 | "print(isinstance(c, complex))" 115 | ] 116 | }, 117 | { 118 | "cell_type": "code", 119 | "execution_count": 2, 120 | "metadata": { 121 | "ExecuteTime": { 122 | "end_time": "2018-05-23T07:23:11.656494Z", 123 | "start_time": "2018-05-23T07:23:08.834838Z" 124 | } 125 | }, 126 | "outputs": [ 127 | { 128 | "name": "stdout", 129 | "output_type": "stream", 130 | "text": [ 131 | "Enter 1st number: 12\n", 132 | "Enter 2nd number: 12\n", 133 | "24\n" 134 | ] 135 | } 136 | ], 137 | "source": [ 138 | "a = input('Enter 1st number: ')\n", 139 | "\n", 140 | "b = input('Enter 2nd number: ')\n", 141 | "\n", 142 | "a1 = int(a)\n", 143 | "\n", 144 | "b1 = int(b)\n", 145 | "\n", 146 | "print(a1+b1)" 147 | ] 148 | }, 149 | { 150 | "cell_type": "markdown", 151 | "metadata": {}, 152 | "source": [ 153 | "## Strings" 154 | ] 155 | }, 156 | { 157 | "cell_type": "code", 158 | "execution_count": 3, 159 | "metadata": { 160 | "ExecuteTime": { 161 | "end_time": "2018-05-23T07:42:58.277777Z", 162 | "start_time": "2018-05-23T07:42:58.272262Z" 163 | } 164 | }, 165 | "outputs": [ 166 | { 167 | "name": "stdout", 168 | "output_type": "stream", 169 | "text": [ 170 | "a\n", 171 | "\n", 172 | "\n", 173 | "22\n" 174 | ] 175 | } 176 | ], 177 | "source": [ 178 | "c = 'a'\n", 179 | "\n", 180 | "print(c)\n", 181 | "\n", 182 | "print(type(c))\n", 183 | "\n", 184 | "s = 'We are leaning Python!'\n", 185 | "\n", 186 | "print(type(s))\n", 187 | "\n", 188 | "print(len(s))\n", 189 | "\n" 190 | ] 191 | }, 192 | { 193 | "cell_type": "code", 194 | "execution_count": 7, 195 | "metadata": { 196 | "ExecuteTime": { 197 | "end_time": "2018-05-23T07:46:09.270947Z", 198 | "start_time": "2018-05-23T07:46:09.265433Z" 199 | } 200 | }, 201 | "outputs": [ 202 | { 203 | "name": "stdout", 204 | "output_type": "stream", 205 | "text": [ 206 | "i\n", 207 | "hi there\n", 208 | "hi10\n" 209 | ] 210 | } 211 | ], 212 | "source": [ 213 | "s = 'hi'\n", 214 | "\n", 215 | "print(s[1])\n", 216 | "\n", 217 | "# print(s[3])\n", 218 | "\n", 219 | "# print(s[1.0])\n", 220 | "\n", 221 | "print(s+' there')\n", 222 | "\n", 223 | "print(s + '10')\n", 224 | "# try print(s + 10) it will throw an error" 225 | ] 226 | }, 227 | { 228 | "cell_type": "code", 229 | "execution_count": 2, 230 | "metadata": { 231 | "ExecuteTime": { 232 | "end_time": "2018-05-23T14:27:13.259069Z", 233 | "start_time": "2018-05-23T14:27:13.254056Z" 234 | } 235 | }, 236 | "outputs": [ 237 | { 238 | "name": "stdout", 239 | "output_type": "stream", 240 | "text": [ 241 | "\n", 242 | "['', 'We', 'are', 'leaning', 'Python!', '']\n", 243 | " We are leaning Python! \n" 244 | ] 245 | } 246 | ], 247 | "source": [ 248 | "# s = ' We are leaning Python! '\n", 249 | "\n", 250 | "# print(s.upper())\n", 251 | "\n", 252 | "# print(s.lower())\n", 253 | "\n", 254 | "# print(s.strip())\n", 255 | "\n", 256 | "# # isalpha(), isdigit(), isspace()\n", 257 | "# # startswith('abc'), endswith('abc')\n", 258 | "# # find('abc') returns first index, otherwise -1\n", 259 | "# # replace('old', 'new')\n", 260 | "# # split(' ') return list\n", 261 | "# # ' '.join(list) return string\n", 262 | "\n", 263 | "\n", 264 | "s = ' We are leaning Python! '\n", 265 | "split_string = s.split(' ')\n", 266 | "print(type(split_string))\n", 267 | "print(split_string)\n", 268 | "print(' '.join(split_string))" 269 | ] 270 | }, 271 | { 272 | "cell_type": "code", 273 | "execution_count": 4, 274 | "metadata": { 275 | "ExecuteTime": { 276 | "end_time": "2018-05-23T14:28:11.464273Z", 277 | "start_time": "2018-05-23T14:28:11.460263Z" 278 | } 279 | }, 280 | "outputs": [ 281 | { 282 | "name": "stdout", 283 | "output_type": "stream", 284 | "text": [ 285 | "ogra\n", 286 | "programiz\n", 287 | "progr\n", 288 | "amiz\n" 289 | ] 290 | } 291 | ], 292 | "source": [ 293 | "s = 'programiz'\n", 294 | "print(s[-7:-3])\n", 295 | "print(s[:])\n", 296 | "print(s[:5])\n", 297 | "print(s[5:])" 298 | ] 299 | }, 300 | { 301 | "cell_type": "markdown", 302 | "metadata": {}, 303 | "source": [ 304 | "## Lists" 305 | ] 306 | }, 307 | { 308 | "cell_type": "code", 309 | "execution_count": 6, 310 | "metadata": { 311 | "ExecuteTime": { 312 | "end_time": "2018-05-23T14:29:41.062353Z", 313 | "start_time": "2018-05-23T14:29:41.058106Z" 314 | } 315 | }, 316 | "outputs": [ 317 | { 318 | "name": "stdout", 319 | "output_type": "stream", 320 | "text": [ 321 | "['shark', 'cuttlefish', 'squid', 'mantisshrimp', 'anemone']\n", 322 | "\n", 323 | "5\n" 324 | ] 325 | } 326 | ], 327 | "source": [ 328 | "sea_creatures = ['shark', 'cuttlefish', 'squid', 'mantisshrimp', 'anemone']\n", 329 | "\n", 330 | "print(sea_creatures)\n", 331 | "\n", 332 | "print(type(sea_creatures))\n", 333 | "\n", 334 | "print(len(sea_creatures))" 335 | ] 336 | }, 337 | { 338 | "cell_type": "code", 339 | "execution_count": 7, 340 | "metadata": { 341 | "ExecuteTime": { 342 | "end_time": "2018-05-23T14:29:43.585890Z", 343 | "start_time": "2018-05-23T14:29:43.579874Z" 344 | } 345 | }, 346 | "outputs": [ 347 | { 348 | "name": "stdout", 349 | "output_type": "stream", 350 | "text": [ 351 | "shark\n", 352 | "cuttlefish\n", 353 | "squid\n", 354 | "mantisshrimp\n", 355 | "anemone\n", 356 | "********************\n", 357 | "anemone\n", 358 | "mantisshrimp\n", 359 | "squid\n", 360 | "cuttlefish\n", 361 | "shark\n" 362 | ] 363 | } 364 | ], 365 | "source": [ 366 | "print(sea_creatures[0])\n", 367 | "\n", 368 | "print(sea_creatures[1])\n", 369 | "\n", 370 | "print(sea_creatures[2])\n", 371 | "\n", 372 | "print(sea_creatures[3])\n", 373 | "\n", 374 | "print(sea_creatures[4])\n", 375 | "\n", 376 | "# try print(sea_creatures[5]) it will throw out of range error\n", 377 | "\n", 378 | "print('*'*20)\n", 379 | "\n", 380 | "print(sea_creatures[-1])\n", 381 | "\n", 382 | "print(sea_creatures[-2])\n", 383 | "\n", 384 | "print(sea_creatures[-3])\n", 385 | "\n", 386 | "print(sea_creatures[-4])\n", 387 | "\n", 388 | "print(sea_creatures[-5])\n", 389 | "\n", 390 | "# try print(sea_creatures[-6]) it will throw out of range error\n" 391 | ] 392 | }, 393 | { 394 | "cell_type": "code", 395 | "execution_count": 36, 396 | "metadata": { 397 | "ExecuteTime": { 398 | "end_time": "2018-05-23T08:13:31.049201Z", 399 | "start_time": "2018-05-23T08:13:31.044688Z" 400 | } 401 | }, 402 | "outputs": [ 403 | { 404 | "name": "stdout", 405 | "output_type": "stream", 406 | "text": [ 407 | "[10, 'Hi', 20.2, True]\n" 408 | ] 409 | } 410 | ], 411 | "source": [ 412 | "l = []\n", 413 | "\n", 414 | "l.append(10)\n", 415 | "\n", 416 | "l.append('Hi')\n", 417 | "\n", 418 | "l.append(20.20)\n", 419 | "\n", 420 | "l.append(True)\n", 421 | "\n", 422 | "print(l)\n", 423 | "\n", 424 | "'''\n", 425 | "This is a multi line comment demonstrated by using triple single-quote\n", 426 | "\n", 427 | "Above mentioned list 'l' is a collection of heterogeneous data types.\n", 428 | "'''" 429 | ] 430 | }, 431 | { 432 | "cell_type": "code", 433 | "execution_count": 9, 434 | "metadata": { 435 | "ExecuteTime": { 436 | "end_time": "2018-05-23T14:32:13.289116Z", 437 | "start_time": "2018-05-23T14:32:13.285105Z" 438 | } 439 | }, 440 | "outputs": [ 441 | { 442 | "name": "stdout", 443 | "output_type": "stream", 444 | "text": [ 445 | "a\n", 446 | "5\n" 447 | ] 448 | } 449 | ], 450 | "source": [ 451 | "\"\"\"\n", 452 | "This is a multi line comment demonstrated by using triple double-quote\n", 453 | "\n", 454 | "This is an example for nested lists.\n", 455 | "\"\"\"\n", 456 | "\n", 457 | "nest_list = ['Happy', [2, 0, 1, 5]]\n", 458 | "\n", 459 | "print(nest_list[0][1])\n", 460 | "\n", 461 | "print(nest_list[1][3])" 462 | ] 463 | }, 464 | { 465 | "cell_type": "code", 466 | "execution_count": 15, 467 | "metadata": { 468 | "ExecuteTime": { 469 | "end_time": "2018-05-23T14:34:14.069750Z", 470 | "start_time": "2018-05-23T14:34:14.065381Z" 471 | } 472 | }, 473 | "outputs": [ 474 | { 475 | "name": "stdout", 476 | "output_type": "stream", 477 | "text": [ 478 | "['shark', 'cuttlefish', 'squid', 'mantisshrimp', 'anemone']\n", 479 | "['shark', 'octopus', 'squid', 'mantisshrimp', 'anemone']\n", 480 | "['shark', 'octopus', 'blobfish', 'mantisshrimp', 'anemone']\n" 481 | ] 482 | } 483 | ], 484 | "source": [ 485 | "\"\"\"\n", 486 | "Because lists are mutable, below written code will\n", 487 | "overwrite existing list\n", 488 | "\n", 489 | "\"\"\"\n", 490 | "\n", 491 | "sea_creatures = ['shark', 'cuttlefish', 'squid', 'mantisshrimp', 'anemone']\n", 492 | "\n", 493 | "print(sea_creatures)\n", 494 | "\n", 495 | "sea_creatures[1] = 'octopus'\n", 496 | "\n", 497 | "print(sea_creatures)\n", 498 | "\n", 499 | "sea_creatures[-3] = 'blobfish'\n", 500 | "\n", 501 | "print(sea_creatures)" 502 | ] 503 | }, 504 | { 505 | "cell_type": "code", 506 | "execution_count": 16, 507 | "metadata": { 508 | "ExecuteTime": { 509 | "end_time": "2018-05-23T14:35:23.128994Z", 510 | "start_time": "2018-05-23T14:35:23.121951Z" 511 | } 512 | }, 513 | "outputs": [ 514 | { 515 | "name": "stdout", 516 | "output_type": "stream", 517 | "text": [ 518 | "['o', 'g', 'r']\n", 519 | "['p', 'r', 'o', 'g']\n", 520 | "['r', 'a', 'm', 'i', 'z']\n", 521 | "['a', 'm', 'i', 'z']\n", 522 | "['p', 'r', 'o', 'g', 'r', 'a', 'm', 'i', 'z']\n", 523 | "['a', 'm']\n", 524 | "['m', 'i', 'z']\n" 525 | ] 526 | } 527 | ], 528 | "source": [ 529 | "# Below we are demonstrating the list slicing operation.\n", 530 | "\n", 531 | "my_list = ['p', 'r', 'o', 'g', 'r', 'a', 'm', 'i', 'z']\n", 532 | "\n", 533 | "print(my_list[2:5])\n", 534 | "\n", 535 | "print(my_list[:-5])\n", 536 | "\n", 537 | "print(my_list[-5:])\n", 538 | "\n", 539 | "print(my_list[5:])\n", 540 | "\n", 541 | "print(my_list[:])\n", 542 | "\n", 543 | "print(my_list[-4:-2])\n", 544 | "\n", 545 | "print(my_list[-3:])\n" 546 | ] 547 | }, 548 | { 549 | "cell_type": "code", 550 | "execution_count": 70, 551 | "metadata": { 552 | "ExecuteTime": { 553 | "end_time": "2018-05-23T03:22:53.059116Z", 554 | "start_time": "2018-05-23T03:22:53.049688Z" 555 | } 556 | }, 557 | "outputs": [], 558 | "source": [ 559 | "# append(element)\n", 560 | "\n", 561 | "# insert(index, element) This will insert 'element' at the given 'index'\n", 562 | "\n", 563 | "# list1.extend(list2) appends list2 at the end of list1\n", 564 | "\n", 565 | "# list.index(element) \n", 566 | "\n", 567 | "# list.index(element, index) find next element starting at 'index'\n", 568 | "\n", 569 | "# list.remove(element)\n", 570 | "\n", 571 | "# list.sort() here list should contain one kind of data type\n", 572 | "\n", 573 | "# list.reverse()\n", 574 | "\n", 575 | "# list.pop(index)\n", 576 | "\n", 577 | "# list.count(element)" 578 | ] 579 | } 580 | ], 581 | "metadata": { 582 | "kernelspec": { 583 | "display_name": "Python 3", 584 | "language": "python", 585 | "name": "python3" 586 | }, 587 | "language_info": { 588 | "codemirror_mode": { 589 | "name": "ipython", 590 | "version": 3 591 | }, 592 | "file_extension": ".py", 593 | "mimetype": "text/x-python", 594 | "name": "python", 595 | "nbconvert_exporter": "python", 596 | "pygments_lexer": "ipython3", 597 | "version": "3.6.5" 598 | }, 599 | "toc": { 600 | "nav_menu": {}, 601 | "number_sections": true, 602 | "sideBar": true, 603 | "skip_h1_title": false, 604 | "title_cell": "Table of Contents", 605 | "title_sidebar": "Contents", 606 | "toc_cell": false, 607 | "toc_position": {}, 608 | "toc_section_display": true, 609 | "toc_window_display": false 610 | }, 611 | "varInspector": { 612 | "cols": { 613 | "lenName": 16, 614 | "lenType": 16, 615 | "lenVar": 40 616 | }, 617 | "kernels_config": { 618 | "python": { 619 | "delete_cmd_postfix": "", 620 | "delete_cmd_prefix": "del ", 621 | "library": "var_list.py", 622 | "varRefreshCmd": "print(var_dic_list())" 623 | }, 624 | "r": { 625 | "delete_cmd_postfix": ") ", 626 | "delete_cmd_prefix": "rm(", 627 | "library": "var_list.r", 628 | "varRefreshCmd": "cat(var_dic_list()) " 629 | } 630 | }, 631 | "types_to_exclude": [ 632 | "module", 633 | "function", 634 | "builtin_function_or_method", 635 | "instance", 636 | "_Feature" 637 | ], 638 | "window_display": false 639 | } 640 | }, 641 | "nbformat": 4, 642 | "nbformat_minor": 2 643 | } 644 | -------------------------------------------------------------------------------- /3. Data Types - 2/1.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Singh-K01/Python-tutorials-Kanav-Bansal/05065cb71ab0a5b9ff17b5befe485294c8395d47/3. Data Types - 2/1.jpeg -------------------------------------------------------------------------------- /4. Decision Statements/1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Singh-K01/Python-tutorials-Kanav-Bansal/05065cb71ab0a5b9ff17b5befe485294c8395d47/4. Decision Statements/1.jpg -------------------------------------------------------------------------------- /4. Decision Statements/2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Singh-K01/Python-tutorials-Kanav-Bansal/05065cb71ab0a5b9ff17b5befe485294c8395d47/4. Decision Statements/2.jpg -------------------------------------------------------------------------------- /4. Decision Statements/3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Singh-K01/Python-tutorials-Kanav-Bansal/05065cb71ab0a5b9ff17b5befe485294c8395d47/4. Decision Statements/3.jpg -------------------------------------------------------------------------------- /4. Decision Statements/decision_control_statement.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## if Statement" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": 10, 20 | "metadata": { 21 | "ExecuteTime": { 22 | "end_time": "2018-05-25T06:51:08.686929Z", 23 | "start_time": "2018-05-25T06:51:08.683420Z" 24 | } 25 | }, 26 | "outputs": [ 27 | { 28 | "name": "stdout", 29 | "output_type": "stream", 30 | "text": [ 31 | "This line will print always\n" 32 | ] 33 | } 34 | ], 35 | "source": [ 36 | "num = 0\n", 37 | "\n", 38 | "if -1 and 0:\n", 39 | " print('Number is positive')\n", 40 | "print('This line will print always')\n", 41 | "\n", 42 | "# 0, None, -ve, +ve" 43 | ] 44 | }, 45 | { 46 | "cell_type": "markdown", 47 | "metadata": {}, 48 | "source": [ 49 | "## if.....else Statement" 50 | ] 51 | }, 52 | { 53 | "cell_type": "markdown", 54 | "metadata": {}, 55 | "source": [ 56 | "" 57 | ] 58 | }, 59 | { 60 | "cell_type": "code", 61 | "execution_count": 11, 62 | "metadata": { 63 | "ExecuteTime": { 64 | "end_time": "2018-05-25T06:54:11.171237Z", 65 | "start_time": "2018-05-25T06:54:11.163714Z" 66 | } 67 | }, 68 | "outputs": [ 69 | { 70 | "name": "stdout", 71 | "output_type": "stream", 72 | "text": [ 73 | "Positive\n", 74 | "This line will print anyways\n" 75 | ] 76 | } 77 | ], 78 | "source": [ 79 | "num = 10\n", 80 | "\n", 81 | "if num > 0:\n", 82 | " print('Positive')\n", 83 | "else:\n", 84 | " print('Negative')\n", 85 | "print('This line will print anyways')" 86 | ] 87 | }, 88 | { 89 | "cell_type": "markdown", 90 | "metadata": {}, 91 | "source": [ 92 | "## if......elif......else Statement" 93 | ] 94 | }, 95 | { 96 | "cell_type": "markdown", 97 | "metadata": {}, 98 | "source": [ 99 | "" 100 | ] 101 | }, 102 | { 103 | "cell_type": "code", 104 | "execution_count": 9, 105 | "metadata": { 106 | "ExecuteTime": { 107 | "end_time": "2018-05-24T17:14:38.565029Z", 108 | "start_time": "2018-05-24T17:14:38.561020Z" 109 | }, 110 | "scrolled": true 111 | }, 112 | "outputs": [ 113 | { 114 | "name": "stdout", 115 | "output_type": "stream", 116 | "text": [ 117 | "Positive\n" 118 | ] 119 | } 120 | ], 121 | "source": [ 122 | "num = 10\n", 123 | "\n", 124 | "if num > 0:\n", 125 | " print('Positive')\n", 126 | "elif num == 0:\n", 127 | " print('ZERO')\n", 128 | "else:\n", 129 | " print('Negative')" 130 | ] 131 | }, 132 | { 133 | "cell_type": "code", 134 | "execution_count": 13, 135 | "metadata": { 136 | "ExecuteTime": { 137 | "end_time": "2018-05-25T07:02:11.826441Z", 138 | "start_time": "2018-05-25T07:02:11.821435Z" 139 | } 140 | }, 141 | "outputs": [ 142 | { 143 | "name": "stdout", 144 | "output_type": "stream", 145 | "text": [ 146 | "B grade\n" 147 | ] 148 | } 149 | ], 150 | "source": [ 151 | "grade = 110\n", 152 | "\n", 153 | "if grade >= 90 and grade<=100:\n", 154 | " print(\"A grade\")\n", 155 | "\n", 156 | "elif grade >=80:\n", 157 | " print(\"B grade\")\n", 158 | "\n", 159 | "elif grade >=70:\n", 160 | " print(\"C grade\")\n", 161 | "\n", 162 | "elif grade >= 65:\n", 163 | " print(\"D grade\")\n", 164 | "\n", 165 | "else:\n", 166 | " print(\"Failing grade\")" 167 | ] 168 | }, 169 | { 170 | "cell_type": "markdown", 171 | "metadata": {}, 172 | "source": [ 173 | "## Nested if - else Statements" 174 | ] 175 | }, 176 | { 177 | "cell_type": "code", 178 | "execution_count": 16, 179 | "metadata": { 180 | "ExecuteTime": { 181 | "end_time": "2018-05-24T17:35:58.681472Z", 182 | "start_time": "2018-05-24T17:35:58.677461Z" 183 | } 184 | }, 185 | "outputs": [ 186 | { 187 | "name": "stdout", 188 | "output_type": "stream", 189 | "text": [ 190 | "Positive\n" 191 | ] 192 | } 193 | ], 194 | "source": [ 195 | "num = 10\n", 196 | "\n", 197 | "if num >= 0:\n", 198 | " if num == 0:\n", 199 | " print('Zero')\n", 200 | " else:\n", 201 | " print('Positive')\n", 202 | "else:\n", 203 | " print('Negative')" 204 | ] 205 | }, 206 | { 207 | "cell_type": "code", 208 | "execution_count": 14, 209 | "metadata": { 210 | "ExecuteTime": { 211 | "end_time": "2018-05-25T07:12:38.862788Z", 212 | "start_time": "2018-05-25T07:12:38.855774Z" 213 | } 214 | }, 215 | "outputs": [ 216 | { 217 | "name": "stdout", 218 | "output_type": "stream", 219 | "text": [ 220 | "Passing grade of: D\n" 221 | ] 222 | } 223 | ], 224 | "source": [ 225 | "grade = 66\n", 226 | "\n", 227 | "if grade >= 65:\n", 228 | " print(\"Passing grade of:\", end = ' ')\n", 229 | "\n", 230 | " if grade >= 90:\n", 231 | " print(\"A\")\n", 232 | "\n", 233 | " elif grade >=80:\n", 234 | " print(\"B\")\n", 235 | "\n", 236 | " elif grade >=70:\n", 237 | " print(\"C\")\n", 238 | "\n", 239 | " elif grade >= 65:\n", 240 | " print(\"D\")\n", 241 | "\n", 242 | "else:\n", 243 | " print(\"Failing grade\")" 244 | ] 245 | }, 246 | { 247 | "cell_type": "markdown", 248 | "metadata": {}, 249 | "source": [ 250 | "## Miscellaneous Topics" 251 | ] 252 | }, 253 | { 254 | "cell_type": "markdown", 255 | "metadata": {}, 256 | "source": [ 257 | "### Input Type Casting" 258 | ] 259 | }, 260 | { 261 | "cell_type": "code", 262 | "execution_count": 15, 263 | "metadata": { 264 | "ExecuteTime": { 265 | "end_time": "2018-05-25T07:15:16.983076Z", 266 | "start_time": "2018-05-25T07:15:12.002148Z" 267 | }, 268 | "scrolled": true 269 | }, 270 | "outputs": [ 271 | { 272 | "name": "stdout", 273 | "output_type": "stream", 274 | "text": [ 275 | "Enter 1st number: 12\n", 276 | "Enter 2nd number: 13\n", 277 | "25\n" 278 | ] 279 | } 280 | ], 281 | "source": [ 282 | "a = int(input('Enter 1st number: '))\n", 283 | "\n", 284 | "b = int(input('Enter 2nd number: '))\n", 285 | "\n", 286 | "print(a+b)" 287 | ] 288 | }, 289 | { 290 | "cell_type": "markdown", 291 | "metadata": {}, 292 | "source": [ 293 | "### Count even and odd" 294 | ] 295 | }, 296 | { 297 | "cell_type": "code", 298 | "execution_count": 16, 299 | "metadata": { 300 | "ExecuteTime": { 301 | "end_time": "2018-05-25T07:28:53.215365Z", 302 | "start_time": "2018-05-25T07:28:15.434453Z" 303 | } 304 | }, 305 | "outputs": [ 306 | { 307 | "name": "stdout", 308 | "output_type": "stream", 309 | "text": [ 310 | "Enter the length of list you want to create: 5\n", 311 | "Enter a number at index 0: 1\n", 312 | "Enter a number at index 1: 2\n", 313 | "Enter a number at index 2: 3\n", 314 | "Enter a number at index 3: 4\n", 315 | "Enter a number at index 4: 5\n", 316 | "Even Count: 2\n", 317 | "Odd Count: 3\n" 318 | ] 319 | } 320 | ], 321 | "source": [ 322 | "# Count even and odd occurences in a List\n", 323 | "\n", 324 | "even = 0\n", 325 | "odd = 0\n", 326 | "\n", 327 | "lst_len = int(input('Enter the length of list you want to create: '))\n", 328 | "\n", 329 | "lst_num = []\n", 330 | "\n", 331 | "for i in range(lst_len):\n", 332 | " num = int(input('Enter a number at index %d: ' % (i)))\n", 333 | " lst_num.append(num)\n", 334 | "\n", 335 | "for num in lst_num:\n", 336 | " if num % 2 == 0:\n", 337 | " even = even + 1\n", 338 | " else:\n", 339 | " odd = odd + 1\n", 340 | "\n", 341 | "print('Even Count: ',even)\n", 342 | "\n", 343 | "print('Odd Count: ', odd)" 344 | ] 345 | }, 346 | { 347 | "cell_type": "markdown", 348 | "metadata": {}, 349 | "source": [ 350 | "### Output Format" 351 | ] 352 | }, 353 | { 354 | "cell_type": "code", 355 | "execution_count": 25, 356 | "metadata": { 357 | "ExecuteTime": { 358 | "end_time": "2018-05-24T18:07:43.944516Z", 359 | "start_time": "2018-05-24T18:07:43.940506Z" 360 | } 361 | }, 362 | "outputs": [ 363 | { 364 | "name": "stdout", 365 | "output_type": "stream", 366 | "text": [ 367 | "The value of a is 1 and b is 0\n", 368 | "The value of a is 1 and b is 0\n", 369 | "The value of b is 0 and a is 1\n" 370 | ] 371 | } 372 | ], 373 | "source": [ 374 | "a = 1; b = 0\n", 375 | "\n", 376 | "print('The value of a is %d and b is %d' % (a, b))\n", 377 | "print('The value of a is {} and b is {}'.format(a, b))\n", 378 | "\n", 379 | "print('The value of b is {} and a is {}'.format(a, b))" 380 | ] 381 | }, 382 | { 383 | "cell_type": "code", 384 | "execution_count": null, 385 | "metadata": {}, 386 | "outputs": [], 387 | "source": [] 388 | } 389 | ], 390 | "metadata": { 391 | "kernelspec": { 392 | "display_name": "Python 3", 393 | "language": "python", 394 | "name": "python3" 395 | }, 396 | "language_info": { 397 | "codemirror_mode": { 398 | "name": "ipython", 399 | "version": 3 400 | }, 401 | "file_extension": ".py", 402 | "mimetype": "text/x-python", 403 | "name": "python", 404 | "nbconvert_exporter": "python", 405 | "pygments_lexer": "ipython3", 406 | "version": "3.6.5" 407 | }, 408 | "toc": { 409 | "nav_menu": {}, 410 | "number_sections": true, 411 | "sideBar": true, 412 | "skip_h1_title": false, 413 | "title_cell": "Table of Contents", 414 | "title_sidebar": "Contents", 415 | "toc_cell": false, 416 | "toc_position": {}, 417 | "toc_section_display": true, 418 | "toc_window_display": false 419 | }, 420 | "varInspector": { 421 | "cols": { 422 | "lenName": 16, 423 | "lenType": 16, 424 | "lenVar": 40 425 | }, 426 | "kernels_config": { 427 | "python": { 428 | "delete_cmd_postfix": "", 429 | "delete_cmd_prefix": "del ", 430 | "library": "var_list.py", 431 | "varRefreshCmd": "print(var_dic_list())" 432 | }, 433 | "r": { 434 | "delete_cmd_postfix": ") ", 435 | "delete_cmd_prefix": "rm(", 436 | "library": "var_list.r", 437 | "varRefreshCmd": "cat(var_dic_list()) " 438 | } 439 | }, 440 | "types_to_exclude": [ 441 | "module", 442 | "function", 443 | "builtin_function_or_method", 444 | "instance", 445 | "_Feature" 446 | ], 447 | "window_display": false 448 | } 449 | }, 450 | "nbformat": 4, 451 | "nbformat_minor": 2 452 | } 453 | -------------------------------------------------------------------------------- /5. Loops/1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Singh-K01/Python-tutorials-Kanav-Bansal/05065cb71ab0a5b9ff17b5befe485294c8395d47/5. Loops/1.jpg -------------------------------------------------------------------------------- /5. Loops/2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Singh-K01/Python-tutorials-Kanav-Bansal/05065cb71ab0a5b9ff17b5befe485294c8395d47/5. Loops/2.jpg -------------------------------------------------------------------------------- /5. Loops/loops.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Dictionary Sorting" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 3, 13 | "metadata": { 14 | "ExecuteTime": { 15 | "end_time": "2018-05-27T14:46:37.010510Z", 16 | "start_time": "2018-05-27T14:46:37.005497Z" 17 | }, 18 | "scrolled": true 19 | }, 20 | "outputs": [ 21 | { 22 | "name": "stdout", 23 | "output_type": "stream", 24 | "text": [ 25 | "dict_keys([100, 102, 103, 104])\n", 26 | "dict_values(['abc', 'xyz', 'lmn', 'def'])\n", 27 | "dict_items([(100, 'abc'), (102, 'xyz'), (103, 'lmn'), (104, 'def')])\n" 28 | ] 29 | } 30 | ], 31 | "source": [ 32 | "d = {100:'abc', 102:'xyz', 103:'lmn', 104:'def'}\n", 33 | "\n", 34 | "print(d.keys())\n", 35 | "\n", 36 | "print(d.values())\n", 37 | "\n", 38 | "print(d.items())" 39 | ] 40 | }, 41 | { 42 | "cell_type": "code", 43 | "execution_count": 2, 44 | "metadata": { 45 | "ExecuteTime": { 46 | "end_time": "2018-05-28T06:59:51.989069Z", 47 | "start_time": "2018-05-28T06:59:51.983563Z" 48 | } 49 | }, 50 | "outputs": [ 51 | { 52 | "name": "stdout", 53 | "output_type": "stream", 54 | "text": [ 55 | "[\"don't\", 'me', 'Please', 'sort']\n" 56 | ] 57 | } 58 | ], 59 | "source": [ 60 | "# d = eval(input('Enter Dictionary: '))\n", 61 | "\n", 62 | "# print(sum(d.values()))\n", 63 | "\n", 64 | "print(sorted(\"Please don't sort me\".split(), key=str.lower))\n", 65 | "# {'a':100,'b':75,'c':90, 'd': 100, 'e':95}" 66 | ] 67 | }, 68 | { 69 | "cell_type": "code", 70 | "execution_count": 17, 71 | "metadata": { 72 | "ExecuteTime": { 73 | "end_time": "2018-05-27T16:09:41.156477Z", 74 | "start_time": "2018-05-27T16:08:44.194584Z" 75 | } 76 | }, 77 | "outputs": [ 78 | { 79 | "name": "stdout", 80 | "output_type": "stream", 81 | "text": [ 82 | "Enter Dictionary: {'a':100,'b':75,'c':90, 'd': 100, 'e':95}\n", 83 | "[('b', 75), ('c', 90), ('e', 95), ('a', 100), ('d', 100)]\n" 84 | ] 85 | } 86 | ], 87 | "source": [ 88 | "import operator\n", 89 | "\n", 90 | "d = eval(input('Enter Dictionary: '))\n", 91 | "\n", 92 | "sorted_d = sorted(d.items(), key=operator.itemgetter(1))\n", 93 | "\n", 94 | "print(sorted_d)" 95 | ] 96 | }, 97 | { 98 | "cell_type": "markdown", 99 | "metadata": {}, 100 | "source": [ 101 | "## Membership Operators" 102 | ] 103 | }, 104 | { 105 | "cell_type": "markdown", 106 | "metadata": {}, 107 | "source": [ 108 | "in and not in are membership operators in Python.\n", 109 | "\n", 110 | "Used to find whether a value or variable is found in a sequence." 111 | ] 112 | }, 113 | { 114 | "cell_type": "code", 115 | "execution_count": 3, 116 | "metadata": { 117 | "ExecuteTime": { 118 | "end_time": "2018-05-28T07:05:28.934187Z", 119 | "start_time": "2018-05-28T07:05:28.929176Z" 120 | } 121 | }, 122 | "outputs": [ 123 | { 124 | "name": "stdout", 125 | "output_type": "stream", 126 | "text": [ 127 | "False\n" 128 | ] 129 | } 130 | ], 131 | "source": [ 132 | "lst = [1,2,3,4]\n", 133 | "\n", 134 | "print(5 in lst)" 135 | ] 136 | }, 137 | { 138 | "cell_type": "code", 139 | "execution_count": 4, 140 | "metadata": { 141 | "ExecuteTime": { 142 | "end_time": "2018-05-28T07:06:59.266839Z", 143 | "start_time": "2018-05-28T07:06:59.261826Z" 144 | } 145 | }, 146 | "outputs": [ 147 | { 148 | "name": "stdout", 149 | "output_type": "stream", 150 | "text": [ 151 | "False\n" 152 | ] 153 | } 154 | ], 155 | "source": [ 156 | "dict = {1:'a', 2:'b'}\n", 157 | "\n", 158 | "print(2 not in dict)" 159 | ] 160 | }, 161 | { 162 | "cell_type": "code", 163 | "execution_count": 5, 164 | "metadata": { 165 | "ExecuteTime": { 166 | "end_time": "2018-05-28T07:07:53.240036Z", 167 | "start_time": "2018-05-28T07:07:53.232939Z" 168 | } 169 | }, 170 | "outputs": [ 171 | { 172 | "name": "stdout", 173 | "output_type": "stream", 174 | "text": [ 175 | "True\n" 176 | ] 177 | } 178 | ], 179 | "source": [ 180 | "s = 'Python'\n", 181 | "\n", 182 | "print('P' in s)" 183 | ] 184 | }, 185 | { 186 | "cell_type": "markdown", 187 | "metadata": {}, 188 | "source": [ 189 | "## while loop" 190 | ] 191 | }, 192 | { 193 | "cell_type": "code", 194 | "execution_count": 6, 195 | "metadata": { 196 | "ExecuteTime": { 197 | "end_time": "2018-05-28T07:16:03.029318Z", 198 | "start_time": "2018-05-28T07:16:03.014570Z" 199 | } 200 | }, 201 | "outputs": [ 202 | { 203 | "name": "stdout", 204 | "output_type": "stream", 205 | "text": [ 206 | "The count is: 0\n", 207 | "The count is: 1\n", 208 | "The count is: 2\n", 209 | "The count is: 3\n", 210 | "The count is: 4\n", 211 | "The count is: 5\n", 212 | "The count is: 6\n", 213 | "The count is: 7\n", 214 | "The count is: 8\n", 215 | "The count is: 9\n", 216 | "Good bye! count is: 10\n" 217 | ] 218 | } 219 | ], 220 | "source": [ 221 | "count = 0\n", 222 | "while(count < 10):\n", 223 | " print('The count is: ', count)\n", 224 | " count = count + 1\n", 225 | "print('Good bye! count is: ', count)" 226 | ] 227 | }, 228 | { 229 | "cell_type": "code", 230 | "execution_count": 21, 231 | "metadata": { 232 | "ExecuteTime": { 233 | "end_time": "2018-05-26T12:28:15.698509Z", 234 | "start_time": "2018-05-26T12:28:15.693997Z" 235 | } 236 | }, 237 | "outputs": [ 238 | { 239 | "name": "stdout", 240 | "output_type": "stream", 241 | "text": [ 242 | "Product = 120\n" 243 | ] 244 | } 245 | ], 246 | "source": [ 247 | "tup = (1,2,3,4,5)\n", 248 | "\n", 249 | "prod = 1\n", 250 | "index = 0\n", 251 | "\n", 252 | "while index < len(tup):\n", 253 | " prod *= tup[index]\n", 254 | " index += 1\n", 255 | "\n", 256 | "print('Product = {}'.format(prod))" 257 | ] 258 | }, 259 | { 260 | "cell_type": "markdown", 261 | "metadata": {}, 262 | "source": [ 263 | "### while....else" 264 | ] 265 | }, 266 | { 267 | "cell_type": "code", 268 | "execution_count": 8, 269 | "metadata": { 270 | "ExecuteTime": { 271 | "end_time": "2018-05-28T07:23:33.929194Z", 272 | "start_time": "2018-05-28T07:23:33.921193Z" 273 | } 274 | }, 275 | "outputs": [ 276 | { 277 | "name": "stdout", 278 | "output_type": "stream", 279 | "text": [ 280 | "No more numbers in tuple.\n", 281 | "Product = 120\n" 282 | ] 283 | } 284 | ], 285 | "source": [ 286 | "tup = (1,2,3,4,5)\n", 287 | "\n", 288 | "prod = 1\n", 289 | "index = 0\n", 290 | "\n", 291 | "while index < len(tup):\n", 292 | " prod *= tup[index]\n", 293 | " index += 1\n", 294 | "else:\n", 295 | " print('No more numbers in tuple.')\n", 296 | "\n", 297 | "print('Product = {}'.format(prod))" 298 | ] 299 | }, 300 | { 301 | "cell_type": "code", 302 | "execution_count": 24, 303 | "metadata": { 304 | "ExecuteTime": { 305 | "end_time": "2018-05-26T15:47:15.771672Z", 306 | "start_time": "2018-05-26T15:47:12.807696Z" 307 | }, 308 | "scrolled": true 309 | }, 310 | "outputs": [ 311 | { 312 | "name": "stdout", 313 | "output_type": "stream", 314 | "text": [ 315 | "Enter a number: 5\n", 316 | "16\n", 317 | "8\n", 318 | "4\n", 319 | "2\n", 320 | "1\n" 321 | ] 322 | } 323 | ], 324 | "source": [ 325 | "def collatz(number):\n", 326 | " if number % 2 == 0:\n", 327 | " print(number // 2)\n", 328 | " return number // 2\n", 329 | " elif number % 2 == 1:\n", 330 | " result = 3 * number + 1\n", 331 | " print(result)\n", 332 | " return result\n", 333 | "\n", 334 | "num = int(input('Enter a number: '))\n", 335 | "\n", 336 | "while num != 1:\n", 337 | " num = collatz(num)" 338 | ] 339 | }, 340 | { 341 | "cell_type": "markdown", 342 | "metadata": {}, 343 | "source": [ 344 | "## for loop" 345 | ] 346 | }, 347 | { 348 | "cell_type": "code", 349 | "execution_count": 27, 350 | "metadata": { 351 | "ExecuteTime": { 352 | "end_time": "2018-05-26T15:51:09.047649Z", 353 | "start_time": "2018-05-26T15:51:09.044141Z" 354 | } 355 | }, 356 | "outputs": [ 357 | { 358 | "name": "stdout", 359 | "output_type": "stream", 360 | "text": [ 361 | "Product = 120\n" 362 | ] 363 | } 364 | ], 365 | "source": [ 366 | "tup = (1,2,3,4,5)\n", 367 | "\n", 368 | "prod = 1\n", 369 | "\n", 370 | "for item in tup:\n", 371 | " prod *= item\n", 372 | "\n", 373 | "print('Product = {}'.format(prod))" 374 | ] 375 | }, 376 | { 377 | "cell_type": "code", 378 | "execution_count": 29, 379 | "metadata": { 380 | "ExecuteTime": { 381 | "end_time": "2018-05-26T15:53:24.867100Z", 382 | "start_time": "2018-05-26T15:53:24.863090Z" 383 | } 384 | }, 385 | "outputs": [ 386 | { 387 | "name": "stdout", 388 | "output_type": "stream", 389 | "text": [ 390 | "The count is: 0\n", 391 | "The count is: 1\n", 392 | "The count is: 2\n", 393 | "The count is: 3\n", 394 | "The count is: 4\n", 395 | "The count is: 5\n", 396 | "The count is: 6\n", 397 | "The count is: 7\n", 398 | "The count is: 8\n", 399 | "The count is: 9\n", 400 | "Good bye!\n" 401 | ] 402 | } 403 | ], 404 | "source": [ 405 | "for count in range(10):\n", 406 | " print('The count is: ', count)\n", 407 | "print('Good bye!')\n", 408 | "\n", 409 | "# try range(1, 10, 1)" 410 | ] 411 | }, 412 | { 413 | "cell_type": "code", 414 | "execution_count": 9, 415 | "metadata": { 416 | "ExecuteTime": { 417 | "end_time": "2018-05-28T07:54:28.023220Z", 418 | "start_time": "2018-05-28T07:54:28.018707Z" 419 | } 420 | }, 421 | "outputs": [ 422 | { 423 | "name": "stdout", 424 | "output_type": "stream", 425 | "text": [ 426 | "10\n", 427 | "20\n", 428 | "30\n", 429 | "40\n", 430 | "50\n", 431 | "60\n" 432 | ] 433 | } 434 | ], 435 | "source": [ 436 | "# Iterating directly element by element\n", 437 | "\n", 438 | "\n", 439 | "# Iterating by sequence index\n", 440 | "lst = [10,20,30,40,50,60]\n", 441 | "for index in range(len(lst)):\n", 442 | " print(lst[index])" 443 | ] 444 | }, 445 | { 446 | "cell_type": "code", 447 | "execution_count": 24, 448 | "metadata": { 449 | "ExecuteTime": { 450 | "end_time": "2018-05-27T16:29:06.269555Z", 451 | "start_time": "2018-05-27T16:29:06.256095Z" 452 | } 453 | }, 454 | "outputs": [ 455 | { 456 | "name": "stdout", 457 | "output_type": "stream", 458 | "text": [ 459 | "LIST ITERATION: \n", 460 | "10 20 30 40 50 60 \n", 461 | "TUPLE ITERATION: \n", 462 | "10 20 30 40 50 60 \n", 463 | "STRING ITERATION: \n", 464 | "P y t h o n \n", 465 | "DICTIONARY ITERATION: \n", 466 | "xyz 123\n", 467 | "abc 345\n" 468 | ] 469 | } 470 | ], 471 | "source": [ 472 | "print('LIST ITERATION: ')\n", 473 | "lst = [10,20,30,40,50,60]\n", 474 | "for element in lst:\n", 475 | " print(element, end=' ')\n", 476 | "print()\n", 477 | "\n", 478 | "print('TUPLE ITERATION: ')\n", 479 | "tup = (10,20,30,40,50,60)\n", 480 | "for element in tup:\n", 481 | " print(element, end=' ')\n", 482 | "print()\n", 483 | "\n", 484 | "print('STRING ITERATION: ')\n", 485 | "myStr = 'Python'\n", 486 | "for element in myStr:\n", 487 | " print(element, end=' ')\n", 488 | "print()\n", 489 | "\n", 490 | "print('DICTIONARY ITERATION: ')\n", 491 | "dict = {}\n", 492 | "dict['xyz'] = 123\n", 493 | "dict['abc'] = 345\n", 494 | "for key in dict:\n", 495 | " print(key, dict[key])" 496 | ] 497 | }, 498 | { 499 | "cell_type": "markdown", 500 | "metadata": {}, 501 | "source": [ 502 | "### for.....else" 503 | ] 504 | }, 505 | { 506 | "cell_type": "code", 507 | "execution_count": 3, 508 | "metadata": { 509 | "ExecuteTime": { 510 | "end_time": "2018-05-27T03:53:22.112032Z", 511 | "start_time": "2018-05-27T03:53:22.096617Z" 512 | } 513 | }, 514 | "outputs": [ 515 | { 516 | "name": "stdout", 517 | "output_type": "stream", 518 | "text": [ 519 | "You have exhausted the list\n", 520 | "Product = 120\n" 521 | ] 522 | } 523 | ], 524 | "source": [ 525 | "tup = (1,2,3,4,5)\n", 526 | "\n", 527 | "prod = 1\n", 528 | "\n", 529 | "for item in tup:\n", 530 | " prod *= item\n", 531 | "else:\n", 532 | " print('You have exhausted the list')\n", 533 | "\n", 534 | "print('Product = {}'.format(prod))" 535 | ] 536 | }, 537 | { 538 | "cell_type": "markdown", 539 | "metadata": {}, 540 | "source": [ 541 | "## Nested Loops" 542 | ] 543 | }, 544 | { 545 | "cell_type": "code", 546 | "execution_count": 11, 547 | "metadata": { 548 | "ExecuteTime": { 549 | "end_time": "2018-05-28T08:03:01.951895Z", 550 | "start_time": "2018-05-28T08:03:01.941868Z" 551 | } 552 | }, 553 | "outputs": [ 554 | { 555 | "name": "stdout", 556 | "output_type": "stream", 557 | "text": [ 558 | "i: 0, j:0\n", 559 | "i: 0, j:1\n", 560 | "i: 0, j:2\n", 561 | "i: 1, j:0\n", 562 | "i: 1, j:1\n", 563 | "i: 1, j:2\n", 564 | "i: 2, j:0\n", 565 | "i: 2, j:1\n", 566 | "i: 2, j:2\n", 567 | "i: 3, j:0\n", 568 | "i: 3, j:1\n", 569 | "i: 3, j:2\n", 570 | "i: 4, j:0\n", 571 | "i: 4, j:1\n", 572 | "i: 4, j:2\n", 573 | "i: 5, j:0\n", 574 | "i: 5, j:1\n", 575 | "i: 5, j:2\n", 576 | "i: 6, j:0\n", 577 | "i: 6, j:1\n", 578 | "i: 6, j:2\n", 579 | "i: 7, j:0\n", 580 | "i: 7, j:1\n", 581 | "i: 7, j:2\n", 582 | "i: 8, j:0\n", 583 | "i: 8, j:1\n", 584 | "i: 8, j:2\n", 585 | "i: 9, j:0\n", 586 | "i: 9, j:1\n", 587 | "i: 9, j:2\n" 588 | ] 589 | } 590 | ], 591 | "source": [ 592 | "i = 0\n", 593 | "\n", 594 | "while i < 10:\n", 595 | " j = 0\n", 596 | " while j < 3:\n", 597 | " print('i: {}, j:{}'.format(i, j))\n", 598 | " j += 1\n", 599 | " \n", 600 | " i += 1" 601 | ] 602 | }, 603 | { 604 | "cell_type": "code", 605 | "execution_count": 12, 606 | "metadata": { 607 | "ExecuteTime": { 608 | "end_time": "2018-05-28T08:09:17.932127Z", 609 | "start_time": "2018-05-28T08:09:17.926647Z" 610 | } 611 | }, 612 | "outputs": [ 613 | { 614 | "name": "stdout", 615 | "output_type": "stream", 616 | "text": [ 617 | "Prime Numbers between 1 & 11 are: \n", 618 | "2\n", 619 | "3\n", 620 | "5\n", 621 | "7\n", 622 | "11\n" 623 | ] 624 | } 625 | ], 626 | "source": [ 627 | "num_1 = 1\n", 628 | "num_2 = 11\n", 629 | "\n", 630 | "print('Prime Numbers between {} & {} are: '.format(num_1,num_2))\n", 631 | "\n", 632 | "for num in range(num_1, num_2+1):\n", 633 | " if num > 1:\n", 634 | " isDivisible = False\n", 635 | " for index in range(2, num):\n", 636 | " if num % index == 0:\n", 637 | " isDivisible = True\n", 638 | " if not isDivisible:\n", 639 | " print(num)\n" 640 | ] 641 | }, 642 | { 643 | "cell_type": "markdown", 644 | "metadata": {}, 645 | "source": [ 646 | "## BREAK & CONTINUE" 647 | ] 648 | }, 649 | { 650 | "cell_type": "markdown", 651 | "metadata": { 652 | "ExecuteTime": { 653 | "end_time": "2018-05-27T04:06:33.272402Z", 654 | "start_time": "2018-05-27T04:06:33.265224Z" 655 | } 656 | }, 657 | "source": [ 658 | "" 659 | ] 660 | }, 661 | { 662 | "cell_type": "code", 663 | "execution_count": 10, 664 | "metadata": { 665 | "ExecuteTime": { 666 | "end_time": "2018-05-27T04:32:35.020303Z", 667 | "start_time": "2018-05-27T04:32:35.011591Z" 668 | } 669 | }, 670 | "outputs": [ 671 | { 672 | "name": "stdout", 673 | "output_type": "stream", 674 | "text": [ 675 | "The count is: 0\n", 676 | "The count is: 1\n", 677 | "The count is: 2\n", 678 | "The count is: 3\n", 679 | "The count is: 4\n", 680 | "Good bye!\n" 681 | ] 682 | } 683 | ], 684 | "source": [ 685 | "for count in range(10):\n", 686 | " if count == 5:\n", 687 | " break\n", 688 | " print('The count is: ', count)\n", 689 | "print('Good bye!')\n", 690 | "# try else" 691 | ] 692 | }, 693 | { 694 | "cell_type": "markdown", 695 | "metadata": {}, 696 | "source": [ 697 | "" 698 | ] 699 | }, 700 | { 701 | "cell_type": "code", 702 | "execution_count": 7, 703 | "metadata": { 704 | "ExecuteTime": { 705 | "end_time": "2018-05-27T15:09:27.512288Z", 706 | "start_time": "2018-05-27T15:09:27.508280Z" 707 | }, 708 | "scrolled": false 709 | }, 710 | "outputs": [ 711 | { 712 | "name": "stdout", 713 | "output_type": "stream", 714 | "text": [ 715 | "The count is: 0\n", 716 | "The count is: 1\n", 717 | "The count is: 2\n", 718 | "The count is: 3\n", 719 | "The count is: 4\n", 720 | "The count is: 6\n", 721 | "The count is: 7\n", 722 | "The count is: 8\n", 723 | "The count is: 9\n", 724 | "Good bye!\n" 725 | ] 726 | } 727 | ], 728 | "source": [ 729 | "for count in range(10):\n", 730 | " if count == 5:\n", 731 | " continue\n", 732 | " print('The count is: ', count)\n", 733 | "print('Good bye!')" 734 | ] 735 | }, 736 | { 737 | "cell_type": "code", 738 | "execution_count": 14, 739 | "metadata": { 740 | "ExecuteTime": { 741 | "end_time": "2018-05-28T08:17:28.941451Z", 742 | "start_time": "2018-05-28T08:17:23.611233Z" 743 | } 744 | }, 745 | "outputs": [ 746 | { 747 | "name": "stdout", 748 | "output_type": "stream", 749 | "text": [ 750 | "Enter a number to check if its even or odd: 7\n", 751 | "Wrong Number!!\\nGAME OVER\n" 752 | ] 753 | } 754 | ], 755 | "source": [ 756 | "while True:\n", 757 | " num = int(input(\"Enter a number to check if its even or odd: \"))\n", 758 | " if num % 2 == 0:\n", 759 | " print('Congratulations, you entered an even number.')\n", 760 | " else:\n", 761 | " break\n", 762 | "print('Wrong Number!!\\\\nGAME OVER')" 763 | ] 764 | }, 765 | { 766 | "cell_type": "markdown", 767 | "metadata": {}, 768 | "source": [ 769 | "## Assignment Discussions" 770 | ] 771 | }, 772 | { 773 | "cell_type": "markdown", 774 | "metadata": {}, 775 | "source": [ 776 | "

\n", 777 | "Q.4- From a list containing ints, strings and floats, make three lists to store them separately
\n", 778 | "Q.7- Create a user defined dictionary and get keys corresponding to the value using for loop.
\n", 779 | "Q.8- Take inputs from user to make a list. Again take one input from user and search it in the list and delete that element, if found. Iterate over list using for loop.\n", 780 | "

" 781 | ] 782 | } 783 | ], 784 | "metadata": { 785 | "kernelspec": { 786 | "display_name": "Python 3", 787 | "language": "python", 788 | "name": "python3" 789 | }, 790 | "language_info": { 791 | "codemirror_mode": { 792 | "name": "ipython", 793 | "version": 3 794 | }, 795 | "file_extension": ".py", 796 | "mimetype": "text/x-python", 797 | "name": "python", 798 | "nbconvert_exporter": "python", 799 | "pygments_lexer": "ipython3", 800 | "version": "3.6.5" 801 | }, 802 | "toc": { 803 | "nav_menu": {}, 804 | "number_sections": true, 805 | "sideBar": true, 806 | "skip_h1_title": false, 807 | "title_cell": "Table of Contents", 808 | "title_sidebar": "Contents", 809 | "toc_cell": false, 810 | "toc_position": {}, 811 | "toc_section_display": true, 812 | "toc_window_display": false 813 | }, 814 | "varInspector": { 815 | "cols": { 816 | "lenName": 16, 817 | "lenType": 16, 818 | "lenVar": 40 819 | }, 820 | "kernels_config": { 821 | "python": { 822 | "delete_cmd_postfix": "", 823 | "delete_cmd_prefix": "del ", 824 | "library": "var_list.py", 825 | "varRefreshCmd": "print(var_dic_list())" 826 | }, 827 | "r": { 828 | "delete_cmd_postfix": ") ", 829 | "delete_cmd_prefix": "rm(", 830 | "library": "var_list.r", 831 | "varRefreshCmd": "cat(var_dic_list()) " 832 | } 833 | }, 834 | "types_to_exclude": [ 835 | "module", 836 | "function", 837 | "builtin_function_or_method", 838 | "instance", 839 | "_Feature" 840 | ], 841 | "window_display": false 842 | } 843 | }, 844 | "nbformat": 4, 845 | "nbformat_minor": 2 846 | } 847 | -------------------------------------------------------------------------------- /6. Functions/functions.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 3, 6 | "metadata": { 7 | "ExecuteTime": { 8 | "end_time": "2018-05-29T06:39:16.857371Z", 9 | "start_time": "2018-05-29T06:39:16.681907Z" 10 | } 11 | }, 12 | "outputs": [ 13 | { 14 | "name": "stdout", 15 | "output_type": "stream", 16 | "text": [ 17 | "1\n", 18 | "2\n", 19 | "4\n", 20 | "huraaaay!\n" 21 | ] 22 | }, 23 | { 24 | "ename": "AttributeError", 25 | "evalue": "'list' object has no attribute 'discard'", 26 | "output_type": "error", 27 | "traceback": [ 28 | "\u001b[1;31m------------------------------------------------------------------\u001b[0m", 29 | "\u001b[1;31mAttributeError\u001b[0m Traceback (most recent call last)", 30 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 9\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'huraaaay!'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 10\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 11\u001b[1;33m \u001b[0mlst\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdiscard\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 31 | "\u001b[1;31mAttributeError\u001b[0m: 'list' object has no attribute 'discard'" 32 | ] 33 | } 34 | ], 35 | "source": [ 36 | "lst = [1,2,3,4]\n", 37 | "\n", 38 | "\n", 39 | "for item in lst:\n", 40 | " if item == 3:\n", 41 | " continue\n", 42 | " print(item)\n", 43 | "else:\n", 44 | " print('huraaaay!')\n", 45 | " \n", 46 | "lst.discard(3)" 47 | ] 48 | }, 49 | { 50 | "cell_type": "markdown", 51 | "metadata": {}, 52 | "source": [ 53 | "## Python Functions" 54 | ] 55 | }, 56 | { 57 | "cell_type": "markdown", 58 | "metadata": {}, 59 | "source": [ 60 | "### Function Definition & Calling a Function" 61 | ] 62 | }, 63 | { 64 | "cell_type": "code", 65 | "execution_count": 8, 66 | "metadata": { 67 | "ExecuteTime": { 68 | "end_time": "2018-05-29T06:56:09.081826Z", 69 | "start_time": "2018-05-29T06:56:09.078321Z" 70 | } 71 | }, 72 | "outputs": [ 73 | { 74 | "name": "stdout", 75 | "output_type": "stream", 76 | "text": [ 77 | "Hello World\n" 78 | ] 79 | } 80 | ], 81 | "source": [ 82 | "def myFirstFunction():\n", 83 | " '''This is my first function. It will print Hello World always.'''\n", 84 | " print('Hello World')\n", 85 | " \n", 86 | "myFirstFunction()\n", 87 | "\n", 88 | "# try to change indentation and line of the code." 89 | ] 90 | }, 91 | { 92 | "cell_type": "code", 93 | "execution_count": 11, 94 | "metadata": { 95 | "ExecuteTime": { 96 | "end_time": "2018-05-29T03:12:57.806089Z", 97 | "start_time": "2018-05-29T03:12:57.799554Z" 98 | } 99 | }, 100 | "outputs": [ 101 | { 102 | "name": "stdout", 103 | "output_type": "stream", 104 | "text": [ 105 | "This is my first function. It will print Hello World always.\n" 106 | ] 107 | } 108 | ], 109 | "source": [ 110 | "print(myFirstFunction.__doc__)" 111 | ] 112 | }, 113 | { 114 | "cell_type": "code", 115 | "execution_count": 9, 116 | "metadata": { 117 | "ExecuteTime": { 118 | "end_time": "2018-05-29T06:59:51.586341Z", 119 | "start_time": "2018-05-29T06:59:51.582833Z" 120 | } 121 | }, 122 | "outputs": [ 123 | { 124 | "name": "stdout", 125 | "output_type": "stream", 126 | "text": [ 127 | "Return the tuple (x//y, x%y). Invariant: div*y + mod == x.\n" 128 | ] 129 | } 130 | ], 131 | "source": [ 132 | "print(divmod.__doc__)" 133 | ] 134 | }, 135 | { 136 | "cell_type": "markdown", 137 | "metadata": {}, 138 | "source": [ 139 | "### Function With Arguments/Parameters" 140 | ] 141 | }, 142 | { 143 | "cell_type": "code", 144 | "execution_count": 3, 145 | "metadata": { 146 | "ExecuteTime": { 147 | "end_time": "2018-05-28T16:41:59.478050Z", 148 | "start_time": "2018-05-28T16:41:59.466948Z" 149 | } 150 | }, 151 | "outputs": [ 152 | { 153 | "name": "stdout", 154 | "output_type": "stream", 155 | "text": [ 156 | "Hello Dehradun\n" 157 | ] 158 | } 159 | ], 160 | "source": [ 161 | "# Function with arguments/parameter\n", 162 | "\n", 163 | "def sayHello(name):\n", 164 | " '''This function wishes hello'''\n", 165 | " print('Hello', name)\n", 166 | " \n", 167 | "sayHello('Dehradun')\n" 168 | ] 169 | }, 170 | { 171 | "cell_type": "code", 172 | "execution_count": 4, 173 | "metadata": { 174 | "ExecuteTime": { 175 | "end_time": "2018-05-28T16:43:47.212524Z", 176 | "start_time": "2018-05-28T16:43:47.207303Z" 177 | } 178 | }, 179 | "outputs": [ 180 | { 181 | "name": "stdout", 182 | "output_type": "stream", 183 | "text": [ 184 | "The square of 10 is 100\n", 185 | "The square of 5 is 25\n" 186 | ] 187 | } 188 | ], 189 | "source": [ 190 | "# Write a function to take a number and square it\n", 191 | "\n", 192 | "def square(num):\n", 193 | " '''This function prints square'''\n", 194 | " print('The square of {} is {}'.format(num, num**2))\n", 195 | "\n", 196 | "square(10)\n", 197 | "square(5)" 198 | ] 199 | }, 200 | { 201 | "cell_type": "markdown", 202 | "metadata": {}, 203 | "source": [ 204 | "### Return Statement" 205 | ] 206 | }, 207 | { 208 | "cell_type": "code", 209 | "execution_count": 10, 210 | "metadata": { 211 | "ExecuteTime": { 212 | "end_time": "2018-05-29T07:10:31.305919Z", 213 | "start_time": "2018-05-29T07:10:31.302411Z" 214 | } 215 | }, 216 | "outputs": [ 217 | { 218 | "name": "stdout", 219 | "output_type": "stream", 220 | "text": [ 221 | "100\n", 222 | "25\n" 223 | ] 224 | } 225 | ], 226 | "source": [ 227 | "def square(num):\n", 228 | " '''This function returns square'''\n", 229 | " return num*num\n", 230 | "\n", 231 | "x = square(10)\n", 232 | "print(x)\n", 233 | "print(square(5))" 234 | ] 235 | }, 236 | { 237 | "cell_type": "code", 238 | "execution_count": 11, 239 | "metadata": { 240 | "ExecuteTime": { 241 | "end_time": "2018-05-29T07:13:09.063774Z", 242 | "start_time": "2018-05-29T07:13:09.059767Z" 243 | } 244 | }, 245 | "outputs": [ 246 | { 247 | "name": "stdout", 248 | "output_type": "stream", 249 | "text": [ 250 | "Hello\n", 251 | "None\n" 252 | ] 253 | } 254 | ], 255 | "source": [ 256 | "def fun():\n", 257 | " print('Hello')\n", 258 | "\n", 259 | "print(fun())" 260 | ] 261 | }, 262 | { 263 | "cell_type": "code", 264 | "execution_count": 9, 265 | "metadata": { 266 | "ExecuteTime": { 267 | "end_time": "2018-05-28T17:05:24.757427Z", 268 | "start_time": "2018-05-28T17:05:24.751898Z" 269 | } 270 | }, 271 | "outputs": [ 272 | { 273 | "name": "stdout", 274 | "output_type": "stream", 275 | "text": [ 276 | "20 10\n" 277 | ] 278 | } 279 | ], 280 | "source": [ 281 | "# Multiple Returns\n", 282 | "\n", 283 | "def add_sub(a,b):\n", 284 | " add = a+b\n", 285 | " sub = a-b\n", 286 | " return add, sub\n", 287 | "\n", 288 | "addition, subtraction = add_sub(15,5)\n", 289 | "\n", 290 | "print(addition, subtraction)" 291 | ] 292 | }, 293 | { 294 | "cell_type": "markdown", 295 | "metadata": {}, 296 | "source": [ 297 | "### Function Arguments" 298 | ] 299 | }, 300 | { 301 | "cell_type": "code", 302 | "execution_count": 13, 303 | "metadata": { 304 | "ExecuteTime": { 305 | "end_time": "2018-05-29T07:21:31.064663Z", 306 | "start_time": "2018-05-29T07:21:31.052129Z" 307 | }, 308 | "scrolled": true 309 | }, 310 | "outputs": [ 311 | { 312 | "name": "stdout", 313 | "output_type": "stream", 314 | "text": [ 315 | "10\n", 316 | "-10\n" 317 | ] 318 | }, 319 | { 320 | "ename": "TypeError", 321 | "evalue": "sub() takes 2 positional arguments but 3 were given", 322 | "output_type": "error", 323 | "traceback": [ 324 | "\u001b[1;31m------------------------------------------------------------------\u001b[0m", 325 | "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", 326 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 5\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[0msub\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m10\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m20\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[0msub\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m20\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m10\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m10\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 327 | "\u001b[1;31mTypeError\u001b[0m: sub() takes 2 positional arguments but 3 were given" 328 | ] 329 | } 330 | ], 331 | "source": [ 332 | "# Positional Argument\n", 333 | "def sub(a, b):\n", 334 | " return a - b\n", 335 | "print(sub(20,10))\n", 336 | "\n", 337 | "print(sub(10,20))\n", 338 | "print(sub(20,10,10))" 339 | ] 340 | }, 341 | { 342 | "cell_type": "code", 343 | "execution_count": 20, 344 | "metadata": { 345 | "ExecuteTime": { 346 | "end_time": "2018-05-29T07:29:40.372159Z", 347 | "start_time": "2018-05-29T07:29:40.360127Z" 348 | }, 349 | "scrolled": true 350 | }, 351 | "outputs": [ 352 | { 353 | "name": "stdout", 354 | "output_type": "stream", 355 | "text": [ 356 | "Hello abc hurrraay!\n" 357 | ] 358 | }, 359 | { 360 | "ename": "TypeError", 361 | "evalue": "sayAnything() got multiple values for argument 'name'", 362 | "output_type": "error", 363 | "traceback": [ 364 | "\u001b[1;31m------------------------------------------------------------------\u001b[0m", 365 | "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", 366 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[0msayAnything\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mname\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m'abc'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0manything\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m'hurrraay!'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 6\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 7\u001b[1;33m \u001b[0msayAnything\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'abc'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mname\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m'hurrraay!'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 367 | "\u001b[1;31mTypeError\u001b[0m: sayAnything() got multiple values for argument 'name'" 368 | ] 369 | } 370 | ], 371 | "source": [ 372 | "# Keyword Argument\n", 373 | "def sayAnything(name, anything):\n", 374 | " print('Hello', name, anything)\n", 375 | "\n", 376 | "sayAnything(name='abc', anything='hurrraay!')\n", 377 | "\n", 378 | "sayAnything('abc', name='hurrraay!')" 379 | ] 380 | }, 381 | { 382 | "cell_type": "code", 383 | "execution_count": 21, 384 | "metadata": { 385 | "ExecuteTime": { 386 | "end_time": "2018-05-29T07:32:02.041082Z", 387 | "start_time": "2018-05-29T07:32:02.031056Z" 388 | } 389 | }, 390 | "outputs": [ 391 | { 392 | "name": "stdout", 393 | "output_type": "stream", 394 | "text": [ 395 | "Hello abc hurray\n", 396 | "Hello Guest Welcome\n" 397 | ] 398 | } 399 | ], 400 | "source": [ 401 | "# Default Argument\n", 402 | "def sayAnything(name = 'Guest', anything='Welcome'):\n", 403 | " print('Hello', name, anything)\n", 404 | "\n", 405 | "sayAnything('abc', 'hurray')\n", 406 | "sayAnything()" 407 | ] 408 | }, 409 | { 410 | "cell_type": "code", 411 | "execution_count": 18, 412 | "metadata": { 413 | "ExecuteTime": { 414 | "end_time": "2018-05-28T17:40:21.621257Z", 415 | "start_time": "2018-05-28T17:40:21.613168Z" 416 | } 417 | }, 418 | "outputs": [ 419 | { 420 | "name": "stdout", 421 | "output_type": "stream", 422 | "text": [ 423 | "Name: abc\n", 424 | "Age: 14\n", 425 | "Marks: 100\n", 426 | "Message: Good Morning\n" 427 | ] 428 | } 429 | ], 430 | "source": [ 431 | "def printInfo(marks, age, name='Guest', msg='Good Morning'):\n", 432 | " print('Name: ', name)\n", 433 | " print('Age: ', age)\n", 434 | " print('Marks: ', marks)\n", 435 | " print('Message: ', msg)\n", 436 | "\n", 437 | "printInfo(100, 14, 'abc')" 438 | ] 439 | }, 440 | { 441 | "cell_type": "markdown", 442 | "metadata": {}, 443 | "source": [ 444 | "### Scope of Variables" 445 | ] 446 | }, 447 | { 448 | "cell_type": "code", 449 | "execution_count": 24, 450 | "metadata": { 451 | "ExecuteTime": { 452 | "end_time": "2018-05-29T07:38:47.189368Z", 453 | "start_time": "2018-05-29T07:38:47.184856Z" 454 | } 455 | }, 456 | "outputs": [ 457 | { 458 | "name": "stdout", 459 | "output_type": "stream", 460 | "text": [ 461 | "30\n", 462 | "30\n" 463 | ] 464 | } 465 | ], 466 | "source": [ 467 | "# Global variable is accessible everywhere\n", 468 | "\n", 469 | "add = 0\n", 470 | "\n", 471 | "def sum(a,b):\n", 472 | " add = a+b\n", 473 | " print(add)\n", 474 | " return add\n", 475 | "\n", 476 | "print(sum(10,20))" 477 | ] 478 | }, 479 | { 480 | "cell_type": "code", 481 | "execution_count": 26, 482 | "metadata": { 483 | "ExecuteTime": { 484 | "end_time": "2018-05-29T07:39:54.039759Z", 485 | "start_time": "2018-05-29T07:39:54.027225Z" 486 | }, 487 | "scrolled": true 488 | }, 489 | "outputs": [ 490 | { 491 | "name": "stdout", 492 | "output_type": "stream", 493 | "text": [ 494 | "30\n" 495 | ] 496 | }, 497 | { 498 | "ename": "NameError", 499 | "evalue": "name 'total' is not defined", 500 | "output_type": "error", 501 | "traceback": [ 502 | "\u001b[1;31m------------------------------------------------------------------\u001b[0m", 503 | "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", 504 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 9\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0madd\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 10\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 11\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mtotal\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 505 | "\u001b[1;31mNameError\u001b[0m: name 'total' is not defined" 506 | ] 507 | } 508 | ], 509 | "source": [ 510 | "# Local Variable\n", 511 | "\n", 512 | "add = 0\n", 513 | "\n", 514 | "def sum(a,b):\n", 515 | " total = a+b\n", 516 | " return total\n", 517 | "add = sum(10,20)\n", 518 | "print(add)\n", 519 | "\n", 520 | "print(total)" 521 | ] 522 | }, 523 | { 524 | "cell_type": "markdown", 525 | "metadata": {}, 526 | "source": [ 527 | "### Pass By Reference" 528 | ] 529 | }, 530 | { 531 | "cell_type": "code", 532 | "execution_count": 21, 533 | "metadata": { 534 | "ExecuteTime": { 535 | "end_time": "2018-05-28T18:10:44.763863Z", 536 | "start_time": "2018-05-28T18:10:44.726402Z" 537 | } 538 | }, 539 | "outputs": [ 540 | { 541 | "name": "stdout", 542 | "output_type": "stream", 543 | "text": [ 544 | "Inside Function: [10, 20, 30, [1, 2, 3]]\n", 545 | "Main Program: [10, 20, 30, [1, 2, 3]]\n" 546 | ] 547 | } 548 | ], 549 | "source": [ 550 | "# Pass by reference\n", 551 | "\n", 552 | "def changeMe(lst):\n", 553 | " '''Changes List'''\n", 554 | " lst.append([1,2,3])\n", 555 | " print('Inside Function: ', lst)\n", 556 | "\n", 557 | "lst = [10,20,30]\n", 558 | "changeMe(lst)\n", 559 | "print('Main Program: ', lst)" 560 | ] 561 | }, 562 | { 563 | "cell_type": "code", 564 | "execution_count": 22, 565 | "metadata": { 566 | "ExecuteTime": { 567 | "end_time": "2018-05-28T18:11:51.778255Z", 568 | "start_time": "2018-05-28T18:11:51.762590Z" 569 | } 570 | }, 571 | "outputs": [ 572 | { 573 | "name": "stdout", 574 | "output_type": "stream", 575 | "text": [ 576 | "Inside Function: [1, 2, 3]\n", 577 | "Main Program: [10, 20, 30]\n" 578 | ] 579 | } 580 | ], 581 | "source": [ 582 | "def changeMe(lst):\n", 583 | " '''Changes List'''\n", 584 | " lst = [1, 2, 3]\n", 585 | " print('Inside Function: ', lst)\n", 586 | "\n", 587 | "lst = [10, 20, 30]\n", 588 | "changeMe(lst)\n", 589 | "print('Main Program: ', lst)" 590 | ] 591 | }, 592 | { 593 | "cell_type": "markdown", 594 | "metadata": {}, 595 | "source": [ 596 | "## Recursion" 597 | ] 598 | }, 599 | { 600 | "cell_type": "markdown", 601 | "metadata": {}, 602 | "source": [ 603 | "A function calling itself. Recursive Functions.\n" 604 | ] 605 | }, 606 | { 607 | "cell_type": "code", 608 | "execution_count": 28, 609 | "metadata": { 610 | "ExecuteTime": { 611 | "end_time": "2018-05-29T08:05:26.397403Z", 612 | "start_time": "2018-05-29T08:05:26.388377Z" 613 | } 614 | }, 615 | "outputs": [ 616 | { 617 | "name": "stdout", 618 | "output_type": "stream", 619 | "text": [ 620 | "Factorial of 6 is 720\n" 621 | ] 622 | } 623 | ], 624 | "source": [ 625 | "def factorial(num):\n", 626 | " '''Recursive Function to find Factorial'''\n", 627 | "# if num == 1:\n", 628 | "# return 1\n", 629 | "# else:\n", 630 | "# return (num * factorial(num-1))\n", 631 | " return 1 if num == 1 else (num * factorial(num-1))\n", 632 | "num = 6\n", 633 | "print('Factorial of {} is {}'.format(num, factorial(num)))" 634 | ] 635 | }, 636 | { 637 | "cell_type": "code", 638 | "execution_count": 34, 639 | "metadata": { 640 | "ExecuteTime": { 641 | "end_time": "2018-05-28T18:48:35.454692Z", 642 | "start_time": "2018-05-28T18:48:35.444682Z" 643 | } 644 | }, 645 | "outputs": [ 646 | { 647 | "name": "stdout", 648 | "output_type": "stream", 649 | "text": [ 650 | "0\n", 651 | "1\n", 652 | "1\n", 653 | "2\n", 654 | "3\n", 655 | "5\n", 656 | "8\n", 657 | "13\n", 658 | "21\n", 659 | "34\n" 660 | ] 661 | } 662 | ], 663 | "source": [ 664 | "def fibonacci(num):\n", 665 | " '''Recursive function to find Fibonacci Sequence'''\n", 666 | " if num == 0 or num== 1:\n", 667 | " return num\n", 668 | " else:\n", 669 | " return fibonacci(num-1) + fibonacci(num-2)\n", 670 | " \n", 671 | "n = 10\n", 672 | "for num in range(n):\n", 673 | " print(fibonacci(num))" 674 | ] 675 | }, 676 | { 677 | "cell_type": "markdown", 678 | "metadata": {}, 679 | "source": [ 680 | "## Assignments" 681 | ] 682 | }, 683 | { 684 | "cell_type": "markdown", 685 | "metadata": {}, 686 | "source": [ 687 | "Q.2- Write a function “perfect()” that determines if parameter number is a perfect number. Use this function in a program that determines and prints all the perfect numbers between 1 and 1000.\n", 688 | "[An integer number is said to be “perfect number” if its factors, including 1(but not the number itself), sum to the number. E.g., 6 is a perfect number because 6=1+2+3].\n", 689 | "\n", 690 | "Q.3- Print multiplication table of 12 using recursion. \n", 691 | "\n", 692 | "Q.4- Write a function to calculate power of a number raised to other ( a^b ) using recursion. \n", 693 | "\n", 694 | "Q.5- Write a function to find factorial of a number but also store the factorials calculated in a dictionary " 695 | ] 696 | }, 697 | { 698 | "cell_type": "markdown", 699 | "metadata": {}, 700 | "source": [ 701 | "## Anonymous or Lambda Functions" 702 | ] 703 | }, 704 | { 705 | "cell_type": "code", 706 | "execution_count": 37, 707 | "metadata": { 708 | "ExecuteTime": { 709 | "end_time": "2018-05-28T18:56:59.658953Z", 710 | "start_time": "2018-05-28T18:56:59.648942Z" 711 | } 712 | }, 713 | "outputs": [ 714 | { 715 | "name": "stdout", 716 | "output_type": "stream", 717 | "text": [ 718 | "100\n" 719 | ] 720 | } 721 | ], 722 | "source": [ 723 | "def square(num):\n", 724 | " '''This function returns square'''\n", 725 | " return num*num\n", 726 | " \n", 727 | "x = square(10)\n", 728 | "print(x)" 729 | ] 730 | }, 731 | { 732 | "cell_type": "code", 733 | "execution_count": 36, 734 | "metadata": { 735 | "ExecuteTime": { 736 | "end_time": "2018-05-28T18:56:02.239044Z", 737 | "start_time": "2018-05-28T18:56:02.234039Z" 738 | } 739 | }, 740 | "outputs": [ 741 | { 742 | "name": "stdout", 743 | "output_type": "stream", 744 | "text": [ 745 | "25\n" 746 | ] 747 | } 748 | ], 749 | "source": [ 750 | "square = lambda x : x**2\n", 751 | "print(square(5))" 752 | ] 753 | } 754 | ], 755 | "metadata": { 756 | "kernelspec": { 757 | "display_name": "Python 3", 758 | "language": "python", 759 | "name": "python3" 760 | }, 761 | "language_info": { 762 | "codemirror_mode": { 763 | "name": "ipython", 764 | "version": 3 765 | }, 766 | "file_extension": ".py", 767 | "mimetype": "text/x-python", 768 | "name": "python", 769 | "nbconvert_exporter": "python", 770 | "pygments_lexer": "ipython3", 771 | "version": "3.6.5" 772 | }, 773 | "toc": { 774 | "nav_menu": {}, 775 | "number_sections": true, 776 | "sideBar": true, 777 | "skip_h1_title": false, 778 | "title_cell": "Table of Contents", 779 | "title_sidebar": "Contents", 780 | "toc_cell": false, 781 | "toc_position": {}, 782 | "toc_section_display": true, 783 | "toc_window_display": false 784 | }, 785 | "varInspector": { 786 | "cols": { 787 | "lenName": 16, 788 | "lenType": 16, 789 | "lenVar": 40 790 | }, 791 | "kernels_config": { 792 | "python": { 793 | "delete_cmd_postfix": "", 794 | "delete_cmd_prefix": "del ", 795 | "library": "var_list.py", 796 | "varRefreshCmd": "print(var_dic_list())" 797 | }, 798 | "r": { 799 | "delete_cmd_postfix": ") ", 800 | "delete_cmd_prefix": "rm(", 801 | "library": "var_list.r", 802 | "varRefreshCmd": "cat(var_dic_list()) " 803 | } 804 | }, 805 | "types_to_exclude": [ 806 | "module", 807 | "function", 808 | "builtin_function_or_method", 809 | "instance", 810 | "_Feature" 811 | ], 812 | "window_display": false 813 | } 814 | }, 815 | "nbformat": 4, 816 | "nbformat_minor": 2 817 | } 818 | -------------------------------------------------------------------------------- /8. OOPs - 1/OOPs_1.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Object Oriented Programming" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "### Class Syntax" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": 1, 20 | "metadata": { 21 | "ExecuteTime": { 22 | "end_time": "2018-05-31T06:29:51.398848Z", 23 | "start_time": "2018-05-31T06:29:51.391831Z" 24 | } 25 | }, 26 | "outputs": [ 27 | { 28 | "name": "stdout", 29 | "output_type": "stream", 30 | "text": [ 31 | "This is my First Class.\n", 32 | "Help on class MyFirstClass in module __main__:\n", 33 | "\n", 34 | "class MyFirstClass(builtins.object)\n", 35 | " | This is my First Class.\n", 36 | " | \n", 37 | " | Data descriptors defined here:\n", 38 | " | \n", 39 | " | __dict__\n", 40 | " | dictionary for instance variables (if defined)\n", 41 | " | \n", 42 | " | __weakref__\n", 43 | " | list of weak references to the object (if defined)\n", 44 | "\n" 45 | ] 46 | } 47 | ], 48 | "source": [ 49 | "class MyFirstClass:\n", 50 | " '''This is my First Class.'''\n", 51 | " pass\n", 52 | " \n", 53 | "print(MyFirstClass.__doc__)\n", 54 | "help(MyFirstClass)" 55 | ] 56 | }, 57 | { 58 | "cell_type": "markdown", 59 | "metadata": {}, 60 | "source": [ 61 | "### Object Creation" 62 | ] 63 | }, 64 | { 65 | "cell_type": "code", 66 | "execution_count": 3, 67 | "metadata": { 68 | "ExecuteTime": { 69 | "end_time": "2018-05-31T00:44:40.430601Z", 70 | "start_time": "2018-05-31T00:44:40.426591Z" 71 | } 72 | }, 73 | "outputs": [], 74 | "source": [ 75 | "my_ref_var = MyFirstClass()" 76 | ] 77 | }, 78 | { 79 | "cell_type": "markdown", 80 | "metadata": {}, 81 | "source": [ 82 | "### Accessing Methods of Class" 83 | ] 84 | }, 85 | { 86 | "cell_type": "code", 87 | "execution_count": 5, 88 | "metadata": { 89 | "ExecuteTime": { 90 | "end_time": "2018-05-31T06:59:54.975039Z", 91 | "start_time": "2018-05-31T06:59:54.968021Z" 92 | } 93 | }, 94 | "outputs": [ 95 | { 96 | "name": "stdout", 97 | "output_type": "stream", 98 | "text": [ 99 | "Hello World\n" 100 | ] 101 | } 102 | ], 103 | "source": [ 104 | "class HelloWorld:\n", 105 | " '''This is my Hello World class'''\n", 106 | " \n", 107 | " def display(self):\n", 108 | " print('Hello World')\n", 109 | " \n", 110 | "h = HelloWorld()\n", 111 | "\n", 112 | "h.display()" 113 | ] 114 | }, 115 | { 116 | "cell_type": "markdown", 117 | "metadata": {}, 118 | "source": [ 119 | "### Constructor" 120 | ] 121 | }, 122 | { 123 | "cell_type": "code", 124 | "execution_count": 26, 125 | "metadata": { 126 | "ExecuteTime": { 127 | "end_time": "2018-05-31T01:42:36.794529Z", 128 | "start_time": "2018-05-31T01:42:36.789527Z" 129 | } 130 | }, 131 | "outputs": [ 132 | { 133 | "name": "stdout", 134 | "output_type": "stream", 135 | "text": [ 136 | "abc 1 90\n" 137 | ] 138 | } 139 | ], 140 | "source": [ 141 | "class Student:\n", 142 | " def __init__(self):\n", 143 | " self.name = 'abc'\n", 144 | " self.rno = 1\n", 145 | " self.marks = 90\n", 146 | " \n", 147 | "s1 = Student()\n", 148 | "\n", 149 | "print(s1.name, s1.rno, s1.marks)" 150 | ] 151 | }, 152 | { 153 | "cell_type": "code", 154 | "execution_count": 8, 155 | "metadata": { 156 | "ExecuteTime": { 157 | "end_time": "2018-05-31T01:04:42.032829Z", 158 | "start_time": "2018-05-31T01:04:42.028818Z" 159 | } 160 | }, 161 | "outputs": [ 162 | { 163 | "name": "stdout", 164 | "output_type": "stream", 165 | "text": [ 166 | "<__main__.Student object at 0x087E0E50>\n" 167 | ] 168 | } 169 | ], 170 | "source": [ 171 | "print(s1)" 172 | ] 173 | }, 174 | { 175 | "cell_type": "code", 176 | "execution_count": 6, 177 | "metadata": { 178 | "ExecuteTime": { 179 | "end_time": "2018-05-31T07:12:30.112227Z", 180 | "start_time": "2018-05-31T07:12:30.105709Z" 181 | } 182 | }, 183 | "outputs": [ 184 | { 185 | "name": "stdout", 186 | "output_type": "stream", 187 | "text": [ 188 | "Constructor is executed\n", 189 | "Constructor is executed\n", 190 | "Constructor is executed\n", 191 | "Function is executed\n", 192 | "Function is executed\n" 193 | ] 194 | } 195 | ], 196 | "source": [ 197 | "class Test:\n", 198 | " def __init__(self):\n", 199 | " print('Constructor is executed')\n", 200 | " \n", 201 | " def fun(self):\n", 202 | " print('Function is executed')\n", 203 | " \n", 204 | "t1 = Test()\n", 205 | "t2 = Test()\n", 206 | "t3 = Test()\n", 207 | "t1.fun()\n", 208 | "t2.fun()\n", 209 | "# def display(self):\n", 210 | "# print('Name: {}, RollNo: {}, Marks: {}'.format(name, rno, marks))\n", 211 | " \n", 212 | "# t3.fun()" 213 | ] 214 | }, 215 | { 216 | "cell_type": "markdown", 217 | "metadata": {}, 218 | "source": [ 219 | "### Constructor With Multiple Parameters" 220 | ] 221 | }, 222 | { 223 | "cell_type": "code", 224 | "execution_count": 11, 225 | "metadata": { 226 | "ExecuteTime": { 227 | "end_time": "2018-05-31T07:33:46.776383Z", 228 | "start_time": "2018-05-31T07:33:46.748825Z" 229 | } 230 | }, 231 | "outputs": [ 232 | { 233 | "name": "stdout", 234 | "output_type": "stream", 235 | "text": [ 236 | "Name: abc, RollNo: 1, Marks: 90\n", 237 | "Name: xyz, RollNo: 2, Marks: 80\n" 238 | ] 239 | }, 240 | { 241 | "data": { 242 | "text/plain": [ 243 | "\"INSTANCE variable should be used using 'self', \\nwhether inside constructor or method\\n\"" 244 | ] 245 | }, 246 | "execution_count": 11, 247 | "metadata": {}, 248 | "output_type": "execute_result" 249 | } 250 | ], 251 | "source": [ 252 | "class Student:\n", 253 | " def __init__(self, name, rno, marks):\n", 254 | " self.name = name\n", 255 | " self.rno = rno\n", 256 | " self.marks = marks\n", 257 | " def display(self):\n", 258 | " print('Name: {}, RollNo: {}, Marks: {}'.\\\n", 259 | " format(self.name, self.rno, self.marks))\n", 260 | "\n", 261 | " \n", 262 | "s1 = Student('abc', 1, 90)\n", 263 | "\n", 264 | "s1.display()\n", 265 | "\n", 266 | "s2 = Student('xyz', 2, 80)\n", 267 | "\n", 268 | "s2.display()\n", 269 | "\n", 270 | "'''INSTANCE variable should be used using 'self', \n", 271 | "whether inside constructor or method\n", 272 | "'''" 273 | ] 274 | }, 275 | { 276 | "cell_type": "markdown", 277 | "metadata": {}, 278 | "source": [ 279 | "## Variables " 280 | ] 281 | }, 282 | { 283 | "cell_type": "markdown", 284 | "metadata": {}, 285 | "source": [ 286 | "### Instance Variable" 287 | ] 288 | }, 289 | { 290 | "cell_type": "code", 291 | "execution_count": 13, 292 | "metadata": { 293 | "ExecuteTime": { 294 | "end_time": "2018-05-31T07:39:52.774447Z", 295 | "start_time": "2018-05-31T07:39:52.767429Z" 296 | } 297 | }, 298 | "outputs": [ 299 | { 300 | "name": "stdout", 301 | "output_type": "stream", 302 | "text": [ 303 | "{'name': 'abc', 'rno': 1, 'marks': 90}\n", 304 | "abc\n", 305 | "Name: abc, RollNo: 1, Marks: 90,\n", 306 | "Name: xyz, RollNo: 2, Marks: 80,\n" 307 | ] 308 | } 309 | ], 310 | "source": [ 311 | "class Student:\n", 312 | " def __init__(self, name, rno, marks):\n", 313 | " self.name = name\n", 314 | " self.rno = rno\n", 315 | " self.marks = marks\n", 316 | " \n", 317 | " def display(self):\n", 318 | " print('Name: {}, RollNo: {}, Marks: {},'.\\\n", 319 | " format(self.name, self.rno, self.marks))\n", 320 | " \n", 321 | "s1 = Student('abc', 1, 90)\n", 322 | "\n", 323 | "print(s1.__dict__)\n", 324 | "\n", 325 | "print(s1.name)\n", 326 | "\n", 327 | "s1.display()\n", 328 | "\n", 329 | "s2 = Student('xyz', 2, 80)\n", 330 | "\n", 331 | "s2.display()" 332 | ] 333 | }, 334 | { 335 | "cell_type": "markdown", 336 | "metadata": {}, 337 | "source": [ 338 | "### Static Variable" 339 | ] 340 | }, 341 | { 342 | "cell_type": "code", 343 | "execution_count": 15, 344 | "metadata": { 345 | "ExecuteTime": { 346 | "end_time": "2018-05-31T07:54:33.058179Z", 347 | "start_time": "2018-05-31T07:54:33.051659Z" 348 | } 349 | }, 350 | "outputs": [ 351 | { 352 | "name": "stdout", 353 | "output_type": "stream", 354 | "text": [ 355 | "Name: abc, RollNo: 1, Marks: 90, College: DIT\n", 356 | "Name: xyz, RollNo: 2, Marks: 80, College: DIT\n" 357 | ] 358 | } 359 | ], 360 | "source": [ 361 | "class Student:\n", 362 | " college = 'DIT'\n", 363 | " def __init__(self, name, rno, marks):\n", 364 | " self.name = name\n", 365 | " self.rno = rno\n", 366 | " self.marks = marks\n", 367 | " \n", 368 | " def display(self):\n", 369 | " print('Name: {}, RollNo: {}, Marks: {},'.\\\n", 370 | " format(self.name, self.rno, self.marks), end=' ')\n", 371 | " print('College:', self.college)\n", 372 | " \n", 373 | "s1 = Student('abc', 1, 90)\n", 374 | "\n", 375 | "s1.display()\n", 376 | "\n", 377 | "s2 = Student('xyz', 2, 80)\n", 378 | "\n", 379 | "s2.display()" 380 | ] 381 | }, 382 | { 383 | "cell_type": "code", 384 | "execution_count": 16, 385 | "metadata": { 386 | "ExecuteTime": { 387 | "end_time": "2018-05-31T07:56:03.118244Z", 388 | "start_time": "2018-05-31T07:56:03.115236Z" 389 | } 390 | }, 391 | "outputs": [], 392 | "source": [ 393 | "# # Lets try to change the STATIC variable using s1\n", 394 | "\n", 395 | "s1.college = 'UIT'\n", 396 | "\n", 397 | "s1.display()\n", 398 | "\n", 399 | "s2.display()" 400 | ] 401 | }, 402 | { 403 | "cell_type": "markdown", 404 | "metadata": {}, 405 | "source": [ 406 | "### Local Variable" 407 | ] 408 | }, 409 | { 410 | "cell_type": "code", 411 | "execution_count": 18, 412 | "metadata": { 413 | "ExecuteTime": { 414 | "end_time": "2018-05-31T08:01:15.905176Z", 415 | "start_time": "2018-05-31T08:01:15.900162Z" 416 | } 417 | }, 418 | "outputs": [ 419 | { 420 | "name": "stdout", 421 | "output_type": "stream", 422 | "text": [ 423 | "10\n", 424 | "20\n" 425 | ] 426 | } 427 | ], 428 | "source": [ 429 | "class Test:\n", 430 | " def m1(self):\n", 431 | " x = 10\n", 432 | " print(x)\n", 433 | " def m2(self):\n", 434 | " y = 20\n", 435 | " print(y)\n", 436 | " \n", 437 | "t = Test()\n", 438 | "t.m1()\n", 439 | "t.m2()" 440 | ] 441 | }, 442 | { 443 | "cell_type": "markdown", 444 | "metadata": {}, 445 | "source": [ 446 | "## Setters & Getters" 447 | ] 448 | }, 449 | { 450 | "cell_type": "code", 451 | "execution_count": 19, 452 | "metadata": { 453 | "ExecuteTime": { 454 | "end_time": "2018-05-31T08:07:08.913643Z", 455 | "start_time": "2018-05-31T08:07:08.898091Z" 456 | } 457 | }, 458 | "outputs": [ 459 | { 460 | "name": "stdout", 461 | "output_type": "stream", 462 | "text": [ 463 | "Name: abc, RollNo: 1, Marks: 90, College: UIT\n", 464 | "Name: xyz, RollNo: 2, Marks: 80, College: DIT\n" 465 | ] 466 | } 467 | ], 468 | "source": [ 469 | "class Student:\n", 470 | " college = 'DIT'\n", 471 | " def __init__(self, name, rno, marks):\n", 472 | " self.name = name\n", 473 | " self.rno = rno\n", 474 | " self.marks = marks\n", 475 | " \n", 476 | " def display(self):\n", 477 | " print('Name: {}, RollNo: {}, Marks: {},'.format(self.name, self.rno, self.marks), end=' ')\n", 478 | " print('College:', self.college)\n", 479 | " \n", 480 | " def setCollege(self, cname):\n", 481 | " self.college = cname\n", 482 | " \n", 483 | " def getName(self):\n", 484 | " return self.name\n", 485 | " \n", 486 | "s1 = Student('abc', 1, 90)\n", 487 | "\n", 488 | "s1.setCollege('UIT')\n", 489 | "\n", 490 | "s1.display()\n", 491 | "\n", 492 | "s2 = Student('xyz', 2, 80)\n", 493 | "\n", 494 | "s2.display()" 495 | ] 496 | }, 497 | { 498 | "cell_type": "code", 499 | "execution_count": 20, 500 | "metadata": { 501 | "ExecuteTime": { 502 | "end_time": "2018-05-31T08:08:19.518050Z", 503 | "start_time": "2018-05-31T08:08:19.513040Z" 504 | } 505 | }, 506 | "outputs": [ 507 | { 508 | "data": { 509 | "text/plain": [ 510 | "'xyz'" 511 | ] 512 | }, 513 | "execution_count": 20, 514 | "metadata": {}, 515 | "output_type": "execute_result" 516 | } 517 | ], 518 | "source": [ 519 | "s2.getName()" 520 | ] 521 | }, 522 | { 523 | "cell_type": "markdown", 524 | "metadata": {}, 525 | "source": [ 526 | "## Assignment" 527 | ] 528 | }, 529 | { 530 | "cell_type": "markdown", 531 | "metadata": {}, 532 | "source": [ 533 | "Q.1- Create a circle class and initialize it with radius. Make two methods getArea and getCircumference inside this class.\n", 534 | "\n", 535 | "Q.2- Create a Student class and initialize it with name and roll number. Make methods to :\n", 536 | "1. Display - It should display all informations of the student.\n", 537 | "2. setAge - It should assign age to student \n", 538 | "3. setMarks - It should assign marks to the student.\n", 539 | "\n", 540 | "Q.3- Create a Temprature class. Make two methods :\n", 541 | "1. convertFahrenheit - It will take celsius and will print it into Fahrenheit.\n", 542 | "2. convertCelsius - It will take Fahrenheit and will convert it into Celsius.\n", 543 | "\n", 544 | "Q.4- Create a class MovieDetails and initialize it with artistname,Year of release and ratings .\n", 545 | "Make methods to\n", 546 | "1. Display-Display the details.\n", 547 | "2. Add- Add the movie details.\n", 548 | "\n", 549 | "Q.5- Create a class Expenditure and initialize it with salary,savings, category , total expenditure.Make the following methods.\n", 550 | "1. Add expenditure according to category .\n", 551 | "2. Calculate total expenditure.\n", 552 | "3. Calculate per day and monthly expenditure.\n", 553 | "\n" 554 | ] 555 | } 556 | ], 557 | "metadata": { 558 | "kernelspec": { 559 | "display_name": "Python 3", 560 | "language": "python", 561 | "name": "python3" 562 | }, 563 | "language_info": { 564 | "codemirror_mode": { 565 | "name": "ipython", 566 | "version": 3 567 | }, 568 | "file_extension": ".py", 569 | "mimetype": "text/x-python", 570 | "name": "python", 571 | "nbconvert_exporter": "python", 572 | "pygments_lexer": "ipython3", 573 | "version": "3.6.5" 574 | }, 575 | "toc": { 576 | "nav_menu": {}, 577 | "number_sections": true, 578 | "sideBar": true, 579 | "skip_h1_title": false, 580 | "title_cell": "Table of Contents", 581 | "title_sidebar": "Contents", 582 | "toc_cell": false, 583 | "toc_position": {}, 584 | "toc_section_display": true, 585 | "toc_window_display": false 586 | }, 587 | "varInspector": { 588 | "cols": { 589 | "lenName": 16, 590 | "lenType": 16, 591 | "lenVar": 40 592 | }, 593 | "kernels_config": { 594 | "python": { 595 | "delete_cmd_postfix": "", 596 | "delete_cmd_prefix": "del ", 597 | "library": "var_list.py", 598 | "varRefreshCmd": "print(var_dic_list())" 599 | }, 600 | "r": { 601 | "delete_cmd_postfix": ") ", 602 | "delete_cmd_prefix": "rm(", 603 | "library": "var_list.r", 604 | "varRefreshCmd": "cat(var_dic_list()) " 605 | } 606 | }, 607 | "types_to_exclude": [ 608 | "module", 609 | "function", 610 | "builtin_function_or_method", 611 | "instance", 612 | "_Feature" 613 | ], 614 | "window_display": false 615 | } 616 | }, 617 | "nbformat": 4, 618 | "nbformat_minor": 2 619 | } 620 | -------------------------------------------------------------------------------- /Assignments/1 - Intro to Python.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Singh-K01/Python-tutorials-Kanav-Bansal/05065cb71ab0a5b9ff17b5befe485294c8395d47/Assignments/1 - Intro to Python.docx -------------------------------------------------------------------------------- /Assignments/10 - Threads and Processes.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Singh-K01/Python-tutorials-Kanav-Bansal/05065cb71ab0a5b9ff17b5befe485294c8395d47/Assignments/10 - Threads and Processes.docx -------------------------------------------------------------------------------- /Assignments/11 - Web Basics and API.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Singh-K01/Python-tutorials-Kanav-Bansal/05065cb71ab0a5b9ff17b5befe485294c8395d47/Assignments/11 - Web Basics and API.docx -------------------------------------------------------------------------------- /Assignments/12 - Exception Handling.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Singh-K01/Python-tutorials-Kanav-Bansal/05065cb71ab0a5b9ff17b5befe485294c8395d47/Assignments/12 - Exception Handling.docx -------------------------------------------------------------------------------- /Assignments/13 - File Handling.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Singh-K01/Python-tutorials-Kanav-Bansal/05065cb71ab0a5b9ff17b5befe485294c8395d47/Assignments/13 - File Handling.docx -------------------------------------------------------------------------------- /Assignments/14 - Regular Expressions.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Singh-K01/Python-tutorials-Kanav-Bansal/05065cb71ab0a5b9ff17b5befe485294c8395d47/Assignments/14 - Regular Expressions.docx -------------------------------------------------------------------------------- /Assignments/15 - Databses in Python.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Singh-K01/Python-tutorials-Kanav-Bansal/05065cb71ab0a5b9ff17b5befe485294c8395d47/Assignments/15 - Databses in Python.docx -------------------------------------------------------------------------------- /Assignments/16 - GUI - I.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Singh-K01/Python-tutorials-Kanav-Bansal/05065cb71ab0a5b9ff17b5befe485294c8395d47/Assignments/16 - GUI - I.docx -------------------------------------------------------------------------------- /Assignments/17 - GUI - II.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Singh-K01/Python-tutorials-Kanav-Bansal/05065cb71ab0a5b9ff17b5befe485294c8395d47/Assignments/17 - GUI - II.docx -------------------------------------------------------------------------------- /Assignments/18 - Pandas.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Singh-K01/Python-tutorials-Kanav-Bansal/05065cb71ab0a5b9ff17b5befe485294c8395d47/Assignments/18 - Pandas.docx -------------------------------------------------------------------------------- /Assignments/2 - Data Types I.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Singh-K01/Python-tutorials-Kanav-Bansal/05065cb71ab0a5b9ff17b5befe485294c8395d47/Assignments/2 - Data Types I.docx -------------------------------------------------------------------------------- /Assignments/3 - Data Types II.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Singh-K01/Python-tutorials-Kanav-Bansal/05065cb71ab0a5b9ff17b5befe485294c8395d47/Assignments/3 - Data Types II.docx -------------------------------------------------------------------------------- /Assignments/4 - Decision Control System.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Singh-K01/Python-tutorials-Kanav-Bansal/05065cb71ab0a5b9ff17b5befe485294c8395d47/Assignments/4 - Decision Control System.docx -------------------------------------------------------------------------------- /Assignments/5 - Loops.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Singh-K01/Python-tutorials-Kanav-Bansal/05065cb71ab0a5b9ff17b5befe485294c8395d47/Assignments/5 - Loops.docx -------------------------------------------------------------------------------- /Assignments/6 - Functions.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Singh-K01/Python-tutorials-Kanav-Bansal/05065cb71ab0a5b9ff17b5befe485294c8395d47/Assignments/6 - Functions.docx -------------------------------------------------------------------------------- /Assignments/7 - Common Modules in Python.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Singh-K01/Python-tutorials-Kanav-Bansal/05065cb71ab0a5b9ff17b5befe485294c8395d47/Assignments/7 - Common Modules in Python.docx -------------------------------------------------------------------------------- /Assignments/8 - Classes and Modules I.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Singh-K01/Python-tutorials-Kanav-Bansal/05065cb71ab0a5b9ff17b5befe485294c8395d47/Assignments/8 - Classes and Modules I.docx -------------------------------------------------------------------------------- /Assignments/9 - Classes and Modules II.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Singh-K01/Python-tutorials-Kanav-Bansal/05065cb71ab0a5b9ff17b5befe485294c8395d47/Assignments/9 - Classes and Modules II.docx -------------------------------------------------------------------------------- /Helicopter/Helicopter.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Singh-K01/Python-tutorials-Kanav-Bansal/05065cb71ab0a5b9ff17b5befe485294c8395d47/Helicopter/Helicopter.png -------------------------------------------------------------------------------- /Helicopter/helicopter_game.py: -------------------------------------------------------------------------------- 1 | import pygame 2 | import time 3 | from random import randint,randrange 4 | 5 | black = (0,0,0) 6 | white = (255,255,255) 7 | 8 | sunset = (253,72,47) 9 | 10 | greenyellow = (184,255,0) 11 | brightblue = (47,228,253) 12 | orange = (255,113,0) 13 | yellow = (255,236,0) 14 | purple = (252,67,255) 15 | 16 | colorChoices = [greenyellow,brightblue,orange,yellow,purple] 17 | 18 | pygame.init() 19 | 20 | surfaceWidth = 800 21 | surfaceHeight = 500 22 | 23 | imageHeight = 43 24 | imageWidth = 100 25 | 26 | surface = pygame.display.set_mode((surfaceWidth,surfaceHeight)) 27 | pygame.display.set_caption('Helicopter') 28 | clock = pygame.time.Clock() 29 | 30 | img = pygame.image.load('Helicopter.png') 31 | 32 | def score(count): 33 | font = pygame.font.Font('freesansbold.ttf', 20) 34 | text = font.render("Score: "+str(count), True, white) 35 | surface.blit(text, [0,0]) 36 | 37 | 38 | def blocks(x_block, y_block, block_width, block_height, gap, colorChoice): 39 | 40 | pygame.draw.rect(surface, colorChoice, [x_block,y_block,block_width,block_height]) 41 | pygame.draw.rect(surface, colorChoice, [x_block,y_block+block_height+gap,block_width, surfaceHeight]) 42 | 43 | 44 | def replay_or_quit(): 45 | for event in pygame.event.get([pygame.KEYDOWN, pygame.KEYUP, pygame.QUIT]): 46 | if event.type == pygame.QUIT: 47 | pygame.quit() 48 | quit() 49 | 50 | elif event.type == pygame.KEYDOWN: 51 | continue 52 | 53 | return event.key 54 | 55 | return None 56 | 57 | def makeTextObjs(text, font): 58 | textSurface = font.render(text, True, sunset) 59 | return textSurface, textSurface.get_rect() 60 | 61 | def msgSurface(text): 62 | smallText = pygame.font.Font('freesansbold.ttf', 20) 63 | largeText = pygame.font.Font('freesansbold.ttf', 150) 64 | 65 | titleTextSurf, titleTextRect = makeTextObjs(text, largeText) 66 | titleTextRect.center = surfaceWidth / 2, surfaceHeight / 2 67 | surface.blit(titleTextSurf, titleTextRect) 68 | 69 | typTextSurf, typTextRect = makeTextObjs('Press any key to continue', smallText) 70 | typTextRect.center = surfaceWidth / 2, ((surfaceHeight / 2) + 100) 71 | surface.blit(typTextSurf, typTextRect) 72 | 73 | pygame.display.update() 74 | time.sleep(1) 75 | 76 | while replay_or_quit() == None: 77 | clock.tick() 78 | 79 | main() 80 | 81 | 82 | 83 | def gameOver(): 84 | msgSurface('Kaboom!') 85 | 86 | def helicopter(x, y, image): 87 | surface.blit(img, (x,y)) 88 | 89 | 90 | def main(): 91 | x = 150 92 | y = 200 93 | y_move = 0 94 | 95 | x_block = surfaceWidth 96 | y_block = 0 97 | 98 | block_width = 75 99 | block_height = randint(0,(surfaceHeight/2)) 100 | gap = imageHeight * 3 101 | block_move = 4 102 | current_score = 0 103 | 104 | 105 | blockColor = colorChoices[randrange(0,len(colorChoices))] 106 | 107 | game_over = False 108 | 109 | while not game_over: 110 | 111 | for event in pygame.event.get(): 112 | if event.type == pygame.QUIT: 113 | game_over = True 114 | 115 | if event.type == pygame.KEYDOWN: 116 | if event.key == pygame.K_UP: 117 | y_move = -5 118 | 119 | if event.type == pygame.KEYUP: 120 | if event.key == pygame.K_UP: 121 | y_move = 5 122 | 123 | y += y_move 124 | 125 | surface.fill(black) 126 | helicopter(x ,y, img) 127 | 128 | 129 | blocks(x_block, y_block, block_width, block_height, gap, blockColor) 130 | score(current_score) 131 | x_block -= block_move 132 | 133 | if y > surfaceHeight-40 or y < 0: 134 | gameOver() 135 | 136 | if x_block < (-1*block_width): 137 | x_block = surfaceWidth 138 | block_height = randint(0, (surfaceHeight / 2)) 139 | blockColor = colorChoices[randrange(0,len(colorChoices))] 140 | current_score+=1 141 | 142 | if x + imageWidth > x_block: 143 | if x < x_block + block_width: 144 | if y < block_height: 145 | if x - imageWidth < block_width + x_block: 146 | gameOver() 147 | 148 | if x + imageWidth > x_block: 149 | if y + imageHeight > block_height+gap: 150 | if x < block_width + x_block: 151 | gameOver() 152 | 153 | #if x_block < (x - block_width) < x_block + block_move: 154 | # current_score += 1 155 | 156 | if 3 <= current_score < 5: 157 | block_move = 5 158 | gap = imageHeight * 2.9 159 | if 5 <= current_score < 8: 160 | block_move = 6 161 | gap = imageHeight *2.8 162 | if 8 <= current_score < 14: 163 | block_move = 7 164 | gap = imageHeight *2.7 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | pygame.display.update() 173 | clock.tick(60) 174 | 175 | main() 176 | pygame.quit() 177 | quit() 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | 194 | -------------------------------------------------------------------------------- /Library Management System/backend.py: -------------------------------------------------------------------------------- 1 | import pymysql as pm 2 | 3 | def connectdb(): 4 | try: 5 | con = pm.connect(host='localhost', database='books', \ 6 | user='root', password='root') 7 | cursor = con.cursor() 8 | query = 'create table if not exists book(id int(5) primary key auto_increment, title varchar(10), author varchar(10), year int(4), isbn int(20))' 9 | cursor.execute(query) 10 | con.commit() 11 | 12 | except pm.DatabaseError as e: 13 | if con: 14 | con.rollback() 15 | print('Problem occured: ', e) 16 | 17 | finally: 18 | if cursor: 19 | cursor.close() 20 | if con: 21 | con.close() 22 | 23 | def insert(title, author, year, isbn): 24 | try: 25 | con = pm.connect(host='localhost', database='books', \ 26 | user='root', password='root') 27 | cursor = con.cursor() 28 | query = 'insert into book values (NULL, %s, %s, %s, %s)' 29 | data = (title, author, year, isbn) 30 | cursor.execute(query, data) 31 | con.commit() 32 | 33 | except pm.DatabaseError as e: 34 | if con: 35 | con.rollback() 36 | print('Problem occured: ', e) 37 | 38 | finally: 39 | if cursor: 40 | cursor.close() 41 | if con: 42 | con.close() 43 | 44 | def view(): 45 | try: 46 | con = pm.connect(host='localhost', database='books', \ 47 | user='root', password='root') 48 | cursor = con.cursor() 49 | query = 'select * from book' 50 | cursor.execute(query) 51 | data = cursor.fetchall() 52 | return data 53 | 54 | except pm.DatabaseError as e: 55 | if con: 56 | con.rollback() 57 | print('Problem occured: ', e) 58 | 59 | finally: 60 | if cursor: 61 | cursor.close() 62 | if con: 63 | con.close() 64 | 65 | def search(title='', author='', year='', isbn=''): 66 | try: 67 | con = pm.connect(host='localhost', database='books', \ 68 | user='root', password='root') 69 | cursor = con.cursor() 70 | query = 'select * from book where title=%s OR author=%s OR year=%s OR isbn=%s' 71 | d = (title, author, year, isbn) 72 | cursor.execute(query, d) 73 | data = cursor.fetchall() 74 | return data 75 | 76 | except pm.DatabaseError as e: 77 | if con: 78 | con.rollback() 79 | print('Problem occured: ', e) 80 | 81 | finally: 82 | if cursor: 83 | cursor.close() 84 | if con: 85 | con.close() 86 | 87 | def delete(id): 88 | try: 89 | con = pm.connect(host='localhost', database='books', \ 90 | user='root', password='root') 91 | cursor = con.cursor() 92 | query = 'delete from book where id=%s' 93 | cursor.execute(query, id) 94 | con.commit() 95 | 96 | except pm.DatabaseError as e: 97 | if con: 98 | con.rollback() 99 | print('Problem occured: ', e) 100 | 101 | finally: 102 | if cursor: 103 | cursor.close() 104 | if con: 105 | con.close() 106 | 107 | def update(id, title, author, year, isbn): 108 | try: 109 | con = pm.connect(host='localhost', database='books', \ 110 | user='root', password='root') 111 | cursor = con.cursor() 112 | query = 'update book set title=%s, author=%s, year=%s, isbn=%s where id=%s' 113 | data = (title, author, year, isbn, id) 114 | cursor.execute(query, data) 115 | con.commit() 116 | 117 | except pm.DatabaseError as e: 118 | if con: 119 | con.rollback() 120 | print('Problem occured: ', e) 121 | 122 | finally: 123 | if cursor: 124 | cursor.close() 125 | if con: 126 | con.close() 127 | -------------------------------------------------------------------------------- /Library Management System/frontend.py: -------------------------------------------------------------------------------- 1 | from tkinter import * 2 | from backend import * 3 | 4 | root = Tk() 5 | 6 | root.title('Library Management System') 7 | 8 | def get_data(): 9 | title=e1.get() 10 | author=e2.get() 11 | year=e3.get() 12 | isbn=e4.get() 13 | return title, author, year, isbn 14 | 15 | 16 | def get_id(): 17 | row = lstbox.curselection() 18 | myString = lstbox.get(row) 19 | myString = myString.split() 20 | id = myString[0] 21 | return (id,) 22 | 23 | 24 | def front_fill(event): 25 | row = lstbox.curselection() 26 | myString = lstbox.get(row) 27 | myString = myString.split(' | ') 28 | title_text.set(myString[1]) 29 | author_text.set(myString[2]) 30 | year_text.set(myString[3]) 31 | isbn_text.set(myString[4]) 32 | 33 | 34 | def front_view(): 35 | lstbox.delete(0, END) 36 | front_data = view() 37 | for row in front_data: 38 | lstbox.insert(END, str(row[0])+' | '+row[1]+' | '+row[2]\ 39 | + ' | '+str(row[3])+' | '+str(row[4])) 40 | 41 | 42 | def front_search(): 43 | lstbox.delete(0, END) 44 | title, author, year, isbn = get_data() 45 | front_data = search(title, author, year, isbn) 46 | for row in front_data: 47 | lstbox.insert(END, str(row[0]) + ' | ' + row[1] + ' | ' + row[2]\ 48 | + ' | ' + str(row[3]) + ' | ' + str(row[4])) 49 | 50 | 51 | def front_add(): 52 | title, author, year, isbn = get_data() 53 | insert(title, author, year, isbn) 54 | front_view() 55 | 56 | 57 | def front_update(): 58 | id = get_id() 59 | title, author, year, isbn = get_data() 60 | update(id, title, author, year, isbn) 61 | front_view() 62 | 63 | 64 | def front_delete(): 65 | id = get_id() 66 | delete(id) 67 | front_view() 68 | 69 | 70 | # define Labels 71 | l1 = Label(root, text='Title') 72 | l1.grid(row=0, column=0) 73 | 74 | l2 = Label(root, text='Author') 75 | l2.grid(row=0, column=2) 76 | 77 | l3 = Label(root, text='Year') 78 | l3.grid(row=1, column=0) 79 | 80 | l4 = Label(root, text='ISBN') 81 | l4.grid(row=1, column=2) 82 | 83 | # define Entries 84 | title_text=StringVar() 85 | e1=Entry(root,textvariable=title_text) 86 | e1.grid(row=0, column=1) 87 | 88 | author_text=StringVar() 89 | e2=Entry(root,textvariable=author_text) 90 | e2.grid(row=0, column=3) 91 | 92 | year_text=StringVar() 93 | e3=Entry(root,textvariable=year_text) 94 | e3.grid(row=1, column=1) 95 | 96 | isbn_text=StringVar() 97 | e4=Entry(root,textvariable=isbn_text) 98 | e4.grid(row=1, column=3) 99 | 100 | # define Listbox 101 | lstbox = Listbox(root, height=6, width=35) 102 | lstbox.grid(row=2, column=0, rowspan=6, columnspan=2) 103 | lstbox.bind('', front_fill) 104 | 105 | 106 | # add scrollbar to listbox 107 | sb = Scrollbar(root) 108 | sb.grid(row=2, column=2, rowspan=6) 109 | 110 | lstbox.configure(yscrollcommand=sb.set) 111 | sb.configure(command=lstbox.yview) 112 | 113 | # define Buttons 114 | b1 = Button(root, text='View All', width=12, command=front_view) 115 | b1.grid(row=2, column=3) 116 | 117 | b1 = Button(root, text='Search Entry', width=12, command=front_search) 118 | b1.grid(row=3, column=3) 119 | 120 | b1 = Button(root, text='Add Entry', width=12, command=front_add) 121 | b1.grid(row=4, column=3) 122 | 123 | b1 = Button(root, text='Update Selected', width=12, command=front_update) 124 | b1.grid(row=5, column=3) 125 | 126 | b1 = Button(root, text='Delete Selected', width=12, command=front_delete) 127 | b1.grid(row=6, column=3) 128 | 129 | b1 = Button(root, text='Close', width=12, command=root.destroy) 130 | b1.grid(row=7, column=3) 131 | 132 | connectdb() 133 | front_view() 134 | 135 | root.resizable(False, False) 136 | 137 | root.mainloop() 138 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # python_notes 2 | 3 | ## Chapters 4 | 1. Intro to Python
5 | 2. Data Types- 1
6 | 3. Data Types- 2
7 | 4. Decision Statement
8 | 5. Loops
9 | 6. Functions
10 | 7. Some important Modules
11 | 8. OOPs - 1
12 | 9. OOPs - 2
13 | 10. Threads
14 | 11. Exception Handling
15 | 12. File Handling
16 | 13. Regular Expression
17 | 14. Databases - MySQL
18 | 15. GUI - 1
19 | 16. GUI - 2
20 | 17. Numpy
21 | 18. Pandas
22 | 23 | ## Other Stuff 24 | 1. Assignments
25 | 2. Mini Project - Helicopter Game
26 | 3. Mini Project - Car Game
27 | 4. Mini Project - Library Management System
28 | 29 | ## References 30 | Time Complexity Analysis CLICK HERE 31 | 32 | -------------------------------------------------------------------------------- /RaceCar/Helicopter.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Singh-K01/Python-tutorials-Kanav-Bansal/05065cb71ab0a5b9ff17b5befe485294c8395d47/RaceCar/Helicopter.png -------------------------------------------------------------------------------- /RaceCar/racecar.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Singh-K01/Python-tutorials-Kanav-Bansal/05065cb71ab0a5b9ff17b5befe485294c8395d47/RaceCar/racecar.png -------------------------------------------------------------------------------- /RaceCar/racecar.py: -------------------------------------------------------------------------------- 1 | import time 2 | 3 | import pygame 4 | 5 | # initiating pygame 6 | pygame.init() 7 | 8 | display_width = 800 9 | display_height = 500 10 | 11 | # Picking the resolution of our display 12 | surface = pygame.display.set_mode((display_width, display_height)) 13 | 14 | pygame.display.set_caption('Helicopter') 15 | 16 | black = (0, 0, 0) # RGB 17 | white = (255, 255, 255) 18 | 19 | helicopter = pygame.image.load('Helicopter.png') 20 | x = 150 21 | y = 200 22 | 23 | y_move = 0 24 | 25 | #################################### 26 | 27 | 28 | def Helicopter(x, y): 29 | surface.blit(helicopter, (x, y)) 30 | 31 | 32 | # def msgSurface(text): 33 | # smallText = pygame.font.Font('freesansbold.ttf', 20) 34 | # largeText = pygame.font.Font('freesansbold.ttf', 150) 35 | # 36 | # titleTextSurf, titleTextRect = makeTextObjs(text, largeText) 37 | # titleTextRect.center = display_width/2, display_height/2 38 | # surface.blit(titleTextSurf, titleTextRect) 39 | # 40 | # typTextSurf, typTextRect = makeTextObjs('Press any key to continue', smallText) 41 | # titleTextRect.center = display_width/2, ((display_height/2)+100) 42 | # surface.blit(typTextSurf, typTextRect) 43 | # 44 | # pygame.display.update() 45 | # time.sleep(1) 46 | # 47 | 48 | 49 | def gameOver(): 50 | # msgSurface('Kaboom!') 51 | quit() 52 | 53 | 54 | #################################### 55 | 56 | # Measuring FPS-Frame per seconds 57 | clock = pygame.time.Clock() 58 | 59 | # Creating a game loop which will run many times a second 60 | game_over = False 61 | 62 | while not game_over: 63 | for event in pygame.event.get(): 64 | if event.type == pygame.QUIT: 65 | game_over = True 66 | 67 | ################################### 68 | if event.type == pygame.KEYDOWN: # if a key is pressed 69 | if event.key == pygame.K_UP: # if that key is UP arrow key 70 | y_move = -5 71 | if event.type == pygame.KEYUP: # if we released a key 72 | if event.key == pygame.K_UP: 73 | y_move = 5 74 | ################################### 75 | 76 | y += y_move # y = y + y_move 77 | 78 | surface.fill(black) 79 | Helicopter(x, y) 80 | 81 | if y > display_height-43 or y < 0: 82 | gameOver() 83 | 84 | pygame.display.update() # update the display 85 | clock.tick(60) # run the above code 60 times per second 86 | 87 | pygame.quit() 88 | 89 | quit() 90 | -------------------------------------------------------------------------------- /car-game-master/README.md: -------------------------------------------------------------------------------- 1 | # car_game 2 | game built using pygame 3 | By Harshit Ahluwalia 4 | -------------------------------------------------------------------------------- /car-game-master/__pycache__/pygame_car.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Singh-K01/Python-tutorials-Kanav-Bansal/05065cb71ab0a5b9ff17b5befe485294c8395d47/car-game-master/__pycache__/pygame_car.cpython-36.pyc -------------------------------------------------------------------------------- /car-game-master/images/car.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Singh-K01/Python-tutorials-Kanav-Bansal/05065cb71ab0a5b9ff17b5befe485294c8395d47/car-game-master/images/car.png -------------------------------------------------------------------------------- /car-game-master/images/car1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Singh-K01/Python-tutorials-Kanav-Bansal/05065cb71ab0a5b9ff17b5befe485294c8395d47/car-game-master/images/car1.png -------------------------------------------------------------------------------- /car-game-master/images/carcrash.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Singh-K01/Python-tutorials-Kanav-Bansal/05065cb71ab0a5b9ff17b5befe485294c8395d47/car-game-master/images/carcrash.png -------------------------------------------------------------------------------- /car-game-master/images/trees.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Singh-K01/Python-tutorials-Kanav-Bansal/05065cb71ab0a5b9ff17b5befe485294c8395d47/car-game-master/images/trees.jpg -------------------------------------------------------------------------------- /car-game-master/music/atlanta.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Singh-K01/Python-tutorials-Kanav-Bansal/05065cb71ab0a5b9ff17b5befe485294c8395d47/car-game-master/music/atlanta.wav -------------------------------------------------------------------------------- /car-game-master/music/coffee_stains.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Singh-K01/Python-tutorials-Kanav-Bansal/05065cb71ab0a5b9ff17b5befe485294c8395d47/car-game-master/music/coffee_stains.wav -------------------------------------------------------------------------------- /car-game-master/music/crash.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Singh-K01/Python-tutorials-Kanav-Bansal/05065cb71ab0a5b9ff17b5befe485294c8395d47/car-game-master/music/crash.wav -------------------------------------------------------------------------------- /car-game-master/pygame_car.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | """ 4 | Hi everyone 5 | This is a simple car game developed using pygame witn python 3 6 | 7 | """ 8 | __author__ = "Mohamed Hamidat, C# and python Developer, hamidatmohamed@yahoo.fr" 9 | 10 | import pygame 11 | import time 12 | import random 13 | 14 | 15 | 16 | #display dimension 17 | display_width = 800 18 | display_height = 600 19 | 20 | #car dimension 21 | car_width = 70 22 | car_height = 140 23 | 24 | #colors 25 | black = (0,0,0) 26 | white = (255,255,255) 27 | blue =(53, 115, 255) 28 | red = (200,0,0) 29 | green = (0, 200, 0) 30 | bright_red = (255,0,0) 31 | bright_green = (0,255,0) 32 | pause = False 33 | score_game = 0 34 | 35 | game_display = pygame.display.set_mode((display_width, display_height)) 36 | clock = pygame.time.Clock() 37 | #game setup 38 | def game_init(): 39 | pygame.init() 40 | pygame.display.set_caption('Fast_and_Curious/Mohamed') 41 | 42 | #game_icon = pygame.image.load('carIcon.png') 43 | #pygame.display.set_icon(game_icon) 44 | 45 | #backgroundImg = pygame.image.load ('way.png') 46 | 47 | ##############---------FONCTIONS--------------################## 48 | 49 | def display(count, x, y, message_format = 'Dodged: %d'): 50 | """display the score""" 51 | # max_dodged = 10 52 | font = pygame.font.SysFont("comicsansms", 20) 53 | text = font.render(message_format%count, True, black) 54 | game_display.blit(text, (x, y)) 55 | 56 | def things(thingX, thingY, thingW, thingH, color): 57 | """draw random things (car or anything)""" 58 | pygame.draw.rect(game_display, color, [thingX, thingY, thingW, thingH]) 59 | 60 | 61 | def line(lineX, lineY, lineW, lineH, color): 62 | """draw way lines """ 63 | pygame.draw.rect(game_display, color, [lineX, lineY, lineW, lineH]) 64 | 65 | 66 | def load_image(x , y, image_name): 67 | img = pygame.image.load(image_name) 68 | game_display.blit(img, (x, y)) 69 | 70 | def text_object(text, font): 71 | textSurface = font.render(text, True, black) 72 | return textSurface, textSurface.get_rect() 73 | 74 | def message_display(text): 75 | """display message after crash""" 76 | largeText = pygame.font.SysFont("comicsansms",115) 77 | textSurf, textRect = text_object(text, largeText) 78 | textRect.center = ((display_width/2) , (display_height/2)) 79 | game_display.blit(textSurf, textRect) 80 | 81 | pygame.display.update() 82 | 83 | time.sleep(2) 84 | 85 | game_loop() 86 | 87 | 88 | def crash(x, y): 89 | car_crash = pygame.image.load('images/carcrash.png') 90 | game_display.blit(car_crash, ((x - 45), (y - 30))) 91 | crash_sound = pygame.mixer.Sound("music/crash.wav") 92 | pygame.mixer.Sound.play(crash_sound) 93 | pygame.mixer.music.stop() 94 | largeText = pygame.font.SysFont("comicsansms",90) 95 | textSurf, textRect = text_object("You Crashed!", largeText) 96 | textRect.center = ((display_width/2) , (display_height/4)) 97 | game_display.blit(textSurf, textRect) 98 | 99 | while True: 100 | for event in pygame.event.get(): 101 | if event.type == pygame.QUIT: 102 | pygame.quit() 103 | quit() 104 | 105 | 106 | 107 | button("Play Again", 150,250,100,50, green, bright_green, game_loop) 108 | button("Quit", 550,250,100,50, red, bright_red, quitgame) 109 | 110 | 111 | pygame.display.update() 112 | clock.tick(15) 113 | 114 | 115 | def button(msg, x, y, w, h, ic, ac, action=None): 116 | """message, dimension, active/inactive color""" 117 | 118 | mouse = pygame.mouse.get_pos() 119 | click = pygame.mouse.get_pressed() 120 | #print(mouse) 121 | 122 | if x+w > mouse[0] > x and y+h > mouse[1] > y: 123 | pygame.draw.rect(game_display, ac,(x, y,w,h)) 124 | if click[0] == 1 and action != None: 125 | action() 126 | 127 | else: 128 | pygame.draw.rect(game_display, ic,(x, y,w,h)) 129 | 130 | smallText = pygame.font.SysFont("comicsansms",20) 131 | textSurf, textRect = text_object(msg, smallText) 132 | textRect.center = ( (x+(w/2)), (y+(h/2)) ) 133 | game_display.blit(textSurf, textRect) 134 | 135 | 136 | def quitgame(): 137 | pygame.quit() 138 | quit() 139 | 140 | def game_unpause(): 141 | global pause 142 | pause = False 143 | 144 | def game_pause(): 145 | ############ 146 | pygame.mixer.music.pause() 147 | ############# 148 | while pause: 149 | for event in pygame.event.get(): 150 | if event.type == pygame.QUIT: 151 | pygame.quit() 152 | quit() 153 | 154 | #gameDisplay.fill(white) 155 | largeText = pygame.font.SysFont("comicsansms",90) 156 | textSurf, textRect = text_object("Pause!", largeText) 157 | textRect.center = ((display_width/2) , (display_height/4)) 158 | game_display.blit(textSurf, textRect) 159 | 160 | button("Continue !", 150,250,100,50, green, bright_green, game_unpause) 161 | button("Quit", 550,250,100,50, red, bright_red, quitgame) 162 | 163 | 164 | pygame.display.update() 165 | clock.tick(15) 166 | 167 | 168 | def game_intro(): 169 | 170 | pygame.mixer.music.load("music/atlanta.wav") 171 | pygame.mixer.music.play(-1) 172 | 173 | intro = True 174 | 175 | while intro: 176 | for event in pygame.event.get(): 177 | if event.type == pygame.QUIT: 178 | pygame.quit() 179 | quit() 180 | game_display.fill(white) 181 | 182 | largeText = pygame.font.SysFont("comicsansms",80) 183 | textSurf, textRect = text_object("Let's Ride !", largeText) 184 | textRect.center = ((display_width/2) , (display_height/2)) 185 | game_display.blit(textSurf, textRect) 186 | 187 | button("GO !", 150,450,100,50, green, bright_green, game_loop) 188 | button("Quit", 550,450,100,50, red, bright_red, quitgame) 189 | 190 | 191 | pygame.display.update() 192 | clock.tick(15) 193 | 194 | def game_loop(): 195 | global pause 196 | global score_game 197 | 198 | pygame.mixer.music.load('music/coffee_stains.wav') 199 | pygame.mixer.music.play(-1) 200 | 201 | x = (display_width * 0.45) 202 | y = (display_height * 0.75) 203 | 204 | x_change = 0 205 | y_change = 0 206 | speed_change = 0 207 | 208 | thing_width = 70 209 | thing_height = 140 210 | 211 | thing_startx = random.randrange(100, display_width - 200) 212 | thing_starty = -600 213 | thing_speed = 4 214 | 215 | lineX = 400 216 | lineY = 0 217 | lineW = 20 218 | lineH = 450 219 | line_speed = 10 220 | 221 | tree_y_right = 600 222 | tree_y_left = 300 223 | tree_h = 600 224 | tree_speed = 10 225 | 226 | dodged = 0 227 | 228 | gameExit = False 229 | 230 | while not gameExit: 231 | for event in pygame.event.get(): 232 | if event.type == pygame.QUIT: 233 | pygame.quit() 234 | quit() 235 | 236 | if event.type== pygame.KEYDOWN: 237 | 238 | if event.key==pygame.K_LEFT: 239 | x_change=-5 240 | if event.key == pygame.K_RIGHT: 241 | x_change=5 242 | 243 | if event.key == pygame.K_p: 244 | pause = True 245 | game_pause() 246 | 247 | if event.type == pygame.KEYUP: 248 | x_change= 0 249 | x += x_change 250 | 251 | game_display.fill(white) 252 | 253 | line(150, 0, 20, display_height, blue) 254 | line(display_width-150, 0, 20, display_height, blue) 255 | 256 | load_image(thing_startx, thing_starty, 'images/car.png') 257 | load_image(80, tree_y_left, 'images/trees.jpg') 258 | load_image(700, tree_y_right, 'images/trees.jpg') 259 | load_image(x,y, 'images/car1.png') 260 | 261 | thing_starty += thing_speed 262 | lineY += line_speed 263 | tree_y_left += tree_speed 264 | tree_y_right += tree_speed 265 | 266 | display(dodged, 5, 25) 267 | display(thing_speed*60 , 5, 50, "Spd: %d px/s") 268 | display(score_game, 5, 5, "Final Score: %d") 269 | 270 | if x > display_width - car_width - 150 or x < 150 : 271 | # 100 way background image 272 | crash(x,y) 273 | 274 | if thing_starty > display_height : 275 | thing_starty = 0 - thing_height # reset y 276 | thing_startx = random.randrange(170, display_width-thing_width-150) 277 | dodged += 1 278 | score_game += 1 279 | thing_speed += 1/20 # accelarate 280 | 281 | if lineY > display_height : 282 | lineY = 0 - lineH # reset y 283 | thing_speed += 1/15 284 | 285 | if tree_y_left > display_height : 286 | tree_y_left = 0 - tree_h # reset y 287 | thing_speed += 1/15 288 | 289 | if tree_y_right > display_height : 290 | tree_y_right = 0 - tree_h # reset y 291 | thing_speed += 1/15 292 | 293 | ### check crash 294 | if y < (thing_starty + thing_height) and y+ car_height >= thing_starty + thing_height: 295 | if x > thing_startx and x < (thing_startx + thing_width) or x + car_width > thing_startx \ 296 | and x + car_width < thing_startx + thing_width : 297 | crash(x, y) 298 | 299 | pygame.display.update() 300 | clock.tick(60) 301 | 302 | def main(): 303 | game_init() 304 | game_intro() 305 | game_loop() 306 | pygame.quit() 307 | quit() 308 | 309 | if __name__ == '__main__': 310 | main() 311 | 312 | 313 | -------------------------------------------------------------------------------- /car-game-master/setup.py: -------------------------------------------------------------------------------- 1 | import cx_Freeze 2 | 3 | 4 | 5 | executables = [cx_Freeze.Executable("pygame_car.py")] 6 | 7 | cx_Freeze.setup( 8 | 9 | name="Fast&curious", 10 | options={"build_exe": {"packages":["pygame"], 11 | "include_files":["racecar.png"]}}, 12 | executables = executables 13 | 14 | ) 15 | 16 | 17 | # shift and right click 18 | # C:/Python34/python setup.py build 19 | 20 | # You can also do something like 21 | # python setup.py bdist_msi 22 | 23 | # If you're on a mac, then you would do 24 | # python setup.py bdist_dmg --------------------------------------------------------------------------------