├── .ipynb_checkpoints ├── Dictionaries-checkpoint.ipynb ├── Files-checkpoint.ipynb ├── Lists-checkpoint.ipynb ├── Numbers-checkpoint.ipynb ├── Print Formatting-checkpoint.ipynb ├── Sets and Booleans-checkpoint.ipynb ├── Strings-checkpoint.ipynb └── Tuples-checkpoint.ipynb ├── Advanced Python Modules ├── .ipynb_checkpoints │ ├── Collections-checkpoint.ipynb │ ├── Datetime-checkpoint.ipynb │ ├── Python Debugger-checkpoint.ipynb │ ├── Regex-checkpoint.ipynb │ ├── StringIO-checkpoint.ipynb │ └── Timing Your Code-checkpoint.ipynb ├── Collections.ipynb ├── Datetime.ipynb ├── Python Debugger.ipynb ├── Regex.ipynb ├── StringIO.ipynb └── Timing Your Code.ipynb ├── Advanced Python Objects and Data Structures ├── .ipynb_checkpoints │ ├── Advanced Dictionaries-checkpoint.ipynb │ ├── Advanced Lists-checkpoint.ipynb │ ├── Advanced Numbers-checkpoint.ipynb │ ├── Advanced Sets-checkpoint.ipynb │ └── Advanced Strings-checkpoint.ipynb ├── Advanced Dictionaries.ipynb ├── Advanced Lists.ipynb ├── Advanced Numbers.ipynb ├── Advanced Sets.ipynb └── Advanced Strings.ipynb ├── Builtin Functions ├── .ipynb_checkpoints │ ├── Advanced Functions Test-checkpoint.ipynb │ ├── All and any-checkpoint.ipynb │ ├── Complex-checkpoint.ipynb │ ├── Enumerate-checkpoint.ipynb │ ├── Filter-checkpoint.ipynb │ ├── Map-checkpoint.ipynb │ ├── Reduce-checkpoint.ipynb │ └── Zip-checkpoint.ipynb ├── Advanced Functions Test.ipynb ├── All and any.ipynb ├── Complex.ipynb ├── Enumerate.ipynb ├── Filter.ipynb ├── Map.ipynb ├── Reduce.ipynb └── Zip.ipynb ├── Comparision_Operators ├── .ipynb_checkpoints │ ├── Chained Comparison Operators-checkpoint.ipynb │ └── Comparison Operators-checkpoint.ipynb ├── Chained Comparison Operators.ipynb └── Comparison Operators.ipynb ├── Decorators ├── .ipynb_checkpoints │ └── Decorators-checkpoint.ipynb ├── Decorators Homework.ipynb └── Decorators.ipynb ├── Errors and Exceptional Handling ├── .ipynb_checkpoints │ ├── Errors and Exceptions Homework-checkpoint.ipynb │ └── Erros and Exceptional Handling-checkpoint.ipynb ├── Errors and Exceptions Homework.ipynb ├── Erros and Exceptional Handling.ipynb └── file1.txt ├── Iterators and Generators ├── .ipynb_checkpoints │ ├── Iterators and Generators Homework-checkpoint.ipynb │ └── Python Generators-checkpoint.ipynb ├── Iterators and Generators Homework.ipynb └── Python Generators.ipynb ├── Methods and Functions ├── .ipynb_checkpoints │ ├── Functions and Methods Homework-checkpoint.ipynb │ ├── Functions-checkpoint.ipynb │ ├── Lambdas-checkpoint.ipynb │ └── Methods-checkpoint.ipynb ├── Functions and Methods Homework.ipynb ├── Functions.ipynb ├── Lambdas.ipynb └── Methods.ipynb ├── Obj_DataStructure_Basics ├── .ipynb_checkpoints │ ├── Lists-checkpoint.ipynb │ └── Objects and Data Structures Assessment-checkpoint.ipynb ├── Dictionaries.ipynb ├── Files.ipynb ├── Lists.ipynb ├── Numbers.ipynb ├── Objects and Data Structures Assessment.ipynb ├── Print Formatting.ipynb ├── Sets and Booleans.ipynb ├── Strings.ipynb ├── Tuples.ipynb ├── file2.txt └── test.txt ├── Object Oriented Programming ├── .ipynb_checkpoints │ ├── OOPS-checkpoint.ipynb │ └── Object Oriented Programming Homework-checkpoint.ipynb ├── OOPS.ipynb └── Object Oriented Programming Homework.ipynb ├── Python Statements ├── .ipynb_checkpoints │ ├── List Comprehensions-checkpoint.ipynb │ ├── Range-checkpoint.ipynb │ ├── Statements Assessment-checkpoint.ipynb │ ├── While Loops-checkpoint.ipynb │ ├── for Loops-checkpoint.ipynb │ └── if, elif and else-checkpoint.ipynb ├── List Comprehensions.ipynb ├── Range.ipynb ├── Statements Assessment.ipynb ├── While Loops.ipynb ├── for Loops.ipynb └── if, elif and else.ipynb ├── README.md └── Tic-Tac-Toe-Game ├── .ipynb_checkpoints └── Milestone Project 1- Walkthrough Steps Workbook-checkpoint.ipynb └── Milestone Project 1- Walkthrough Steps Workbook.ipynb /.ipynb_checkpoints/Numbers-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": { 7 | "collapsed": false 8 | }, 9 | "outputs": [ 10 | { 11 | "data": { 12 | "text/plain": [ 13 | "5" 14 | ] 15 | }, 16 | "execution_count": 1, 17 | "metadata": {}, 18 | "output_type": "execute_result" 19 | } 20 | ], 21 | "source": [ 22 | "#Addition\n", 23 | "2+3" 24 | ] 25 | }, 26 | { 27 | "cell_type": "code", 28 | "execution_count": 2, 29 | "metadata": { 30 | "collapsed": false 31 | }, 32 | "outputs": [ 33 | { 34 | "data": { 35 | "text/plain": [ 36 | "3" 37 | ] 38 | }, 39 | "execution_count": 2, 40 | "metadata": {}, 41 | "output_type": "execute_result" 42 | } 43 | ], 44 | "source": [ 45 | "#Subtraction\n", 46 | "5-2" 47 | ] 48 | }, 49 | { 50 | "cell_type": "code", 51 | "execution_count": 3, 52 | "metadata": { 53 | "collapsed": false 54 | }, 55 | "outputs": [ 56 | { 57 | "data": { 58 | "text/plain": [ 59 | "6" 60 | ] 61 | }, 62 | "execution_count": 3, 63 | "metadata": {}, 64 | "output_type": "execute_result" 65 | } 66 | ], 67 | "source": [ 68 | "#Multiplication\n", 69 | "3*2" 70 | ] 71 | }, 72 | { 73 | "cell_type": "code", 74 | "execution_count": 8, 75 | "metadata": { 76 | "collapsed": false 77 | }, 78 | "outputs": [ 79 | { 80 | "data": { 81 | "text/plain": [ 82 | "1" 83 | ] 84 | }, 85 | "execution_count": 8, 86 | "metadata": {}, 87 | "output_type": "execute_result" 88 | } 89 | ], 90 | "source": [ 91 | "#Division\n", 92 | "7.0/2\n" 93 | ] 94 | }, 95 | { 96 | "cell_type": "code", 97 | "execution_count": 9, 98 | "metadata": { 99 | "collapsed": false 100 | }, 101 | "outputs": [ 102 | { 103 | "data": { 104 | "text/plain": [ 105 | "4" 106 | ] 107 | }, 108 | "execution_count": 9, 109 | "metadata": {}, 110 | "output_type": "execute_result" 111 | } 112 | ], 113 | "source": [ 114 | "#Integer Value Division\n", 115 | "8/2" 116 | ] 117 | }, 118 | { 119 | "cell_type": "code", 120 | "execution_count": 10, 121 | "metadata": { 122 | "collapsed": false 123 | }, 124 | "outputs": [ 125 | { 126 | "data": { 127 | "text/plain": [ 128 | "1" 129 | ] 130 | }, 131 | "execution_count": 10, 132 | "metadata": {}, 133 | "output_type": "execute_result" 134 | } 135 | ], 136 | "source": [ 137 | "#Remainder Division\n", 138 | "7%2" 139 | ] 140 | }, 141 | { 142 | "cell_type": "code", 143 | "execution_count": 11, 144 | "metadata": { 145 | "collapsed": false 146 | }, 147 | "outputs": [ 148 | { 149 | "data": { 150 | "text/plain": [ 151 | "1.5" 152 | ] 153 | }, 154 | "execution_count": 11, 155 | "metadata": {}, 156 | "output_type": "execute_result" 157 | } 158 | ], 159 | "source": [ 160 | "from __future__ import division\n", 161 | "3/2" 162 | ] 163 | }, 164 | { 165 | "cell_type": "code", 166 | "execution_count": 12, 167 | "metadata": { 168 | "collapsed": false 169 | }, 170 | "outputs": [ 171 | { 172 | "data": { 173 | "text/plain": [ 174 | "8" 175 | ] 176 | }, 177 | "execution_count": 12, 178 | "metadata": {}, 179 | "output_type": "execute_result" 180 | } 181 | ], 182 | "source": [ 183 | "2**3" 184 | ] 185 | }, 186 | { 187 | "cell_type": "code", 188 | "execution_count": 13, 189 | "metadata": { 190 | "collapsed": true 191 | }, 192 | "outputs": [], 193 | "source": [ 194 | "#Value Assignment\n", 195 | "a=5" 196 | ] 197 | }, 198 | { 199 | "cell_type": "code", 200 | "execution_count": 14, 201 | "metadata": { 202 | "collapsed": true 203 | }, 204 | "outputs": [], 205 | "source": [ 206 | "b=6" 207 | ] 208 | }, 209 | { 210 | "cell_type": "code", 211 | "execution_count": 15, 212 | "metadata": { 213 | "collapsed": false 214 | }, 215 | "outputs": [ 216 | { 217 | "data": { 218 | "text/plain": [ 219 | "11" 220 | ] 221 | }, 222 | "execution_count": 15, 223 | "metadata": {}, 224 | "output_type": "execute_result" 225 | } 226 | ], 227 | "source": [ 228 | "a+b" 229 | ] 230 | }, 231 | { 232 | "cell_type": "code", 233 | "execution_count": null, 234 | "metadata": { 235 | "collapsed": true 236 | }, 237 | "outputs": [], 238 | "source": [] 239 | } 240 | ], 241 | "metadata": { 242 | "kernelspec": { 243 | "display_name": "Python 2", 244 | "language": "python", 245 | "name": "python2" 246 | }, 247 | "language_info": { 248 | "codemirror_mode": { 249 | "name": "ipython", 250 | "version": 2 251 | }, 252 | "file_extension": ".py", 253 | "mimetype": "text/x-python", 254 | "name": "python", 255 | "nbconvert_exporter": "python", 256 | "pygments_lexer": "ipython2", 257 | "version": "2.7.11" 258 | } 259 | }, 260 | "nbformat": 4, 261 | "nbformat_minor": 0 262 | } 263 | -------------------------------------------------------------------------------- /.ipynb_checkpoints/Print Formatting-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": { 7 | "collapsed": false 8 | }, 9 | "outputs": [ 10 | { 11 | "name": "stdout", 12 | "output_type": "stream", 13 | "text": [ 14 | "Hello World!\n" 15 | ] 16 | } 17 | ], 18 | "source": [ 19 | "#Basic Example of Print Formatting\n", 20 | "print 'Hello World!'" 21 | ] 22 | }, 23 | { 24 | "cell_type": "code", 25 | "execution_count": 3, 26 | "metadata": { 27 | "collapsed": false 28 | }, 29 | "outputs": [ 30 | { 31 | "name": "stdout", 32 | "output_type": "stream", 33 | "text": [ 34 | "Hello World\n" 35 | ] 36 | } 37 | ], 38 | "source": [ 39 | "s = 'Hello World'\n", 40 | "print s" 41 | ] 42 | }, 43 | { 44 | "cell_type": "code", 45 | "execution_count": 5, 46 | "metadata": { 47 | "collapsed": false 48 | }, 49 | "outputs": [ 50 | { 51 | "name": "stdout", 52 | "output_type": "stream", 53 | "text": [ 54 | "We can also print Strings with a mod and variable name Hello World\n" 55 | ] 56 | } 57 | ], 58 | "source": [ 59 | "print 'We can also print Strings with a mod and variable name %s' %(s)" 60 | ] 61 | }, 62 | { 63 | "cell_type": "markdown", 64 | "metadata": {}, 65 | "source": [ 66 | "Floating Point Numbers\n", 67 | "Floating point numbers use the format %n1.n2f where the n1 is the total minimum number of digits the string should contain (these may be filled with whitespace if the entire number does not have this many digits. The n2 placeholder stands for how many numbers to show past the decimal point." 68 | ] 69 | }, 70 | { 71 | "cell_type": "code", 72 | "execution_count": 6, 73 | "metadata": { 74 | "collapsed": false 75 | }, 76 | "outputs": [ 77 | { 78 | "name": "stdout", 79 | "output_type": "stream", 80 | "text": [ 81 | "Floating Point Numbers : 1.22\n" 82 | ] 83 | } 84 | ], 85 | "source": [ 86 | "#Examples of floating point numbers\n", 87 | "print 'Floating Point Numbers : %1.2f' %(1.22334455)" 88 | ] 89 | }, 90 | { 91 | "cell_type": "code", 92 | "execution_count": 7, 93 | "metadata": { 94 | "collapsed": false 95 | }, 96 | "outputs": [ 97 | { 98 | "name": "stdout", 99 | "output_type": "stream", 100 | "text": [ 101 | "Floating Point Numbers : 1.2233\n" 102 | ] 103 | } 104 | ], 105 | "source": [ 106 | "print 'Floating Point Numbers : %1.4f' %(1.22334455)" 107 | ] 108 | }, 109 | { 110 | "cell_type": "code", 111 | "execution_count": 11, 112 | "metadata": { 113 | "collapsed": false 114 | }, 115 | "outputs": [ 116 | { 117 | "name": "stdout", 118 | "output_type": "stream", 119 | "text": [ 120 | "Floating Point Numbers : 1.2233445499999999750428969\n" 121 | ] 122 | } 123 | ], 124 | "source": [ 125 | "print 'Floating Point Numbers : %30.25f' %(1.22334455)" 126 | ] 127 | }, 128 | { 129 | "cell_type": "code", 130 | "execution_count": 12, 131 | "metadata": { 132 | "collapsed": false 133 | }, 134 | "outputs": [ 135 | { 136 | "name": "stdout", 137 | "output_type": "stream", 138 | "text": [ 139 | "This is one number : 123 and this is another number : 'Python'\n" 140 | ] 141 | } 142 | ], 143 | "source": [ 144 | "#Calling two values into print string\n", 145 | "print 'This is one number : %s and this is another number : %r' %(123, \"Python\")" 146 | ] 147 | }, 148 | { 149 | "cell_type": "code", 150 | "execution_count": 14, 151 | "metadata": { 152 | "collapsed": false 153 | }, 154 | "outputs": [ 155 | { 156 | "name": "stdout", 157 | "output_type": "stream", 158 | "text": [ 159 | "This is one number : 123 and this is another number : 'Python'. This is Decimal number : 3.15\n" 160 | ] 161 | } 162 | ], 163 | "source": [ 164 | "#Multiple Formatting\n", 165 | "print 'This is one number : %s and this is another number : %r. This is Decimal number : %1.2f' %(123, \"Python\", 3.1456)" 166 | ] 167 | }, 168 | { 169 | "cell_type": "markdown", 170 | "metadata": {}, 171 | "source": [ 172 | "# Using the string .format() method\n", 173 | "The best way to format objects into your strings for print statements is using the format method. The syntax is:\n", 174 | "'String here {var1} then also {var2}'.format(var1='something1',var2='something2')\n" 175 | ] 176 | }, 177 | { 178 | "cell_type": "code", 179 | "execution_count": 15, 180 | "metadata": { 181 | "collapsed": false 182 | }, 183 | "outputs": [ 184 | { 185 | "name": "stdout", 186 | "output_type": "stream", 187 | "text": [ 188 | "This is Object 1: Hey, this is Object2: 123 and this is another object: 2.057\n" 189 | ] 190 | } 191 | ], 192 | "source": [ 193 | "print 'This is Object 1: %s, this is Object2: %s and this is another object: %1.3f'%(\"Hey\", 123, 2.0567)" 194 | ] 195 | }, 196 | { 197 | "cell_type": "markdown", 198 | "metadata": {}, 199 | "source": [ 200 | "# That is the basics of string formatting! Remember that Python 3 uses a print() function, not the print statement!" 201 | ] 202 | } 203 | ], 204 | "metadata": { 205 | "kernelspec": { 206 | "display_name": "Python 2", 207 | "language": "python", 208 | "name": "python2" 209 | }, 210 | "language_info": { 211 | "codemirror_mode": { 212 | "name": "ipython", 213 | "version": 2 214 | }, 215 | "file_extension": ".py", 216 | "mimetype": "text/x-python", 217 | "name": "python", 218 | "nbconvert_exporter": "python", 219 | "pygments_lexer": "ipython2", 220 | "version": "2.7.11" 221 | } 222 | }, 223 | "nbformat": 4, 224 | "nbformat_minor": 0 225 | } 226 | -------------------------------------------------------------------------------- /.ipynb_checkpoints/Tuples-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Tuples\n", 8 | "In Python tuples are very similar to lists, however, unlike lists they are immutable meaning they can not be changed. You would use tuples to present things that shouldn't be changed, such as days of the week, or dates on a calendar.\n" 9 | ] 10 | }, 11 | { 12 | "cell_type": "code", 13 | "execution_count": 1, 14 | "metadata": { 15 | "collapsed": true 16 | }, 17 | "outputs": [], 18 | "source": [ 19 | "t = (1,1,1)" 20 | ] 21 | }, 22 | { 23 | "cell_type": "code", 24 | "execution_count": 2, 25 | "metadata": { 26 | "collapsed": false 27 | }, 28 | "outputs": [ 29 | { 30 | "data": { 31 | "text/plain": [ 32 | "(1, 2, 3)" 33 | ] 34 | }, 35 | "execution_count": 2, 36 | "metadata": {}, 37 | "output_type": "execute_result" 38 | } 39 | ], 40 | "source": [ 41 | "t" 42 | ] 43 | }, 44 | { 45 | "cell_type": "code", 46 | "execution_count": 5, 47 | "metadata": { 48 | "collapsed": false 49 | }, 50 | "outputs": [ 51 | { 52 | "data": { 53 | "text/plain": [ 54 | "3" 55 | ] 56 | }, 57 | "execution_count": 5, 58 | "metadata": {}, 59 | "output_type": "execute_result" 60 | } 61 | ], 62 | "source": [ 63 | "#Lenth of Tuple\n", 64 | "len(t)" 65 | ] 66 | }, 67 | { 68 | "cell_type": "code", 69 | "execution_count": 7, 70 | "metadata": { 71 | "collapsed": false 72 | }, 73 | "outputs": [ 74 | { 75 | "data": { 76 | "text/plain": [ 77 | "1" 78 | ] 79 | }, 80 | "execution_count": 7, 81 | "metadata": {}, 82 | "output_type": "execute_result" 83 | } 84 | ], 85 | "source": [ 86 | "#Almost tuples share same kind of methods with Lists, just that they are immutable\n", 87 | "t[0]" 88 | ] 89 | }, 90 | { 91 | "cell_type": "code", 92 | "execution_count": 8, 93 | "metadata": { 94 | "collapsed": true 95 | }, 96 | "outputs": [], 97 | "source": [ 98 | "t2 = (1, (1,1,1), 1)" 99 | ] 100 | }, 101 | { 102 | "cell_type": "code", 103 | "execution_count": 9, 104 | "metadata": { 105 | "collapsed": false 106 | }, 107 | "outputs": [ 108 | { 109 | "data": { 110 | "text/plain": [ 111 | "4" 112 | ] 113 | }, 114 | "execution_count": 9, 115 | "metadata": {}, 116 | "output_type": "execute_result" 117 | } 118 | ], 119 | "source": [ 120 | "t2[1][2]" 121 | ] 122 | }, 123 | { 124 | "cell_type": "code", 125 | "execution_count": 16, 126 | "metadata": { 127 | "collapsed": false 128 | }, 129 | "outputs": [ 130 | { 131 | "data": { 132 | "text/plain": [ 133 | "(1, (2, 3, 4), 5)" 134 | ] 135 | }, 136 | "execution_count": 16, 137 | "metadata": {}, 138 | "output_type": "execute_result" 139 | } 140 | ], 141 | "source": [ 142 | "t2" 143 | ] 144 | }, 145 | { 146 | "cell_type": "code", 147 | "execution_count": 11, 148 | "metadata": { 149 | "collapsed": true 150 | }, 151 | "outputs": [], 152 | "source": [ 153 | "t3 = ('Sports', 1)" 154 | ] 155 | }, 156 | { 157 | "cell_type": "code", 158 | "execution_count": 17, 159 | "metadata": { 160 | "collapsed": false 161 | }, 162 | "outputs": [ 163 | { 164 | "data": { 165 | "text/plain": [ 166 | "1" 167 | ] 168 | }, 169 | "execution_count": 17, 170 | "metadata": {}, 171 | "output_type": "execute_result" 172 | } 173 | ], 174 | "source": [ 175 | "# Returns value at value of index mentioned\n", 176 | "t3.index(1)" 177 | ] 178 | }, 179 | { 180 | "cell_type": "code", 181 | "execution_count": 20, 182 | "metadata": { 183 | "collapsed": false 184 | }, 185 | "outputs": [ 186 | { 187 | "data": { 188 | "text/plain": [ 189 | "1" 190 | ] 191 | }, 192 | "execution_count": 20, 193 | "metadata": {}, 194 | "output_type": "execute_result" 195 | } 196 | ], 197 | "source": [ 198 | "# Returns how many times a value is repeated\n", 199 | "t.count(1)" 200 | ] 201 | }, 202 | { 203 | "cell_type": "code", 204 | "execution_count": null, 205 | "metadata": { 206 | "collapsed": true 207 | }, 208 | "outputs": [], 209 | "source": [] 210 | } 211 | ], 212 | "metadata": { 213 | "kernelspec": { 214 | "display_name": "Python 2", 215 | "language": "python", 216 | "name": "python2" 217 | }, 218 | "language_info": { 219 | "codemirror_mode": { 220 | "name": "ipython", 221 | "version": 2 222 | }, 223 | "file_extension": ".py", 224 | "mimetype": "text/x-python", 225 | "name": "python", 226 | "nbconvert_exporter": "python", 227 | "pygments_lexer": "ipython2", 228 | "version": "2.7.11" 229 | } 230 | }, 231 | "nbformat": 4, 232 | "nbformat_minor": 0 233 | } 234 | -------------------------------------------------------------------------------- /Advanced Python Modules/.ipynb_checkpoints/StringIO-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# StringIO\n", 8 | "The StringIO module implements an in-memory file like object. \n", 9 | "This object can then be used as input or output to most functions that would expect a standard file object.\n" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 1, 15 | "metadata": { 16 | "collapsed": true 17 | }, 18 | "outputs": [], 19 | "source": [ 20 | "import StringIO" 21 | ] 22 | }, 23 | { 24 | "cell_type": "code", 25 | "execution_count": 2, 26 | "metadata": { 27 | "collapsed": true 28 | }, 29 | "outputs": [], 30 | "source": [ 31 | "testString = 'This is test string for this notebook'" 32 | ] 33 | }, 34 | { 35 | "cell_type": "code", 36 | "execution_count": 3, 37 | "metadata": { 38 | "collapsed": true 39 | }, 40 | "outputs": [], 41 | "source": [ 42 | "t = StringIO.StringIO(testString)" 43 | ] 44 | }, 45 | { 46 | "cell_type": "code", 47 | "execution_count": 4, 48 | "metadata": { 49 | "collapsed": false 50 | }, 51 | "outputs": [ 52 | { 53 | "data": { 54 | "text/plain": [ 55 | "'This is test string for this notebook'" 56 | ] 57 | }, 58 | "execution_count": 4, 59 | "metadata": {}, 60 | "output_type": "execute_result" 61 | } 62 | ], 63 | "source": [ 64 | "t.read()" 65 | ] 66 | }, 67 | { 68 | "cell_type": "code", 69 | "execution_count": 5, 70 | "metadata": { 71 | "collapsed": true 72 | }, 73 | "outputs": [], 74 | "source": [ 75 | "t.seek(0)" 76 | ] 77 | }, 78 | { 79 | "cell_type": "code", 80 | "execution_count": 6, 81 | "metadata": { 82 | "collapsed": false 83 | }, 84 | "outputs": [ 85 | { 86 | "data": { 87 | "text/plain": [ 88 | "'This is test string for this notebook'" 89 | ] 90 | }, 91 | "execution_count": 6, 92 | "metadata": {}, 93 | "output_type": "execute_result" 94 | } 95 | ], 96 | "source": [ 97 | "t.read()" 98 | ] 99 | }, 100 | { 101 | "cell_type": "code", 102 | "execution_count": 7, 103 | "metadata": { 104 | "collapsed": true 105 | }, 106 | "outputs": [], 107 | "source": [ 108 | "t.write('. This is second line of test string')" 109 | ] 110 | }, 111 | { 112 | "cell_type": "code", 113 | "execution_count": 10, 114 | "metadata": { 115 | "collapsed": true 116 | }, 117 | "outputs": [], 118 | "source": [ 119 | "t.seek(1)" 120 | ] 121 | }, 122 | { 123 | "cell_type": "code", 124 | "execution_count": 13, 125 | "metadata": { 126 | "collapsed": false 127 | }, 128 | "outputs": [ 129 | { 130 | "data": { 131 | "text/plain": [ 132 | "''" 133 | ] 134 | }, 135 | "execution_count": 13, 136 | "metadata": {}, 137 | "output_type": "execute_result" 138 | } 139 | ], 140 | "source": [ 141 | "t.read()" 142 | ] 143 | }, 144 | { 145 | "cell_type": "code", 146 | "execution_count": 14, 147 | "metadata": { 148 | "collapsed": true 149 | }, 150 | "outputs": [], 151 | "source": [ 152 | "t.seek(0)" 153 | ] 154 | }, 155 | { 156 | "cell_type": "code", 157 | "execution_count": 15, 158 | "metadata": { 159 | "collapsed": false 160 | }, 161 | "outputs": [ 162 | { 163 | "data": { 164 | "text/plain": [ 165 | "'This is test string for this notebookThis is second line of test string'" 166 | ] 167 | }, 168 | "execution_count": 15, 169 | "metadata": {}, 170 | "output_type": "execute_result" 171 | } 172 | ], 173 | "source": [ 174 | "t.read()" 175 | ] 176 | }, 177 | { 178 | "cell_type": "markdown", 179 | "metadata": {}, 180 | "source": [ 181 | "Now you've seen how we can use StringIO to turn normal strings into in-memory file objects in our code. \n", 182 | "\n", 183 | "This kind of action has various use cases, especially in web scraping cases where you want to read some string you scraped as a file." 184 | ] 185 | } 186 | ], 187 | "metadata": { 188 | "kernelspec": { 189 | "display_name": "Python 2", 190 | "language": "python", 191 | "name": "python2" 192 | }, 193 | "language_info": { 194 | "codemirror_mode": { 195 | "name": "ipython", 196 | "version": 2 197 | }, 198 | "file_extension": ".py", 199 | "mimetype": "text/x-python", 200 | "name": "python", 201 | "nbconvert_exporter": "python", 202 | "pygments_lexer": "ipython2", 203 | "version": "2.7.11" 204 | } 205 | }, 206 | "nbformat": 4, 207 | "nbformat_minor": 0 208 | } 209 | -------------------------------------------------------------------------------- /Advanced Python Modules/.ipynb_checkpoints/Timing Your Code-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Timing your code\n", 8 | "\n", 9 | "Sometimes its important to know how long your code is taking to run, or at least know if a particular line of code is slowing down your entire project. Python has a built-in timing module to do this.\n", 10 | "\n", 11 | "This module provides a simple way to time small bits of Python code. It has both a Command-Line Interface as well as a callable one. It avoids a number of common traps for measuring execution times." 12 | ] 13 | }, 14 | { 15 | "cell_type": "code", 16 | "execution_count": 1, 17 | "metadata": { 18 | "collapsed": true 19 | }, 20 | "outputs": [], 21 | "source": [ 22 | "import timeit" 23 | ] 24 | }, 25 | { 26 | "cell_type": "code", 27 | "execution_count": 11, 28 | "metadata": { 29 | "collapsed": false 30 | }, 31 | "outputs": [ 32 | { 33 | "data": { 34 | "text/plain": [ 35 | "0.2940559387207031" 36 | ] 37 | }, 38 | "execution_count": 11, 39 | "metadata": {}, 40 | "output_type": "execute_result" 41 | } 42 | ], 43 | "source": [ 44 | "#For Loop\n", 45 | "\n", 46 | "#timeit.timeit('\"-\".join(str(n) for n in range(1000))', number=10000)\n", 47 | "\n", 48 | "timeit.timeit('\"-\".join(str(n) for n in range(100))', number=10000)" 49 | ] 50 | }, 51 | { 52 | "cell_type": "code", 53 | "execution_count": 12, 54 | "metadata": { 55 | "collapsed": false 56 | }, 57 | "outputs": [ 58 | { 59 | "data": { 60 | "text/plain": [ 61 | "0.2818911075592041" 62 | ] 63 | }, 64 | "execution_count": 12, 65 | "metadata": {}, 66 | "output_type": "execute_result" 67 | } 68 | ], 69 | "source": [ 70 | "#List Comprehension\n", 71 | "timeit.timeit('\"-\".join([str(n) for n in range(100)])', number=10000)" 72 | ] 73 | }, 74 | { 75 | "cell_type": "code", 76 | "execution_count": 13, 77 | "metadata": { 78 | "collapsed": false 79 | }, 80 | "outputs": [ 81 | { 82 | "data": { 83 | "text/plain": [ 84 | "0.16641688346862793" 85 | ] 86 | }, 87 | "execution_count": 13, 88 | "metadata": {}, 89 | "output_type": "execute_result" 90 | } 91 | ], 92 | "source": [ 93 | "#Map\n", 94 | "timeit.timeit('\"-\".join(map(str,range(100)))', number=10000)" 95 | ] 96 | }, 97 | { 98 | "cell_type": "markdown", 99 | "metadata": {}, 100 | "source": [ 101 | "iPython's %timeit will perform the code in the same line a certain number of times (loops) \n", 102 | "and will give you the fastest performance time (best of 3)." 103 | ] 104 | }, 105 | { 106 | "cell_type": "code", 107 | "execution_count": 16, 108 | "metadata": { 109 | "collapsed": false 110 | }, 111 | "outputs": [ 112 | { 113 | "name": "stdout", 114 | "output_type": "stream", 115 | "text": [ 116 | "10000 loops, best of 3: 26.2 µs per loop\n" 117 | ] 118 | } 119 | ], 120 | "source": [ 121 | "%timeit \"-\".join(str(n) for n in range(100))" 122 | ] 123 | }, 124 | { 125 | "cell_type": "code", 126 | "execution_count": 19, 127 | "metadata": { 128 | "collapsed": false 129 | }, 130 | "outputs": [ 131 | { 132 | "name": "stdout", 133 | "output_type": "stream", 134 | "text": [ 135 | "10000 loops, best of 3: 24.3 µs per loop\n" 136 | ] 137 | } 138 | ], 139 | "source": [ 140 | "%timeit \"-\".join([str(n) for n in range(100)])" 141 | ] 142 | }, 143 | { 144 | "cell_type": "code", 145 | "execution_count": 20, 146 | "metadata": { 147 | "collapsed": false 148 | }, 149 | "outputs": [ 150 | { 151 | "name": "stdout", 152 | "output_type": "stream", 153 | "text": [ 154 | "100000 loops, best of 3: 14.5 µs per loop\n" 155 | ] 156 | } 157 | ], 158 | "source": [ 159 | "%timeit \"-\".join(map(str,range(100)))" 160 | ] 161 | }, 162 | { 163 | "cell_type": "markdown", 164 | "metadata": {}, 165 | "source": [ 166 | "Its also important to note that iPython will limit the amount of real time it will spend on its timeit procedure.\n", 167 | "\n", 168 | "For instance if running 100000 loops took 10 minutes, iPython would automatically reduce the number of loops to something more reasonable like 100 or 1000.\n" 169 | ] 170 | } 171 | ], 172 | "metadata": { 173 | "kernelspec": { 174 | "display_name": "Python 2", 175 | "language": "python", 176 | "name": "python2" 177 | }, 178 | "language_info": { 179 | "codemirror_mode": { 180 | "name": "ipython", 181 | "version": 2 182 | }, 183 | "file_extension": ".py", 184 | "mimetype": "text/x-python", 185 | "name": "python", 186 | "nbconvert_exporter": "python", 187 | "pygments_lexer": "ipython2", 188 | "version": "2.7.11" 189 | } 190 | }, 191 | "nbformat": 4, 192 | "nbformat_minor": 0 193 | } 194 | -------------------------------------------------------------------------------- /Advanced Python Modules/StringIO.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# StringIO\n", 8 | "The StringIO module implements an in-memory file like object. \n", 9 | "This object can then be used as input or output to most functions that would expect a standard file object.\n" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 1, 15 | "metadata": { 16 | "collapsed": true 17 | }, 18 | "outputs": [], 19 | "source": [ 20 | "import StringIO" 21 | ] 22 | }, 23 | { 24 | "cell_type": "code", 25 | "execution_count": 2, 26 | "metadata": { 27 | "collapsed": true 28 | }, 29 | "outputs": [], 30 | "source": [ 31 | "testString = 'This is test string for this notebook'" 32 | ] 33 | }, 34 | { 35 | "cell_type": "code", 36 | "execution_count": 3, 37 | "metadata": { 38 | "collapsed": true 39 | }, 40 | "outputs": [], 41 | "source": [ 42 | "t = StringIO.StringIO(testString)" 43 | ] 44 | }, 45 | { 46 | "cell_type": "code", 47 | "execution_count": 4, 48 | "metadata": { 49 | "collapsed": false 50 | }, 51 | "outputs": [ 52 | { 53 | "data": { 54 | "text/plain": [ 55 | "'This is test string for this notebook'" 56 | ] 57 | }, 58 | "execution_count": 4, 59 | "metadata": {}, 60 | "output_type": "execute_result" 61 | } 62 | ], 63 | "source": [ 64 | "t.read()" 65 | ] 66 | }, 67 | { 68 | "cell_type": "code", 69 | "execution_count": 5, 70 | "metadata": { 71 | "collapsed": true 72 | }, 73 | "outputs": [], 74 | "source": [ 75 | "t.seek(0)" 76 | ] 77 | }, 78 | { 79 | "cell_type": "code", 80 | "execution_count": 6, 81 | "metadata": { 82 | "collapsed": false 83 | }, 84 | "outputs": [ 85 | { 86 | "data": { 87 | "text/plain": [ 88 | "'This is test string for this notebook'" 89 | ] 90 | }, 91 | "execution_count": 6, 92 | "metadata": {}, 93 | "output_type": "execute_result" 94 | } 95 | ], 96 | "source": [ 97 | "t.read()" 98 | ] 99 | }, 100 | { 101 | "cell_type": "code", 102 | "execution_count": 7, 103 | "metadata": { 104 | "collapsed": true 105 | }, 106 | "outputs": [], 107 | "source": [ 108 | "t.write('. This is second line of test string')" 109 | ] 110 | }, 111 | { 112 | "cell_type": "code", 113 | "execution_count": 10, 114 | "metadata": { 115 | "collapsed": true 116 | }, 117 | "outputs": [], 118 | "source": [ 119 | "t.seek(1)" 120 | ] 121 | }, 122 | { 123 | "cell_type": "code", 124 | "execution_count": 13, 125 | "metadata": { 126 | "collapsed": false 127 | }, 128 | "outputs": [ 129 | { 130 | "data": { 131 | "text/plain": [ 132 | "''" 133 | ] 134 | }, 135 | "execution_count": 13, 136 | "metadata": {}, 137 | "output_type": "execute_result" 138 | } 139 | ], 140 | "source": [ 141 | "t.read()" 142 | ] 143 | }, 144 | { 145 | "cell_type": "code", 146 | "execution_count": 14, 147 | "metadata": { 148 | "collapsed": true 149 | }, 150 | "outputs": [], 151 | "source": [ 152 | "t.seek(0)" 153 | ] 154 | }, 155 | { 156 | "cell_type": "code", 157 | "execution_count": 15, 158 | "metadata": { 159 | "collapsed": false 160 | }, 161 | "outputs": [ 162 | { 163 | "data": { 164 | "text/plain": [ 165 | "'This is test string for this notebookThis is second line of test string'" 166 | ] 167 | }, 168 | "execution_count": 15, 169 | "metadata": {}, 170 | "output_type": "execute_result" 171 | } 172 | ], 173 | "source": [ 174 | "t.read()" 175 | ] 176 | }, 177 | { 178 | "cell_type": "markdown", 179 | "metadata": {}, 180 | "source": [ 181 | "Now you've seen how we can use StringIO to turn normal strings into in-memory file objects in our code. \n", 182 | "\n", 183 | "This kind of action has various use cases, especially in web scraping cases where you want to read some string you scraped as a file." 184 | ] 185 | } 186 | ], 187 | "metadata": { 188 | "kernelspec": { 189 | "display_name": "Python 2", 190 | "language": "python", 191 | "name": "python2" 192 | }, 193 | "language_info": { 194 | "codemirror_mode": { 195 | "name": "ipython", 196 | "version": 2 197 | }, 198 | "file_extension": ".py", 199 | "mimetype": "text/x-python", 200 | "name": "python", 201 | "nbconvert_exporter": "python", 202 | "pygments_lexer": "ipython2", 203 | "version": "2.7.11" 204 | } 205 | }, 206 | "nbformat": 4, 207 | "nbformat_minor": 0 208 | } 209 | -------------------------------------------------------------------------------- /Advanced Python Modules/Timing Your Code.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Timing your code\n", 8 | "\n", 9 | "Sometimes its important to know how long your code is taking to run, or at least know if a particular line of code is slowing down your entire project. Python has a built-in timing module to do this.\n", 10 | "\n", 11 | "This module provides a simple way to time small bits of Python code. It has both a Command-Line Interface as well as a callable one. It avoids a number of common traps for measuring execution times." 12 | ] 13 | }, 14 | { 15 | "cell_type": "code", 16 | "execution_count": 1, 17 | "metadata": { 18 | "collapsed": true 19 | }, 20 | "outputs": [], 21 | "source": [ 22 | "import timeit" 23 | ] 24 | }, 25 | { 26 | "cell_type": "code", 27 | "execution_count": 11, 28 | "metadata": { 29 | "collapsed": false 30 | }, 31 | "outputs": [ 32 | { 33 | "data": { 34 | "text/plain": [ 35 | "0.2940559387207031" 36 | ] 37 | }, 38 | "execution_count": 11, 39 | "metadata": {}, 40 | "output_type": "execute_result" 41 | } 42 | ], 43 | "source": [ 44 | "#For Loop\n", 45 | "\n", 46 | "#timeit.timeit('\"-\".join(str(n) for n in range(1000))', number=10000)\n", 47 | "\n", 48 | "timeit.timeit('\"-\".join(str(n) for n in range(100))', number=10000)" 49 | ] 50 | }, 51 | { 52 | "cell_type": "code", 53 | "execution_count": 12, 54 | "metadata": { 55 | "collapsed": false 56 | }, 57 | "outputs": [ 58 | { 59 | "data": { 60 | "text/plain": [ 61 | "0.2818911075592041" 62 | ] 63 | }, 64 | "execution_count": 12, 65 | "metadata": {}, 66 | "output_type": "execute_result" 67 | } 68 | ], 69 | "source": [ 70 | "#List Comprehension\n", 71 | "timeit.timeit('\"-\".join([str(n) for n in range(100)])', number=10000)" 72 | ] 73 | }, 74 | { 75 | "cell_type": "code", 76 | "execution_count": 13, 77 | "metadata": { 78 | "collapsed": false 79 | }, 80 | "outputs": [ 81 | { 82 | "data": { 83 | "text/plain": [ 84 | "0.16641688346862793" 85 | ] 86 | }, 87 | "execution_count": 13, 88 | "metadata": {}, 89 | "output_type": "execute_result" 90 | } 91 | ], 92 | "source": [ 93 | "#Map\n", 94 | "timeit.timeit('\"-\".join(map(str,range(100)))', number=10000)" 95 | ] 96 | }, 97 | { 98 | "cell_type": "markdown", 99 | "metadata": {}, 100 | "source": [ 101 | "iPython's %timeit will perform the code in the same line a certain number of times (loops) \n", 102 | "and will give you the fastest performance time (best of 3)." 103 | ] 104 | }, 105 | { 106 | "cell_type": "code", 107 | "execution_count": 16, 108 | "metadata": { 109 | "collapsed": false 110 | }, 111 | "outputs": [ 112 | { 113 | "name": "stdout", 114 | "output_type": "stream", 115 | "text": [ 116 | "10000 loops, best of 3: 26.2 µs per loop\n" 117 | ] 118 | } 119 | ], 120 | "source": [ 121 | "%timeit \"-\".join(str(n) for n in range(100))" 122 | ] 123 | }, 124 | { 125 | "cell_type": "code", 126 | "execution_count": 19, 127 | "metadata": { 128 | "collapsed": false 129 | }, 130 | "outputs": [ 131 | { 132 | "name": "stdout", 133 | "output_type": "stream", 134 | "text": [ 135 | "10000 loops, best of 3: 24.3 µs per loop\n" 136 | ] 137 | } 138 | ], 139 | "source": [ 140 | "%timeit \"-\".join([str(n) for n in range(100)])" 141 | ] 142 | }, 143 | { 144 | "cell_type": "code", 145 | "execution_count": 20, 146 | "metadata": { 147 | "collapsed": false 148 | }, 149 | "outputs": [ 150 | { 151 | "name": "stdout", 152 | "output_type": "stream", 153 | "text": [ 154 | "100000 loops, best of 3: 14.5 µs per loop\n" 155 | ] 156 | } 157 | ], 158 | "source": [ 159 | "%timeit \"-\".join(map(str,range(100)))" 160 | ] 161 | }, 162 | { 163 | "cell_type": "markdown", 164 | "metadata": {}, 165 | "source": [ 166 | "Its also important to note that iPython will limit the amount of real time it will spend on its timeit procedure.\n", 167 | "\n", 168 | "For instance if running 100000 loops took 10 minutes, iPython would automatically reduce the number of loops to something more reasonable like 100 or 1000.\n" 169 | ] 170 | } 171 | ], 172 | "metadata": { 173 | "kernelspec": { 174 | "display_name": "Python 2", 175 | "language": "python", 176 | "name": "python2" 177 | }, 178 | "language_info": { 179 | "codemirror_mode": { 180 | "name": "ipython", 181 | "version": 2 182 | }, 183 | "file_extension": ".py", 184 | "mimetype": "text/x-python", 185 | "name": "python", 186 | "nbconvert_exporter": "python", 187 | "pygments_lexer": "ipython2", 188 | "version": "2.7.11" 189 | } 190 | }, 191 | "nbformat": 4, 192 | "nbformat_minor": 0 193 | } 194 | -------------------------------------------------------------------------------- /Advanced Python Objects and Data Structures/.ipynb_checkpoints/Advanced Dictionaries-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": { 7 | "collapsed": true 8 | }, 9 | "outputs": [], 10 | "source": [ 11 | "d = {'k1':1,'k2':2}" 12 | ] 13 | }, 14 | { 15 | "cell_type": "markdown", 16 | "metadata": {}, 17 | "source": [ 18 | "### Dictionary Comprehensions\n", 19 | "\n", 20 | "Just like List Comprehensions, Dictionary Data Types also support their own version of comprehension for quick creation.\n", 21 | "It is not as commonly used as List Comprehensions" 22 | ] 23 | }, 24 | { 25 | "cell_type": "code", 26 | "execution_count": 3, 27 | "metadata": { 28 | "collapsed": false 29 | }, 30 | "outputs": [ 31 | { 32 | "data": { 33 | "text/plain": [ 34 | "{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81}" 35 | ] 36 | }, 37 | "execution_count": 3, 38 | "metadata": {}, 39 | "output_type": "execute_result" 40 | } 41 | ], 42 | "source": [ 43 | "{x:x**2 for x in range(10)}" 44 | ] 45 | }, 46 | { 47 | "cell_type": "markdown", 48 | "metadata": {}, 49 | "source": [ 50 | "One of the reasons it is not as common is the difficulty in \n", 51 | "structuring the key names that are not based off the values." 52 | ] 53 | }, 54 | { 55 | "cell_type": "code", 56 | "execution_count": 5, 57 | "metadata": { 58 | "collapsed": false 59 | }, 60 | "outputs": [ 61 | { 62 | "name": "stdout", 63 | "output_type": "stream", 64 | "text": [ 65 | "k2\n", 66 | "k1\n" 67 | ] 68 | } 69 | ], 70 | "source": [ 71 | "for k in d.iterkeys():\n", 72 | " print k" 73 | ] 74 | }, 75 | { 76 | "cell_type": "code", 77 | "execution_count": 6, 78 | "metadata": { 79 | "collapsed": false 80 | }, 81 | "outputs": [ 82 | { 83 | "name": "stdout", 84 | "output_type": "stream", 85 | "text": [ 86 | "2\n", 87 | "1\n" 88 | ] 89 | } 90 | ], 91 | "source": [ 92 | "for k in d.itervalues():\n", 93 | " print k" 94 | ] 95 | }, 96 | { 97 | "cell_type": "code", 98 | "execution_count": 7, 99 | "metadata": { 100 | "collapsed": false 101 | }, 102 | "outputs": [ 103 | { 104 | "name": "stdout", 105 | "output_type": "stream", 106 | "text": [ 107 | "('k2', 2)\n", 108 | "('k1', 1)\n" 109 | ] 110 | } 111 | ], 112 | "source": [ 113 | "for k in d.iteritems():\n", 114 | " print k" 115 | ] 116 | }, 117 | { 118 | "cell_type": "markdown", 119 | "metadata": {}, 120 | "source": [ 121 | "You can use the view methods to view items keys and values." 122 | ] 123 | }, 124 | { 125 | "cell_type": "code", 126 | "execution_count": 8, 127 | "metadata": { 128 | "collapsed": false 129 | }, 130 | "outputs": [ 131 | { 132 | "data": { 133 | "text/plain": [ 134 | "dict_items([('k2', 2), ('k1', 1)])" 135 | ] 136 | }, 137 | "execution_count": 8, 138 | "metadata": {}, 139 | "output_type": "execute_result" 140 | } 141 | ], 142 | "source": [ 143 | "d.viewitems()" 144 | ] 145 | }, 146 | { 147 | "cell_type": "code", 148 | "execution_count": 9, 149 | "metadata": { 150 | "collapsed": false 151 | }, 152 | "outputs": [ 153 | { 154 | "data": { 155 | "text/plain": [ 156 | "dict_keys(['k2', 'k1'])" 157 | ] 158 | }, 159 | "execution_count": 9, 160 | "metadata": {}, 161 | "output_type": "execute_result" 162 | } 163 | ], 164 | "source": [ 165 | "d.viewkeys()" 166 | ] 167 | }, 168 | { 169 | "cell_type": "code", 170 | "execution_count": 10, 171 | "metadata": { 172 | "collapsed": false 173 | }, 174 | "outputs": [ 175 | { 176 | "data": { 177 | "text/plain": [ 178 | "dict_values([2, 1])" 179 | ] 180 | }, 181 | "execution_count": 10, 182 | "metadata": {}, 183 | "output_type": "execute_result" 184 | } 185 | ], 186 | "source": [ 187 | "d.viewvalues()" 188 | ] 189 | }, 190 | { 191 | "cell_type": "code", 192 | "execution_count": null, 193 | "metadata": { 194 | "collapsed": true 195 | }, 196 | "outputs": [], 197 | "source": [] 198 | } 199 | ], 200 | "metadata": { 201 | "kernelspec": { 202 | "display_name": "Python 2", 203 | "language": "python", 204 | "name": "python2" 205 | }, 206 | "language_info": { 207 | "codemirror_mode": { 208 | "name": "ipython", 209 | "version": 2 210 | }, 211 | "file_extension": ".py", 212 | "mimetype": "text/x-python", 213 | "name": "python", 214 | "nbconvert_exporter": "python", 215 | "pygments_lexer": "ipython2", 216 | "version": "2.7.11" 217 | } 218 | }, 219 | "nbformat": 4, 220 | "nbformat_minor": 0 221 | } 222 | -------------------------------------------------------------------------------- /Advanced Python Objects and Data Structures/Advanced Dictionaries.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": { 7 | "collapsed": true 8 | }, 9 | "outputs": [], 10 | "source": [ 11 | "d = {'k1':1,'k2':2}" 12 | ] 13 | }, 14 | { 15 | "cell_type": "markdown", 16 | "metadata": {}, 17 | "source": [ 18 | "### Dictionary Comprehensions\n", 19 | "\n", 20 | "Just like List Comprehensions, Dictionary Data Types also support their own version of comprehension for quick creation.\n", 21 | "It is not as commonly used as List Comprehensions" 22 | ] 23 | }, 24 | { 25 | "cell_type": "code", 26 | "execution_count": 3, 27 | "metadata": { 28 | "collapsed": false 29 | }, 30 | "outputs": [ 31 | { 32 | "data": { 33 | "text/plain": [ 34 | "{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81}" 35 | ] 36 | }, 37 | "execution_count": 3, 38 | "metadata": {}, 39 | "output_type": "execute_result" 40 | } 41 | ], 42 | "source": [ 43 | "{x:x**2 for x in range(10)}" 44 | ] 45 | }, 46 | { 47 | "cell_type": "markdown", 48 | "metadata": {}, 49 | "source": [ 50 | "One of the reasons it is not as common is the difficulty in \n", 51 | "structuring the key names that are not based off the values." 52 | ] 53 | }, 54 | { 55 | "cell_type": "code", 56 | "execution_count": 5, 57 | "metadata": { 58 | "collapsed": false 59 | }, 60 | "outputs": [ 61 | { 62 | "name": "stdout", 63 | "output_type": "stream", 64 | "text": [ 65 | "k2\n", 66 | "k1\n" 67 | ] 68 | } 69 | ], 70 | "source": [ 71 | "for k in d.iterkeys():\n", 72 | " print k" 73 | ] 74 | }, 75 | { 76 | "cell_type": "code", 77 | "execution_count": 6, 78 | "metadata": { 79 | "collapsed": false 80 | }, 81 | "outputs": [ 82 | { 83 | "name": "stdout", 84 | "output_type": "stream", 85 | "text": [ 86 | "2\n", 87 | "1\n" 88 | ] 89 | } 90 | ], 91 | "source": [ 92 | "for k in d.itervalues():\n", 93 | " print k" 94 | ] 95 | }, 96 | { 97 | "cell_type": "code", 98 | "execution_count": 7, 99 | "metadata": { 100 | "collapsed": false 101 | }, 102 | "outputs": [ 103 | { 104 | "name": "stdout", 105 | "output_type": "stream", 106 | "text": [ 107 | "('k2', 2)\n", 108 | "('k1', 1)\n" 109 | ] 110 | } 111 | ], 112 | "source": [ 113 | "for k in d.iteritems():\n", 114 | " print k" 115 | ] 116 | }, 117 | { 118 | "cell_type": "markdown", 119 | "metadata": {}, 120 | "source": [ 121 | "You can use the view methods to view items keys and values." 122 | ] 123 | }, 124 | { 125 | "cell_type": "code", 126 | "execution_count": 8, 127 | "metadata": { 128 | "collapsed": false 129 | }, 130 | "outputs": [ 131 | { 132 | "data": { 133 | "text/plain": [ 134 | "dict_items([('k2', 2), ('k1', 1)])" 135 | ] 136 | }, 137 | "execution_count": 8, 138 | "metadata": {}, 139 | "output_type": "execute_result" 140 | } 141 | ], 142 | "source": [ 143 | "d.viewitems()" 144 | ] 145 | }, 146 | { 147 | "cell_type": "code", 148 | "execution_count": 9, 149 | "metadata": { 150 | "collapsed": false 151 | }, 152 | "outputs": [ 153 | { 154 | "data": { 155 | "text/plain": [ 156 | "dict_keys(['k2', 'k1'])" 157 | ] 158 | }, 159 | "execution_count": 9, 160 | "metadata": {}, 161 | "output_type": "execute_result" 162 | } 163 | ], 164 | "source": [ 165 | "d.viewkeys()" 166 | ] 167 | }, 168 | { 169 | "cell_type": "code", 170 | "execution_count": 10, 171 | "metadata": { 172 | "collapsed": false 173 | }, 174 | "outputs": [ 175 | { 176 | "data": { 177 | "text/plain": [ 178 | "dict_values([2, 1])" 179 | ] 180 | }, 181 | "execution_count": 10, 182 | "metadata": {}, 183 | "output_type": "execute_result" 184 | } 185 | ], 186 | "source": [ 187 | "d.viewvalues()" 188 | ] 189 | }, 190 | { 191 | "cell_type": "code", 192 | "execution_count": null, 193 | "metadata": { 194 | "collapsed": true 195 | }, 196 | "outputs": [], 197 | "source": [] 198 | } 199 | ], 200 | "metadata": { 201 | "kernelspec": { 202 | "display_name": "Python 2", 203 | "language": "python", 204 | "name": "python2" 205 | }, 206 | "language_info": { 207 | "codemirror_mode": { 208 | "name": "ipython", 209 | "version": 2 210 | }, 211 | "file_extension": ".py", 212 | "mimetype": "text/x-python", 213 | "name": "python", 214 | "nbconvert_exporter": "python", 215 | "pygments_lexer": "ipython2", 216 | "version": "2.7.11" 217 | } 218 | }, 219 | "nbformat": 4, 220 | "nbformat_minor": 0 221 | } 222 | -------------------------------------------------------------------------------- /Advanced Python Objects and Data Structures/Advanced Numbers.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Advanced Numbers" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "### Hexadecimal" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": 1, 20 | "metadata": { 21 | "collapsed": false 22 | }, 23 | "outputs": [ 24 | { 25 | "data": { 26 | "text/plain": [ 27 | "'0x7b'" 28 | ] 29 | }, 30 | "execution_count": 1, 31 | "metadata": {}, 32 | "output_type": "execute_result" 33 | } 34 | ], 35 | "source": [ 36 | "hex(123)" 37 | ] 38 | }, 39 | { 40 | "cell_type": "code", 41 | "execution_count": 2, 42 | "metadata": { 43 | "collapsed": false 44 | }, 45 | "outputs": [ 46 | { 47 | "data": { 48 | "text/plain": [ 49 | "'0x159'" 50 | ] 51 | }, 52 | "execution_count": 2, 53 | "metadata": {}, 54 | "output_type": "execute_result" 55 | } 56 | ], 57 | "source": [ 58 | "hex(345)" 59 | ] 60 | }, 61 | { 62 | "cell_type": "markdown", 63 | "metadata": {}, 64 | "source": [ 65 | "### Binary" 66 | ] 67 | }, 68 | { 69 | "cell_type": "code", 70 | "execution_count": 4, 71 | "metadata": { 72 | "collapsed": false 73 | }, 74 | "outputs": [ 75 | { 76 | "data": { 77 | "text/plain": [ 78 | "'0b10011010010'" 79 | ] 80 | }, 81 | "execution_count": 4, 82 | "metadata": {}, 83 | "output_type": "execute_result" 84 | } 85 | ], 86 | "source": [ 87 | "bin(1234)" 88 | ] 89 | }, 90 | { 91 | "cell_type": "code", 92 | "execution_count": 6, 93 | "metadata": { 94 | "collapsed": false 95 | }, 96 | "outputs": [ 97 | { 98 | "data": { 99 | "text/plain": [ 100 | "'0b1000110111'" 101 | ] 102 | }, 103 | "execution_count": 6, 104 | "metadata": {}, 105 | "output_type": "execute_result" 106 | } 107 | ], 108 | "source": [ 109 | "bin(567)" 110 | ] 111 | }, 112 | { 113 | "cell_type": "markdown", 114 | "metadata": {}, 115 | "source": [ 116 | "### Absolute" 117 | ] 118 | }, 119 | { 120 | "cell_type": "code", 121 | "execution_count": 7, 122 | "metadata": { 123 | "collapsed": false 124 | }, 125 | "outputs": [ 126 | { 127 | "data": { 128 | "text/plain": [ 129 | "3" 130 | ] 131 | }, 132 | "execution_count": 7, 133 | "metadata": {}, 134 | "output_type": "execute_result" 135 | } 136 | ], 137 | "source": [ 138 | "abs(-3)" 139 | ] 140 | }, 141 | { 142 | "cell_type": "code", 143 | "execution_count": 8, 144 | "metadata": { 145 | "collapsed": false 146 | }, 147 | "outputs": [ 148 | { 149 | "data": { 150 | "text/plain": [ 151 | "5" 152 | ] 153 | }, 154 | "execution_count": 8, 155 | "metadata": {}, 156 | "output_type": "execute_result" 157 | } 158 | ], 159 | "source": [ 160 | "abs(-5)" 161 | ] 162 | }, 163 | { 164 | "cell_type": "markdown", 165 | "metadata": {}, 166 | "source": [ 167 | "### Round" 168 | ] 169 | }, 170 | { 171 | "cell_type": "code", 172 | "execution_count": 9, 173 | "metadata": { 174 | "collapsed": false 175 | }, 176 | "outputs": [ 177 | { 178 | "data": { 179 | "text/plain": [ 180 | "3.0" 181 | ] 182 | }, 183 | "execution_count": 9, 184 | "metadata": {}, 185 | "output_type": "execute_result" 186 | } 187 | ], 188 | "source": [ 189 | "round(3.4)" 190 | ] 191 | }, 192 | { 193 | "cell_type": "code", 194 | "execution_count": 10, 195 | "metadata": { 196 | "collapsed": false 197 | }, 198 | "outputs": [ 199 | { 200 | "data": { 201 | "text/plain": [ 202 | "4.0" 203 | ] 204 | }, 205 | "execution_count": 10, 206 | "metadata": {}, 207 | "output_type": "execute_result" 208 | } 209 | ], 210 | "source": [ 211 | "round(4.153)" 212 | ] 213 | }, 214 | { 215 | "cell_type": "markdown", 216 | "metadata": {}, 217 | "source": [ 218 | "### POW" 219 | ] 220 | }, 221 | { 222 | "cell_type": "code", 223 | "execution_count": 11, 224 | "metadata": { 225 | "collapsed": false 226 | }, 227 | "outputs": [ 228 | { 229 | "data": { 230 | "text/plain": [ 231 | "16" 232 | ] 233 | }, 234 | "execution_count": 11, 235 | "metadata": {}, 236 | "output_type": "execute_result" 237 | } 238 | ], 239 | "source": [ 240 | "pow(2,4)" 241 | ] 242 | }, 243 | { 244 | "cell_type": "code", 245 | "execution_count": 12, 246 | "metadata": { 247 | "collapsed": false 248 | }, 249 | "outputs": [ 250 | { 251 | "data": { 252 | "text/plain": [ 253 | "729" 254 | ] 255 | }, 256 | "execution_count": 12, 257 | "metadata": {}, 258 | "output_type": "execute_result" 259 | } 260 | ], 261 | "source": [ 262 | "pow(3,6)" 263 | ] 264 | }, 265 | { 266 | "cell_type": "code", 267 | "execution_count": null, 268 | "metadata": { 269 | "collapsed": true 270 | }, 271 | "outputs": [], 272 | "source": [] 273 | } 274 | ], 275 | "metadata": { 276 | "kernelspec": { 277 | "display_name": "Python 2", 278 | "language": "python", 279 | "name": "python2" 280 | }, 281 | "language_info": { 282 | "codemirror_mode": { 283 | "name": "ipython", 284 | "version": 2 285 | }, 286 | "file_extension": ".py", 287 | "mimetype": "text/x-python", 288 | "name": "python", 289 | "nbconvert_exporter": "python", 290 | "pygments_lexer": "ipython2", 291 | "version": "2.7.11" 292 | } 293 | }, 294 | "nbformat": 4, 295 | "nbformat_minor": 0 296 | } 297 | -------------------------------------------------------------------------------- /Builtin Functions/.ipynb_checkpoints/All and any-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# all() and any()\n", 8 | "\n", 9 | "all() and any() are built-in functions in Python that allow us to conveniently check for boolean matching in an iterable. \n", 10 | "\n", 11 | "all() will return True if all elements in an iterable are True. It is the same as this function code:" 12 | ] 13 | }, 14 | { 15 | "cell_type": "code", 16 | "execution_count": 5, 17 | "metadata": { 18 | "collapsed": true 19 | }, 20 | "outputs": [], 21 | "source": [ 22 | "def all(iterable):\n", 23 | " for element in iterable:\n", 24 | " if not element:\n", 25 | " return False\n", 26 | " return True\n", 27 | "\n", 28 | "#any() will return True if any of the elements in the iterable are True. It is equivalent to the following function code:\n", 29 | "def any(iterable):\n", 30 | " for element in iterable:\n", 31 | " if element:\n", 32 | " return True\n", 33 | " return False" 34 | ] 35 | }, 36 | { 37 | "cell_type": "code", 38 | "execution_count": 1, 39 | "metadata": { 40 | "collapsed": true 41 | }, 42 | "outputs": [], 43 | "source": [ 44 | "p = [True, True, False, True]" 45 | ] 46 | }, 47 | { 48 | "cell_type": "code", 49 | "execution_count": 2, 50 | "metadata": { 51 | "collapsed": false 52 | }, 53 | "outputs": [ 54 | { 55 | "data": { 56 | "text/plain": [ 57 | "False" 58 | ] 59 | }, 60 | "execution_count": 2, 61 | "metadata": {}, 62 | "output_type": "execute_result" 63 | } 64 | ], 65 | "source": [ 66 | "all(p)" 67 | ] 68 | }, 69 | { 70 | "cell_type": "code", 71 | "execution_count": 3, 72 | "metadata": { 73 | "collapsed": false 74 | }, 75 | "outputs": [ 76 | { 77 | "data": { 78 | "text/plain": [ 79 | "True" 80 | ] 81 | }, 82 | "execution_count": 3, 83 | "metadata": {}, 84 | "output_type": "execute_result" 85 | } 86 | ], 87 | "source": [ 88 | "any(p)" 89 | ] 90 | }, 91 | { 92 | "cell_type": "code", 93 | "execution_count": null, 94 | "metadata": { 95 | "collapsed": true 96 | }, 97 | "outputs": [], 98 | "source": [] 99 | } 100 | ], 101 | "metadata": { 102 | "kernelspec": { 103 | "display_name": "Python 2", 104 | "language": "python", 105 | "name": "python2" 106 | }, 107 | "language_info": { 108 | "codemirror_mode": { 109 | "name": "ipython", 110 | "version": 2 111 | }, 112 | "file_extension": ".py", 113 | "mimetype": "text/x-python", 114 | "name": "python", 115 | "nbconvert_exporter": "python", 116 | "pygments_lexer": "ipython2", 117 | "version": "2.7.11" 118 | } 119 | }, 120 | "nbformat": 4, 121 | "nbformat_minor": 0 122 | } 123 | -------------------------------------------------------------------------------- /Builtin Functions/.ipynb_checkpoints/Complex-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# complex()\n", 8 | "\n", 9 | "complex() returns a complex number with the value real + imag*1j or converts a string or number to a complex number.\n", 10 | "\n", 11 | "If the first parameter is a string, it will be interpreted as a complex number and the function must be called without a second parameter. \n", 12 | "\n", 13 | "The second parameter can never be a string. \n", 14 | "\n", 15 | "Each argument may be any numeric type (including complex).\n", 16 | "\n", 17 | "If imag is omitted, it defaults to zero and the constructor serves as a numeric conversion like int and float. \n", 18 | "\n", 19 | "If both arguments are omitted, returns 0j.\n", 20 | "\n", 21 | "If you are doing math or engineering that requires complex numbers (such as dynamics,control systems, or impedance of a circuit) this is a useful tool to have in Python." 22 | ] 23 | }, 24 | { 25 | "cell_type": "code", 26 | "execution_count": 1, 27 | "metadata": { 28 | "collapsed": false 29 | }, 30 | "outputs": [ 31 | { 32 | "data": { 33 | "text/plain": [ 34 | "(2+3j)" 35 | ] 36 | }, 37 | "execution_count": 1, 38 | "metadata": {}, 39 | "output_type": "execute_result" 40 | } 41 | ], 42 | "source": [ 43 | "#create 2+3j\n", 44 | "complex(2,3)" 45 | ] 46 | }, 47 | { 48 | "cell_type": "code", 49 | "execution_count": 3, 50 | "metadata": { 51 | "collapsed": false 52 | }, 53 | "outputs": [ 54 | { 55 | "data": { 56 | "text/plain": [ 57 | "(3+4j)" 58 | ] 59 | }, 60 | "execution_count": 3, 61 | "metadata": {}, 62 | "output_type": "execute_result" 63 | } 64 | ], 65 | "source": [ 66 | "#we can also pass strings\n", 67 | "complex('3+4j')" 68 | ] 69 | }, 70 | { 71 | "cell_type": "code", 72 | "execution_count": null, 73 | "metadata": { 74 | "collapsed": true 75 | }, 76 | "outputs": [], 77 | "source": [] 78 | } 79 | ], 80 | "metadata": { 81 | "kernelspec": { 82 | "display_name": "Python 2", 83 | "language": "python", 84 | "name": "python2" 85 | }, 86 | "language_info": { 87 | "codemirror_mode": { 88 | "name": "ipython", 89 | "version": 2 90 | }, 91 | "file_extension": ".py", 92 | "mimetype": "text/x-python", 93 | "name": "python", 94 | "nbconvert_exporter": "python", 95 | "pygments_lexer": "ipython2", 96 | "version": "2.7.11" 97 | } 98 | }, 99 | "nbformat": 4, 100 | "nbformat_minor": 0 101 | } 102 | -------------------------------------------------------------------------------- /Builtin Functions/.ipynb_checkpoints/Enumerate-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# enumerate()\n", 8 | "\n", 9 | "In this lecture we will learn about an extremely useful built-in function: enumerate(). \n", 10 | "\n", 11 | "Enumerate allows you to keep a count as you iterate through an object. It does this by returning a tuple in the form (count,element). The function itself is equivalent to:\n", 12 | "def enumerate(sequence, start=0):\n", 13 | " n = start\n", 14 | " for elem in sequence:\n", 15 | " yield n, elem\n", 16 | " n += 1" 17 | ] 18 | }, 19 | { 20 | "cell_type": "code", 21 | "execution_count": 6, 22 | "metadata": { 23 | "collapsed": false 24 | }, 25 | "outputs": [], 26 | "source": [ 27 | "l = \"Hello World\"\n", 28 | "lst = l.split()" 29 | ] 30 | }, 31 | { 32 | "cell_type": "code", 33 | "execution_count": 7, 34 | "metadata": { 35 | "collapsed": false 36 | }, 37 | "outputs": [ 38 | { 39 | "name": "stdout", 40 | "output_type": "stream", 41 | "text": [ 42 | "0\n", 43 | "Hello\n", 44 | "1\n", 45 | "World\n" 46 | ] 47 | } 48 | ], 49 | "source": [ 50 | "for count,item in enumerate(lst):\n", 51 | " print count\n", 52 | " print item" 53 | ] 54 | }, 55 | { 56 | "cell_type": "code", 57 | "execution_count": null, 58 | "metadata": { 59 | "collapsed": true 60 | }, 61 | "outputs": [], 62 | "source": [] 63 | } 64 | ], 65 | "metadata": { 66 | "kernelspec": { 67 | "display_name": "Python 2", 68 | "language": "python", 69 | "name": "python2" 70 | }, 71 | "language_info": { 72 | "codemirror_mode": { 73 | "name": "ipython", 74 | "version": 2 75 | }, 76 | "file_extension": ".py", 77 | "mimetype": "text/x-python", 78 | "name": "python", 79 | "nbconvert_exporter": "python", 80 | "pygments_lexer": "ipython2", 81 | "version": "2.7.11" 82 | } 83 | }, 84 | "nbformat": 4, 85 | "nbformat_minor": 0 86 | } 87 | -------------------------------------------------------------------------------- /Builtin Functions/.ipynb_checkpoints/Filter-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# filter\n", 8 | "\n", 9 | "The function filter(function, list) offers a convenient way to filter out all the elements of an iterable, for which the function returns True.\n", 10 | "\n", 11 | "The function filter(function(),l) needs a function as its first argument. \n", 12 | "The function needs to return a Boolean value (either True or False).\n", 13 | "This function will be applied to every element of the iterable. \n", 14 | "Only if the function returns True will the element of the iterable be included in the result.\n" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": 2, 20 | "metadata": { 21 | "collapsed": false 22 | }, 23 | "outputs": [], 24 | "source": [ 25 | "def even_num(num):\n", 26 | " if num%2==0:\n", 27 | " return True" 28 | ] 29 | }, 30 | { 31 | "cell_type": "code", 32 | "execution_count": 4, 33 | "metadata": { 34 | "collapsed": false 35 | }, 36 | "outputs": [ 37 | { 38 | "data": { 39 | "text/plain": [ 40 | "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]" 41 | ] 42 | }, 43 | "execution_count": 4, 44 | "metadata": {}, 45 | "output_type": "execute_result" 46 | } 47 | ], 48 | "source": [ 49 | "L = range(20)\n", 50 | "L" 51 | ] 52 | }, 53 | { 54 | "cell_type": "code", 55 | "execution_count": 5, 56 | "metadata": { 57 | "collapsed": false 58 | }, 59 | "outputs": [ 60 | { 61 | "data": { 62 | "text/plain": [ 63 | "[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]" 64 | ] 65 | }, 66 | "execution_count": 5, 67 | "metadata": {}, 68 | "output_type": "execute_result" 69 | } 70 | ], 71 | "source": [ 72 | "filter(even_num,L)" 73 | ] 74 | }, 75 | { 76 | "cell_type": "markdown", 77 | "metadata": {}, 78 | "source": [ 79 | "Map Function is applying function on collection, Filter functions applies the condition and prints the results\n", 80 | "that satisfies the condition" 81 | ] 82 | }, 83 | { 84 | "cell_type": "code", 85 | "execution_count": 6, 86 | "metadata": { 87 | "collapsed": false 88 | }, 89 | "outputs": [ 90 | { 91 | "data": { 92 | "text/plain": [ 93 | "[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]" 94 | ] 95 | }, 96 | "execution_count": 6, 97 | "metadata": {}, 98 | "output_type": "execute_result" 99 | } 100 | ], 101 | "source": [ 102 | "filter(lambda x:x%2==0, L)" 103 | ] 104 | }, 105 | { 106 | "cell_type": "code", 107 | "execution_count": null, 108 | "metadata": { 109 | "collapsed": true 110 | }, 111 | "outputs": [], 112 | "source": [] 113 | } 114 | ], 115 | "metadata": { 116 | "kernelspec": { 117 | "display_name": "Python 2", 118 | "language": "python", 119 | "name": "python2" 120 | }, 121 | "language_info": { 122 | "codemirror_mode": { 123 | "name": "ipython", 124 | "version": 2 125 | }, 126 | "file_extension": ".py", 127 | "mimetype": "text/x-python", 128 | "name": "python", 129 | "nbconvert_exporter": "python", 130 | "pygments_lexer": "ipython2", 131 | "version": "2.7.11" 132 | } 133 | }, 134 | "nbformat": 4, 135 | "nbformat_minor": 0 136 | } 137 | -------------------------------------------------------------------------------- /Builtin Functions/.ipynb_checkpoints/Map-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# map()\n", 8 | "\n", 9 | "map() is a function that takes in two arguments: a function and a sequence iterable. In the form: map(function, sequence)\n", 10 | "\n", 11 | "The first argument is the name of a function and the second a sequence (e.g. a list). map() applies the function to all the elements of the sequence. It returns a new list with the elements changed by function." 12 | ] 13 | }, 14 | { 15 | "cell_type": "code", 16 | "execution_count": 1, 17 | "metadata": { 18 | "collapsed": true 19 | }, 20 | "outputs": [], 21 | "source": [ 22 | "#Define two functions for demo\n", 23 | "def fahrenheit(T):\n", 24 | " return ((float(9)/5)*T + 32)\n", 25 | "def celsius(T):\n", 26 | " return (float(5)/9)*(T-32)\n", 27 | " \n", 28 | "temp = [0, 22.5, 40,100]" 29 | ] 30 | }, 31 | { 32 | "cell_type": "code", 33 | "execution_count": 3, 34 | "metadata": { 35 | "collapsed": false 36 | }, 37 | "outputs": [ 38 | { 39 | "name": "stdout", 40 | "output_type": "stream", 41 | "text": [ 42 | "[32.0, 72.5, 104.0, 212.0]\n" 43 | ] 44 | } 45 | ], 46 | "source": [ 47 | "# Here we are applying fahenheit function on sequence of values, list called temp\n", 48 | "#This 'applying' is called Mapping\n", 49 | "FtoT = map(fahrenheit,temp)\n", 50 | "\n", 51 | "print FtoT" 52 | ] 53 | }, 54 | { 55 | "cell_type": "code", 56 | "execution_count": 4, 57 | "metadata": { 58 | "collapsed": false 59 | }, 60 | "outputs": [ 61 | { 62 | "name": "stdout", 63 | "output_type": "stream", 64 | "text": [ 65 | "[0.0, 22.5, 40.0, 100.0]\n" 66 | ] 67 | } 68 | ], 69 | "source": [ 70 | "#Now we convert back to Celsius\n", 71 | "TtoC = map(celsius,FtoT)\n", 72 | "\n", 73 | "print TtoC" 74 | ] 75 | }, 76 | { 77 | "cell_type": "markdown", 78 | "metadata": {}, 79 | "source": [ 80 | "Using Lambda expression we can reduce the overhead of defining functions explicitly" 81 | ] 82 | }, 83 | { 84 | "cell_type": "code", 85 | "execution_count": 5, 86 | "metadata": { 87 | "collapsed": false 88 | }, 89 | "outputs": [ 90 | { 91 | "data": { 92 | "text/plain": [ 93 | "[0.0, 22.5, 40.0, 100.0]" 94 | ] 95 | }, 96 | "execution_count": 5, 97 | "metadata": {}, 98 | "output_type": "execute_result" 99 | } 100 | ], 101 | "source": [ 102 | "map(lambda x: (5.0/9)*(x - 32), FtoT)" 103 | ] 104 | }, 105 | { 106 | "cell_type": "code", 107 | "execution_count": 8, 108 | "metadata": { 109 | "collapsed": false 110 | }, 111 | "outputs": [ 112 | { 113 | "data": { 114 | "text/plain": [ 115 | "[32.0, 72.5, 104.0, 212.0]" 116 | ] 117 | }, 118 | "execution_count": 8, 119 | "metadata": {}, 120 | "output_type": "execute_result" 121 | } 122 | ], 123 | "source": [ 124 | "map(lambda y:(float(9.0)/5)*y+32, temp)" 125 | ] 126 | }, 127 | { 128 | "cell_type": "markdown", 129 | "metadata": {}, 130 | "source": [ 131 | "Using map is much more commonly used with lambda expressions since the entire purpose of map() \n", 132 | "is to save effort on having to create manual for loops.\n", 133 | "\n", 134 | "map() can be applied to more than one iterable. The iterables have to have the same length.\n", 135 | "\n", 136 | "For instance, if we are working with two lists-map() will apply its lambda function to the \n", 137 | "elements of the argument lists, i.e. it first applies to the elements with the 0th index, \n", 138 | "then to the elements with the 1st index until the n-th index is reached." 139 | ] 140 | }, 141 | { 142 | "cell_type": "code", 143 | "execution_count": 10, 144 | "metadata": { 145 | "collapsed": false 146 | }, 147 | "outputs": [ 148 | { 149 | "data": { 150 | "text/plain": [ 151 | "[10, 10, 10, 10, 10]" 152 | ] 153 | }, 154 | "execution_count": 10, 155 | "metadata": {}, 156 | "output_type": "execute_result" 157 | } 158 | ], 159 | "source": [ 160 | "#Here for map and Lambda to work, our collections should be of same length \n", 161 | "#if len(a) != len(b)|len(c), below logic doesn't apply\n", 162 | "a = [1,2,3,4,5]\n", 163 | "b = [9,8,7,6,5]\n", 164 | "c = [1,3,5,7,9]\n", 165 | "\n", 166 | "map(lambda x,y:x+y, a,b)" 167 | ] 168 | }, 169 | { 170 | "cell_type": "code", 171 | "execution_count": 11, 172 | "metadata": { 173 | "collapsed": false 174 | }, 175 | "outputs": [ 176 | { 177 | "data": { 178 | "text/plain": [ 179 | "[11, 13, 15, 17, 19]" 180 | ] 181 | }, 182 | "execution_count": 11, 183 | "metadata": {}, 184 | "output_type": "execute_result" 185 | } 186 | ], 187 | "source": [ 188 | "map(lambda x,y,z:x+y+z, a,b,c)" 189 | ] 190 | }, 191 | { 192 | "cell_type": "code", 193 | "execution_count": null, 194 | "metadata": { 195 | "collapsed": true 196 | }, 197 | "outputs": [], 198 | "source": [] 199 | } 200 | ], 201 | "metadata": { 202 | "kernelspec": { 203 | "display_name": "Python 2", 204 | "language": "python", 205 | "name": "python2" 206 | }, 207 | "language_info": { 208 | "codemirror_mode": { 209 | "name": "ipython", 210 | "version": 2 211 | }, 212 | "file_extension": ".py", 213 | "mimetype": "text/x-python", 214 | "name": "python", 215 | "nbconvert_exporter": "python", 216 | "pygments_lexer": "ipython2", 217 | "version": "2.7.11" 218 | } 219 | }, 220 | "nbformat": 4, 221 | "nbformat_minor": 0 222 | } 223 | -------------------------------------------------------------------------------- /Builtin Functions/.ipynb_checkpoints/Reduce-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# reduce()\n", 8 | "\n", 9 | "Many times students have difficulty understanding reduce() so pay careful attention to this lecture. The function reduce(function, sequence) continually applies the function to the sequence. It then returns a single value.\n", 10 | "\n", 11 | "If seq = [ s1, s2, s3, ... , sn ], calling reduce(function, sequence) works like this:\n", 12 | "\n", 13 | "At first the first two elements of seq will be applied to function, i.e. func(s1,s2)\n", 14 | "\n", 15 | "The list on which reduce() works looks now like this: [ function(s1, s2), s3, ... , sn ]\n", 16 | "\n", 17 | "In the next step the function will be applied on the previous result and the third element of the list, i.e. function(function(s1, s2),s3)\n", 18 | "\n", 19 | "The list looks like this now: [ function(function(s1, s2),s3), ... , sn ]\n", 20 | "\n", 21 | "It continues like this until just one element is left and return this element as the result of reduce()\n" 22 | ] 23 | }, 24 | { 25 | "cell_type": "code", 26 | "execution_count": 1, 27 | "metadata": { 28 | "collapsed": false 29 | }, 30 | "outputs": [ 31 | { 32 | "data": { 33 | "text/plain": [ 34 | "220" 35 | ] 36 | }, 37 | "execution_count": 1, 38 | "metadata": {}, 39 | "output_type": "execute_result" 40 | } 41 | ], 42 | "source": [ 43 | "l = [40,50,60,70]\n", 44 | "\n", 45 | "reduce(lambda x,y:x+y, l)" 46 | ] 47 | }, 48 | { 49 | "cell_type": "code", 50 | "execution_count": null, 51 | "metadata": { 52 | "collapsed": true 53 | }, 54 | "outputs": [], 55 | "source": [] 56 | } 57 | ], 58 | "metadata": { 59 | "kernelspec": { 60 | "display_name": "Python 2", 61 | "language": "python", 62 | "name": "python2" 63 | }, 64 | "language_info": { 65 | "codemirror_mode": { 66 | "name": "ipython", 67 | "version": 2 68 | }, 69 | "file_extension": ".py", 70 | "mimetype": "text/x-python", 71 | "name": "python", 72 | "nbconvert_exporter": "python", 73 | "pygments_lexer": "ipython2", 74 | "version": "2.7.11" 75 | } 76 | }, 77 | "nbformat": 4, 78 | "nbformat_minor": 0 79 | } 80 | -------------------------------------------------------------------------------- /Builtin Functions/.ipynb_checkpoints/Zip-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# zip\n", 8 | "\n", 9 | "zip() makes an iterator that aggregates elements from each of the iterables.\n", 10 | "\n", 11 | "Returns an iterator of tuples, where the i-th tuple contains the i-th element from each of the argument sequences or iterables. The iterator stops when the shortest input iterable is exhausted. \n", 12 | "\n", 13 | "With a single iterable argument, it returns an iterator of 1-tuples. With no arguments, it returns an empty iterator.\n", 14 | "\n", 15 | "zip() is equivalent to:\n", 16 | "\n", 17 | "def zip(*iterables):\n", 18 | " # zip('ABCD', 'xy') --> Ax By\n", 19 | " sentinel = object()\n", 20 | " iterators = [iter(it) for it in iterables]\n", 21 | " while iterators:\n", 22 | " result = []\n", 23 | " for it in iterators:\n", 24 | " elem = next(it, sentinel)\n", 25 | " if elem is sentinel:\n", 26 | " return\n", 27 | " result.append(elem)\n", 28 | " yield tuple(result)\n", 29 | "\n", 30 | "\n", 31 | "zip() should only be used with unequal length inputs when you don’t care about trailing, unmatched values from the longer iterables.\n" 32 | ] 33 | }, 34 | { 35 | "cell_type": "code", 36 | "execution_count": 1, 37 | "metadata": { 38 | "collapsed": false 39 | }, 40 | "outputs": [ 41 | { 42 | "data": { 43 | "text/plain": [ 44 | "[(1, 4), (2, 5), (3, 6)]" 45 | ] 46 | }, 47 | "execution_count": 1, 48 | "metadata": {}, 49 | "output_type": "execute_result" 50 | } 51 | ], 52 | "source": [ 53 | "x = [1,2,3]\n", 54 | "y = [4,5,6]\n", 55 | "\n", 56 | "zip(x,y)" 57 | ] 58 | }, 59 | { 60 | "cell_type": "code", 61 | "execution_count": 2, 62 | "metadata": { 63 | "collapsed": false 64 | }, 65 | "outputs": [ 66 | { 67 | "data": { 68 | "text/plain": [ 69 | "[(1, 4), (2, 5), (3, 6)]" 70 | ] 71 | }, 72 | "execution_count": 2, 73 | "metadata": {}, 74 | "output_type": "execute_result" 75 | } 76 | ], 77 | "source": [ 78 | "a = [1,2,3]\n", 79 | "b = [4,5,6,7,8]\n", 80 | "\n", 81 | "zip(a,b)" 82 | ] 83 | }, 84 | { 85 | "cell_type": "code", 86 | "execution_count": 3, 87 | "metadata": { 88 | "collapsed": false 89 | }, 90 | "outputs": [ 91 | { 92 | "data": { 93 | "text/plain": [ 94 | "[('a', 'c'), ('b', 'd')]" 95 | ] 96 | }, 97 | "execution_count": 3, 98 | "metadata": {}, 99 | "output_type": "execute_result" 100 | } 101 | ], 102 | "source": [ 103 | "d1 = {'a':1, 'b':2}\n", 104 | "d2 = {'c':3, 'd':4}\n", 105 | "\n", 106 | "zip(d1,d2)" 107 | ] 108 | }, 109 | { 110 | "cell_type": "code", 111 | "execution_count": 10, 112 | "metadata": { 113 | "collapsed": false 114 | }, 115 | "outputs": [ 116 | { 117 | "data": { 118 | "text/plain": [ 119 | "[('c', 1), ('d', 2)]" 120 | ] 121 | }, 122 | "execution_count": 10, 123 | "metadata": {}, 124 | "output_type": "execute_result" 125 | } 126 | ], 127 | "source": [ 128 | "zip(d2,d1.itervalues())" 129 | ] 130 | }, 131 | { 132 | "cell_type": "code", 133 | "execution_count": 11, 134 | "metadata": { 135 | "collapsed": false 136 | }, 137 | "outputs": [ 138 | { 139 | "data": { 140 | "text/plain": [ 141 | "[('a', 3), ('b', 4)]" 142 | ] 143 | }, 144 | "execution_count": 11, 145 | "metadata": {}, 146 | "output_type": "execute_result" 147 | } 148 | ], 149 | "source": [ 150 | "zip(d1,d2.itervalues())" 151 | ] 152 | }, 153 | { 154 | "cell_type": "code", 155 | "execution_count": null, 156 | "metadata": { 157 | "collapsed": true 158 | }, 159 | "outputs": [], 160 | "source": [] 161 | } 162 | ], 163 | "metadata": { 164 | "kernelspec": { 165 | "display_name": "Python 2", 166 | "language": "python", 167 | "name": "python2" 168 | }, 169 | "language_info": { 170 | "codemirror_mode": { 171 | "name": "ipython", 172 | "version": 2 173 | }, 174 | "file_extension": ".py", 175 | "mimetype": "text/x-python", 176 | "name": "python", 177 | "nbconvert_exporter": "python", 178 | "pygments_lexer": "ipython2", 179 | "version": "2.7.11" 180 | } 181 | }, 182 | "nbformat": 4, 183 | "nbformat_minor": 0 184 | } 185 | -------------------------------------------------------------------------------- /Builtin Functions/All and any.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# all() and any()\n", 8 | "\n", 9 | "all() and any() are built-in functions in Python that allow us to conveniently check for boolean matching in an iterable. \n", 10 | "\n", 11 | "all() will return True if all elements in an iterable are True. It is the same as this function code:" 12 | ] 13 | }, 14 | { 15 | "cell_type": "code", 16 | "execution_count": 5, 17 | "metadata": { 18 | "collapsed": true 19 | }, 20 | "outputs": [], 21 | "source": [ 22 | "def all(iterable):\n", 23 | " for element in iterable:\n", 24 | " if not element:\n", 25 | " return False\n", 26 | " return True\n", 27 | "\n", 28 | "#any() will return True if any of the elements in the iterable are True. It is equivalent to the following function code:\n", 29 | "def any(iterable):\n", 30 | " for element in iterable:\n", 31 | " if element:\n", 32 | " return True\n", 33 | " return False" 34 | ] 35 | }, 36 | { 37 | "cell_type": "code", 38 | "execution_count": 1, 39 | "metadata": { 40 | "collapsed": true 41 | }, 42 | "outputs": [], 43 | "source": [ 44 | "p = [True, True, False, True]" 45 | ] 46 | }, 47 | { 48 | "cell_type": "code", 49 | "execution_count": 2, 50 | "metadata": { 51 | "collapsed": false 52 | }, 53 | "outputs": [ 54 | { 55 | "data": { 56 | "text/plain": [ 57 | "False" 58 | ] 59 | }, 60 | "execution_count": 2, 61 | "metadata": {}, 62 | "output_type": "execute_result" 63 | } 64 | ], 65 | "source": [ 66 | "all(p)" 67 | ] 68 | }, 69 | { 70 | "cell_type": "code", 71 | "execution_count": 3, 72 | "metadata": { 73 | "collapsed": false 74 | }, 75 | "outputs": [ 76 | { 77 | "data": { 78 | "text/plain": [ 79 | "True" 80 | ] 81 | }, 82 | "execution_count": 3, 83 | "metadata": {}, 84 | "output_type": "execute_result" 85 | } 86 | ], 87 | "source": [ 88 | "any(p)" 89 | ] 90 | }, 91 | { 92 | "cell_type": "code", 93 | "execution_count": null, 94 | "metadata": { 95 | "collapsed": true 96 | }, 97 | "outputs": [], 98 | "source": [] 99 | } 100 | ], 101 | "metadata": { 102 | "kernelspec": { 103 | "display_name": "Python 2", 104 | "language": "python", 105 | "name": "python2" 106 | }, 107 | "language_info": { 108 | "codemirror_mode": { 109 | "name": "ipython", 110 | "version": 2 111 | }, 112 | "file_extension": ".py", 113 | "mimetype": "text/x-python", 114 | "name": "python", 115 | "nbconvert_exporter": "python", 116 | "pygments_lexer": "ipython2", 117 | "version": "2.7.11" 118 | } 119 | }, 120 | "nbformat": 4, 121 | "nbformat_minor": 0 122 | } 123 | -------------------------------------------------------------------------------- /Builtin Functions/Complex.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# complex()\n", 8 | "\n", 9 | "complex() returns a complex number with the value real + imag*1j or converts a string or number to a complex number.\n", 10 | "\n", 11 | "If the first parameter is a string, it will be interpreted as a complex number and the function must be called without a second parameter. \n", 12 | "\n", 13 | "The second parameter can never be a string. \n", 14 | "\n", 15 | "Each argument may be any numeric type (including complex).\n", 16 | "\n", 17 | "If imag is omitted, it defaults to zero and the constructor serves as a numeric conversion like int and float. \n", 18 | "\n", 19 | "If both arguments are omitted, returns 0j.\n", 20 | "\n", 21 | "If you are doing math or engineering that requires complex numbers (such as dynamics,control systems, or impedance of a circuit) this is a useful tool to have in Python." 22 | ] 23 | }, 24 | { 25 | "cell_type": "code", 26 | "execution_count": 1, 27 | "metadata": { 28 | "collapsed": false 29 | }, 30 | "outputs": [ 31 | { 32 | "data": { 33 | "text/plain": [ 34 | "(2+3j)" 35 | ] 36 | }, 37 | "execution_count": 1, 38 | "metadata": {}, 39 | "output_type": "execute_result" 40 | } 41 | ], 42 | "source": [ 43 | "#create 2+3j\n", 44 | "complex(2,3)" 45 | ] 46 | }, 47 | { 48 | "cell_type": "code", 49 | "execution_count": 3, 50 | "metadata": { 51 | "collapsed": false 52 | }, 53 | "outputs": [ 54 | { 55 | "data": { 56 | "text/plain": [ 57 | "(3+4j)" 58 | ] 59 | }, 60 | "execution_count": 3, 61 | "metadata": {}, 62 | "output_type": "execute_result" 63 | } 64 | ], 65 | "source": [ 66 | "#we can also pass strings\n", 67 | "complex('3+4j')" 68 | ] 69 | }, 70 | { 71 | "cell_type": "code", 72 | "execution_count": null, 73 | "metadata": { 74 | "collapsed": true 75 | }, 76 | "outputs": [], 77 | "source": [] 78 | } 79 | ], 80 | "metadata": { 81 | "kernelspec": { 82 | "display_name": "Python 2", 83 | "language": "python", 84 | "name": "python2" 85 | }, 86 | "language_info": { 87 | "codemirror_mode": { 88 | "name": "ipython", 89 | "version": 2 90 | }, 91 | "file_extension": ".py", 92 | "mimetype": "text/x-python", 93 | "name": "python", 94 | "nbconvert_exporter": "python", 95 | "pygments_lexer": "ipython2", 96 | "version": "2.7.11" 97 | } 98 | }, 99 | "nbformat": 4, 100 | "nbformat_minor": 0 101 | } 102 | -------------------------------------------------------------------------------- /Builtin Functions/Enumerate.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# enumerate()\n", 8 | "\n", 9 | "In this lecture we will learn about an extremely useful built-in function: enumerate(). \n", 10 | "\n", 11 | "Enumerate allows you to keep a count as you iterate through an object. It does this by returning a tuple in the form (count,element). The function itself is equivalent to:\n", 12 | "def enumerate(sequence, start=0):\n", 13 | " n = start\n", 14 | " for elem in sequence:\n", 15 | " yield n, elem\n", 16 | " n += 1" 17 | ] 18 | }, 19 | { 20 | "cell_type": "code", 21 | "execution_count": 6, 22 | "metadata": { 23 | "collapsed": false 24 | }, 25 | "outputs": [], 26 | "source": [ 27 | "l = \"Hello World\"\n", 28 | "lst = l.split()" 29 | ] 30 | }, 31 | { 32 | "cell_type": "code", 33 | "execution_count": 7, 34 | "metadata": { 35 | "collapsed": false 36 | }, 37 | "outputs": [ 38 | { 39 | "name": "stdout", 40 | "output_type": "stream", 41 | "text": [ 42 | "0\n", 43 | "Hello\n", 44 | "1\n", 45 | "World\n" 46 | ] 47 | } 48 | ], 49 | "source": [ 50 | "for count,item in enumerate(lst):\n", 51 | " print count\n", 52 | " print item" 53 | ] 54 | }, 55 | { 56 | "cell_type": "code", 57 | "execution_count": null, 58 | "metadata": { 59 | "collapsed": true 60 | }, 61 | "outputs": [], 62 | "source": [] 63 | } 64 | ], 65 | "metadata": { 66 | "kernelspec": { 67 | "display_name": "Python 2", 68 | "language": "python", 69 | "name": "python2" 70 | }, 71 | "language_info": { 72 | "codemirror_mode": { 73 | "name": "ipython", 74 | "version": 2 75 | }, 76 | "file_extension": ".py", 77 | "mimetype": "text/x-python", 78 | "name": "python", 79 | "nbconvert_exporter": "python", 80 | "pygments_lexer": "ipython2", 81 | "version": "2.7.11" 82 | } 83 | }, 84 | "nbformat": 4, 85 | "nbformat_minor": 0 86 | } 87 | -------------------------------------------------------------------------------- /Builtin Functions/Filter.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# filter\n", 8 | "\n", 9 | "The function filter(function, list) offers a convenient way to filter out all the elements of an iterable, for which the function returns True.\n", 10 | "\n", 11 | "The function filter(function(),l) needs a function as its first argument. \n", 12 | "The function needs to return a Boolean value (either True or False).\n", 13 | "This function will be applied to every element of the iterable. \n", 14 | "Only if the function returns True will the element of the iterable be included in the result.\n" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": 2, 20 | "metadata": { 21 | "collapsed": false 22 | }, 23 | "outputs": [], 24 | "source": [ 25 | "def even_num(num):\n", 26 | " if num%2==0:\n", 27 | " return True" 28 | ] 29 | }, 30 | { 31 | "cell_type": "code", 32 | "execution_count": 4, 33 | "metadata": { 34 | "collapsed": false 35 | }, 36 | "outputs": [ 37 | { 38 | "data": { 39 | "text/plain": [ 40 | "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]" 41 | ] 42 | }, 43 | "execution_count": 4, 44 | "metadata": {}, 45 | "output_type": "execute_result" 46 | } 47 | ], 48 | "source": [ 49 | "L = range(20)\n", 50 | "L" 51 | ] 52 | }, 53 | { 54 | "cell_type": "code", 55 | "execution_count": 5, 56 | "metadata": { 57 | "collapsed": false 58 | }, 59 | "outputs": [ 60 | { 61 | "data": { 62 | "text/plain": [ 63 | "[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]" 64 | ] 65 | }, 66 | "execution_count": 5, 67 | "metadata": {}, 68 | "output_type": "execute_result" 69 | } 70 | ], 71 | "source": [ 72 | "filter(even_num,L)" 73 | ] 74 | }, 75 | { 76 | "cell_type": "markdown", 77 | "metadata": {}, 78 | "source": [ 79 | "Map Function is applying function on collection, Filter functions applies the condition and prints the results\n", 80 | "that satisfies the condition" 81 | ] 82 | }, 83 | { 84 | "cell_type": "code", 85 | "execution_count": 6, 86 | "metadata": { 87 | "collapsed": false 88 | }, 89 | "outputs": [ 90 | { 91 | "data": { 92 | "text/plain": [ 93 | "[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]" 94 | ] 95 | }, 96 | "execution_count": 6, 97 | "metadata": {}, 98 | "output_type": "execute_result" 99 | } 100 | ], 101 | "source": [ 102 | "filter(lambda x:x%2==0, L)" 103 | ] 104 | }, 105 | { 106 | "cell_type": "code", 107 | "execution_count": null, 108 | "metadata": { 109 | "collapsed": true 110 | }, 111 | "outputs": [], 112 | "source": [] 113 | } 114 | ], 115 | "metadata": { 116 | "kernelspec": { 117 | "display_name": "Python 2", 118 | "language": "python", 119 | "name": "python2" 120 | }, 121 | "language_info": { 122 | "codemirror_mode": { 123 | "name": "ipython", 124 | "version": 2 125 | }, 126 | "file_extension": ".py", 127 | "mimetype": "text/x-python", 128 | "name": "python", 129 | "nbconvert_exporter": "python", 130 | "pygments_lexer": "ipython2", 131 | "version": "2.7.11" 132 | } 133 | }, 134 | "nbformat": 4, 135 | "nbformat_minor": 0 136 | } 137 | -------------------------------------------------------------------------------- /Builtin Functions/Map.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# map()\n", 8 | "\n", 9 | "map() is a function that takes in two arguments: a function and a sequence iterable. In the form: map(function, sequence)\n", 10 | "\n", 11 | "The first argument is the name of a function and the second a sequence (e.g. a list). map() applies the function to all the elements of the sequence. It returns a new list with the elements changed by function." 12 | ] 13 | }, 14 | { 15 | "cell_type": "code", 16 | "execution_count": 1, 17 | "metadata": { 18 | "collapsed": true 19 | }, 20 | "outputs": [], 21 | "source": [ 22 | "#Define two functions for demo\n", 23 | "def fahrenheit(T):\n", 24 | " return ((float(9)/5)*T + 32)\n", 25 | "def celsius(T):\n", 26 | " return (float(5)/9)*(T-32)\n", 27 | " \n", 28 | "temp = [0, 22.5, 40,100]" 29 | ] 30 | }, 31 | { 32 | "cell_type": "code", 33 | "execution_count": 3, 34 | "metadata": { 35 | "collapsed": false 36 | }, 37 | "outputs": [ 38 | { 39 | "name": "stdout", 40 | "output_type": "stream", 41 | "text": [ 42 | "[32.0, 72.5, 104.0, 212.0]\n" 43 | ] 44 | } 45 | ], 46 | "source": [ 47 | "# Here we are applying fahenheit function on sequence of values, list called temp\n", 48 | "#This 'applying' is called Mapping\n", 49 | "FtoT = map(fahrenheit,temp)\n", 50 | "\n", 51 | "print FtoT" 52 | ] 53 | }, 54 | { 55 | "cell_type": "code", 56 | "execution_count": 4, 57 | "metadata": { 58 | "collapsed": false 59 | }, 60 | "outputs": [ 61 | { 62 | "name": "stdout", 63 | "output_type": "stream", 64 | "text": [ 65 | "[0.0, 22.5, 40.0, 100.0]\n" 66 | ] 67 | } 68 | ], 69 | "source": [ 70 | "#Now we convert back to Celsius\n", 71 | "TtoC = map(celsius,FtoT)\n", 72 | "\n", 73 | "print TtoC" 74 | ] 75 | }, 76 | { 77 | "cell_type": "markdown", 78 | "metadata": {}, 79 | "source": [ 80 | "Using Lambda expression we can reduce the overhead of defining functions explicitly" 81 | ] 82 | }, 83 | { 84 | "cell_type": "code", 85 | "execution_count": 5, 86 | "metadata": { 87 | "collapsed": false 88 | }, 89 | "outputs": [ 90 | { 91 | "data": { 92 | "text/plain": [ 93 | "[0.0, 22.5, 40.0, 100.0]" 94 | ] 95 | }, 96 | "execution_count": 5, 97 | "metadata": {}, 98 | "output_type": "execute_result" 99 | } 100 | ], 101 | "source": [ 102 | "map(lambda x: (5.0/9)*(x - 32), FtoT)" 103 | ] 104 | }, 105 | { 106 | "cell_type": "code", 107 | "execution_count": 8, 108 | "metadata": { 109 | "collapsed": false 110 | }, 111 | "outputs": [ 112 | { 113 | "data": { 114 | "text/plain": [ 115 | "[32.0, 72.5, 104.0, 212.0]" 116 | ] 117 | }, 118 | "execution_count": 8, 119 | "metadata": {}, 120 | "output_type": "execute_result" 121 | } 122 | ], 123 | "source": [ 124 | "map(lambda y:(float(9.0)/5)*y+32, temp)" 125 | ] 126 | }, 127 | { 128 | "cell_type": "markdown", 129 | "metadata": {}, 130 | "source": [ 131 | "Using map is much more commonly used with lambda expressions since the entire purpose of map() \n", 132 | "is to save effort on having to create manual for loops.\n", 133 | "\n", 134 | "map() can be applied to more than one iterable. The iterables have to have the same length.\n", 135 | "\n", 136 | "For instance, if we are working with two lists-map() will apply its lambda function to the \n", 137 | "elements of the argument lists, i.e. it first applies to the elements with the 0th index, \n", 138 | "then to the elements with the 1st index until the n-th index is reached." 139 | ] 140 | }, 141 | { 142 | "cell_type": "code", 143 | "execution_count": 10, 144 | "metadata": { 145 | "collapsed": false 146 | }, 147 | "outputs": [ 148 | { 149 | "data": { 150 | "text/plain": [ 151 | "[10, 10, 10, 10, 10]" 152 | ] 153 | }, 154 | "execution_count": 10, 155 | "metadata": {}, 156 | "output_type": "execute_result" 157 | } 158 | ], 159 | "source": [ 160 | "#Here for map and Lambda to work, our collections should be of same length \n", 161 | "#if len(a) != len(b)|len(c), below logic doesn't apply\n", 162 | "a = [1,2,3,4,5]\n", 163 | "b = [9,8,7,6,5]\n", 164 | "c = [1,3,5,7,9]\n", 165 | "\n", 166 | "map(lambda x,y:x+y, a,b)" 167 | ] 168 | }, 169 | { 170 | "cell_type": "code", 171 | "execution_count": 11, 172 | "metadata": { 173 | "collapsed": false 174 | }, 175 | "outputs": [ 176 | { 177 | "data": { 178 | "text/plain": [ 179 | "[11, 13, 15, 17, 19]" 180 | ] 181 | }, 182 | "execution_count": 11, 183 | "metadata": {}, 184 | "output_type": "execute_result" 185 | } 186 | ], 187 | "source": [ 188 | "map(lambda x,y,z:x+y+z, a,b,c)" 189 | ] 190 | }, 191 | { 192 | "cell_type": "code", 193 | "execution_count": null, 194 | "metadata": { 195 | "collapsed": true 196 | }, 197 | "outputs": [], 198 | "source": [] 199 | } 200 | ], 201 | "metadata": { 202 | "kernelspec": { 203 | "display_name": "Python 2", 204 | "language": "python", 205 | "name": "python2" 206 | }, 207 | "language_info": { 208 | "codemirror_mode": { 209 | "name": "ipython", 210 | "version": 2 211 | }, 212 | "file_extension": ".py", 213 | "mimetype": "text/x-python", 214 | "name": "python", 215 | "nbconvert_exporter": "python", 216 | "pygments_lexer": "ipython2", 217 | "version": "2.7.11" 218 | } 219 | }, 220 | "nbformat": 4, 221 | "nbformat_minor": 0 222 | } 223 | -------------------------------------------------------------------------------- /Builtin Functions/Reduce.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# reduce()\n", 8 | "\n", 9 | "Many times students have difficulty understanding reduce() so pay careful attention to this lecture. The function reduce(function, sequence) continually applies the function to the sequence. It then returns a single value.\n", 10 | "\n", 11 | "If seq = [ s1, s2, s3, ... , sn ], calling reduce(function, sequence) works like this:\n", 12 | "\n", 13 | "At first the first two elements of seq will be applied to function, i.e. func(s1,s2)\n", 14 | "\n", 15 | "The list on which reduce() works looks now like this: [ function(s1, s2), s3, ... , sn ]\n", 16 | "\n", 17 | "In the next step the function will be applied on the previous result and the third element of the list, i.e. function(function(s1, s2),s3)\n", 18 | "\n", 19 | "The list looks like this now: [ function(function(s1, s2),s3), ... , sn ]\n", 20 | "\n", 21 | "It continues like this until just one element is left and return this element as the result of reduce()\n" 22 | ] 23 | }, 24 | { 25 | "cell_type": "code", 26 | "execution_count": 1, 27 | "metadata": { 28 | "collapsed": false 29 | }, 30 | "outputs": [ 31 | { 32 | "data": { 33 | "text/plain": [ 34 | "220" 35 | ] 36 | }, 37 | "execution_count": 1, 38 | "metadata": {}, 39 | "output_type": "execute_result" 40 | } 41 | ], 42 | "source": [ 43 | "l = [40,50,60,70]\n", 44 | "\n", 45 | "reduce(lambda x,y:x+y, l)" 46 | ] 47 | }, 48 | { 49 | "cell_type": "code", 50 | "execution_count": 5, 51 | "metadata": { 52 | "collapsed": false 53 | }, 54 | "outputs": [ 55 | { 56 | "ename": "AttributeError", 57 | "evalue": "'int' object has no attribute 'join'", 58 | "output_type": "error", 59 | "traceback": [ 60 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 61 | "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", 62 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mL\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;36m4\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m5\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m6\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m7\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mreduce\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;32mlambda\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0my\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mjoin\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0my\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mL\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 63 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m(x, y)\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mL\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;36m4\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m5\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m6\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m7\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mreduce\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;32mlambda\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0my\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mjoin\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0my\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mL\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 64 | "\u001b[0;31mAttributeError\u001b[0m: 'int' object has no attribute 'join'" 65 | ] 66 | } 67 | ], 68 | "source": [ 69 | "L = [4,5,6,7]\n", 70 | "\n", 71 | "reduce(lambda x,y:x., L)" 72 | ] 73 | }, 74 | { 75 | "cell_type": "code", 76 | "execution_count": null, 77 | "metadata": { 78 | "collapsed": true 79 | }, 80 | "outputs": [], 81 | "source": [] 82 | } 83 | ], 84 | "metadata": { 85 | "kernelspec": { 86 | "display_name": "Python 2", 87 | "language": "python", 88 | "name": "python2" 89 | }, 90 | "language_info": { 91 | "codemirror_mode": { 92 | "name": "ipython", 93 | "version": 2 94 | }, 95 | "file_extension": ".py", 96 | "mimetype": "text/x-python", 97 | "name": "python", 98 | "nbconvert_exporter": "python", 99 | "pygments_lexer": "ipython2", 100 | "version": "2.7.11" 101 | } 102 | }, 103 | "nbformat": 4, 104 | "nbformat_minor": 0 105 | } 106 | -------------------------------------------------------------------------------- /Builtin Functions/Zip.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# zip\n", 8 | "\n", 9 | "zip() makes an iterator that aggregates elements from each of the iterables.\n", 10 | "\n", 11 | "Returns an iterator of tuples, where the i-th tuple contains the i-th element from each of the argument sequences or iterables. The iterator stops when the shortest input iterable is exhausted. \n", 12 | "\n", 13 | "With a single iterable argument, it returns an iterator of 1-tuples. With no arguments, it returns an empty iterator.\n", 14 | "\n", 15 | "zip() is equivalent to:\n", 16 | "\n", 17 | "def zip(*iterables):\n", 18 | " # zip('ABCD', 'xy') --> Ax By\n", 19 | " sentinel = object()\n", 20 | " iterators = [iter(it) for it in iterables]\n", 21 | " while iterators:\n", 22 | " result = []\n", 23 | " for it in iterators:\n", 24 | " elem = next(it, sentinel)\n", 25 | " if elem is sentinel:\n", 26 | " return\n", 27 | " result.append(elem)\n", 28 | " yield tuple(result)\n", 29 | "\n", 30 | "\n", 31 | "zip() should only be used with unequal length inputs when you don’t care about trailing, unmatched values from the longer iterables.\n" 32 | ] 33 | }, 34 | { 35 | "cell_type": "code", 36 | "execution_count": 1, 37 | "metadata": { 38 | "collapsed": false 39 | }, 40 | "outputs": [ 41 | { 42 | "data": { 43 | "text/plain": [ 44 | "[(1, 4), (2, 5), (3, 6)]" 45 | ] 46 | }, 47 | "execution_count": 1, 48 | "metadata": {}, 49 | "output_type": "execute_result" 50 | } 51 | ], 52 | "source": [ 53 | "x = [1,2,3]\n", 54 | "y = [4,5,6]\n", 55 | "\n", 56 | "zip(x,y)" 57 | ] 58 | }, 59 | { 60 | "cell_type": "code", 61 | "execution_count": 2, 62 | "metadata": { 63 | "collapsed": false 64 | }, 65 | "outputs": [ 66 | { 67 | "data": { 68 | "text/plain": [ 69 | "[(1, 4), (2, 5), (3, 6)]" 70 | ] 71 | }, 72 | "execution_count": 2, 73 | "metadata": {}, 74 | "output_type": "execute_result" 75 | } 76 | ], 77 | "source": [ 78 | "a = [1,2,3]\n", 79 | "b = [4,5,6,7,8]\n", 80 | "\n", 81 | "zip(a,b)" 82 | ] 83 | }, 84 | { 85 | "cell_type": "code", 86 | "execution_count": 3, 87 | "metadata": { 88 | "collapsed": false 89 | }, 90 | "outputs": [ 91 | { 92 | "data": { 93 | "text/plain": [ 94 | "[('a', 'c'), ('b', 'd')]" 95 | ] 96 | }, 97 | "execution_count": 3, 98 | "metadata": {}, 99 | "output_type": "execute_result" 100 | } 101 | ], 102 | "source": [ 103 | "d1 = {'a':1, 'b':2}\n", 104 | "d2 = {'c':3, 'd':4}\n", 105 | "\n", 106 | "zip(d1,d2)" 107 | ] 108 | }, 109 | { 110 | "cell_type": "code", 111 | "execution_count": 10, 112 | "metadata": { 113 | "collapsed": false 114 | }, 115 | "outputs": [ 116 | { 117 | "data": { 118 | "text/plain": [ 119 | "[('c', 1), ('d', 2)]" 120 | ] 121 | }, 122 | "execution_count": 10, 123 | "metadata": {}, 124 | "output_type": "execute_result" 125 | } 126 | ], 127 | "source": [ 128 | "zip(d2,d1.itervalues())" 129 | ] 130 | }, 131 | { 132 | "cell_type": "code", 133 | "execution_count": 11, 134 | "metadata": { 135 | "collapsed": false 136 | }, 137 | "outputs": [ 138 | { 139 | "data": { 140 | "text/plain": [ 141 | "[('a', 3), ('b', 4)]" 142 | ] 143 | }, 144 | "execution_count": 11, 145 | "metadata": {}, 146 | "output_type": "execute_result" 147 | } 148 | ], 149 | "source": [ 150 | "zip(d1,d2.itervalues())" 151 | ] 152 | }, 153 | { 154 | "cell_type": "code", 155 | "execution_count": null, 156 | "metadata": { 157 | "collapsed": true 158 | }, 159 | "outputs": [], 160 | "source": [] 161 | } 162 | ], 163 | "metadata": { 164 | "kernelspec": { 165 | "display_name": "Python 2", 166 | "language": "python", 167 | "name": "python2" 168 | }, 169 | "language_info": { 170 | "codemirror_mode": { 171 | "name": "ipython", 172 | "version": 2 173 | }, 174 | "file_extension": ".py", 175 | "mimetype": "text/x-python", 176 | "name": "python", 177 | "nbconvert_exporter": "python", 178 | "pygments_lexer": "ipython2", 179 | "version": "2.7.11" 180 | } 181 | }, 182 | "nbformat": 4, 183 | "nbformat_minor": 0 184 | } 185 | -------------------------------------------------------------------------------- /Comparision_Operators/.ipynb_checkpoints/Chained Comparison Operators-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Chained Comparison Operators\n", 8 | "An interesting feature of Python is the ability to chain multiple comparisons to perform a more complex test. You can use these chained comparisons as a shorthand for larger Boolean Expressions.\n" 9 | ] 10 | }, 11 | { 12 | "cell_type": "code", 13 | "execution_count": 1, 14 | "metadata": { 15 | "collapsed": false 16 | }, 17 | "outputs": [ 18 | { 19 | "data": { 20 | "text/plain": [ 21 | "True" 22 | ] 23 | }, 24 | "execution_count": 1, 25 | "metadata": {}, 26 | "output_type": "execute_result" 27 | } 28 | ], 29 | "source": [ 30 | "1 < 2 < 3" 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": 2, 36 | "metadata": { 37 | "collapsed": false 38 | }, 39 | "outputs": [ 40 | { 41 | "data": { 42 | "text/plain": [ 43 | "True" 44 | ] 45 | }, 46 | "execution_count": 2, 47 | "metadata": {}, 48 | "output_type": "execute_result" 49 | } 50 | ], 51 | "source": [ 52 | "1 < 2 and 2 < 3" 53 | ] 54 | }, 55 | { 56 | "cell_type": "code", 57 | "execution_count": 3, 58 | "metadata": { 59 | "collapsed": false 60 | }, 61 | "outputs": [ 62 | { 63 | "data": { 64 | "text/plain": [ 65 | "True" 66 | ] 67 | }, 68 | "execution_count": 3, 69 | "metadata": {}, 70 | "output_type": "execute_result" 71 | } 72 | ], 73 | "source": [ 74 | "1 < 2 or 1 > 2" 75 | ] 76 | }, 77 | { 78 | "cell_type": "code", 79 | "execution_count": 4, 80 | "metadata": { 81 | "collapsed": false 82 | }, 83 | "outputs": [ 84 | { 85 | "data": { 86 | "text/plain": [ 87 | "True" 88 | ] 89 | }, 90 | "execution_count": 4, 91 | "metadata": {}, 92 | "output_type": "execute_result" 93 | } 94 | ], 95 | "source": [ 96 | "1 < 3 > 2" 97 | ] 98 | }, 99 | { 100 | "cell_type": "code", 101 | "execution_count": 5, 102 | "metadata": { 103 | "collapsed": false 104 | }, 105 | "outputs": [ 106 | { 107 | "data": { 108 | "text/plain": [ 109 | "False" 110 | ] 111 | }, 112 | "execution_count": 5, 113 | "metadata": {}, 114 | "output_type": "execute_result" 115 | } 116 | ], 117 | "source": [ 118 | "1 == 100 and 99 == 99" 119 | ] 120 | }, 121 | { 122 | "cell_type": "code", 123 | "execution_count": 6, 124 | "metadata": { 125 | "collapsed": false 126 | }, 127 | "outputs": [ 128 | { 129 | "data": { 130 | "text/plain": [ 131 | "True" 132 | ] 133 | }, 134 | "execution_count": 6, 135 | "metadata": {}, 136 | "output_type": "execute_result" 137 | } 138 | ], 139 | "source": [ 140 | "1 == 100 or 101 == 101" 141 | ] 142 | }, 143 | { 144 | "cell_type": "code", 145 | "execution_count": null, 146 | "metadata": { 147 | "collapsed": true 148 | }, 149 | "outputs": [], 150 | "source": [] 151 | } 152 | ], 153 | "metadata": { 154 | "kernelspec": { 155 | "display_name": "Python 2", 156 | "language": "python", 157 | "name": "python2" 158 | }, 159 | "language_info": { 160 | "codemirror_mode": { 161 | "name": "ipython", 162 | "version": 2 163 | }, 164 | "file_extension": ".py", 165 | "mimetype": "text/x-python", 166 | "name": "python", 167 | "nbconvert_exporter": "python", 168 | "pygments_lexer": "ipython2", 169 | "version": "2.7.11" 170 | } 171 | }, 172 | "nbformat": 4, 173 | "nbformat_minor": 0 174 | } 175 | -------------------------------------------------------------------------------- /Comparision_Operators/.ipynb_checkpoints/Comparison Operators-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": { 7 | "collapsed": false 8 | }, 9 | "outputs": [ 10 | { 11 | "data": { 12 | "text/plain": [ 13 | "False" 14 | ] 15 | }, 16 | "execution_count": 1, 17 | "metadata": {}, 18 | "output_type": "execute_result" 19 | } 20 | ], 21 | "source": [ 22 | "2 > 3" 23 | ] 24 | }, 25 | { 26 | "cell_type": "code", 27 | "execution_count": 2, 28 | "metadata": { 29 | "collapsed": false 30 | }, 31 | "outputs": [ 32 | { 33 | "data": { 34 | "text/plain": [ 35 | "True" 36 | ] 37 | }, 38 | "execution_count": 2, 39 | "metadata": {}, 40 | "output_type": "execute_result" 41 | } 42 | ], 43 | "source": [ 44 | "4 > 3" 45 | ] 46 | }, 47 | { 48 | "cell_type": "code", 49 | "execution_count": 3, 50 | "metadata": { 51 | "collapsed": false 52 | }, 53 | "outputs": [ 54 | { 55 | "data": { 56 | "text/plain": [ 57 | "True" 58 | ] 59 | }, 60 | "execution_count": 3, 61 | "metadata": {}, 62 | "output_type": "execute_result" 63 | } 64 | ], 65 | "source": [ 66 | "2 >= 2" 67 | ] 68 | }, 69 | { 70 | "cell_type": "code", 71 | "execution_count": 4, 72 | "metadata": { 73 | "collapsed": false 74 | }, 75 | "outputs": [ 76 | { 77 | "data": { 78 | "text/plain": [ 79 | "False" 80 | ] 81 | }, 82 | "execution_count": 4, 83 | "metadata": {}, 84 | "output_type": "execute_result" 85 | } 86 | ], 87 | "source": [ 88 | "1 < 0" 89 | ] 90 | }, 91 | { 92 | "cell_type": "code", 93 | "execution_count": 5, 94 | "metadata": { 95 | "collapsed": false 96 | }, 97 | "outputs": [ 98 | { 99 | "data": { 100 | "text/plain": [ 101 | "True" 102 | ] 103 | }, 104 | "execution_count": 5, 105 | "metadata": {}, 106 | "output_type": "execute_result" 107 | } 108 | ], 109 | "source": [ 110 | "2 < 3" 111 | ] 112 | }, 113 | { 114 | "cell_type": "code", 115 | "execution_count": 6, 116 | "metadata": { 117 | "collapsed": false 118 | }, 119 | "outputs": [ 120 | { 121 | "data": { 122 | "text/plain": [ 123 | "True" 124 | ] 125 | }, 126 | "execution_count": 6, 127 | "metadata": {}, 128 | "output_type": "execute_result" 129 | } 130 | ], 131 | "source": [ 132 | "2 <= 2" 133 | ] 134 | }, 135 | { 136 | "cell_type": "code", 137 | "execution_count": 7, 138 | "metadata": { 139 | "collapsed": false 140 | }, 141 | "outputs": [ 142 | { 143 | "data": { 144 | "text/plain": [ 145 | "True" 146 | ] 147 | }, 148 | "execution_count": 7, 149 | "metadata": {}, 150 | "output_type": "execute_result" 151 | } 152 | ], 153 | "source": [ 154 | "3 <> 2" 155 | ] 156 | }, 157 | { 158 | "cell_type": "code", 159 | "execution_count": 8, 160 | "metadata": { 161 | "collapsed": false 162 | }, 163 | "outputs": [ 164 | { 165 | "data": { 166 | "text/plain": [ 167 | "True" 168 | ] 169 | }, 170 | "execution_count": 8, 171 | "metadata": {}, 172 | "output_type": "execute_result" 173 | } 174 | ], 175 | "source": [ 176 | "2 != 3" 177 | ] 178 | }, 179 | { 180 | "cell_type": "code", 181 | "execution_count": 9, 182 | "metadata": { 183 | "collapsed": false 184 | }, 185 | "outputs": [ 186 | { 187 | "data": { 188 | "text/plain": [ 189 | "False" 190 | ] 191 | }, 192 | "execution_count": 9, 193 | "metadata": {}, 194 | "output_type": "execute_result" 195 | } 196 | ], 197 | "source": [ 198 | "3 == 2" 199 | ] 200 | }, 201 | { 202 | "cell_type": "code", 203 | "execution_count": 10, 204 | "metadata": { 205 | "collapsed": false 206 | }, 207 | "outputs": [ 208 | { 209 | "data": { 210 | "text/plain": [ 211 | "True" 212 | ] 213 | }, 214 | "execution_count": 10, 215 | "metadata": {}, 216 | "output_type": "execute_result" 217 | } 218 | ], 219 | "source": [ 220 | "3 == 3" 221 | ] 222 | }, 223 | { 224 | "cell_type": "code", 225 | "execution_count": null, 226 | "metadata": { 227 | "collapsed": true 228 | }, 229 | "outputs": [], 230 | "source": [] 231 | } 232 | ], 233 | "metadata": { 234 | "kernelspec": { 235 | "display_name": "Python 2", 236 | "language": "python", 237 | "name": "python2" 238 | }, 239 | "language_info": { 240 | "codemirror_mode": { 241 | "name": "ipython", 242 | "version": 2 243 | }, 244 | "file_extension": ".py", 245 | "mimetype": "text/x-python", 246 | "name": "python", 247 | "nbconvert_exporter": "python", 248 | "pygments_lexer": "ipython2", 249 | "version": "2.7.11" 250 | } 251 | }, 252 | "nbformat": 4, 253 | "nbformat_minor": 0 254 | } 255 | -------------------------------------------------------------------------------- /Comparision_Operators/Chained Comparison Operators.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Chained Comparison Operators\n", 8 | "An interesting feature of Python is the ability to chain multiple comparisons to perform a more complex test. You can use these chained comparisons as a shorthand for larger Boolean Expressions.\n" 9 | ] 10 | }, 11 | { 12 | "cell_type": "code", 13 | "execution_count": 1, 14 | "metadata": { 15 | "collapsed": false 16 | }, 17 | "outputs": [ 18 | { 19 | "data": { 20 | "text/plain": [ 21 | "True" 22 | ] 23 | }, 24 | "execution_count": 1, 25 | "metadata": {}, 26 | "output_type": "execute_result" 27 | } 28 | ], 29 | "source": [ 30 | "1 < 2 < 3" 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": 2, 36 | "metadata": { 37 | "collapsed": false 38 | }, 39 | "outputs": [ 40 | { 41 | "data": { 42 | "text/plain": [ 43 | "True" 44 | ] 45 | }, 46 | "execution_count": 2, 47 | "metadata": {}, 48 | "output_type": "execute_result" 49 | } 50 | ], 51 | "source": [ 52 | "1 < 2 and 2 < 3" 53 | ] 54 | }, 55 | { 56 | "cell_type": "code", 57 | "execution_count": 3, 58 | "metadata": { 59 | "collapsed": false 60 | }, 61 | "outputs": [ 62 | { 63 | "data": { 64 | "text/plain": [ 65 | "True" 66 | ] 67 | }, 68 | "execution_count": 3, 69 | "metadata": {}, 70 | "output_type": "execute_result" 71 | } 72 | ], 73 | "source": [ 74 | "1 < 2 or 1 > 2" 75 | ] 76 | }, 77 | { 78 | "cell_type": "code", 79 | "execution_count": 4, 80 | "metadata": { 81 | "collapsed": false 82 | }, 83 | "outputs": [ 84 | { 85 | "data": { 86 | "text/plain": [ 87 | "True" 88 | ] 89 | }, 90 | "execution_count": 4, 91 | "metadata": {}, 92 | "output_type": "execute_result" 93 | } 94 | ], 95 | "source": [ 96 | "1 < 3 > 2" 97 | ] 98 | }, 99 | { 100 | "cell_type": "code", 101 | "execution_count": 5, 102 | "metadata": { 103 | "collapsed": false 104 | }, 105 | "outputs": [ 106 | { 107 | "data": { 108 | "text/plain": [ 109 | "False" 110 | ] 111 | }, 112 | "execution_count": 5, 113 | "metadata": {}, 114 | "output_type": "execute_result" 115 | } 116 | ], 117 | "source": [ 118 | "1 == 100 and 99 == 99" 119 | ] 120 | }, 121 | { 122 | "cell_type": "code", 123 | "execution_count": 6, 124 | "metadata": { 125 | "collapsed": false 126 | }, 127 | "outputs": [ 128 | { 129 | "data": { 130 | "text/plain": [ 131 | "True" 132 | ] 133 | }, 134 | "execution_count": 6, 135 | "metadata": {}, 136 | "output_type": "execute_result" 137 | } 138 | ], 139 | "source": [ 140 | "1 == 100 or 101 == 101" 141 | ] 142 | }, 143 | { 144 | "cell_type": "code", 145 | "execution_count": null, 146 | "metadata": { 147 | "collapsed": true 148 | }, 149 | "outputs": [], 150 | "source": [] 151 | } 152 | ], 153 | "metadata": { 154 | "kernelspec": { 155 | "display_name": "Python 2", 156 | "language": "python", 157 | "name": "python2" 158 | }, 159 | "language_info": { 160 | "codemirror_mode": { 161 | "name": "ipython", 162 | "version": 2 163 | }, 164 | "file_extension": ".py", 165 | "mimetype": "text/x-python", 166 | "name": "python", 167 | "nbconvert_exporter": "python", 168 | "pygments_lexer": "ipython2", 169 | "version": "2.7.11" 170 | } 171 | }, 172 | "nbformat": 4, 173 | "nbformat_minor": 0 174 | } 175 | -------------------------------------------------------------------------------- /Comparision_Operators/Comparison Operators.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": { 7 | "collapsed": false 8 | }, 9 | "outputs": [ 10 | { 11 | "data": { 12 | "text/plain": [ 13 | "False" 14 | ] 15 | }, 16 | "execution_count": 1, 17 | "metadata": {}, 18 | "output_type": "execute_result" 19 | } 20 | ], 21 | "source": [ 22 | "2 > 3" 23 | ] 24 | }, 25 | { 26 | "cell_type": "code", 27 | "execution_count": 2, 28 | "metadata": { 29 | "collapsed": false 30 | }, 31 | "outputs": [ 32 | { 33 | "data": { 34 | "text/plain": [ 35 | "True" 36 | ] 37 | }, 38 | "execution_count": 2, 39 | "metadata": {}, 40 | "output_type": "execute_result" 41 | } 42 | ], 43 | "source": [ 44 | "4 > 3" 45 | ] 46 | }, 47 | { 48 | "cell_type": "code", 49 | "execution_count": 3, 50 | "metadata": { 51 | "collapsed": false 52 | }, 53 | "outputs": [ 54 | { 55 | "data": { 56 | "text/plain": [ 57 | "True" 58 | ] 59 | }, 60 | "execution_count": 3, 61 | "metadata": {}, 62 | "output_type": "execute_result" 63 | } 64 | ], 65 | "source": [ 66 | "2 >= 2" 67 | ] 68 | }, 69 | { 70 | "cell_type": "code", 71 | "execution_count": 4, 72 | "metadata": { 73 | "collapsed": false 74 | }, 75 | "outputs": [ 76 | { 77 | "data": { 78 | "text/plain": [ 79 | "False" 80 | ] 81 | }, 82 | "execution_count": 4, 83 | "metadata": {}, 84 | "output_type": "execute_result" 85 | } 86 | ], 87 | "source": [ 88 | "1 < 0" 89 | ] 90 | }, 91 | { 92 | "cell_type": "code", 93 | "execution_count": 5, 94 | "metadata": { 95 | "collapsed": false 96 | }, 97 | "outputs": [ 98 | { 99 | "data": { 100 | "text/plain": [ 101 | "True" 102 | ] 103 | }, 104 | "execution_count": 5, 105 | "metadata": {}, 106 | "output_type": "execute_result" 107 | } 108 | ], 109 | "source": [ 110 | "2 < 3" 111 | ] 112 | }, 113 | { 114 | "cell_type": "code", 115 | "execution_count": 6, 116 | "metadata": { 117 | "collapsed": false 118 | }, 119 | "outputs": [ 120 | { 121 | "data": { 122 | "text/plain": [ 123 | "True" 124 | ] 125 | }, 126 | "execution_count": 6, 127 | "metadata": {}, 128 | "output_type": "execute_result" 129 | } 130 | ], 131 | "source": [ 132 | "2 <= 2" 133 | ] 134 | }, 135 | { 136 | "cell_type": "code", 137 | "execution_count": 7, 138 | "metadata": { 139 | "collapsed": false 140 | }, 141 | "outputs": [ 142 | { 143 | "data": { 144 | "text/plain": [ 145 | "True" 146 | ] 147 | }, 148 | "execution_count": 7, 149 | "metadata": {}, 150 | "output_type": "execute_result" 151 | } 152 | ], 153 | "source": [ 154 | "3 <> 2" 155 | ] 156 | }, 157 | { 158 | "cell_type": "code", 159 | "execution_count": 8, 160 | "metadata": { 161 | "collapsed": false 162 | }, 163 | "outputs": [ 164 | { 165 | "data": { 166 | "text/plain": [ 167 | "True" 168 | ] 169 | }, 170 | "execution_count": 8, 171 | "metadata": {}, 172 | "output_type": "execute_result" 173 | } 174 | ], 175 | "source": [ 176 | "2 != 3" 177 | ] 178 | }, 179 | { 180 | "cell_type": "code", 181 | "execution_count": 9, 182 | "metadata": { 183 | "collapsed": false 184 | }, 185 | "outputs": [ 186 | { 187 | "data": { 188 | "text/plain": [ 189 | "False" 190 | ] 191 | }, 192 | "execution_count": 9, 193 | "metadata": {}, 194 | "output_type": "execute_result" 195 | } 196 | ], 197 | "source": [ 198 | "3 == 2" 199 | ] 200 | }, 201 | { 202 | "cell_type": "code", 203 | "execution_count": 10, 204 | "metadata": { 205 | "collapsed": false 206 | }, 207 | "outputs": [ 208 | { 209 | "data": { 210 | "text/plain": [ 211 | "True" 212 | ] 213 | }, 214 | "execution_count": 10, 215 | "metadata": {}, 216 | "output_type": "execute_result" 217 | } 218 | ], 219 | "source": [ 220 | "3 == 3" 221 | ] 222 | }, 223 | { 224 | "cell_type": "code", 225 | "execution_count": null, 226 | "metadata": { 227 | "collapsed": true 228 | }, 229 | "outputs": [], 230 | "source": [] 231 | } 232 | ], 233 | "metadata": { 234 | "kernelspec": { 235 | "display_name": "Python 2", 236 | "language": "python", 237 | "name": "python2" 238 | }, 239 | "language_info": { 240 | "codemirror_mode": { 241 | "name": "ipython", 242 | "version": 2 243 | }, 244 | "file_extension": ".py", 245 | "mimetype": "text/x-python", 246 | "name": "python", 247 | "nbconvert_exporter": "python", 248 | "pygments_lexer": "ipython2", 249 | "version": "2.7.11" 250 | } 251 | }, 252 | "nbformat": 4, 253 | "nbformat_minor": 0 254 | } 255 | -------------------------------------------------------------------------------- /Decorators/Decorators Homework.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "#Decorators Homework (Optional)\n", 8 | "\n", 9 | "Since you won't run into decorators until further in your coding career, this homework is optional. Check out the Web Framework [Flask](http://flask.pocoo.org/). You can use Flask to create web pages with Python (as long as you know some HTML and CSS) and they use decorators a lot! Learn how they use [view decorators](http://flask.pocoo.org/docs/0.10/patterns/viewdecorators/). Don't worry if you don't completely understand everything about Flask, the main point of this optional homework is that you have an awareness of decorators in Web Frameworks, that way if you decide to become a \"Full-Stack\" Python Web Developer, you won't find yourself perplexed by decorators. You can also check out [Django](https://www.djangoproject.com/) another (and more popular) web framework for Python which is a bit more heavy duty.\n", 10 | "\n", 11 | "Also for some additional info:\n", 12 | "\n", 13 | "A framework is a type of software library that provides generic functionality which can be extended by the programmer to build applications. Flask and Django are good examples of frameworks intended for web development.\n", 14 | "\n", 15 | "A framework is distinguished from a simple library or API. An API is a piece of software that a developer can use in his application. A framework is more encompassing: your entire application is structured around the framework (i.e. it provides the framework around which you build your software).\n", 16 | "\n", 17 | "##Great job!" 18 | ] 19 | } 20 | ], 21 | "metadata": { 22 | "kernelspec": { 23 | "display_name": "Python 2", 24 | "language": "python", 25 | "name": "python2" 26 | }, 27 | "language_info": { 28 | "codemirror_mode": { 29 | "name": "ipython", 30 | "version": 2 31 | }, 32 | "file_extension": ".py", 33 | "mimetype": "text/x-python", 34 | "name": "python", 35 | "nbconvert_exporter": "python", 36 | "pygments_lexer": "ipython2", 37 | "version": "2.7.10" 38 | } 39 | }, 40 | "nbformat": 4, 41 | "nbformat_minor": 0 42 | } 43 | -------------------------------------------------------------------------------- /Errors and Exceptional Handling/.ipynb_checkpoints/Errors and Exceptions Homework-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "#Errors and Exceptions Homework - " 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "###Problem 1\n", 15 | "Handle the exception thrown by the code below by using try and except blocks." 16 | ] 17 | }, 18 | { 19 | "cell_type": "code", 20 | "execution_count": 1, 21 | "metadata": { 22 | "collapsed": false 23 | }, 24 | "outputs": [ 25 | { 26 | "name": "stdout", 27 | "output_type": "stream", 28 | "text": [ 29 | "Loop through a valid integer\n", 30 | "a\n", 31 | "Loop through a valid integer\n", 32 | "b\n", 33 | "Loop through a valid integer\n", 34 | "c\n" 35 | ] 36 | } 37 | ], 38 | "source": [ 39 | "for i in ['a','b','c']:\n", 40 | " try:\n", 41 | " print i**2\n", 42 | " except:\n", 43 | " print 'Loop through a valid integer'\n", 44 | " print i" 45 | ] 46 | }, 47 | { 48 | "cell_type": "markdown", 49 | "metadata": {}, 50 | "source": [ 51 | "###Problem 2\n", 52 | "Handle the exception thrown by the code below by using **try** and **except** blocks. Then use a **finally** block to print 'All Done.'" 53 | ] 54 | }, 55 | { 56 | "cell_type": "code", 57 | "execution_count": 2, 58 | "metadata": { 59 | "collapsed": false 60 | }, 61 | "outputs": [ 62 | { 63 | "name": "stdout", 64 | "output_type": "stream", 65 | "text": [ 66 | "Division not possible\n", 67 | "All Done\n" 68 | ] 69 | } 70 | ], 71 | "source": [ 72 | "x = 5\n", 73 | "y = 0\n", 74 | "\n", 75 | "try:\n", 76 | " z = x/y\n", 77 | "except:\n", 78 | " print 'Division not possible'\n", 79 | "finally:\n", 80 | " print 'All Done'" 81 | ] 82 | }, 83 | { 84 | "cell_type": "markdown", 85 | "metadata": {}, 86 | "source": [ 87 | "###Problem 3\n", 88 | "Write a function that asks for an integer and prints the square of it. Use a while loop with a try,except, else block to account for incorrect inputs." 89 | ] 90 | }, 91 | { 92 | "cell_type": "code", 93 | "execution_count": null, 94 | "metadata": { 95 | "collapsed": false 96 | }, 97 | "outputs": [], 98 | "source": [ 99 | "def ask():\n", 100 | " \n", 101 | " while True:\n", 102 | " try:\n", 103 | " i = int(raw_input('Enter an integer: '))\n", 104 | " except:\n", 105 | " print 'Enter an integer'\n", 106 | " i = int(raw_input('Enter an integer'))\n", 107 | " return i**2\n", 108 | " continue\n", 109 | " else:\n", 110 | " print 'This is an integer'\n", 111 | " break\n", 112 | " \n", 113 | " \n", 114 | " print 'The square of given input is ', i**2" 115 | ] 116 | }, 117 | { 118 | "cell_type": "code", 119 | "execution_count": null, 120 | "metadata": { 121 | "collapsed": false 122 | }, 123 | "outputs": [], 124 | "source": [ 125 | "ask()" 126 | ] 127 | }, 128 | { 129 | "cell_type": "markdown", 130 | "metadata": {}, 131 | "source": [ 132 | "#Great Job!" 133 | ] 134 | } 135 | ], 136 | "metadata": { 137 | "kernelspec": { 138 | "display_name": "Python 2", 139 | "language": "python", 140 | "name": "python2" 141 | }, 142 | "language_info": { 143 | "codemirror_mode": { 144 | "name": "ipython", 145 | "version": 2 146 | }, 147 | "file_extension": ".py", 148 | "mimetype": "text/x-python", 149 | "name": "python", 150 | "nbconvert_exporter": "python", 151 | "pygments_lexer": "ipython2", 152 | "version": "2.7.11" 153 | } 154 | }, 155 | "nbformat": 4, 156 | "nbformat_minor": 0 157 | } 158 | -------------------------------------------------------------------------------- /Errors and Exceptional Handling/.ipynb_checkpoints/Erros and Exceptional Handling-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "Errors detected during execution are called exceptions and are not unconditionally fatal." 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 3, 13 | "metadata": { 14 | "collapsed": false 15 | }, 16 | "outputs": [ 17 | { 18 | "name": "stdout", 19 | "output_type": "stream", 20 | "text": [ 21 | "Content written successfully\n" 22 | ] 23 | } 24 | ], 25 | "source": [ 26 | "#this file got created due to 'w' tag i.e, write permissions\n", 27 | "try:\n", 28 | " t = open('file1.txt', 'w')\n", 29 | " t.write('Test write this line')\n", 30 | "except IOError:\n", 31 | " print ' Can not find specfied filename to read '\n", 32 | "else:\n", 33 | " print 'Content written successfully'\n", 34 | " t.close()" 35 | ] 36 | }, 37 | { 38 | "cell_type": "code", 39 | "execution_count": 4, 40 | "metadata": { 41 | "collapsed": false 42 | }, 43 | "outputs": [ 44 | { 45 | "name": "stdout", 46 | "output_type": "stream", 47 | "text": [ 48 | " Can not find specfied filename to read \n" 49 | ] 50 | } 51 | ], 52 | "source": [ 53 | "#this file did not create coz of 'r' tag i.e, read permissions\n", 54 | "try:\n", 55 | " c = open('file1.txt', 'r')\n", 56 | " c.write('Test write this line')\n", 57 | "except IOError: # This will only check for an IOError exception and then execute this print statement\n", 58 | " print ' Can not find specfied filename to read '\n", 59 | "else:\n", 60 | " print 'Content written successfully'\n", 61 | " c.close()" 62 | ] 63 | }, 64 | { 65 | "cell_type": "code", 66 | "execution_count": 5, 67 | "metadata": { 68 | "collapsed": false 69 | }, 70 | "outputs": [ 71 | { 72 | "name": "stdout", 73 | "output_type": "stream", 74 | "text": [ 75 | " Can not find specfied filename to read \n", 76 | "Finally blocks are always executed\n" 77 | ] 78 | } 79 | ], 80 | "source": [ 81 | "try:\n", 82 | " d = open('file1.txt', 'r')\n", 83 | " d.write('Test write this line')\n", 84 | "except: # This will check for any exception and then execute this print statement\n", 85 | " print ' Can not find specfied filename to read '\n", 86 | "else:\n", 87 | " print 'Content written successfully'\n", 88 | "finally:\n", 89 | " print 'Finally blocks are always executed'\n", 90 | " c.close()" 91 | ] 92 | }, 93 | { 94 | "cell_type": "code", 95 | "execution_count": null, 96 | "metadata": { 97 | "collapsed": false 98 | }, 99 | "outputs": [], 100 | "source": [ 101 | "def askint():\n", 102 | " \n", 103 | " while True:\n", 104 | " \n", 105 | " try:\n", 106 | " val = int(raw_input('Enter an Integer'))\n", 107 | " except:\n", 108 | " print 'It is not an integer '\n", 109 | " print 'Please enter an integer'\n", 110 | " val = int(raw_input('Enter an Integer'))\n", 111 | " continue\n", 112 | " else:\n", 113 | " print 'It is an integer'\n", 114 | " break\n", 115 | " finally:\n", 116 | " print 'This is finally block'\n", 117 | " \n", 118 | " print val\n", 119 | " \n", 120 | " " 121 | ] 122 | }, 123 | { 124 | "cell_type": "code", 125 | "execution_count": null, 126 | "metadata": { 127 | "collapsed": true 128 | }, 129 | "outputs": [], 130 | "source": [] 131 | }, 132 | { 133 | "cell_type": "code", 134 | "execution_count": null, 135 | "metadata": { 136 | "collapsed": true 137 | }, 138 | "outputs": [], 139 | "source": [] 140 | }, 141 | { 142 | "cell_type": "code", 143 | "execution_count": null, 144 | "metadata": { 145 | "collapsed": true 146 | }, 147 | "outputs": [], 148 | "source": [] 149 | } 150 | ], 151 | "metadata": { 152 | "kernelspec": { 153 | "display_name": "Python 2", 154 | "language": "python", 155 | "name": "python2" 156 | }, 157 | "language_info": { 158 | "codemirror_mode": { 159 | "name": "ipython", 160 | "version": 2 161 | }, 162 | "file_extension": ".py", 163 | "mimetype": "text/x-python", 164 | "name": "python", 165 | "nbconvert_exporter": "python", 166 | "pygments_lexer": "ipython2", 167 | "version": "2.7.11" 168 | } 169 | }, 170 | "nbformat": 4, 171 | "nbformat_minor": 0 172 | } 173 | -------------------------------------------------------------------------------- /Errors and Exceptional Handling/Errors and Exceptions Homework.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "#Errors and Exceptions Homework - " 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "###Problem 1\n", 15 | "Handle the exception thrown by the code below by using try and except blocks." 16 | ] 17 | }, 18 | { 19 | "cell_type": "code", 20 | "execution_count": 1, 21 | "metadata": { 22 | "collapsed": false 23 | }, 24 | "outputs": [ 25 | { 26 | "name": "stdout", 27 | "output_type": "stream", 28 | "text": [ 29 | "Loop through a valid integer\n", 30 | "a\n", 31 | "Loop through a valid integer\n", 32 | "b\n", 33 | "Loop through a valid integer\n", 34 | "c\n" 35 | ] 36 | } 37 | ], 38 | "source": [ 39 | "for i in ['a','b','c']:\n", 40 | " try:\n", 41 | " print i**2\n", 42 | " except:\n", 43 | " print 'Loop through a valid integer'\n", 44 | " print i" 45 | ] 46 | }, 47 | { 48 | "cell_type": "markdown", 49 | "metadata": {}, 50 | "source": [ 51 | "###Problem 2\n", 52 | "Handle the exception thrown by the code below by using **try** and **except** blocks. Then use a **finally** block to print 'All Done.'" 53 | ] 54 | }, 55 | { 56 | "cell_type": "code", 57 | "execution_count": 2, 58 | "metadata": { 59 | "collapsed": false 60 | }, 61 | "outputs": [ 62 | { 63 | "name": "stdout", 64 | "output_type": "stream", 65 | "text": [ 66 | "Division not possible\n", 67 | "All Done\n" 68 | ] 69 | } 70 | ], 71 | "source": [ 72 | "x = 5\n", 73 | "y = 0\n", 74 | "\n", 75 | "try:\n", 76 | " z = x/y\n", 77 | "except:\n", 78 | " print 'Division not possible'\n", 79 | "finally:\n", 80 | " print 'All Done'" 81 | ] 82 | }, 83 | { 84 | "cell_type": "markdown", 85 | "metadata": {}, 86 | "source": [ 87 | "###Problem 3\n", 88 | "Write a function that asks for an integer and prints the square of it. Use a while loop with a try,except, else block to account for incorrect inputs." 89 | ] 90 | }, 91 | { 92 | "cell_type": "code", 93 | "execution_count": null, 94 | "metadata": { 95 | "collapsed": false 96 | }, 97 | "outputs": [], 98 | "source": [ 99 | "def ask():\n", 100 | " \n", 101 | " while True:\n", 102 | " try:\n", 103 | " i = int(raw_input('Enter an integer: '))\n", 104 | " except:\n", 105 | " print 'Enter an integer'\n", 106 | " i = int(raw_input('Enter an integer'))\n", 107 | " return i**2\n", 108 | " continue\n", 109 | " else:\n", 110 | " print 'This is an integer'\n", 111 | " break\n", 112 | " \n", 113 | " \n", 114 | " print 'The square of given input is ', i**2" 115 | ] 116 | }, 117 | { 118 | "cell_type": "code", 119 | "execution_count": null, 120 | "metadata": { 121 | "collapsed": false 122 | }, 123 | "outputs": [], 124 | "source": [ 125 | "ask()" 126 | ] 127 | }, 128 | { 129 | "cell_type": "markdown", 130 | "metadata": {}, 131 | "source": [ 132 | "#Great Job!" 133 | ] 134 | } 135 | ], 136 | "metadata": { 137 | "kernelspec": { 138 | "display_name": "Python 2", 139 | "language": "python", 140 | "name": "python2" 141 | }, 142 | "language_info": { 143 | "codemirror_mode": { 144 | "name": "ipython", 145 | "version": 2 146 | }, 147 | "file_extension": ".py", 148 | "mimetype": "text/x-python", 149 | "name": "python", 150 | "nbconvert_exporter": "python", 151 | "pygments_lexer": "ipython2", 152 | "version": "2.7.11" 153 | } 154 | }, 155 | "nbformat": 4, 156 | "nbformat_minor": 0 157 | } 158 | -------------------------------------------------------------------------------- /Errors and Exceptional Handling/Erros and Exceptional Handling.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "Errors detected during execution are called exceptions and are not unconditionally fatal." 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 3, 13 | "metadata": { 14 | "collapsed": false 15 | }, 16 | "outputs": [ 17 | { 18 | "name": "stdout", 19 | "output_type": "stream", 20 | "text": [ 21 | "Content written successfully\n" 22 | ] 23 | } 24 | ], 25 | "source": [ 26 | "#this file got created due to 'w' tag i.e, write permissions\n", 27 | "try:\n", 28 | " t = open('file1.txt', 'w')\n", 29 | " t.write('Test write this line')\n", 30 | "except IOError:\n", 31 | " print ' Can not find specfied filename to read '\n", 32 | "else:\n", 33 | " print 'Content written successfully'\n", 34 | " t.close()" 35 | ] 36 | }, 37 | { 38 | "cell_type": "code", 39 | "execution_count": 4, 40 | "metadata": { 41 | "collapsed": false 42 | }, 43 | "outputs": [ 44 | { 45 | "name": "stdout", 46 | "output_type": "stream", 47 | "text": [ 48 | " Can not find specfied filename to read \n" 49 | ] 50 | } 51 | ], 52 | "source": [ 53 | "#this file did not create coz of 'r' tag i.e, read permissions\n", 54 | "try:\n", 55 | " c = open('file1.txt', 'r')\n", 56 | " c.write('Test write this line')\n", 57 | "except IOError: # This will only check for an IOError exception and then execute this print statement\n", 58 | " print ' Can not find specfied filename to read '\n", 59 | "else:\n", 60 | " print 'Content written successfully'\n", 61 | " c.close()" 62 | ] 63 | }, 64 | { 65 | "cell_type": "code", 66 | "execution_count": 5, 67 | "metadata": { 68 | "collapsed": false 69 | }, 70 | "outputs": [ 71 | { 72 | "name": "stdout", 73 | "output_type": "stream", 74 | "text": [ 75 | " Can not find specfied filename to read \n", 76 | "Finally blocks are always executed\n" 77 | ] 78 | } 79 | ], 80 | "source": [ 81 | "try:\n", 82 | " d = open('file1.txt', 'r')\n", 83 | " d.write('Test write this line')\n", 84 | "except: # This will check for any exception and then execute this print statement\n", 85 | " print ' Can not find specfied filename to read '\n", 86 | "else:\n", 87 | " print 'Content written successfully'\n", 88 | "finally:\n", 89 | " print 'Finally blocks are always executed'\n", 90 | " c.close()" 91 | ] 92 | }, 93 | { 94 | "cell_type": "code", 95 | "execution_count": null, 96 | "metadata": { 97 | "collapsed": false 98 | }, 99 | "outputs": [], 100 | "source": [ 101 | "def askint():\n", 102 | " \n", 103 | " while True:\n", 104 | " \n", 105 | " try:\n", 106 | " val = int(raw_input('Enter an Integer'))\n", 107 | " except:\n", 108 | " print 'It is not an integer '\n", 109 | " print 'Please enter an integer'\n", 110 | " val = int(raw_input('Enter an Integer'))\n", 111 | " continue\n", 112 | " else:\n", 113 | " print 'It is an integer'\n", 114 | " break\n", 115 | " finally:\n", 116 | " print 'This is finally block'\n", 117 | " \n", 118 | " print val\n", 119 | " \n", 120 | " " 121 | ] 122 | }, 123 | { 124 | "cell_type": "code", 125 | "execution_count": null, 126 | "metadata": { 127 | "collapsed": true 128 | }, 129 | "outputs": [], 130 | "source": [ 131 | "askint()" 132 | ] 133 | }, 134 | { 135 | "cell_type": "code", 136 | "execution_count": null, 137 | "metadata": { 138 | "collapsed": true 139 | }, 140 | "outputs": [], 141 | "source": [] 142 | }, 143 | { 144 | "cell_type": "code", 145 | "execution_count": null, 146 | "metadata": { 147 | "collapsed": true 148 | }, 149 | "outputs": [], 150 | "source": [] 151 | } 152 | ], 153 | "metadata": { 154 | "kernelspec": { 155 | "display_name": "Python 2", 156 | "language": "python", 157 | "name": "python2" 158 | }, 159 | "language_info": { 160 | "codemirror_mode": { 161 | "name": "ipython", 162 | "version": 2 163 | }, 164 | "file_extension": ".py", 165 | "mimetype": "text/x-python", 166 | "name": "python", 167 | "nbconvert_exporter": "python", 168 | "pygments_lexer": "ipython2", 169 | "version": "2.7.11" 170 | } 171 | }, 172 | "nbformat": 4, 173 | "nbformat_minor": 0 174 | } 175 | -------------------------------------------------------------------------------- /Errors and Exceptional Handling/file1.txt: -------------------------------------------------------------------------------- 1 | Test write this line -------------------------------------------------------------------------------- /Iterators and Generators/.ipynb_checkpoints/Iterators and Generators Homework-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "#Iterators and Generators Homework \n", 8 | "\n", 9 | "###Problem 1\n", 10 | "\n", 11 | "Create a generator that generates the squares of numbers up to some number N." 12 | ] 13 | }, 14 | { 15 | "cell_type": "code", 16 | "execution_count": 8, 17 | "metadata": { 18 | "collapsed": false 19 | }, 20 | "outputs": [], 21 | "source": [ 22 | "def gensquares(N):\n", 23 | " for i in range(N+1):\n", 24 | " yield i**2" 25 | ] 26 | }, 27 | { 28 | "cell_type": "code", 29 | "execution_count": 9, 30 | "metadata": { 31 | "collapsed": false 32 | }, 33 | "outputs": [ 34 | { 35 | "name": "stdout", 36 | "output_type": "stream", 37 | "text": [ 38 | "0\n", 39 | "1\n", 40 | "4\n", 41 | "9\n", 42 | "16\n", 43 | "25\n", 44 | "36\n", 45 | "49\n", 46 | "64\n", 47 | "81\n", 48 | "100\n" 49 | ] 50 | } 51 | ], 52 | "source": [ 53 | "for x in gensquares(10):\n", 54 | " print x" 55 | ] 56 | }, 57 | { 58 | "cell_type": "markdown", 59 | "metadata": {}, 60 | "source": [ 61 | "###Problem 2\n", 62 | "\n", 63 | "Create a generator that yields \"n\" random numbers between a low and high number (that are inputs). Note: Use the random library. For example:" 64 | ] 65 | }, 66 | { 67 | "cell_type": "code", 68 | "execution_count": 37, 69 | "metadata": { 70 | "collapsed": false 71 | }, 72 | "outputs": [ 73 | { 74 | "data": { 75 | "text/plain": [ 76 | "7" 77 | ] 78 | }, 79 | "execution_count": 37, 80 | "metadata": {}, 81 | "output_type": "execute_result" 82 | } 83 | ], 84 | "source": [ 85 | "import random\n", 86 | "\n", 87 | "random.randint(1,10)" 88 | ] 89 | }, 90 | { 91 | "cell_type": "code", 92 | "execution_count": 40, 93 | "metadata": { 94 | "collapsed": true 95 | }, 96 | "outputs": [], 97 | "source": [ 98 | "def rand_num(low,high,n):\n", 99 | " \n", 100 | " for i in range(n):\n", 101 | " yield random.randint(low, high)" 102 | ] 103 | }, 104 | { 105 | "cell_type": "code", 106 | "execution_count": 41, 107 | "metadata": { 108 | "collapsed": false 109 | }, 110 | "outputs": [ 111 | { 112 | "name": "stdout", 113 | "output_type": "stream", 114 | "text": [ 115 | "9\n", 116 | "1\n", 117 | "5\n", 118 | "3\n", 119 | "1\n", 120 | "2\n", 121 | "9\n", 122 | "8\n", 123 | "7\n", 124 | "9\n", 125 | "10\n", 126 | "8\n" 127 | ] 128 | } 129 | ], 130 | "source": [ 131 | "for num in rand_num(1,10,12):\n", 132 | " print num" 133 | ] 134 | }, 135 | { 136 | "cell_type": "markdown", 137 | "metadata": {}, 138 | "source": [ 139 | "###Problem 3\n", 140 | "\n", 141 | "Use the iter() function to convert the string below \n" 142 | ] 143 | }, 144 | { 145 | "cell_type": "code", 146 | "execution_count": 22, 147 | "metadata": { 148 | "collapsed": false 149 | }, 150 | "outputs": [ 151 | { 152 | "data": { 153 | "text/plain": [ 154 | "'h'" 155 | ] 156 | }, 157 | "execution_count": 22, 158 | "metadata": {}, 159 | "output_type": "execute_result" 160 | } 161 | ], 162 | "source": [ 163 | "s = 'hello'\n", 164 | "\n", 165 | "#code here\n", 166 | "S = iter(s)\n", 167 | "\n", 168 | "next(S)" 169 | ] 170 | }, 171 | { 172 | "cell_type": "markdown", 173 | "metadata": {}, 174 | "source": [ 175 | "###Problem 4\n", 176 | "Explain a use case for a generator using a yield statement where you would not want to use a normal function with a return statement.\n", 177 | "\n" 178 | ] 179 | }, 180 | { 181 | "cell_type": "markdown", 182 | "metadata": {}, 183 | "source": [] 184 | }, 185 | { 186 | "cell_type": "markdown", 187 | "metadata": {}, 188 | "source": [ 189 | "### Extra Credit!\n", 190 | "Can you explain what *gencomp* is in the code below? (Note: We never covered this in lecture! You will have to do some googling/Stack Overflowing!)" 191 | ] 192 | }, 193 | { 194 | "cell_type": "code", 195 | "execution_count": 18, 196 | "metadata": { 197 | "collapsed": false 198 | }, 199 | "outputs": [ 200 | { 201 | "name": "stdout", 202 | "output_type": "stream", 203 | "text": [ 204 | "4\n", 205 | "5\n" 206 | ] 207 | } 208 | ], 209 | "source": [ 210 | "my_list = [1,2,3,4,5]\n", 211 | "\n", 212 | "gencomp = (item for item in my_list if item > 3)\n", 213 | "\n", 214 | "for item in gencomp:\n", 215 | " print item" 216 | ] 217 | }, 218 | { 219 | "cell_type": "markdown", 220 | "metadata": {}, 221 | "source": [ 222 | "Hint google: generator comprehension is!\n", 223 | "\n", 224 | "#Great Job!" 225 | ] 226 | } 227 | ], 228 | "metadata": { 229 | "kernelspec": { 230 | "display_name": "Python 2", 231 | "language": "python", 232 | "name": "python2" 233 | }, 234 | "language_info": { 235 | "codemirror_mode": { 236 | "name": "ipython", 237 | "version": 2 238 | }, 239 | "file_extension": ".py", 240 | "mimetype": "text/x-python", 241 | "name": "python", 242 | "nbconvert_exporter": "python", 243 | "pygments_lexer": "ipython2", 244 | "version": "2.7.11" 245 | } 246 | }, 247 | "nbformat": 4, 248 | "nbformat_minor": 0 249 | } 250 | -------------------------------------------------------------------------------- /Iterators and Generators/Iterators and Generators Homework.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "#Iterators and Generators Homework \n", 8 | "\n", 9 | "###Problem 1\n", 10 | "\n", 11 | "Create a generator that generates the squares of numbers up to some number N." 12 | ] 13 | }, 14 | { 15 | "cell_type": "code", 16 | "execution_count": 8, 17 | "metadata": { 18 | "collapsed": false 19 | }, 20 | "outputs": [], 21 | "source": [ 22 | "def gensquares(N):\n", 23 | " for i in range(N+1):\n", 24 | " yield i**2" 25 | ] 26 | }, 27 | { 28 | "cell_type": "code", 29 | "execution_count": 9, 30 | "metadata": { 31 | "collapsed": false 32 | }, 33 | "outputs": [ 34 | { 35 | "name": "stdout", 36 | "output_type": "stream", 37 | "text": [ 38 | "0\n", 39 | "1\n", 40 | "4\n", 41 | "9\n", 42 | "16\n", 43 | "25\n", 44 | "36\n", 45 | "49\n", 46 | "64\n", 47 | "81\n", 48 | "100\n" 49 | ] 50 | } 51 | ], 52 | "source": [ 53 | "for x in gensquares(10):\n", 54 | " print x" 55 | ] 56 | }, 57 | { 58 | "cell_type": "markdown", 59 | "metadata": {}, 60 | "source": [ 61 | "###Problem 2\n", 62 | "\n", 63 | "Create a generator that yields \"n\" random numbers between a low and high number (that are inputs). Note: Use the random library. For example:" 64 | ] 65 | }, 66 | { 67 | "cell_type": "code", 68 | "execution_count": 37, 69 | "metadata": { 70 | "collapsed": false 71 | }, 72 | "outputs": [ 73 | { 74 | "data": { 75 | "text/plain": [ 76 | "7" 77 | ] 78 | }, 79 | "execution_count": 37, 80 | "metadata": {}, 81 | "output_type": "execute_result" 82 | } 83 | ], 84 | "source": [ 85 | "import random\n", 86 | "\n", 87 | "random.randint(1,10)" 88 | ] 89 | }, 90 | { 91 | "cell_type": "code", 92 | "execution_count": 40, 93 | "metadata": { 94 | "collapsed": true 95 | }, 96 | "outputs": [], 97 | "source": [ 98 | "def rand_num(low,high,n):\n", 99 | " \n", 100 | " for i in range(n):\n", 101 | " yield random.randint(low, high)" 102 | ] 103 | }, 104 | { 105 | "cell_type": "code", 106 | "execution_count": 41, 107 | "metadata": { 108 | "collapsed": false 109 | }, 110 | "outputs": [ 111 | { 112 | "name": "stdout", 113 | "output_type": "stream", 114 | "text": [ 115 | "9\n", 116 | "1\n", 117 | "5\n", 118 | "3\n", 119 | "1\n", 120 | "2\n", 121 | "9\n", 122 | "8\n", 123 | "7\n", 124 | "9\n", 125 | "10\n", 126 | "8\n" 127 | ] 128 | } 129 | ], 130 | "source": [ 131 | "for num in rand_num(1,10,12):\n", 132 | " print num" 133 | ] 134 | }, 135 | { 136 | "cell_type": "markdown", 137 | "metadata": {}, 138 | "source": [ 139 | "###Problem 3\n", 140 | "\n", 141 | "Use the iter() function to convert the string below \n" 142 | ] 143 | }, 144 | { 145 | "cell_type": "code", 146 | "execution_count": 22, 147 | "metadata": { 148 | "collapsed": false 149 | }, 150 | "outputs": [ 151 | { 152 | "data": { 153 | "text/plain": [ 154 | "'h'" 155 | ] 156 | }, 157 | "execution_count": 22, 158 | "metadata": {}, 159 | "output_type": "execute_result" 160 | } 161 | ], 162 | "source": [ 163 | "s = 'hello'\n", 164 | "\n", 165 | "#code here\n", 166 | "S = iter(s)\n", 167 | "\n", 168 | "next(S)" 169 | ] 170 | }, 171 | { 172 | "cell_type": "markdown", 173 | "metadata": {}, 174 | "source": [ 175 | "###Problem 4\n", 176 | "Explain a use case for a generator using a yield statement where you would not want to use a normal function with a return statement.\n", 177 | "\n" 178 | ] 179 | }, 180 | { 181 | "cell_type": "markdown", 182 | "metadata": {}, 183 | "source": [] 184 | }, 185 | { 186 | "cell_type": "markdown", 187 | "metadata": {}, 188 | "source": [ 189 | "### Extra Credit!\n", 190 | "Can you explain what *gencomp* is in the code below? (Note: We never covered this in lecture! You will have to do some googling/Stack Overflowing!)" 191 | ] 192 | }, 193 | { 194 | "cell_type": "code", 195 | "execution_count": 18, 196 | "metadata": { 197 | "collapsed": false 198 | }, 199 | "outputs": [ 200 | { 201 | "name": "stdout", 202 | "output_type": "stream", 203 | "text": [ 204 | "4\n", 205 | "5\n" 206 | ] 207 | } 208 | ], 209 | "source": [ 210 | "my_list = [1,2,3,4,5]\n", 211 | "\n", 212 | "gencomp = (item for item in my_list if item > 3)\n", 213 | "\n", 214 | "for item in gencomp:\n", 215 | " print item" 216 | ] 217 | }, 218 | { 219 | "cell_type": "markdown", 220 | "metadata": {}, 221 | "source": [ 222 | "Hint google: generator comprehension is!\n", 223 | "\n", 224 | "#Great Job!" 225 | ] 226 | } 227 | ], 228 | "metadata": { 229 | "kernelspec": { 230 | "display_name": "Python 2", 231 | "language": "python", 232 | "name": "python2" 233 | }, 234 | "language_info": { 235 | "codemirror_mode": { 236 | "name": "ipython", 237 | "version": 2 238 | }, 239 | "file_extension": ".py", 240 | "mimetype": "text/x-python", 241 | "name": "python", 242 | "nbconvert_exporter": "python", 243 | "pygments_lexer": "ipython2", 244 | "version": "2.7.11" 245 | } 246 | }, 247 | "nbformat": 4, 248 | "nbformat_minor": 0 249 | } 250 | -------------------------------------------------------------------------------- /Methods and Functions/.ipynb_checkpoints/Functions-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Functions\n", 8 | "\n", 9 | "### So what is a function?\n", 10 | "\n", 11 | "Formally, a function is a useful device that groups together a set of statements so they can be run more than once. They can also let us specify parameters that can serve as inputs to the functions.\n", 12 | "On a more fundamental level, functions allow us to not have to repeatedly write the same code again and again. If you remember back to the lessons on strings and lists, remember that we used a function len() to get the length of a string. Since checking the length of a sequence is a common task you would want to write a function that can do this repeatedly at command.\n" 13 | ] 14 | }, 15 | { 16 | "cell_type": "markdown", 17 | "metadata": {}, 18 | "source": [ 19 | "### def statements" 20 | ] 21 | }, 22 | { 23 | "cell_type": "code", 24 | "execution_count": 1, 25 | "metadata": { 26 | "collapsed": true 27 | }, 28 | "outputs": [], 29 | "source": [ 30 | "#Function Syntax\n", 31 | "def name_of_function(arg1,arg2):\n", 32 | " '''\n", 33 | " This is where the function's Document String (doc-string) goes\n", 34 | " '''\n", 35 | " # Do stuff here\n", 36 | " #return desired result" 37 | ] 38 | }, 39 | { 40 | "cell_type": "markdown", 41 | "metadata": {}, 42 | "source": [ 43 | "We begin with def then a space followed by the name of the function. Try to keep names relevant, for example len() is a good name for a length() function. Also be careful with names, you wouldn't want to call a function the same name as a built-in function in Python (such as len).\n", 44 | "\n", 45 | "Next come a pair of parenthesis with a number of arguments separated by a comma. These arguments are the inputs for your function. You'll be able to use these inputs in your function and reference them. \n", 46 | "\n", 47 | "After this you put a colon. Now here is the important step, you must indent to begin the code inside your function correctly. Python makes use of whitespace to organize code. Lots of other programing languages do not do this, so keep that in mind." 48 | ] 49 | }, 50 | { 51 | "cell_type": "code", 52 | "execution_count": 3, 53 | "metadata": { 54 | "collapsed": false 55 | }, 56 | "outputs": [ 57 | { 58 | "name": "stdout", 59 | "output_type": "stream", 60 | "text": [ 61 | "helloooooo!\n" 62 | ] 63 | } 64 | ], 65 | "source": [ 66 | "#Example 1, Normal Function\n", 67 | "def say_hello():\n", 68 | " print 'helloooooo!'\n", 69 | "\n", 70 | "say_hello()" 71 | ] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "execution_count": 13, 76 | "metadata": { 77 | "collapsed": false 78 | }, 79 | "outputs": [ 80 | { 81 | "data": { 82 | "text/plain": [ 83 | "3" 84 | ] 85 | }, 86 | "execution_count": 13, 87 | "metadata": {}, 88 | "output_type": "execute_result" 89 | } 90 | ], 91 | "source": [ 92 | "#Example 2, Function with arguements\n", 93 | "def arg_func(num1, num2):\n", 94 | " return num1 + num2\n", 95 | "\n", 96 | "arg_func(2,1)" 97 | ] 98 | }, 99 | { 100 | "cell_type": "code", 101 | "execution_count": 15, 102 | "metadata": { 103 | "collapsed": false 104 | }, 105 | "outputs": [], 106 | "source": [ 107 | "#Example 3, Function to find if a number is prime or not (Naive Method)\n", 108 | "def prime_func(num):\n", 109 | " for n in range(2,num):\n", 110 | " if num%n==0:\n", 111 | " print 'not prime'\n", 112 | " break\n", 113 | " else:\n", 114 | " print 'prime'" 115 | ] 116 | }, 117 | { 118 | "cell_type": "code", 119 | "execution_count": 21, 120 | "metadata": { 121 | "collapsed": false 122 | }, 123 | "outputs": [ 124 | { 125 | "name": "stdout", 126 | "output_type": "stream", 127 | "text": [ 128 | "not prime\n", 129 | "not prime\n" 130 | ] 131 | } 132 | ], 133 | "source": [ 134 | "prime_func(16)" 135 | ] 136 | } 137 | ], 138 | "metadata": { 139 | "kernelspec": { 140 | "display_name": "Python 2", 141 | "language": "python", 142 | "name": "python2" 143 | }, 144 | "language_info": { 145 | "codemirror_mode": { 146 | "name": "ipython", 147 | "version": 2 148 | }, 149 | "file_extension": ".py", 150 | "mimetype": "text/x-python", 151 | "name": "python", 152 | "nbconvert_exporter": "python", 153 | "pygments_lexer": "ipython2", 154 | "version": "2.7.11" 155 | } 156 | }, 157 | "nbformat": 4, 158 | "nbformat_minor": 0 159 | } 160 | -------------------------------------------------------------------------------- /Methods and Functions/.ipynb_checkpoints/Methods-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Methods\n", 8 | "Methods will perform specific actions on the object and can also take arguments, just like a function. \n", 9 | "\n", 10 | "Methods are in the form:\n", 11 | "object.method(arg1,arg2,etc...)" 12 | ] 13 | }, 14 | { 15 | "cell_type": "code", 16 | "execution_count": 1, 17 | "metadata": { 18 | "collapsed": true 19 | }, 20 | "outputs": [], 21 | "source": [ 22 | "#Quick Example \n", 23 | "l = [1,2,2,3,4,5,5,6]" 24 | ] 25 | }, 26 | { 27 | "cell_type": "code", 28 | "execution_count": 2, 29 | "metadata": { 30 | "collapsed": false 31 | }, 32 | "outputs": [ 33 | { 34 | "data": { 35 | "text/plain": [ 36 | "2" 37 | ] 38 | }, 39 | "execution_count": 2, 40 | "metadata": {}, 41 | "output_type": "execute_result" 42 | } 43 | ], 44 | "source": [ 45 | "#l is object, count is method and 2 is arguement\n", 46 | "l.count(2)" 47 | ] 48 | }, 49 | { 50 | "cell_type": "code", 51 | "execution_count": 3, 52 | "metadata": { 53 | "collapsed": true 54 | }, 55 | "outputs": [], 56 | "source": [ 57 | "l.append(7)" 58 | ] 59 | }, 60 | { 61 | "cell_type": "code", 62 | "execution_count": 4, 63 | "metadata": { 64 | "collapsed": false 65 | }, 66 | "outputs": [ 67 | { 68 | "data": { 69 | "text/plain": [ 70 | "[1, 2, 2, 3, 4, 5, 5, 6, 7]" 71 | ] 72 | }, 73 | "execution_count": 4, 74 | "metadata": {}, 75 | "output_type": "execute_result" 76 | } 77 | ], 78 | "source": [ 79 | "l" 80 | ] 81 | } 82 | ], 83 | "metadata": { 84 | "kernelspec": { 85 | "display_name": "Python 2", 86 | "language": "python", 87 | "name": "python2" 88 | }, 89 | "language_info": { 90 | "codemirror_mode": { 91 | "name": "ipython", 92 | "version": 2 93 | }, 94 | "file_extension": ".py", 95 | "mimetype": "text/x-python", 96 | "name": "python", 97 | "nbconvert_exporter": "python", 98 | "pygments_lexer": "ipython2", 99 | "version": "2.7.11" 100 | } 101 | }, 102 | "nbformat": 4, 103 | "nbformat_minor": 0 104 | } 105 | -------------------------------------------------------------------------------- /Methods and Functions/Functions.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Functions\n", 8 | "\n", 9 | "### So what is a function?\n", 10 | "\n", 11 | "Formally, a function is a useful device that groups together a set of statements so they can be run more than once. They can also let us specify parameters that can serve as inputs to the functions.\n", 12 | "On a more fundamental level, functions allow us to not have to repeatedly write the same code again and again. If you remember back to the lessons on strings and lists, remember that we used a function len() to get the length of a string. Since checking the length of a sequence is a common task you would want to write a function that can do this repeatedly at command.\n" 13 | ] 14 | }, 15 | { 16 | "cell_type": "markdown", 17 | "metadata": {}, 18 | "source": [ 19 | "### def statements" 20 | ] 21 | }, 22 | { 23 | "cell_type": "code", 24 | "execution_count": 1, 25 | "metadata": { 26 | "collapsed": true 27 | }, 28 | "outputs": [], 29 | "source": [ 30 | "#Function Syntax\n", 31 | "def name_of_function(arg1,arg2):\n", 32 | " '''\n", 33 | " This is where the function's Document String (doc-string) goes\n", 34 | " '''\n", 35 | " # Do stuff here\n", 36 | " #return desired result" 37 | ] 38 | }, 39 | { 40 | "cell_type": "markdown", 41 | "metadata": {}, 42 | "source": [ 43 | "We begin with def then a space followed by the name of the function. Try to keep names relevant, for example len() is a good name for a length() function. Also be careful with names, you wouldn't want to call a function the same name as a built-in function in Python (such as len).\n", 44 | "\n", 45 | "Next come a pair of parenthesis with a number of arguments separated by a comma. These arguments are the inputs for your function. You'll be able to use these inputs in your function and reference them. \n", 46 | "\n", 47 | "After this you put a colon. Now here is the important step, you must indent to begin the code inside your function correctly. Python makes use of whitespace to organize code. Lots of other programing languages do not do this, so keep that in mind." 48 | ] 49 | }, 50 | { 51 | "cell_type": "code", 52 | "execution_count": 3, 53 | "metadata": { 54 | "collapsed": false 55 | }, 56 | "outputs": [ 57 | { 58 | "name": "stdout", 59 | "output_type": "stream", 60 | "text": [ 61 | "helloooooo!\n" 62 | ] 63 | } 64 | ], 65 | "source": [ 66 | "#Example 1, Normal Function\n", 67 | "def say_hello():\n", 68 | " print 'helloooooo!'\n", 69 | "\n", 70 | "say_hello()" 71 | ] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "execution_count": 13, 76 | "metadata": { 77 | "collapsed": false 78 | }, 79 | "outputs": [ 80 | { 81 | "data": { 82 | "text/plain": [ 83 | "3" 84 | ] 85 | }, 86 | "execution_count": 13, 87 | "metadata": {}, 88 | "output_type": "execute_result" 89 | } 90 | ], 91 | "source": [ 92 | "#Example 2, Function with arguements\n", 93 | "def arg_func(num1, num2):\n", 94 | " return num1 + num2\n", 95 | "\n", 96 | "arg_func(2,1)" 97 | ] 98 | }, 99 | { 100 | "cell_type": "code", 101 | "execution_count": 15, 102 | "metadata": { 103 | "collapsed": false 104 | }, 105 | "outputs": [], 106 | "source": [ 107 | "#Example 3, Function to find if a number is prime or not (Naive Method)\n", 108 | "def prime_func(num):\n", 109 | " for n in range(2,num):\n", 110 | " if num%n==0:\n", 111 | " print 'not prime'\n", 112 | " break\n", 113 | " else:\n", 114 | " print 'prime'" 115 | ] 116 | }, 117 | { 118 | "cell_type": "code", 119 | "execution_count": 21, 120 | "metadata": { 121 | "collapsed": false 122 | }, 123 | "outputs": [ 124 | { 125 | "name": "stdout", 126 | "output_type": "stream", 127 | "text": [ 128 | "not prime\n", 129 | "not prime\n" 130 | ] 131 | } 132 | ], 133 | "source": [ 134 | "prime_func(16)" 135 | ] 136 | } 137 | ], 138 | "metadata": { 139 | "kernelspec": { 140 | "display_name": "Python 2", 141 | "language": "python", 142 | "name": "python2" 143 | }, 144 | "language_info": { 145 | "codemirror_mode": { 146 | "name": "ipython", 147 | "version": 2 148 | }, 149 | "file_extension": ".py", 150 | "mimetype": "text/x-python", 151 | "name": "python", 152 | "nbconvert_exporter": "python", 153 | "pygments_lexer": "ipython2", 154 | "version": "2.7.11" 155 | } 156 | }, 157 | "nbformat": 4, 158 | "nbformat_minor": 0 159 | } 160 | -------------------------------------------------------------------------------- /Methods and Functions/Methods.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Methods\n", 8 | "Methods will perform specific actions on the object and can also take arguments, just like a function. \n", 9 | "\n", 10 | "Methods are in the form:\n", 11 | "object.method(arg1,arg2,etc...)" 12 | ] 13 | }, 14 | { 15 | "cell_type": "code", 16 | "execution_count": 1, 17 | "metadata": { 18 | "collapsed": true 19 | }, 20 | "outputs": [], 21 | "source": [ 22 | "#Quick Example \n", 23 | "l = [1,2,2,3,4,5,5,6]" 24 | ] 25 | }, 26 | { 27 | "cell_type": "code", 28 | "execution_count": 2, 29 | "metadata": { 30 | "collapsed": false 31 | }, 32 | "outputs": [ 33 | { 34 | "data": { 35 | "text/plain": [ 36 | "2" 37 | ] 38 | }, 39 | "execution_count": 2, 40 | "metadata": {}, 41 | "output_type": "execute_result" 42 | } 43 | ], 44 | "source": [ 45 | "#l is object, count is method and 2 is arguement\n", 46 | "l.count(2)" 47 | ] 48 | }, 49 | { 50 | "cell_type": "code", 51 | "execution_count": 3, 52 | "metadata": { 53 | "collapsed": true 54 | }, 55 | "outputs": [], 56 | "source": [ 57 | "l.append(7)" 58 | ] 59 | }, 60 | { 61 | "cell_type": "code", 62 | "execution_count": 4, 63 | "metadata": { 64 | "collapsed": false 65 | }, 66 | "outputs": [ 67 | { 68 | "data": { 69 | "text/plain": [ 70 | "[1, 2, 2, 3, 4, 5, 5, 6, 7]" 71 | ] 72 | }, 73 | "execution_count": 4, 74 | "metadata": {}, 75 | "output_type": "execute_result" 76 | } 77 | ], 78 | "source": [ 79 | "l" 80 | ] 81 | } 82 | ], 83 | "metadata": { 84 | "kernelspec": { 85 | "display_name": "Python 2", 86 | "language": "python", 87 | "name": "python2" 88 | }, 89 | "language_info": { 90 | "codemirror_mode": { 91 | "name": "ipython", 92 | "version": 2 93 | }, 94 | "file_extension": ".py", 95 | "mimetype": "text/x-python", 96 | "name": "python", 97 | "nbconvert_exporter": "python", 98 | "pygments_lexer": "ipython2", 99 | "version": "2.7.11" 100 | } 101 | }, 102 | "nbformat": 4, 103 | "nbformat_minor": 0 104 | } 105 | -------------------------------------------------------------------------------- /Obj_DataStructure_Basics/Numbers.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": { 7 | "collapsed": false 8 | }, 9 | "outputs": [ 10 | { 11 | "data": { 12 | "text/plain": [ 13 | "5" 14 | ] 15 | }, 16 | "execution_count": 1, 17 | "metadata": {}, 18 | "output_type": "execute_result" 19 | } 20 | ], 21 | "source": [ 22 | "#Addition\n", 23 | "2+3" 24 | ] 25 | }, 26 | { 27 | "cell_type": "code", 28 | "execution_count": 2, 29 | "metadata": { 30 | "collapsed": false 31 | }, 32 | "outputs": [ 33 | { 34 | "data": { 35 | "text/plain": [ 36 | "3" 37 | ] 38 | }, 39 | "execution_count": 2, 40 | "metadata": {}, 41 | "output_type": "execute_result" 42 | } 43 | ], 44 | "source": [ 45 | "#Subtraction\n", 46 | "5-2" 47 | ] 48 | }, 49 | { 50 | "cell_type": "code", 51 | "execution_count": 3, 52 | "metadata": { 53 | "collapsed": false 54 | }, 55 | "outputs": [ 56 | { 57 | "data": { 58 | "text/plain": [ 59 | "6" 60 | ] 61 | }, 62 | "execution_count": 3, 63 | "metadata": {}, 64 | "output_type": "execute_result" 65 | } 66 | ], 67 | "source": [ 68 | "#Multiplication\n", 69 | "3*2" 70 | ] 71 | }, 72 | { 73 | "cell_type": "code", 74 | "execution_count": 8, 75 | "metadata": { 76 | "collapsed": false 77 | }, 78 | "outputs": [ 79 | { 80 | "data": { 81 | "text/plain": [ 82 | "1" 83 | ] 84 | }, 85 | "execution_count": 8, 86 | "metadata": {}, 87 | "output_type": "execute_result" 88 | } 89 | ], 90 | "source": [ 91 | "#Division\n", 92 | "7.0/2\n" 93 | ] 94 | }, 95 | { 96 | "cell_type": "code", 97 | "execution_count": 9, 98 | "metadata": { 99 | "collapsed": false 100 | }, 101 | "outputs": [ 102 | { 103 | "data": { 104 | "text/plain": [ 105 | "4" 106 | ] 107 | }, 108 | "execution_count": 9, 109 | "metadata": {}, 110 | "output_type": "execute_result" 111 | } 112 | ], 113 | "source": [ 114 | "#Integer Value Division\n", 115 | "8/2" 116 | ] 117 | }, 118 | { 119 | "cell_type": "code", 120 | "execution_count": 10, 121 | "metadata": { 122 | "collapsed": false 123 | }, 124 | "outputs": [ 125 | { 126 | "data": { 127 | "text/plain": [ 128 | "1" 129 | ] 130 | }, 131 | "execution_count": 10, 132 | "metadata": {}, 133 | "output_type": "execute_result" 134 | } 135 | ], 136 | "source": [ 137 | "#Remainder Division\n", 138 | "7%2" 139 | ] 140 | }, 141 | { 142 | "cell_type": "code", 143 | "execution_count": 11, 144 | "metadata": { 145 | "collapsed": false 146 | }, 147 | "outputs": [ 148 | { 149 | "data": { 150 | "text/plain": [ 151 | "1.5" 152 | ] 153 | }, 154 | "execution_count": 11, 155 | "metadata": {}, 156 | "output_type": "execute_result" 157 | } 158 | ], 159 | "source": [ 160 | "from __future__ import division\n", 161 | "3/2" 162 | ] 163 | }, 164 | { 165 | "cell_type": "code", 166 | "execution_count": 12, 167 | "metadata": { 168 | "collapsed": false 169 | }, 170 | "outputs": [ 171 | { 172 | "data": { 173 | "text/plain": [ 174 | "8" 175 | ] 176 | }, 177 | "execution_count": 12, 178 | "metadata": {}, 179 | "output_type": "execute_result" 180 | } 181 | ], 182 | "source": [ 183 | "2**3" 184 | ] 185 | }, 186 | { 187 | "cell_type": "code", 188 | "execution_count": 13, 189 | "metadata": { 190 | "collapsed": true 191 | }, 192 | "outputs": [], 193 | "source": [ 194 | "#Value Assignment\n", 195 | "a=5" 196 | ] 197 | }, 198 | { 199 | "cell_type": "code", 200 | "execution_count": 14, 201 | "metadata": { 202 | "collapsed": true 203 | }, 204 | "outputs": [], 205 | "source": [ 206 | "b=6" 207 | ] 208 | }, 209 | { 210 | "cell_type": "code", 211 | "execution_count": 15, 212 | "metadata": { 213 | "collapsed": false 214 | }, 215 | "outputs": [ 216 | { 217 | "data": { 218 | "text/plain": [ 219 | "11" 220 | ] 221 | }, 222 | "execution_count": 15, 223 | "metadata": {}, 224 | "output_type": "execute_result" 225 | } 226 | ], 227 | "source": [ 228 | "a+b" 229 | ] 230 | }, 231 | { 232 | "cell_type": "code", 233 | "execution_count": null, 234 | "metadata": { 235 | "collapsed": true 236 | }, 237 | "outputs": [], 238 | "source": [] 239 | } 240 | ], 241 | "metadata": { 242 | "kernelspec": { 243 | "display_name": "Python 2", 244 | "language": "python", 245 | "name": "python2" 246 | }, 247 | "language_info": { 248 | "codemirror_mode": { 249 | "name": "ipython", 250 | "version": 2 251 | }, 252 | "file_extension": ".py", 253 | "mimetype": "text/x-python", 254 | "name": "python", 255 | "nbconvert_exporter": "python", 256 | "pygments_lexer": "ipython2", 257 | "version": "2.7.11" 258 | } 259 | }, 260 | "nbformat": 4, 261 | "nbformat_minor": 0 262 | } 263 | -------------------------------------------------------------------------------- /Obj_DataStructure_Basics/Print Formatting.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": { 7 | "collapsed": false 8 | }, 9 | "outputs": [ 10 | { 11 | "name": "stdout", 12 | "output_type": "stream", 13 | "text": [ 14 | "Hello World!\n" 15 | ] 16 | } 17 | ], 18 | "source": [ 19 | "#Basic Example of Print Formatting\n", 20 | "print 'Hello World!'" 21 | ] 22 | }, 23 | { 24 | "cell_type": "code", 25 | "execution_count": 3, 26 | "metadata": { 27 | "collapsed": false 28 | }, 29 | "outputs": [ 30 | { 31 | "name": "stdout", 32 | "output_type": "stream", 33 | "text": [ 34 | "Hello World\n" 35 | ] 36 | } 37 | ], 38 | "source": [ 39 | "s = 'Hello World'\n", 40 | "print s" 41 | ] 42 | }, 43 | { 44 | "cell_type": "code", 45 | "execution_count": 5, 46 | "metadata": { 47 | "collapsed": false 48 | }, 49 | "outputs": [ 50 | { 51 | "name": "stdout", 52 | "output_type": "stream", 53 | "text": [ 54 | "We can also print Strings with a mod and variable name Hello World\n" 55 | ] 56 | } 57 | ], 58 | "source": [ 59 | "print 'We can also print Strings with a mod and variable name %s' %(s)" 60 | ] 61 | }, 62 | { 63 | "cell_type": "markdown", 64 | "metadata": {}, 65 | "source": [ 66 | "Floating Point Numbers\n", 67 | "Floating point numbers use the format %n1.n2f where the n1 is the total minimum number of digits the string should contain (these may be filled with whitespace if the entire number does not have this many digits. The n2 placeholder stands for how many numbers to show past the decimal point." 68 | ] 69 | }, 70 | { 71 | "cell_type": "code", 72 | "execution_count": 6, 73 | "metadata": { 74 | "collapsed": false 75 | }, 76 | "outputs": [ 77 | { 78 | "name": "stdout", 79 | "output_type": "stream", 80 | "text": [ 81 | "Floating Point Numbers : 1.22\n" 82 | ] 83 | } 84 | ], 85 | "source": [ 86 | "#Examples of floating point numbers\n", 87 | "print 'Floating Point Numbers : %1.2f' %(1.22334455)" 88 | ] 89 | }, 90 | { 91 | "cell_type": "code", 92 | "execution_count": 7, 93 | "metadata": { 94 | "collapsed": false 95 | }, 96 | "outputs": [ 97 | { 98 | "name": "stdout", 99 | "output_type": "stream", 100 | "text": [ 101 | "Floating Point Numbers : 1.2233\n" 102 | ] 103 | } 104 | ], 105 | "source": [ 106 | "print 'Floating Point Numbers : %1.4f' %(1.22334455)" 107 | ] 108 | }, 109 | { 110 | "cell_type": "code", 111 | "execution_count": 11, 112 | "metadata": { 113 | "collapsed": false 114 | }, 115 | "outputs": [ 116 | { 117 | "name": "stdout", 118 | "output_type": "stream", 119 | "text": [ 120 | "Floating Point Numbers : 1.2233445499999999750428969\n" 121 | ] 122 | } 123 | ], 124 | "source": [ 125 | "print 'Floating Point Numbers : %30.25f' %(1.22334455)" 126 | ] 127 | }, 128 | { 129 | "cell_type": "code", 130 | "execution_count": 12, 131 | "metadata": { 132 | "collapsed": false 133 | }, 134 | "outputs": [ 135 | { 136 | "name": "stdout", 137 | "output_type": "stream", 138 | "text": [ 139 | "This is one number : 123 and this is another number : 'Python'\n" 140 | ] 141 | } 142 | ], 143 | "source": [ 144 | "#Calling two values into print string\n", 145 | "print 'This is one number : %s and this is another number : %r' %(123, \"Python\")" 146 | ] 147 | }, 148 | { 149 | "cell_type": "code", 150 | "execution_count": 14, 151 | "metadata": { 152 | "collapsed": false 153 | }, 154 | "outputs": [ 155 | { 156 | "name": "stdout", 157 | "output_type": "stream", 158 | "text": [ 159 | "This is one number : 123 and this is another number : 'Python'. This is Decimal number : 3.15\n" 160 | ] 161 | } 162 | ], 163 | "source": [ 164 | "#Multiple Formatting\n", 165 | "print 'This is one number : %s and this is another number : %r. This is Decimal number : %1.2f' %(123, \"Python\", 3.1456)" 166 | ] 167 | }, 168 | { 169 | "cell_type": "markdown", 170 | "metadata": {}, 171 | "source": [ 172 | "# Using the string .format() method\n", 173 | "The best way to format objects into your strings for print statements is using the format method. The syntax is:\n", 174 | "'String here {var1} then also {var2}'.format(var1='something1',var2='something2')\n" 175 | ] 176 | }, 177 | { 178 | "cell_type": "code", 179 | "execution_count": 15, 180 | "metadata": { 181 | "collapsed": false 182 | }, 183 | "outputs": [ 184 | { 185 | "name": "stdout", 186 | "output_type": "stream", 187 | "text": [ 188 | "This is Object 1: Hey, this is Object2: 123 and this is another object: 2.057\n" 189 | ] 190 | } 191 | ], 192 | "source": [ 193 | "print 'This is Object 1: %s, this is Object2: %s and this is another object: %1.3f'%(\"Hey\", 123, 2.0567)" 194 | ] 195 | }, 196 | { 197 | "cell_type": "markdown", 198 | "metadata": {}, 199 | "source": [ 200 | "# That is the basics of string formatting! Remember that Python 3 uses a print() function, not the print statement!" 201 | ] 202 | } 203 | ], 204 | "metadata": { 205 | "kernelspec": { 206 | "display_name": "Python 2", 207 | "language": "python", 208 | "name": "python2" 209 | }, 210 | "language_info": { 211 | "codemirror_mode": { 212 | "name": "ipython", 213 | "version": 2 214 | }, 215 | "file_extension": ".py", 216 | "mimetype": "text/x-python", 217 | "name": "python", 218 | "nbconvert_exporter": "python", 219 | "pygments_lexer": "ipython2", 220 | "version": "2.7.11" 221 | } 222 | }, 223 | "nbformat": 4, 224 | "nbformat_minor": 0 225 | } 226 | -------------------------------------------------------------------------------- /Obj_DataStructure_Basics/Tuples.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Tuples\n", 8 | "In Python tuples are very similar to lists, however, unlike lists they are immutable meaning they can not be changed. You would use tuples to present things that shouldn't be changed, such as days of the week, or dates on a calendar.\n" 9 | ] 10 | }, 11 | { 12 | "cell_type": "code", 13 | "execution_count": 1, 14 | "metadata": { 15 | "collapsed": true 16 | }, 17 | "outputs": [], 18 | "source": [ 19 | "t = (1,1,1)" 20 | ] 21 | }, 22 | { 23 | "cell_type": "code", 24 | "execution_count": 2, 25 | "metadata": { 26 | "collapsed": false 27 | }, 28 | "outputs": [ 29 | { 30 | "data": { 31 | "text/plain": [ 32 | "(1, 2, 3)" 33 | ] 34 | }, 35 | "execution_count": 2, 36 | "metadata": {}, 37 | "output_type": "execute_result" 38 | } 39 | ], 40 | "source": [ 41 | "t" 42 | ] 43 | }, 44 | { 45 | "cell_type": "code", 46 | "execution_count": 5, 47 | "metadata": { 48 | "collapsed": false 49 | }, 50 | "outputs": [ 51 | { 52 | "data": { 53 | "text/plain": [ 54 | "3" 55 | ] 56 | }, 57 | "execution_count": 5, 58 | "metadata": {}, 59 | "output_type": "execute_result" 60 | } 61 | ], 62 | "source": [ 63 | "#Lenth of Tuple\n", 64 | "len(t)" 65 | ] 66 | }, 67 | { 68 | "cell_type": "code", 69 | "execution_count": 7, 70 | "metadata": { 71 | "collapsed": false 72 | }, 73 | "outputs": [ 74 | { 75 | "data": { 76 | "text/plain": [ 77 | "1" 78 | ] 79 | }, 80 | "execution_count": 7, 81 | "metadata": {}, 82 | "output_type": "execute_result" 83 | } 84 | ], 85 | "source": [ 86 | "#Almost tuples share same kind of methods with Lists, just that they are immutable\n", 87 | "t[0]" 88 | ] 89 | }, 90 | { 91 | "cell_type": "code", 92 | "execution_count": 8, 93 | "metadata": { 94 | "collapsed": true 95 | }, 96 | "outputs": [], 97 | "source": [ 98 | "t2 = (1, (1,1,1), 1)" 99 | ] 100 | }, 101 | { 102 | "cell_type": "code", 103 | "execution_count": 9, 104 | "metadata": { 105 | "collapsed": false 106 | }, 107 | "outputs": [ 108 | { 109 | "data": { 110 | "text/plain": [ 111 | "4" 112 | ] 113 | }, 114 | "execution_count": 9, 115 | "metadata": {}, 116 | "output_type": "execute_result" 117 | } 118 | ], 119 | "source": [ 120 | "t2[1][2]" 121 | ] 122 | }, 123 | { 124 | "cell_type": "code", 125 | "execution_count": 16, 126 | "metadata": { 127 | "collapsed": false 128 | }, 129 | "outputs": [ 130 | { 131 | "data": { 132 | "text/plain": [ 133 | "(1, (2, 3, 4), 5)" 134 | ] 135 | }, 136 | "execution_count": 16, 137 | "metadata": {}, 138 | "output_type": "execute_result" 139 | } 140 | ], 141 | "source": [ 142 | "t2" 143 | ] 144 | }, 145 | { 146 | "cell_type": "code", 147 | "execution_count": 11, 148 | "metadata": { 149 | "collapsed": true 150 | }, 151 | "outputs": [], 152 | "source": [ 153 | "t3 = ('Sports', 1)" 154 | ] 155 | }, 156 | { 157 | "cell_type": "code", 158 | "execution_count": 17, 159 | "metadata": { 160 | "collapsed": false 161 | }, 162 | "outputs": [ 163 | { 164 | "data": { 165 | "text/plain": [ 166 | "1" 167 | ] 168 | }, 169 | "execution_count": 17, 170 | "metadata": {}, 171 | "output_type": "execute_result" 172 | } 173 | ], 174 | "source": [ 175 | "# Returns value at value of index mentioned\n", 176 | "t3.index(1)" 177 | ] 178 | }, 179 | { 180 | "cell_type": "code", 181 | "execution_count": 20, 182 | "metadata": { 183 | "collapsed": false 184 | }, 185 | "outputs": [ 186 | { 187 | "data": { 188 | "text/plain": [ 189 | "1" 190 | ] 191 | }, 192 | "execution_count": 20, 193 | "metadata": {}, 194 | "output_type": "execute_result" 195 | } 196 | ], 197 | "source": [ 198 | "# Returns how many times a value is repeated\n", 199 | "t.count(1)" 200 | ] 201 | }, 202 | { 203 | "cell_type": "code", 204 | "execution_count": null, 205 | "metadata": { 206 | "collapsed": true 207 | }, 208 | "outputs": [], 209 | "source": [] 210 | } 211 | ], 212 | "metadata": { 213 | "kernelspec": { 214 | "display_name": "Python 2", 215 | "language": "python", 216 | "name": "python2" 217 | }, 218 | "language_info": { 219 | "codemirror_mode": { 220 | "name": "ipython", 221 | "version": 2 222 | }, 223 | "file_extension": ".py", 224 | "mimetype": "text/x-python", 225 | "name": "python", 226 | "nbconvert_exporter": "python", 227 | "pygments_lexer": "ipython2", 228 | "version": "2.7.11" 229 | } 230 | }, 231 | "nbformat": 4, 232 | "nbformat_minor": 0 233 | } 234 | -------------------------------------------------------------------------------- /Obj_DataStructure_Basics/file2.txt: -------------------------------------------------------------------------------- 1 | First Line 2 | 3 | Second Line -------------------------------------------------------------------------------- /Obj_DataStructure_Basics/test.txt: -------------------------------------------------------------------------------- 1 | This is new line -------------------------------------------------------------------------------- /Object Oriented Programming/.ipynb_checkpoints/Object Oriented Programming Homework-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Object Oriented Programming\n", 8 | "## Homework Assignment\n", 9 | "\n", 10 | "####Problem 1\n", 11 | "Fill in the Line class methods to accept coordinate as a pair of tuples and return the slope and distance of the line." 12 | ] 13 | }, 14 | { 15 | "cell_type": "code", 16 | "execution_count": 18, 17 | "metadata": { 18 | "collapsed": true 19 | }, 20 | "outputs": [], 21 | "source": [ 22 | "class Line(object):\n", 23 | " \n", 24 | " def __init__(self,coor1,coor2):\n", 25 | " pass\n", 26 | " \n", 27 | " def distance(self):\n", 28 | " pass\n", 29 | " \n", 30 | " def slope(self):\n", 31 | " pass" 32 | ] 33 | }, 34 | { 35 | "cell_type": "code", 36 | "execution_count": null, 37 | "metadata": { 38 | "collapsed": false 39 | }, 40 | "outputs": [], 41 | "source": [ 42 | "# EXAMPLE OUTPUT\n", 43 | "\n", 44 | "coordinate1 = (3,2)\n", 45 | "coordinate2 = (8,10)\n", 46 | "\n", 47 | "li = Line(coordinate1,coordinate2)" 48 | ] 49 | }, 50 | { 51 | "cell_type": "code", 52 | "execution_count": 20, 53 | "metadata": { 54 | "collapsed": false 55 | }, 56 | "outputs": [ 57 | { 58 | "data": { 59 | "text/plain": [ 60 | "9.433981132056603" 61 | ] 62 | }, 63 | "execution_count": 20, 64 | "metadata": {}, 65 | "output_type": "execute_result" 66 | } 67 | ], 68 | "source": [ 69 | "li.distance()" 70 | ] 71 | }, 72 | { 73 | "cell_type": "code", 74 | "execution_count": 21, 75 | "metadata": { 76 | "collapsed": false 77 | }, 78 | "outputs": [ 79 | { 80 | "data": { 81 | "text/plain": [ 82 | "1.6" 83 | ] 84 | }, 85 | "execution_count": 21, 86 | "metadata": {}, 87 | "output_type": "execute_result" 88 | } 89 | ], 90 | "source": [ 91 | "li.slope()" 92 | ] 93 | }, 94 | { 95 | "cell_type": "markdown", 96 | "metadata": {}, 97 | "source": [ 98 | "________\n", 99 | "####Problem 2" 100 | ] 101 | }, 102 | { 103 | "cell_type": "markdown", 104 | "metadata": {}, 105 | "source": [ 106 | "Fill in the class " 107 | ] 108 | }, 109 | { 110 | "cell_type": "code", 111 | "execution_count": 31, 112 | "metadata": { 113 | "collapsed": false 114 | }, 115 | "outputs": [], 116 | "source": [ 117 | "class Cylinder(object):\n", 118 | " \n", 119 | " def __init__(self,height=1,radius=1):\n", 120 | " pass\n", 121 | " \n", 122 | " def volume(self):\n", 123 | " pass\n", 124 | " \n", 125 | " def surface_area(self):\n", 126 | " pass" 127 | ] 128 | }, 129 | { 130 | "cell_type": "code", 131 | "execution_count": 32, 132 | "metadata": { 133 | "collapsed": true 134 | }, 135 | "outputs": [], 136 | "source": [ 137 | "# EXAMPLE OUTPUT\n", 138 | "c = Cylinder(2,3)" 139 | ] 140 | }, 141 | { 142 | "cell_type": "code", 143 | "execution_count": 34, 144 | "metadata": { 145 | "collapsed": false 146 | }, 147 | "outputs": [ 148 | { 149 | "data": { 150 | "text/plain": [ 151 | "56.52" 152 | ] 153 | }, 154 | "execution_count": 34, 155 | "metadata": {}, 156 | "output_type": "execute_result" 157 | } 158 | ], 159 | "source": [ 160 | "c.volume()" 161 | ] 162 | }, 163 | { 164 | "cell_type": "code", 165 | "execution_count": 35, 166 | "metadata": { 167 | "collapsed": false 168 | }, 169 | "outputs": [ 170 | { 171 | "data": { 172 | "text/plain": [ 173 | "94.2" 174 | ] 175 | }, 176 | "execution_count": 35, 177 | "metadata": {}, 178 | "output_type": "execute_result" 179 | } 180 | ], 181 | "source": [ 182 | "c.surface_area()" 183 | ] 184 | }, 185 | { 186 | "cell_type": "code", 187 | "execution_count": null, 188 | "metadata": { 189 | "collapsed": true 190 | }, 191 | "outputs": [], 192 | "source": [] 193 | } 194 | ], 195 | "metadata": { 196 | "kernelspec": { 197 | "display_name": "Python 2", 198 | "language": "python", 199 | "name": "python2" 200 | }, 201 | "language_info": { 202 | "codemirror_mode": { 203 | "name": "ipython", 204 | "version": 2 205 | }, 206 | "file_extension": ".py", 207 | "mimetype": "text/x-python", 208 | "name": "python", 209 | "nbconvert_exporter": "python", 210 | "pygments_lexer": "ipython2", 211 | "version": "2.7.11" 212 | } 213 | }, 214 | "nbformat": 4, 215 | "nbformat_minor": 0 216 | } 217 | -------------------------------------------------------------------------------- /Object Oriented Programming/Object Oriented Programming Homework.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Object Oriented Programming\n", 8 | "## Homework Assignment\n", 9 | "\n", 10 | "####Problem 1\n", 11 | "Fill in the Line class methods to accept coordinate as a pair of tuples and return the slope and distance of the line." 12 | ] 13 | }, 14 | { 15 | "cell_type": "code", 16 | "execution_count": 22, 17 | "metadata": { 18 | "collapsed": false 19 | }, 20 | "outputs": [], 21 | "source": [ 22 | "class Line(object):\n", 23 | " \n", 24 | " def __init__(self,coor1,coor2):\n", 25 | " self.coor1 = coor1\n", 26 | " self.coor2 = coor2\n", 27 | " \n", 28 | " def distance(self):\n", 29 | " x1,y1 = self.coor1\n", 30 | " x2,y2 = self.coor2\n", 31 | " return ((x2 - x1)**2 + (y2 - y1)**2)**0.5\n", 32 | " \n", 33 | " def slope(self):\n", 34 | " x1,y1 = self.coor1\n", 35 | " x2,y2 = self.coor2\n", 36 | " return float((y2 - y1)) / (x2 - x1)" 37 | ] 38 | }, 39 | { 40 | "cell_type": "code", 41 | "execution_count": 23, 42 | "metadata": { 43 | "collapsed": false 44 | }, 45 | "outputs": [], 46 | "source": [ 47 | "# EXAMPLE OUTPUT\n", 48 | "\n", 49 | "coordinate1 = (3,2)\n", 50 | "coordinate2 = (8,10)\n", 51 | "\n", 52 | "li = Line(coordinate1,coordinate2)" 53 | ] 54 | }, 55 | { 56 | "cell_type": "code", 57 | "execution_count": 24, 58 | "metadata": { 59 | "collapsed": false 60 | }, 61 | "outputs": [ 62 | { 63 | "data": { 64 | "text/plain": [ 65 | "9.433981132056603" 66 | ] 67 | }, 68 | "execution_count": 24, 69 | "metadata": {}, 70 | "output_type": "execute_result" 71 | } 72 | ], 73 | "source": [ 74 | "li.distance()" 75 | ] 76 | }, 77 | { 78 | "cell_type": "code", 79 | "execution_count": 25, 80 | "metadata": { 81 | "collapsed": false 82 | }, 83 | "outputs": [ 84 | { 85 | "data": { 86 | "text/plain": [ 87 | "1.6" 88 | ] 89 | }, 90 | "execution_count": 25, 91 | "metadata": {}, 92 | "output_type": "execute_result" 93 | } 94 | ], 95 | "source": [ 96 | "li.slope()" 97 | ] 98 | }, 99 | { 100 | "cell_type": "markdown", 101 | "metadata": {}, 102 | "source": [ 103 | "________\n", 104 | "####Problem 2" 105 | ] 106 | }, 107 | { 108 | "cell_type": "markdown", 109 | "metadata": {}, 110 | "source": [ 111 | "Fill in the class " 112 | ] 113 | }, 114 | { 115 | "cell_type": "code", 116 | "execution_count": 26, 117 | "metadata": { 118 | "collapsed": false 119 | }, 120 | "outputs": [], 121 | "source": [ 122 | "class Cylinder(object):\n", 123 | " \n", 124 | " def __init__(self,height=1,radius=1):\n", 125 | " self.height=height\n", 126 | " self.radius=radius\n", 127 | " \n", 128 | " def volume(self):\n", 129 | " return 3.14 * self.height * self.radius**2\n", 130 | " \n", 131 | " def surface_area(self):\n", 132 | " top = 3.14 * self.radius**2\n", 133 | " return 2*top+ 2 * 3.14 * self.radius * self.height" 134 | ] 135 | }, 136 | { 137 | "cell_type": "code", 138 | "execution_count": 27, 139 | "metadata": { 140 | "collapsed": true 141 | }, 142 | "outputs": [], 143 | "source": [ 144 | "# EXAMPLE OUTPUT\n", 145 | "c = Cylinder(2,3)" 146 | ] 147 | }, 148 | { 149 | "cell_type": "code", 150 | "execution_count": 28, 151 | "metadata": { 152 | "collapsed": false 153 | }, 154 | "outputs": [ 155 | { 156 | "data": { 157 | "text/plain": [ 158 | "56.52" 159 | ] 160 | }, 161 | "execution_count": 28, 162 | "metadata": {}, 163 | "output_type": "execute_result" 164 | } 165 | ], 166 | "source": [ 167 | "c.volume()" 168 | ] 169 | }, 170 | { 171 | "cell_type": "code", 172 | "execution_count": 29, 173 | "metadata": { 174 | "collapsed": false 175 | }, 176 | "outputs": [ 177 | { 178 | "data": { 179 | "text/plain": [ 180 | "94.2" 181 | ] 182 | }, 183 | "execution_count": 29, 184 | "metadata": {}, 185 | "output_type": "execute_result" 186 | } 187 | ], 188 | "source": [ 189 | "c.surface_area()" 190 | ] 191 | }, 192 | { 193 | "cell_type": "code", 194 | "execution_count": null, 195 | "metadata": { 196 | "collapsed": true 197 | }, 198 | "outputs": [], 199 | "source": [] 200 | } 201 | ], 202 | "metadata": { 203 | "kernelspec": { 204 | "display_name": "Python 2", 205 | "language": "python", 206 | "name": "python2" 207 | }, 208 | "language_info": { 209 | "codemirror_mode": { 210 | "name": "ipython", 211 | "version": 2 212 | }, 213 | "file_extension": ".py", 214 | "mimetype": "text/x-python", 215 | "name": "python", 216 | "nbconvert_exporter": "python", 217 | "pygments_lexer": "ipython2", 218 | "version": "2.7.11" 219 | } 220 | }, 221 | "nbformat": 4, 222 | "nbformat_minor": 0 223 | } 224 | -------------------------------------------------------------------------------- /Python Statements/.ipynb_checkpoints/List Comprehensions-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Comprehensions\n", 8 | "In addition to sequence operations and list methods, Python includes a more advanced operation called a list comprehension.\n", 9 | "List comprehensions allow us to build out lists using a different notation. You can think of it as essentially a one line for loop built inside of brackets." 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 1, 15 | "metadata": { 16 | "collapsed": false 17 | }, 18 | "outputs": [ 19 | { 20 | "name": "stdout", 21 | "output_type": "stream", 22 | "text": [ 23 | "w\n", 24 | "o\n", 25 | "r\n", 26 | "d\n" 27 | ] 28 | } 29 | ], 30 | "source": [ 31 | "#This is normal for loop\n", 32 | "l = \"word\"\n", 33 | "\n", 34 | "for x in l:\n", 35 | " print x" 36 | ] 37 | }, 38 | { 39 | "cell_type": "code", 40 | "execution_count": 2, 41 | "metadata": { 42 | "collapsed": true 43 | }, 44 | "outputs": [], 45 | "source": [ 46 | "#Grabs every character in string\n", 47 | "lst = [x for x in \"word\"]" 48 | ] 49 | }, 50 | { 51 | "cell_type": "code", 52 | "execution_count": 3, 53 | "metadata": { 54 | "collapsed": false 55 | }, 56 | "outputs": [ 57 | { 58 | "data": { 59 | "text/plain": [ 60 | "['w', 'o', 'r', 'd']" 61 | ] 62 | }, 63 | "execution_count": 3, 64 | "metadata": {}, 65 | "output_type": "execute_result" 66 | } 67 | ], 68 | "source": [ 69 | "lst" 70 | ] 71 | }, 72 | { 73 | "cell_type": "code", 74 | "execution_count": 4, 75 | "metadata": { 76 | "collapsed": true 77 | }, 78 | "outputs": [], 79 | "source": [ 80 | "#Squares every number in given range\n", 81 | "sqr = [y**2 for y in range(0,11)]" 82 | ] 83 | }, 84 | { 85 | "cell_type": "code", 86 | "execution_count": 5, 87 | "metadata": { 88 | "collapsed": false 89 | }, 90 | "outputs": [ 91 | { 92 | "data": { 93 | "text/plain": [ 94 | "[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100]" 95 | ] 96 | }, 97 | "execution_count": 5, 98 | "metadata": {}, 99 | "output_type": "execute_result" 100 | } 101 | ], 102 | "source": [ 103 | "sqr" 104 | ] 105 | }, 106 | { 107 | "cell_type": "code", 108 | "execution_count": 6, 109 | "metadata": { 110 | "collapsed": false 111 | }, 112 | "outputs": [ 113 | { 114 | "data": { 115 | "text/plain": [ 116 | "[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20]" 117 | ] 118 | }, 119 | "execution_count": 6, 120 | "metadata": {}, 121 | "output_type": "execute_result" 122 | } 123 | ], 124 | "source": [ 125 | "#All even numbers in given range\n", 126 | "evenNums = [z for z in range(0,21) if z%2==0]\n", 127 | "evenNums" 128 | ] 129 | }, 130 | { 131 | "cell_type": "code", 132 | "execution_count": 7, 133 | "metadata": { 134 | "collapsed": false 135 | }, 136 | "outputs": [ 137 | { 138 | "data": { 139 | "text/plain": [ 140 | "[0, 1, 16, 81, 256, 625, 1296, 2401, 4096, 6561, 10000]" 141 | ] 142 | }, 143 | "execution_count": 7, 144 | "metadata": {}, 145 | "output_type": "execute_result" 146 | } 147 | ], 148 | "source": [ 149 | "#We can also perform Nested List Comprehensions\n", 150 | "lst = [ x**2 for x in [x**2 for x in range(11)]]\n", 151 | "lst" 152 | ] 153 | } 154 | ], 155 | "metadata": { 156 | "kernelspec": { 157 | "display_name": "Python 2", 158 | "language": "python", 159 | "name": "python2" 160 | }, 161 | "language_info": { 162 | "codemirror_mode": { 163 | "name": "ipython", 164 | "version": 2 165 | }, 166 | "file_extension": ".py", 167 | "mimetype": "text/x-python", 168 | "name": "python", 169 | "nbconvert_exporter": "python", 170 | "pygments_lexer": "ipython2", 171 | "version": "2.7.11" 172 | } 173 | }, 174 | "nbformat": 4, 175 | "nbformat_minor": 0 176 | } 177 | -------------------------------------------------------------------------------- /Python Statements/.ipynb_checkpoints/Range-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": { 7 | "collapsed": false 8 | }, 9 | "outputs": [ 10 | { 11 | "data": { 12 | "text/plain": [ 13 | "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]" 14 | ] 15 | }, 16 | "execution_count": 1, 17 | "metadata": {}, 18 | "output_type": "execute_result" 19 | } 20 | ], 21 | "source": [ 22 | "range(0,10)" 23 | ] 24 | }, 25 | { 26 | "cell_type": "code", 27 | "execution_count": 2, 28 | "metadata": { 29 | "collapsed": true 30 | }, 31 | "outputs": [], 32 | "source": [ 33 | "x = range(0,10)" 34 | ] 35 | }, 36 | { 37 | "cell_type": "code", 38 | "execution_count": 3, 39 | "metadata": { 40 | "collapsed": false 41 | }, 42 | "outputs": [ 43 | { 44 | "data": { 45 | "text/plain": [ 46 | "list" 47 | ] 48 | }, 49 | "execution_count": 3, 50 | "metadata": {}, 51 | "output_type": "execute_result" 52 | } 53 | ], 54 | "source": [ 55 | "type(x)" 56 | ] 57 | }, 58 | { 59 | "cell_type": "code", 60 | "execution_count": 4, 61 | "metadata": { 62 | "collapsed": true 63 | }, 64 | "outputs": [], 65 | "source": [ 66 | "start=0\n", 67 | "stop = 26" 68 | ] 69 | }, 70 | { 71 | "cell_type": "code", 72 | "execution_count": 17, 73 | "metadata": { 74 | "collapsed": true 75 | }, 76 | "outputs": [], 77 | "source": [ 78 | "y = range(start,stop,2)" 79 | ] 80 | }, 81 | { 82 | "cell_type": "code", 83 | "execution_count": 18, 84 | "metadata": { 85 | "collapsed": false 86 | }, 87 | "outputs": [ 88 | { 89 | "data": { 90 | "text/plain": [ 91 | "[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24]" 92 | ] 93 | }, 94 | "execution_count": 18, 95 | "metadata": {}, 96 | "output_type": "execute_result" 97 | } 98 | ], 99 | "source": [ 100 | "y" 101 | ] 102 | }, 103 | { 104 | "cell_type": "markdown", 105 | "metadata": {}, 106 | "source": [ 107 | "You might have been wondering, what happens if I want to use a huge range of numbers? Can my computer store that all in memory?\n", 108 | "Great thinking! This is a dilemma that can be solve with the use of a generator. For a simplified explanation: A generator allows the generation of generated objects that are provided at that instance but does not store every instance generated into memory.\n", 109 | "This means a generator would not create a list to generate like range() does, but instead provide a one time generation of the numbers in that range. Python 2 has a built-in range generator called xrange(). It is recommended to use xrange() for for loops in Python 2.\n", 110 | "The good news is in Python 3, range() behaves as a generator and you don't need to worry about it. Let's see a quick example with xrange()" 111 | ] 112 | }, 113 | { 114 | "cell_type": "markdown", 115 | "metadata": {}, 116 | "source": [ 117 | "So the main takeaway here is for Python 2, if you are using range() in a way that you don't need to save the results in a list, use xrange() instead. For Python 3, use range() in any instance.\n", 118 | "You should now have a good understanding of how to use range() in either version of Python." 119 | ] 120 | }, 121 | { 122 | "cell_type": "code", 123 | "execution_count": null, 124 | "metadata": { 125 | "collapsed": true 126 | }, 127 | "outputs": [], 128 | "source": [] 129 | } 130 | ], 131 | "metadata": { 132 | "kernelspec": { 133 | "display_name": "Python 2", 134 | "language": "python", 135 | "name": "python2" 136 | }, 137 | "language_info": { 138 | "codemirror_mode": { 139 | "name": "ipython", 140 | "version": 2 141 | }, 142 | "file_extension": ".py", 143 | "mimetype": "text/x-python", 144 | "name": "python", 145 | "nbconvert_exporter": "python", 146 | "pygments_lexer": "ipython2", 147 | "version": "2.7.11" 148 | } 149 | }, 150 | "nbformat": 4, 151 | "nbformat_minor": 0 152 | } 153 | -------------------------------------------------------------------------------- /Python Statements/.ipynb_checkpoints/While Loops-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": { 7 | "collapsed": false 8 | }, 9 | "outputs": [ 10 | { 11 | "name": "stdout", 12 | "output_type": "stream", 13 | "text": [ 14 | "1\n", 15 | "2\n", 16 | "3\n", 17 | "4\n", 18 | "5\n", 19 | "6\n", 20 | "7\n", 21 | "8\n", 22 | "9\n" 23 | ] 24 | } 25 | ], 26 | "source": [ 27 | "x = 1\n", 28 | "\n", 29 | "while x < 10:\n", 30 | " print x\n", 31 | " x += 1" 32 | ] 33 | }, 34 | { 35 | "cell_type": "code", 36 | "execution_count": null, 37 | "metadata": { 38 | "collapsed": true 39 | }, 40 | "outputs": [], 41 | "source": [ 42 | "y = 1\n", 43 | "\n", 44 | "while y < 20:\n", 45 | " print y\n", 46 | " y += 1" 47 | ] 48 | }, 49 | { 50 | "cell_type": "code", 51 | "execution_count": null, 52 | "metadata": { 53 | "collapsed": true 54 | }, 55 | "outputs": [], 56 | "source": [] 57 | } 58 | ], 59 | "metadata": { 60 | "kernelspec": { 61 | "display_name": "Python 2", 62 | "language": "python", 63 | "name": "python2" 64 | }, 65 | "language_info": { 66 | "codemirror_mode": { 67 | "name": "ipython", 68 | "version": 2 69 | }, 70 | "file_extension": ".py", 71 | "mimetype": "text/x-python", 72 | "name": "python", 73 | "nbconvert_exporter": "python", 74 | "pygments_lexer": "ipython2", 75 | "version": "2.7.11" 76 | } 77 | }, 78 | "nbformat": 4, 79 | "nbformat_minor": 0 80 | } 81 | -------------------------------------------------------------------------------- /Python Statements/.ipynb_checkpoints/for Loops-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Problem 1: Iterating through a List" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 1, 13 | "metadata": { 14 | "collapsed": false 15 | }, 16 | "outputs": [ 17 | { 18 | "name": "stdout", 19 | "output_type": "stream", 20 | "text": [ 21 | "1\n", 22 | "2\n", 23 | "3\n", 24 | "4\n", 25 | "5\n", 26 | "6\n", 27 | "7\n", 28 | "8\n", 29 | "9\n" 30 | ] 31 | } 32 | ], 33 | "source": [ 34 | "p = [1,2,3,4,5,6,7,8,9]\n", 35 | "\n", 36 | "for i in p:\n", 37 | " print i" 38 | ] 39 | }, 40 | { 41 | "cell_type": "markdown", 42 | "metadata": {}, 43 | "source": [ 44 | "# Problem 2: Printing Even Numbers" 45 | ] 46 | }, 47 | { 48 | "cell_type": "code", 49 | "execution_count": 2, 50 | "metadata": { 51 | "collapsed": false 52 | }, 53 | "outputs": [ 54 | { 55 | "name": "stdout", 56 | "output_type": "stream", 57 | "text": [ 58 | "2\n", 59 | "4\n", 60 | "6\n", 61 | "8\n" 62 | ] 63 | } 64 | ], 65 | "source": [ 66 | "for j in p:\n", 67 | " if j%2 == 0:\n", 68 | " print j" 69 | ] 70 | }, 71 | { 72 | "cell_type": "markdown", 73 | "metadata": {}, 74 | "source": [ 75 | "# Problem 3: Printing Odd Numbers" 76 | ] 77 | }, 78 | { 79 | "cell_type": "code", 80 | "execution_count": 4, 81 | "metadata": { 82 | "collapsed": false 83 | }, 84 | "outputs": [ 85 | { 86 | "name": "stdout", 87 | "output_type": "stream", 88 | "text": [ 89 | "1\n", 90 | "3\n", 91 | "5\n", 92 | "7\n", 93 | "9\n" 94 | ] 95 | } 96 | ], 97 | "source": [ 98 | "for k in p:\n", 99 | " if k%2 != 0:\n", 100 | " print k" 101 | ] 102 | }, 103 | { 104 | "cell_type": "markdown", 105 | "metadata": {}, 106 | "source": [ 107 | "For Loop in Python is powerful and it is often used to iterate over collections like tuples, Dictionaries and Lists." 108 | ] 109 | } 110 | ], 111 | "metadata": { 112 | "kernelspec": { 113 | "display_name": "Python 2", 114 | "language": "python", 115 | "name": "python2" 116 | }, 117 | "language_info": { 118 | "codemirror_mode": { 119 | "name": "ipython", 120 | "version": 2 121 | }, 122 | "file_extension": ".py", 123 | "mimetype": "text/x-python", 124 | "name": "python", 125 | "nbconvert_exporter": "python", 126 | "pygments_lexer": "ipython2", 127 | "version": "2.7.11" 128 | } 129 | }, 130 | "nbformat": 4, 131 | "nbformat_minor": 0 132 | } 133 | -------------------------------------------------------------------------------- /Python Statements/.ipynb_checkpoints/if, elif and else-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 3, 6 | "metadata": { 7 | "collapsed": false 8 | }, 9 | "outputs": [ 10 | { 11 | "name": "stdout", 12 | "output_type": "stream", 13 | "text": [ 14 | "It is not\n" 15 | ] 16 | } 17 | ], 18 | "source": [ 19 | "x = False;\n", 20 | "if x == True:\n", 21 | " print 'It is True'\n", 22 | "else:\n", 23 | " print 'It is not'" 24 | ] 25 | }, 26 | { 27 | "cell_type": "code", 28 | "execution_count": 5, 29 | "metadata": { 30 | "collapsed": false 31 | }, 32 | "outputs": [ 33 | { 34 | "name": "stdout", 35 | "output_type": "stream", 36 | "text": [ 37 | "It is 3\n" 38 | ] 39 | } 40 | ], 41 | "source": [ 42 | "x = 3\n", 43 | "\n", 44 | "if x == 1:\n", 45 | " print 'It is 1'\n", 46 | "elif x == 2:\n", 47 | " print 'It is 2'\n", 48 | "else:\n", 49 | " print 'It is 3'" 50 | ] 51 | }, 52 | { 53 | "cell_type": "code", 54 | "execution_count": null, 55 | "metadata": { 56 | "collapsed": true 57 | }, 58 | "outputs": [], 59 | "source": [] 60 | } 61 | ], 62 | "metadata": { 63 | "kernelspec": { 64 | "display_name": "Python 2", 65 | "language": "python", 66 | "name": "python2" 67 | }, 68 | "language_info": { 69 | "codemirror_mode": { 70 | "name": "ipython", 71 | "version": 2 72 | }, 73 | "file_extension": ".py", 74 | "mimetype": "text/x-python", 75 | "name": "python", 76 | "nbconvert_exporter": "python", 77 | "pygments_lexer": "ipython2", 78 | "version": "2.7.11" 79 | } 80 | }, 81 | "nbformat": 4, 82 | "nbformat_minor": 0 83 | } 84 | -------------------------------------------------------------------------------- /Python Statements/List Comprehensions.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Comprehensions\n", 8 | "In addition to sequence operations and list methods, Python includes a more advanced operation called a list comprehension.\n", 9 | "List comprehensions allow us to build out lists using a different notation. You can think of it as essentially a one line for loop built inside of brackets." 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 1, 15 | "metadata": { 16 | "collapsed": false 17 | }, 18 | "outputs": [ 19 | { 20 | "name": "stdout", 21 | "output_type": "stream", 22 | "text": [ 23 | "w\n", 24 | "o\n", 25 | "r\n", 26 | "d\n" 27 | ] 28 | } 29 | ], 30 | "source": [ 31 | "#This is normal for loop\n", 32 | "l = \"word\"\n", 33 | "\n", 34 | "for x in l:\n", 35 | " print x" 36 | ] 37 | }, 38 | { 39 | "cell_type": "code", 40 | "execution_count": 2, 41 | "metadata": { 42 | "collapsed": true 43 | }, 44 | "outputs": [], 45 | "source": [ 46 | "#Grabs every character in string\n", 47 | "lst = [x for x in \"word\"]" 48 | ] 49 | }, 50 | { 51 | "cell_type": "code", 52 | "execution_count": 3, 53 | "metadata": { 54 | "collapsed": false 55 | }, 56 | "outputs": [ 57 | { 58 | "data": { 59 | "text/plain": [ 60 | "['w', 'o', 'r', 'd']" 61 | ] 62 | }, 63 | "execution_count": 3, 64 | "metadata": {}, 65 | "output_type": "execute_result" 66 | } 67 | ], 68 | "source": [ 69 | "lst" 70 | ] 71 | }, 72 | { 73 | "cell_type": "code", 74 | "execution_count": 4, 75 | "metadata": { 76 | "collapsed": true 77 | }, 78 | "outputs": [], 79 | "source": [ 80 | "#Squares every number in given range\n", 81 | "sqr = [y**2 for y in range(0,11)]" 82 | ] 83 | }, 84 | { 85 | "cell_type": "code", 86 | "execution_count": 5, 87 | "metadata": { 88 | "collapsed": false 89 | }, 90 | "outputs": [ 91 | { 92 | "data": { 93 | "text/plain": [ 94 | "[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100]" 95 | ] 96 | }, 97 | "execution_count": 5, 98 | "metadata": {}, 99 | "output_type": "execute_result" 100 | } 101 | ], 102 | "source": [ 103 | "sqr" 104 | ] 105 | }, 106 | { 107 | "cell_type": "code", 108 | "execution_count": 6, 109 | "metadata": { 110 | "collapsed": false 111 | }, 112 | "outputs": [ 113 | { 114 | "data": { 115 | "text/plain": [ 116 | "[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20]" 117 | ] 118 | }, 119 | "execution_count": 6, 120 | "metadata": {}, 121 | "output_type": "execute_result" 122 | } 123 | ], 124 | "source": [ 125 | "#All even numbers in given range\n", 126 | "evenNums = [z for z in range(0,21) if z%2==0]\n", 127 | "evenNums" 128 | ] 129 | }, 130 | { 131 | "cell_type": "code", 132 | "execution_count": 7, 133 | "metadata": { 134 | "collapsed": false 135 | }, 136 | "outputs": [ 137 | { 138 | "data": { 139 | "text/plain": [ 140 | "[0, 1, 16, 81, 256, 625, 1296, 2401, 4096, 6561, 10000]" 141 | ] 142 | }, 143 | "execution_count": 7, 144 | "metadata": {}, 145 | "output_type": "execute_result" 146 | } 147 | ], 148 | "source": [ 149 | "#We can also perform Nested List Comprehensions\n", 150 | "lst = [ x**2 for x in [x**2 for x in range(11)]]\n", 151 | "lst" 152 | ] 153 | } 154 | ], 155 | "metadata": { 156 | "kernelspec": { 157 | "display_name": "Python 2", 158 | "language": "python", 159 | "name": "python2" 160 | }, 161 | "language_info": { 162 | "codemirror_mode": { 163 | "name": "ipython", 164 | "version": 2 165 | }, 166 | "file_extension": ".py", 167 | "mimetype": "text/x-python", 168 | "name": "python", 169 | "nbconvert_exporter": "python", 170 | "pygments_lexer": "ipython2", 171 | "version": "2.7.11" 172 | } 173 | }, 174 | "nbformat": 4, 175 | "nbformat_minor": 0 176 | } 177 | -------------------------------------------------------------------------------- /Python Statements/Range.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": { 7 | "collapsed": false 8 | }, 9 | "outputs": [ 10 | { 11 | "data": { 12 | "text/plain": [ 13 | "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]" 14 | ] 15 | }, 16 | "execution_count": 1, 17 | "metadata": {}, 18 | "output_type": "execute_result" 19 | } 20 | ], 21 | "source": [ 22 | "range(0,10)" 23 | ] 24 | }, 25 | { 26 | "cell_type": "code", 27 | "execution_count": 2, 28 | "metadata": { 29 | "collapsed": true 30 | }, 31 | "outputs": [], 32 | "source": [ 33 | "x = range(0,10)" 34 | ] 35 | }, 36 | { 37 | "cell_type": "code", 38 | "execution_count": 3, 39 | "metadata": { 40 | "collapsed": false 41 | }, 42 | "outputs": [ 43 | { 44 | "data": { 45 | "text/plain": [ 46 | "list" 47 | ] 48 | }, 49 | "execution_count": 3, 50 | "metadata": {}, 51 | "output_type": "execute_result" 52 | } 53 | ], 54 | "source": [ 55 | "type(x)" 56 | ] 57 | }, 58 | { 59 | "cell_type": "code", 60 | "execution_count": 4, 61 | "metadata": { 62 | "collapsed": true 63 | }, 64 | "outputs": [], 65 | "source": [ 66 | "start=0\n", 67 | "stop = 26" 68 | ] 69 | }, 70 | { 71 | "cell_type": "code", 72 | "execution_count": 17, 73 | "metadata": { 74 | "collapsed": true 75 | }, 76 | "outputs": [], 77 | "source": [ 78 | "y = range(start,stop,2)" 79 | ] 80 | }, 81 | { 82 | "cell_type": "code", 83 | "execution_count": 18, 84 | "metadata": { 85 | "collapsed": false 86 | }, 87 | "outputs": [ 88 | { 89 | "data": { 90 | "text/plain": [ 91 | "[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24]" 92 | ] 93 | }, 94 | "execution_count": 18, 95 | "metadata": {}, 96 | "output_type": "execute_result" 97 | } 98 | ], 99 | "source": [ 100 | "y" 101 | ] 102 | }, 103 | { 104 | "cell_type": "markdown", 105 | "metadata": {}, 106 | "source": [ 107 | "You might have been wondering, what happens if I want to use a huge range of numbers? Can my computer store that all in memory?\n", 108 | "Great thinking! This is a dilemma that can be solve with the use of a generator. For a simplified explanation: A generator allows the generation of generated objects that are provided at that instance but does not store every instance generated into memory.\n", 109 | "This means a generator would not create a list to generate like range() does, but instead provide a one time generation of the numbers in that range. Python 2 has a built-in range generator called xrange(). It is recommended to use xrange() for for loops in Python 2.\n", 110 | "The good news is in Python 3, range() behaves as a generator and you don't need to worry about it. Let's see a quick example with xrange()" 111 | ] 112 | }, 113 | { 114 | "cell_type": "markdown", 115 | "metadata": {}, 116 | "source": [ 117 | "So the main takeaway here is for Python 2, if you are using range() in a way that you don't need to save the results in a list, use xrange() instead. For Python 3, use range() in any instance.\n", 118 | "You should now have a good understanding of how to use range() in either version of Python." 119 | ] 120 | }, 121 | { 122 | "cell_type": "code", 123 | "execution_count": null, 124 | "metadata": { 125 | "collapsed": true 126 | }, 127 | "outputs": [], 128 | "source": [] 129 | } 130 | ], 131 | "metadata": { 132 | "kernelspec": { 133 | "display_name": "Python 2", 134 | "language": "python", 135 | "name": "python2" 136 | }, 137 | "language_info": { 138 | "codemirror_mode": { 139 | "name": "ipython", 140 | "version": 2 141 | }, 142 | "file_extension": ".py", 143 | "mimetype": "text/x-python", 144 | "name": "python", 145 | "nbconvert_exporter": "python", 146 | "pygments_lexer": "ipython2", 147 | "version": "2.7.11" 148 | } 149 | }, 150 | "nbformat": 4, 151 | "nbformat_minor": 0 152 | } 153 | -------------------------------------------------------------------------------- /Python Statements/While Loops.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": { 7 | "collapsed": false 8 | }, 9 | "outputs": [ 10 | { 11 | "name": "stdout", 12 | "output_type": "stream", 13 | "text": [ 14 | "1\n", 15 | "2\n", 16 | "3\n", 17 | "4\n", 18 | "5\n", 19 | "6\n", 20 | "7\n", 21 | "8\n", 22 | "9\n" 23 | ] 24 | } 25 | ], 26 | "source": [ 27 | "x = 1\n", 28 | "\n", 29 | "while x < 10:\n", 30 | " print x\n", 31 | " x += 1" 32 | ] 33 | }, 34 | { 35 | "cell_type": "code", 36 | "execution_count": null, 37 | "metadata": { 38 | "collapsed": true 39 | }, 40 | "outputs": [], 41 | "source": [ 42 | "y = 1\n", 43 | "\n", 44 | "while y < 20:\n", 45 | " print y\n", 46 | " y += 1" 47 | ] 48 | }, 49 | { 50 | "cell_type": "code", 51 | "execution_count": null, 52 | "metadata": { 53 | "collapsed": true 54 | }, 55 | "outputs": [], 56 | "source": [] 57 | } 58 | ], 59 | "metadata": { 60 | "kernelspec": { 61 | "display_name": "Python 2", 62 | "language": "python", 63 | "name": "python2" 64 | }, 65 | "language_info": { 66 | "codemirror_mode": { 67 | "name": "ipython", 68 | "version": 2 69 | }, 70 | "file_extension": ".py", 71 | "mimetype": "text/x-python", 72 | "name": "python", 73 | "nbconvert_exporter": "python", 74 | "pygments_lexer": "ipython2", 75 | "version": "2.7.11" 76 | } 77 | }, 78 | "nbformat": 4, 79 | "nbformat_minor": 0 80 | } 81 | -------------------------------------------------------------------------------- /Python Statements/for Loops.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Problem 1: Iterating through a List" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 1, 13 | "metadata": { 14 | "collapsed": false 15 | }, 16 | "outputs": [ 17 | { 18 | "name": "stdout", 19 | "output_type": "stream", 20 | "text": [ 21 | "1\n", 22 | "2\n", 23 | "3\n", 24 | "4\n", 25 | "5\n", 26 | "6\n", 27 | "7\n", 28 | "8\n", 29 | "9\n" 30 | ] 31 | } 32 | ], 33 | "source": [ 34 | "p = [1,2,3,4,5,6,7,8,9]\n", 35 | "\n", 36 | "for i in p:\n", 37 | " print i" 38 | ] 39 | }, 40 | { 41 | "cell_type": "markdown", 42 | "metadata": {}, 43 | "source": [ 44 | "# Problem 2: Printing Even Numbers" 45 | ] 46 | }, 47 | { 48 | "cell_type": "code", 49 | "execution_count": 2, 50 | "metadata": { 51 | "collapsed": false 52 | }, 53 | "outputs": [ 54 | { 55 | "name": "stdout", 56 | "output_type": "stream", 57 | "text": [ 58 | "2\n", 59 | "4\n", 60 | "6\n", 61 | "8\n" 62 | ] 63 | } 64 | ], 65 | "source": [ 66 | "for j in p:\n", 67 | " if j%2 == 0:\n", 68 | " print j" 69 | ] 70 | }, 71 | { 72 | "cell_type": "markdown", 73 | "metadata": {}, 74 | "source": [ 75 | "# Problem 3: Printing Odd Numbers" 76 | ] 77 | }, 78 | { 79 | "cell_type": "code", 80 | "execution_count": 4, 81 | "metadata": { 82 | "collapsed": false 83 | }, 84 | "outputs": [ 85 | { 86 | "name": "stdout", 87 | "output_type": "stream", 88 | "text": [ 89 | "1\n", 90 | "3\n", 91 | "5\n", 92 | "7\n", 93 | "9\n" 94 | ] 95 | } 96 | ], 97 | "source": [ 98 | "for k in p:\n", 99 | " if k%2 != 0:\n", 100 | " print k" 101 | ] 102 | }, 103 | { 104 | "cell_type": "markdown", 105 | "metadata": {}, 106 | "source": [ 107 | "For Loop in Python is powerful and it is often used to iterate over collections like tuples, Dictionaries and Lists." 108 | ] 109 | } 110 | ], 111 | "metadata": { 112 | "kernelspec": { 113 | "display_name": "Python 2", 114 | "language": "python", 115 | "name": "python2" 116 | }, 117 | "language_info": { 118 | "codemirror_mode": { 119 | "name": "ipython", 120 | "version": 2 121 | }, 122 | "file_extension": ".py", 123 | "mimetype": "text/x-python", 124 | "name": "python", 125 | "nbconvert_exporter": "python", 126 | "pygments_lexer": "ipython2", 127 | "version": "2.7.11" 128 | } 129 | }, 130 | "nbformat": 4, 131 | "nbformat_minor": 0 132 | } 133 | -------------------------------------------------------------------------------- /Python Statements/if, elif and else.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 3, 6 | "metadata": { 7 | "collapsed": false 8 | }, 9 | "outputs": [ 10 | { 11 | "name": "stdout", 12 | "output_type": "stream", 13 | "text": [ 14 | "It is not\n" 15 | ] 16 | } 17 | ], 18 | "source": [ 19 | "x = False;\n", 20 | "if x == True:\n", 21 | " print 'It is True'\n", 22 | "else:\n", 23 | " print 'It is not'" 24 | ] 25 | }, 26 | { 27 | "cell_type": "code", 28 | "execution_count": 5, 29 | "metadata": { 30 | "collapsed": false 31 | }, 32 | "outputs": [ 33 | { 34 | "name": "stdout", 35 | "output_type": "stream", 36 | "text": [ 37 | "It is 3\n" 38 | ] 39 | } 40 | ], 41 | "source": [ 42 | "x = 3\n", 43 | "\n", 44 | "if x == 1:\n", 45 | " print 'It is 1'\n", 46 | "elif x == 2:\n", 47 | " print 'It is 2'\n", 48 | "else:\n", 49 | " print 'It is 3'" 50 | ] 51 | }, 52 | { 53 | "cell_type": "code", 54 | "execution_count": null, 55 | "metadata": { 56 | "collapsed": true 57 | }, 58 | "outputs": [], 59 | "source": [] 60 | } 61 | ], 62 | "metadata": { 63 | "kernelspec": { 64 | "display_name": "Python 2", 65 | "language": "python", 66 | "name": "python2" 67 | }, 68 | "language_info": { 69 | "codemirror_mode": { 70 | "name": "ipython", 71 | "version": 2 72 | }, 73 | "file_extension": ".py", 74 | "mimetype": "text/x-python", 75 | "name": "python", 76 | "nbconvert_exporter": "python", 77 | "pygments_lexer": "ipython2", 78 | "version": "2.7.11" 79 | } 80 | }, 81 | "nbformat": 4, 82 | "nbformat_minor": 0 83 | } 84 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Complete-Python-Bootcamp 2 | Jose Portilla Python Bootcamp on Udemy 3 | 4 | ## Overview 5 | This is the most comprehensive, yet straight-forward, course for the Python programming language on Udemy! Whether you have never programmed before, already know basic syntax, or want to learn about the advanced features of Python, this course is for you! In this course we will teach you both versions of Python (2 and 3) so you can easily adapt your skill set to either version! 6 | 7 | With over 100 lectures and more than 10 hours of video this comprehensive course leaves no stone unturned! This course includes quizzes,tests, and homework assignments as well as 3 major projects to create a Python project portfolio! 8 | 9 | This course will teach you Python in a practical manner, with every lecture comes a full coding screencast and a corresponding code notebook! Learn in whatever manner is best for you! 10 | 11 | You will get lifetime access to over 100 lectures plus corresponding Notebooks for the lectures! This includes future updates covering Flask development and GUI creation! 12 | 13 | ## Prereqs 14 | Access to a computer with an internet connection. 15 | 16 | ## Take Aways 17 | Use Python to automate tasks! 18 | Use Python to solve real world problems! 19 | Use Python to complete your own projects! 20 | Use Python to create games! 21 | 22 | ## Target Audience 23 | Beginners who have never programmed before. 24 | Programmers switching languages to Python. 25 | Intermediate Python programmers who want to level up their skills! 26 | 27 | ## About the Instructor 28 | Jose Marcial Portilla has a BS and MS in Mechanical Engineering from Santa Clara University and years of experience as a professional instructor and trainer for Data Science and programming. He has publications and patents in various fields such as microfluidics, materials science, and data science technologies. Over the course of his career he has developed a skill set in analyzing data and he hopes to use his experience in teaching and data science to help other people learn the power of programming the ability to analyze data, as well as present the data in clear and beautiful visualizations. Currently he works as the Head of Data Science for Pierian Data Inc. and provides in-person data science and python training courses to a variety of companies all over the world, including top banks such as Credit Suisse. Feel free to contact him on LinkedIn for more information on in-person training sessions. 29 | --------------------------------------------------------------------------------