From 4ffb1a1891e6c37484c9925801d30cc274e823df Mon Sep 17 00:00:00 2001 From: cif2cif Date: Mon, 15 Feb 2016 09:01:37 +0100 Subject: [PATCH] Updated to 2016 and clean cells --- python/1_0_Intro_Python.ipynb | 7 +- python/1_1_Notebooks.ipynb | 6 +- python/1_2_Numbers_Strings.ipynb | 642 ++++------------------------ python/1_3_Sequences.ipynb | 600 ++++---------------------- python/1_4_Sets.ipynb | 340 +++------------ python/1_5_ControlFlow.ipynb | 301 ++----------- python/1_6_Functions.ipynb | 192 ++------- python/1_7_Variables.ipynb | 4 +- python/1_8_Classes.ipynb | 92 +--- python/1_9_Errors_Exceptions.ipynb | 117 +---- python/1__10_Modules_Packages.ipynb | 65 +-- 11 files changed, 353 insertions(+), 2013 deletions(-) diff --git a/python/1_0_Intro_Python.ipynb b/python/1_0_Intro_Python.ipynb index 0a00b21..83f5840 100644 --- a/python/1_0_Intro_Python.ipynb +++ b/python/1_0_Intro_Python.ipynb @@ -18,7 +18,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Department of Telematic Engineering Systems, Universidad Politécnica de Madrid, © 2015 Carlos A. Iglesias" + "Department of Telematic Engineering Systems, Universidad Politécnica de Madrid, © 2016 Carlos A. Iglesias" ] }, { @@ -57,8 +57,7 @@ " 4. [Variables](1_7_Variables.ipynb)\n", " 5. [Classes](1_8_Classes.ipynb)\n", " 6. [Errors and Exceptions](1_9_Errors_Exceptions.ipynb)\n", - " 7. [Modules and Packages](1__10_Modules_Packages.ipynb)\n", - " 8. Testing / Generadores / Decoradores" + " 7. [Modules and Packages](1__10_Modules_Packages.ipynb)" ] }, { @@ -96,7 +95,7 @@ "source": [ "The notebook is freely licensed under under the [Creative Commons Attribution Share-Alike license](https://creativecommons.org/licenses/by/2.0/). \n", "\n", - "© 2015 Carlos A. Iglesias, Universidad Politécnica de Madrid." + "© 2016 Carlos A. Iglesias, Universidad Politécnica de Madrid." ] } ], diff --git a/python/1_1_Notebooks.ipynb b/python/1_1_Notebooks.ipynb index 177fb93..2292ce9 100644 --- a/python/1_1_Notebooks.ipynb +++ b/python/1_1_Notebooks.ipynb @@ -18,7 +18,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Department of Telematic Engineering Systems, Universidad Politécnica de Madrid, © 2015 Carlos A. Iglesias" + "Department of Telematic Engineering Systems, Universidad Politécnica de Madrid, © 2016 Carlos A. Iglesias" ] }, { @@ -160,7 +160,7 @@ "source": [ "The notebook is freely licensed under under the [Creative Commons Attribution Share-Alike license](https://creativecommons.org/licenses/by/2.0/). \n", "\n", - "© 2015 Carlos A. Iglesias, Universidad Politécnica de Madrid." + "© 2016 Carlos A. Iglesias, Universidad Politécnica de Madrid." ] } ], @@ -180,7 +180,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.4.3+" + "version": "3.5.1" } }, "nbformat": 4, diff --git a/python/1_2_Numbers_Strings.ipynb b/python/1_2_Numbers_Strings.ipynb index 8094151..9544acb 100644 --- a/python/1_2_Numbers_Strings.ipynb +++ b/python/1_2_Numbers_Strings.ipynb @@ -18,7 +18,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Department of Telematic Engineering Systems, Universidad Politécnica de Madrid, © 2015 Carlos A. Iglesias" + "Department of Telematic Engineering Systems, Universidad Politécnica de Madrid, © 2016 Carlos A. Iglesias" ] }, { @@ -48,66 +48,33 @@ }, { "cell_type": "code", - "execution_count": 52, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "False" - ] - }, - "execution_count": 52, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "True and False # operations with booleans" ] }, { "cell_type": "code", - "execution_count": 53, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "False" - ] - }, - "execution_count": 53, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "not True" ] }, { "cell_type": "code", - "execution_count": 54, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "True" - ] - }, - "execution_count": 54, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "True or False" ] @@ -132,264 +99,132 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "4" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "2 + 2 # 2 plus 2 (integers)" ] }, { "cell_type": "code", - "execution_count": 4, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "6.0" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "2.0 * 3.0 # 2.0 times 3.0 (floats)" ] }, { "cell_type": "code", - "execution_count": 6, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "16.0" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "2.0 ** 4.0 # 2.0 to the power of 4 (float)" ] }, { "cell_type": "code", - "execution_count": 8, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "(8+9j)" - ] - }, - "execution_count": 8, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "(3 + 4j) + (5 + 5j) #add two complex numbers" ] }, { "cell_type": "code", - "execution_count": 17, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "3.3333333333333335" - ] - }, - "execution_count": 17, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "10 / 3 # classic division" ] }, { "cell_type": "code", - "execution_count": 18, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "3" - ] - }, - "execution_count": 18, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "10 // 3 # floor division" ] }, { "cell_type": "code", - "execution_count": 20, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "1" - ] - }, - "execution_count": 20, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "10 % 3 # remainder" ] }, { "cell_type": "code", - "execution_count": 25, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "inf" - ] - }, - "execution_count": 25, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "10e158*17e158 #overflow shown as 'inf', infinitive" ] }, { "cell_type": "code", - "execution_count": 21, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "int" - ] - }, - "execution_count": 21, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "type(10)" ] }, { "cell_type": "code", - "execution_count": 22, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "complex" - ] - }, - "execution_count": 22, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "type(2 + 3j)" ] }, { "cell_type": "code", - "execution_count": 23, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "float" - ] - }, - "execution_count": 23, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "type(2.1)" ] }, { "cell_type": "code", - "execution_count": 24, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "float" - ] - }, - "execution_count": 24, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "type(2E3)" ] @@ -414,110 +249,55 @@ }, { "cell_type": "code", - "execution_count": 26, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "'This is a string'" - ] - }, - "execution_count": 26, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "\"This is a string\"" ] }, { "cell_type": "code", - "execution_count": 27, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "'This is also a string'" - ] - }, - "execution_count": 27, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "'This is also a string'" ] }, { "cell_type": "code", - "execution_count": 29, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "\"This is a string containing single quotes 'hi'\"" - ] - }, - "execution_count": 29, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "\"This is a string containing single quotes 'hi'\"" ] }, { "cell_type": "code", - "execution_count": 30, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "'This is string containing double quotes \"hi\"'" - ] - }, - "execution_count": 30, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "'This is string containing double quotes \"hi\"'" ] }, { "cell_type": "code", - "execution_count": 48, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "'This is \\n\\na long string\\n\\n'" - ] - }, - "execution_count": 48, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "'''This is \n", "\n", @@ -536,88 +316,44 @@ }, { "cell_type": "code", - "execution_count": 50, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "'String with special characters: \\n newline, \\x07 beep and \\\\ slash'" - ] - }, - "execution_count": 50, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "\"String with special characters: \\n newline, \\a beep and \\\\ slash\"" ] }, { "cell_type": "code", - "execution_count": 42, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "'concatenate two strings'" - ] - }, - "execution_count": 42, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "\"concatenate \" + \"two strings\" #use of '+' for concatenating two strings" ] }, { "cell_type": "code", - "execution_count": 31, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "4" - ] - }, - "execution_count": 31, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "len('hola') # length of a string" ] }, { "cell_type": "code", - "execution_count": 51, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "str" - ] - }, - "execution_count": 51, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "type(\"hola\")" ] @@ -631,7 +367,7 @@ }, { "cell_type": "code", - "execution_count": 34, + "execution_count": null, "metadata": { "collapsed": true }, @@ -642,110 +378,55 @@ }, { "cell_type": "code", - "execution_count": 33, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "'hola'" - ] - }, - "execution_count": 33, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "s # get the value of s" ] }, { "cell_type": "code", - "execution_count": 35, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "'h'" - ] - }, - "execution_count": 35, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "s[0]" ] }, { "cell_type": "code", - "execution_count": 36, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "'o'" - ] - }, - "execution_count": 36, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "s[1]" ] }, { "cell_type": "code", - "execution_count": 37, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "'a'" - ] - }, - "execution_count": 37, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "s[3]" ] }, { "cell_type": "code", - "execution_count": 38, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "'a'" - ] - }, - "execution_count": 38, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "s [-1] # we can start from the beginning (index 0, 1, 2, ...) or from the last position (-1, -2, ...)" ] @@ -759,110 +440,55 @@ }, { "cell_type": "code", - "execution_count": 56, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "'ho'" - ] - }, - "execution_count": 56, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "s[0:2] #slice [0,2)" ] }, { "cell_type": "code", - "execution_count": 57, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "'ho'" - ] - }, - "execution_count": 57, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "s[:2] #slice [0,2)" ] }, { "cell_type": "code", - "execution_count": 58, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "'hola'" - ] - }, - "execution_count": 58, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "s[:] #slice [0, len(s)]" ] }, { "cell_type": "code", - "execution_count": 59, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "'ho'" - ] - }, - "execution_count": 59, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "s[:-2]" ] }, { "cell_type": "code", - "execution_count": 61, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "'ho'" - ] - }, - "execution_count": 61, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "s[-4:-2]" ] @@ -880,7 +506,7 @@ }, { "cell_type": "code", - "execution_count": 72, + "execution_count": null, "metadata": { "collapsed": true }, @@ -891,88 +517,44 @@ }, { "cell_type": "code", - "execution_count": 73, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "'This is a string'" - ] - }, - "execution_count": 73, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "se[::1] # moves from 0 to len, and the index is incremented by 1" ] }, { "cell_type": "code", - "execution_count": 80, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "'Ti sasr'" - ] - }, - "execution_count": 80, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "se[0:14:2] #take the even indexed characters from 0 to 14" ] }, { "cell_type": "code", - "execution_count": 93, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "'gnirts a si sihT'" - ] - }, - "execution_count": 93, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "se[::-1] #reverse the string" ] }, { "cell_type": "code", - "execution_count": 92, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "'gnirts a si'" - ] - }, - "execution_count": 92, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "se[:4:-1]" ] @@ -986,7 +568,7 @@ }, { "cell_type": "code", - "execution_count": 97, + "execution_count": null, "metadata": { "collapsed": true }, @@ -997,22 +579,11 @@ }, { "cell_type": "code", - "execution_count": 101, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "'This is a string plus This is a string plus bbb'" - ] - }, - "execution_count": 101, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "se + \" plus \" + se + \" plus \"+ a*3" ] @@ -1028,66 +599,33 @@ }, { "cell_type": "code", - "execution_count": 102, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "'hola'" - ] - }, - "execution_count": 102, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "s.lower()" ] }, { "cell_type": "code", - "execution_count": 103, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "'HOLA'" - ] - }, - "execution_count": 103, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "s.upper()" ] }, { "cell_type": "code", - "execution_count": 106, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "['h', 'la']" - ] - }, - "execution_count": 106, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "s.split('o') # splits String " ] @@ -1143,7 +681,7 @@ "source": [ "The notebook is freely licensed under under the [Creative Commons Attribution Share-Alike license](https://creativecommons.org/licenses/by/2.0/). \n", "\n", - "© 2015 Carlos A. Iglesias, Universidad Politécnica de Madrid." + "© 2016 Carlos A. Iglesias, Universidad Politécnica de Madrid." ] } ], @@ -1163,7 +701,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.4.3+" + "version": "3.5.1" } }, "nbformat": 4, diff --git a/python/1_3_Sequences.ipynb b/python/1_3_Sequences.ipynb index e2cf863..6570794 100644 --- a/python/1_3_Sequences.ipynb +++ b/python/1_3_Sequences.ipynb @@ -18,7 +18,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Department of Telematic Engineering Systems, Universidad Politécnica de Madrid, © 2015 Carlos A. Iglesias" + "Department of Telematic Engineering Systems, Universidad Politécnica de Madrid, © 2016 Carlos A. Iglesias" ] }, { @@ -51,7 +51,7 @@ }, { "cell_type": "code", - "execution_count": 137, + "execution_count": null, "metadata": { "collapsed": false }, @@ -62,51 +62,29 @@ }, { "cell_type": "code", - "execution_count": 138, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "[1, 2, 3, 4, 5, 6]" - ] - }, - "execution_count": 138, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "l" ] }, { "cell_type": "code", - "execution_count": 139, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "[1, 2, 3]" - ] - }, - "execution_count": 139, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "l[0:3] # we can use slicing in sequence types" ] }, { "cell_type": "code", - "execution_count": 140, + "execution_count": null, "metadata": { "collapsed": true }, @@ -117,95 +95,51 @@ }, { "cell_type": "code", - "execution_count": 141, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "[1, 0.0, 'hola']" - ] - }, - "execution_count": 141, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "other_list" ] }, { "cell_type": "code", - "execution_count": 142, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "[1, 2, 3, 4, 5, 6, 1, 0.0, 'hola']" - ] - }, - "execution_count": 142, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "l + other_list # we can add lists (append)" ] }, { "cell_type": "code", - "execution_count": 143, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "[1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6]" - ] - }, - "execution_count": 143, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "l * 3 # we can add n times a list" ] }, { "cell_type": "code", - "execution_count": 144, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "6" - ] - }, - "execution_count": 144, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "len(l) # length of a list (as Strings)" ] }, { "cell_type": "code", - "execution_count": 145, + "execution_count": null, "metadata": { "collapsed": true }, @@ -216,110 +150,55 @@ }, { "cell_type": "code", - "execution_count": 146, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "[1, 2, 3, 4, 5, 6, 7]" - ] - }, - "execution_count": 146, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "l" ] }, { "cell_type": "code", - "execution_count": 147, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "7" - ] - }, - "execution_count": 147, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "l.pop() # remove last element" ] }, { "cell_type": "code", - "execution_count": 148, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "[1, 2, 3, 4, 5, 6]" - ] - }, - "execution_count": 148, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "l" ] }, { "cell_type": "code", - "execution_count": 149, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "3" - ] - }, - "execution_count": 149, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "l.pop(2) # remove element at index 2" ] }, { "cell_type": "code", - "execution_count": 150, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "[1, 2, 4, 5, 6]" - ] - }, - "execution_count": 150, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "l" ] @@ -335,7 +214,7 @@ }, { "cell_type": "code", - "execution_count": 151, + "execution_count": null, "metadata": { "collapsed": false }, @@ -346,29 +225,18 @@ }, { "cell_type": "code", - "execution_count": 152, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "[1, 2, 3, 4, 5, 6]" - ] - }, - "execution_count": 152, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "l" ] }, { "cell_type": "code", - "execution_count": 153, + "execution_count": null, "metadata": { "collapsed": true }, @@ -379,29 +247,18 @@ }, { "cell_type": "code", - "execution_count": 154, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "[6, 5, 4, 3, 2, 1]" - ] - }, - "execution_count": 154, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "l" ] }, { "cell_type": "code", - "execution_count": 155, + "execution_count": null, "metadata": { "collapsed": true }, @@ -412,29 +269,18 @@ }, { "cell_type": "code", - "execution_count": 156, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "[1, 2, 3, 4, 5, 6]" - ] - }, - "execution_count": 156, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "l" ] }, { "cell_type": "code", - "execution_count": 157, + "execution_count": null, "metadata": { "collapsed": true }, @@ -445,29 +291,18 @@ }, { "cell_type": "code", - "execution_count": 158, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "[1, 2, 4, 5, 6]" - ] - }, - "execution_count": 158, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "l" ] }, { "cell_type": "code", - "execution_count": 159, + "execution_count": null, "metadata": { "collapsed": false }, @@ -478,140 +313,73 @@ }, { "cell_type": "code", - "execution_count": 160, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "[0, 2, 4, 5, 6]" - ] - }, - "execution_count": 160, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "l" ] }, { "cell_type": "code", - "execution_count": 161, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "True" - ] - }, - "execution_count": 161, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "2 in l # check if an element is in a list" ] }, { "cell_type": "code", - "execution_count": 162, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "False" - ] - }, - "execution_count": 162, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "7 in l # check if an element is in a list " ] }, { "cell_type": "code", - "execution_count": 163, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "False" - ] - }, - "execution_count": 163, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "4 not in l # check if an element is not in a list" ] }, { "cell_type": "code", - "execution_count": 164, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "2" - ] - }, - "execution_count": 164, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "l.index(4) # search for an item" ] }, { "cell_type": "code", - "execution_count": 165, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "ename": "ValueError", - "evalue": "-1 is not in list", - "output_type": "error", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", - "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0ml\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m-\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;31m# search for an item, error since it is not in the list\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[1;31mValueError\u001b[0m: -1 is not in list" - ] - } - ], + "outputs": [], "source": [ "l.index(-1) # search for an item, error since it is not in the list" ] }, { "cell_type": "code", - "execution_count": 166, + "execution_count": null, "metadata": { "collapsed": false }, @@ -622,88 +390,44 @@ }, { "cell_type": "code", - "execution_count": 167, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "[[1, 2], [3, 4]]" - ] - }, - "execution_count": 167, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "matrix" ] }, { "cell_type": "code", - "execution_count": 168, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "1" - ] - }, - "execution_count": 168, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "matrix[0][0]" ] }, { "cell_type": "code", - "execution_count": 169, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "2" - ] - }, - "execution_count": 169, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "matrix[0][1]" ] }, { "cell_type": "code", - "execution_count": 170, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "list" - ] - }, - "execution_count": 170, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "type(matrix)" ] @@ -730,7 +454,7 @@ }, { "cell_type": "code", - "execution_count": 171, + "execution_count": null, "metadata": { "collapsed": false }, @@ -741,22 +465,11 @@ }, { "cell_type": "code", - "execution_count": 172, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "('a', 1)" - ] - }, - "execution_count": 172, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "tuple" ] @@ -772,133 +485,66 @@ }, { "cell_type": "code", - "execution_count": 173, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "(1, 'a')" - ] - }, - "execution_count": 173, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "tuple[::-1]" ] }, { "cell_type": "code", - "execution_count": 174, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "2" - ] - }, - "execution_count": 174, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "len(tuple)" ] }, { "cell_type": "code", - "execution_count": 175, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "('a', 1, 'a', 1, 'b', 'c', 2.1, True)" - ] - }, - "execution_count": 175, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "tuple * 2 + ('b', 'c', 2.1, True)" ] }, { "cell_type": "code", - "execution_count": 176, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "1" - ] - }, - "execution_count": 176, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "tuple[1]" ] }, { "cell_type": "code", - "execution_count": 177, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "ename": "TypeError", - "evalue": "'tuple' object does not support item assignment", - "output_type": "error", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", - "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mtuple\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m2\u001b[0m \u001b[1;31m# Error, tuples are inmutable\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[1;31mTypeError\u001b[0m: 'tuple' object does not support item assignment" - ] - } - ], + "outputs": [], "source": [ "tuple[1] = 2 # Error, tuples are inmutable" ] }, { "cell_type": "code", - "execution_count": 178, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "tuple" - ] - }, - "execution_count": 178, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "type(tuple)" ] @@ -923,7 +569,7 @@ }, { "cell_type": "code", - "execution_count": 179, + "execution_count": null, "metadata": { "collapsed": false }, @@ -934,132 +580,66 @@ }, { "cell_type": "code", - "execution_count": 180, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "range(0, 10)" - ] - }, - "execution_count": 180, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "r" ] }, { "cell_type": "code", - "execution_count": 181, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "True" - ] - }, - "execution_count": 181, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "5 in r # check if a number is in a range" ] }, { "cell_type": "code", - "execution_count": 182, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "2" - ] - }, - "execution_count": 182, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "r[2] # Get a value" ] }, { "cell_type": "code", - "execution_count": 183, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "range" - ] - }, - "execution_count": 183, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "type(r)" ] }, { "cell_type": "code", - "execution_count": 184, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]" - ] - }, - "execution_count": 184, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "list(range(10))" ] }, { "cell_type": "code", - "execution_count": 185, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "[1, 3, 5, 7, 9]" - ] - }, - "execution_count": 185, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "list(range(1,10,2))" ] @@ -1077,7 +657,7 @@ "source": [ "The notebook is freely licensed under under the [Creative Commons Attribution Share-Alike license](https://creativecommons.org/licenses/by/2.0/). \n", "\n", - "© 2015 Carlos A. Iglesias, Universidad Politécnica de Madrid." + "© 2016 Carlos A. Iglesias, Universidad Politécnica de Madrid." ] } ], @@ -1097,7 +677,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.4.3+" + "version": "3.5.1" } }, "nbformat": 4, diff --git a/python/1_4_Sets.ipynb b/python/1_4_Sets.ipynb index 91093dc..38d5fd5 100644 --- a/python/1_4_Sets.ipynb +++ b/python/1_4_Sets.ipynb @@ -18,7 +18,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Department of Telematic Engineering Systems, Universidad Politécnica de Madrid, © 2015 Carlos A. Iglesias" + "Department of Telematic Engineering Systems, Universidad Politécnica de Madrid, © 2016 Carlos A. Iglesias" ] }, { @@ -51,22 +51,11 @@ }, { "cell_type": "code", - "execution_count": 413, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "set()" - ] - }, - "execution_count": 413, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "my_set = set() #create a set\n", "my_set" @@ -74,22 +63,11 @@ }, { "cell_type": "code", - "execution_count": 414, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "{1}" - ] - }, - "execution_count": 414, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "my_set.add(1) # add an element\n", "my_set" @@ -97,7 +75,7 @@ }, { "cell_type": "code", - "execution_count": 415, + "execution_count": null, "metadata": { "collapsed": false }, @@ -108,44 +86,22 @@ }, { "cell_type": "code", - "execution_count": 416, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "{1, 2}" - ] - }, - "execution_count": 416, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "my_set" ] }, { "cell_type": "code", - "execution_count": 417, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "{1, 2, 3}" - ] - }, - "execution_count": 417, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "my_set.add(3) # add another one\n", "my_set" @@ -153,22 +109,11 @@ }, { "cell_type": "code", - "execution_count": 418, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "{1, 2, 3}" - ] - }, - "execution_count": 418, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "my_set.add(1) #try to add a repeated element\n", "my_set" @@ -176,22 +121,11 @@ }, { "cell_type": "code", - "execution_count": 419, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}" - ] - }, - "execution_count": 419, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "s2 = set(range(10)) # we can create a set from a range\n", "s2" @@ -199,7 +133,7 @@ }, { "cell_type": "code", - "execution_count": 420, + "execution_count": null, "metadata": { "collapsed": false }, @@ -210,22 +144,11 @@ }, { "cell_type": "code", - "execution_count": 421, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "{'a', 'b', 'c'}" - ] - }, - "execution_count": 421, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "s3 = set(l) # if we create a set from a list, elements are not repeated\n", "s3" @@ -233,88 +156,44 @@ }, { "cell_type": "code", - "execution_count": 422, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "3" - ] - }, - "execution_count": 422, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "len(s3) " ] }, { "cell_type": "code", - "execution_count": 423, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "{0, 1, 2, 3, 4, 5, 'c', 6, 7, 8, 9, 'a', 'b'}" - ] - }, - "execution_count": 423, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "s3.union(s2) # we can use set methods: union(), intersection(), difference(), ..." ] }, { "cell_type": "code", - "execution_count": 424, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "True" - ] - }, - "execution_count": 424, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "3 in my_set #check membership" ] }, { "cell_type": "code", - "execution_count": 425, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "set" - ] - }, - "execution_count": 425, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "type(s3)" ] @@ -328,22 +207,11 @@ }, { "cell_type": "code", - "execution_count": 426, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "{'key1': 1, 'key2': 2, 'key3': 3}" - ] - }, - "execution_count": 426, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "my_dictionary = {'key1': 1, 'key2': 2, 'key3': 3} # pairs of key-value mappings\n", "my_dictionary" @@ -351,44 +219,22 @@ }, { "cell_type": "code", - "execution_count": 427, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "1" - ] - }, - "execution_count": 427, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "my_dictionary['key1'] #retrieve a value given a key" ] }, { "cell_type": "code", - "execution_count": 428, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "{'key1': 1, 'key2': 2, 'key3': 3}" - ] - }, - "execution_count": 428, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "my_dict = dict()\n", "my_dict['key1'] = 1\n", @@ -399,44 +245,22 @@ }, { "cell_type": "code", - "execution_count": 429, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "True" - ] - }, - "execution_count": 429, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "my_dict == my_dictionary # check if both dictionaries are equal" ] }, { "cell_type": "code", - "execution_count": 430, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "{'one': {'two': {'three': 'Nested dict'}}}" - ] - }, - "execution_count": 430, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "my_dict2 = {'one': {'two': {'three': 'Nested dict'}}} #nested dictionary\n", "my_dict2" @@ -444,22 +268,11 @@ }, { "cell_type": "code", - "execution_count": 431, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "'Nested dict'" - ] - }, - "execution_count": 431, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "my_dict2['one']['two']['three'] #access the value" ] @@ -475,110 +288,55 @@ }, { "cell_type": "code", - "execution_count": 473, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "dict_keys(['key2', 'key3', 'key1'])" - ] - }, - "execution_count": 473, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "my_dict.keys() # in Python3 we get a View object that changes when the dictionary changes" ] }, { "cell_type": "code", - "execution_count": 478, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "['key2', 'key3', 'key1']" - ] - }, - "execution_count": 478, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "list(my_dict.keys()) # we can convert it to a list, we see dicionaries are unordered" ] }, { "cell_type": "code", - "execution_count": 474, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "dict_values([2, 3, 1])" - ] - }, - "execution_count": 474, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "my_dict.values()" ] }, { "cell_type": "code", - "execution_count": 476, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "[2, 3, 1]" - ] - }, - "execution_count": 476, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "list(my_dict.values())" ] }, { "cell_type": "code", - "execution_count": 479, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "dict" - ] - }, - "execution_count": 479, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "type(my_dict)" ] @@ -596,7 +354,7 @@ "source": [ "The notebook is freely licensed under under the [Creative Commons Attribution Share-Alike license](https://creativecommons.org/licenses/by/2.0/). \n", "\n", - "© 2015 Carlos A. Iglesias, Universidad Politécnica de Madrid." + "© 2016 Carlos A. Iglesias, Universidad Politécnica de Madrid." ] } ], @@ -616,7 +374,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.4.3+" + "version": "3.5.1" } }, "nbformat": 4, diff --git a/python/1_5_ControlFlow.ipynb b/python/1_5_ControlFlow.ipynb index b3187f0..1ee82fa 100644 --- a/python/1_5_ControlFlow.ipynb +++ b/python/1_5_ControlFlow.ipynb @@ -18,7 +18,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Department of Telematic Engineering Systems, Universidad Politécnica de Madrid, © 2015 Carlos A. Iglesias" + "Department of Telematic Engineering Systems, Universidad Politécnica de Madrid, © 2016 Carlos A. Iglesias" ] }, { @@ -68,22 +68,11 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "9" - ] - }, - "execution_count": 22, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "import random # import random before using it\n", "x = random.randrange(1, 10) # generate a random integer between [1, 10] (both included)\n", @@ -92,19 +81,11 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Too small\n" - ] - } - ], + "outputs": [], "source": [ "# Execute several times in order the previous cell and this one\n", "if x < 5:\n", @@ -117,19 +98,11 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Too small\n" - ] - } - ], + "outputs": [], "source": [ "# Only one branch\n", "if x <= 5:\n", @@ -140,19 +113,11 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Too big\n" - ] - } - ], + "outputs": [], "source": [ "# Python has no switch statement for multiple branches\n", "if x <= 4:\n", @@ -181,28 +146,11 @@ }, { "cell_type": "code", - "execution_count": 27, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "0\n", - "1\n", - "2\n", - "3\n", - "4\n", - "5\n", - "6\n", - "7\n", - "8\n", - "9\n" - ] - } - ], + "outputs": [], "source": [ "# for with ranges\n", "for i in range(10): \n", @@ -211,22 +159,11 @@ }, { "cell_type": "code", - "execution_count": 29, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "1\n", - "2\n", - "3\n", - "4\n" - ] - } - ], + "outputs": [], "source": [ "# for with lists\n", "l = [1, 2, 3, 4]\n", @@ -236,21 +173,11 @@ }, { "cell_type": "code", - "execution_count": 30, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "1\n", - "a\n", - "b\n" - ] - } - ], + "outputs": [], "source": [ "# for with tuples\n", "t = (1, 'a', 'b')\n", @@ -260,21 +187,11 @@ }, { "cell_type": "code", - "execution_count": 31, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "k2\n", - "k1\n", - "k3\n" - ] - } - ], + "outputs": [], "source": [ "# for with dictionaries\n", "d = {'k1':'v1', 'k2':'v2', 'k3':'v3'}\n", @@ -284,21 +201,11 @@ }, { "cell_type": "code", - "execution_count": 34, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "k2 v2\n", - "k1 v1\n", - "k3 v3\n" - ] - } - ], + "outputs": [], "source": [ "# We get only the keys. If we want the pairs we need to create a generator (we will see this later)\n", "for k,v in d.items():\n", @@ -314,22 +221,11 @@ }, { "cell_type": "code", - "execution_count": 56, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "5\n", - "4\n", - "3\n", - "2\n" - ] - } - ], + "outputs": [], "source": [ "x = 5\n", "while x > 1:\n", @@ -339,21 +235,11 @@ }, { "cell_type": "code", - "execution_count": 45, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "2\n", - "3\n", - "4\n" - ] - } - ], + "outputs": [], "source": [ "# Else is optional\n", "x = 2\n", @@ -379,20 +265,11 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Goal found at index c\n", - "End loop\n" - ] - } - ], + "outputs": [], "source": [ "# Example find an element, else executed at the end\n", "list = ['a', 'b', 'c']\n", @@ -406,19 +283,11 @@ }, { "cell_type": "code", - "execution_count": 58, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "End loop\n" - ] - } - ], + "outputs": [], "source": [ "# Example else\n", "list = []\n", @@ -432,19 +301,11 @@ }, { "cell_type": "code", - "execution_count": 60, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Goal found at index c\n" - ] - } - ], + "outputs": [], "source": [ "# We improve above code with break\n", "# Example else\n", @@ -460,19 +321,11 @@ }, { "cell_type": "code", - "execution_count": 62, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Goal not found\n" - ] - } - ], + "outputs": [], "source": [ "# We improve above code with break\n", "# Example else\n", @@ -488,28 +341,11 @@ }, { "cell_type": "code", - "execution_count": 50, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "1\n", - "2\n", - "4\n", - "5\n", - "7\n", - "8\n", - "10\n", - "11\n", - "13\n", - "14\n" - ] - } - ], + "outputs": [], "source": [ "# Print numbers from 0 to 15 which are not multiple of 3\n", "for i in range(15):\n", @@ -520,19 +356,11 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Found 4\n" - ] - } - ], + "outputs": [], "source": [ "# Find the first occurrence of an element in a list\n", "l = [1, 2, 3, 4, 1, 2]\n", @@ -547,19 +375,11 @@ }, { "cell_type": "code", - "execution_count": 64, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Big\n" - ] - } - ], + "outputs": [], "source": [ "# Example of pass, when we do not want to do anything\n", "x = 4\n", @@ -586,22 +406,11 @@ }, { "cell_type": "code", - "execution_count": 71, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]" - ] - }, - "execution_count": 71, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "# Syntax: first what we want to include in the list (x) and then how to obtain x\n", "# list = {x : x in {0 ... 9}}\n", @@ -611,22 +420,11 @@ }, { "cell_type": "code", - "execution_count": 70, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]" - ] - }, - "execution_count": 70, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "# list = {x² : x in {0 ... 9}}\n", "list = [x*x for x in range(10)]\n", @@ -635,22 +433,11 @@ }, { "cell_type": "code", - "execution_count": 72, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "[0, 4, 16, 36, 64]" - ] - }, - "execution_count": 72, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "# list = {x² : x in {0 ... 9}, x is even}\n", "list = [x*x for x in range(10) if x % 2 == 0]\n", @@ -670,7 +457,7 @@ "source": [ "The notebook is freely licensed under under the [Creative Commons Attribution Share-Alike license](https://creativecommons.org/licenses/by/2.0/). \n", "\n", - "© 2015 Carlos A. Iglesias, Universidad Politécnica de Madrid." + "© 2016 Carlos A. Iglesias, Universidad Politécnica de Madrid." ] } ], diff --git a/python/1_6_Functions.ipynb b/python/1_6_Functions.ipynb index 8b03161..aba9236 100644 --- a/python/1_6_Functions.ipynb +++ b/python/1_6_Functions.ipynb @@ -18,7 +18,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Department of Telematic Engineering Systems, Universidad Politécnica de Madrid, © 2015 Carlos A. Iglesias" + "Department of Telematic Engineering Systems, Universidad Politécnica de Madrid, © 2016 Carlos A. Iglesias" ] }, { @@ -41,19 +41,11 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "5\n" - ] - } - ], + "outputs": [], "source": [ "def sum(a, b):\n", " return a + b\n", @@ -63,19 +55,11 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "5\n" - ] - } - ], + "outputs": [], "source": [ "#keyword parameters\n", "d = sum(a=2, b=3)\n", @@ -84,21 +68,11 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "***\n", - "hi\n", - "***\n" - ] - } - ], + "outputs": [], "source": [ "def greetings():\n", " print('***')\n", @@ -110,21 +84,11 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "***\n", - "hi\n", - "***\n" - ] - } - ], + "outputs": [], "source": [ "# We can assign a function to a variable. Fun\n", "d = greetings()" @@ -132,44 +96,22 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "NoneType" - ] - }, - "execution_count": 14, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "type(d)" ] }, { "cell_type": "code", - "execution_count": 16, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "function" - ] - }, - "execution_count": 16, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "type(greetings)" ] @@ -184,20 +126,11 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[3, 2, 1]\n", - "Receives a list and returns the list in reverse order\n" - ] - } - ], + "outputs": [], "source": [ "def reverse(l):\n", " \"\"\"Receives a list and returns the list in reverse order\"\"\"\n", @@ -220,20 +153,11 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "2\n", - "5\n" - ] - } - ], + "outputs": [], "source": [ "def sum(a, b=0):\n", " return a + b\n", @@ -250,20 +174,11 @@ }, { "cell_type": "code", - "execution_count": 25, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "1\n", - "6\n" - ] - } - ], + "outputs": [], "source": [ "#variable number of arguments: *\n", "def sum(a, *l):\n", @@ -278,20 +193,11 @@ }, { "cell_type": "code", - "execution_count": 28, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "7\n", - "13\n" - ] - } - ], + "outputs": [], "source": [ "#Packing \n", "l = [1, 2, 3]\n", @@ -314,20 +220,11 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "c 27\n", - "c 27\n" - ] - } - ], + "outputs": [], "source": [ "def sq(x):\n", " return x**x\n", @@ -366,21 +263,11 @@ }, { "cell_type": "code", - "execution_count": 33, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "1 2 3 4\n", - "1,2,3,4\n", - "1,2,3,4#1|2|3|4\n" - ] - } - ], + "outputs": [], "source": [ "print(1, 2, 3, 4)\n", "print(1, 2, 3, 4, sep=',')\n", @@ -397,21 +284,11 @@ }, { "cell_type": "code", - "execution_count": 41, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Number: 1,2\n", - "PI #3.141592653589793#\n", - "PI # 3.14#\n" - ] - } - ], + "outputs": [], "source": [ "import math\n", "print('Number: {},{}'.format(1, 2)) #replaces [] inside the string by the arguments of format\n", @@ -430,20 +307,11 @@ }, { "cell_type": "code", - "execution_count": 45, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Enter a number 5\n", - "5\n" - ] - } - ], + "outputs": [], "source": [ "num = input('Enter a number ')\n", "print(num)" @@ -462,7 +330,7 @@ "source": [ "The notebook is freely licensed under under the [Creative Commons Attribution Share-Alike license](https://creativecommons.org/licenses/by/2.0/). \n", "\n", - "© 2015 Carlos A. Iglesias, Universidad Politécnica de Madrid." + "© 2016 Carlos A. Iglesias, Universidad Politécnica de Madrid." ] } ], diff --git a/python/1_7_Variables.ipynb b/python/1_7_Variables.ipynb index 4f95565..d27a5c9 100644 --- a/python/1_7_Variables.ipynb +++ b/python/1_7_Variables.ipynb @@ -18,7 +18,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Department of Telematic Engineering Systems, Universidad Politécnica de Madrid, © 2015 Carlos A. Iglesias" + "Department of Telematic Engineering Systems, Universidad Politécnica de Madrid, © 2016 Carlos A. Iglesias" ] }, { @@ -302,7 +302,7 @@ "source": [ "The notebook is freely licensed under under the [Creative Commons Attribution Share-Alike license](https://creativecommons.org/licenses/by/2.0/). \n", "\n", - "© 2015 Carlos A. Iglesias, Universidad Politécnica de Madrid." + "© 2016 Carlos A. Iglesias, Universidad Politécnica de Madrid." ] } ], diff --git a/python/1_8_Classes.ipynb b/python/1_8_Classes.ipynb index b20323c..7f2b53f 100644 --- a/python/1_8_Classes.ipynb +++ b/python/1_8_Classes.ipynb @@ -18,7 +18,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Department of Telematic Engineering Systems, Universidad Politécnica de Madrid, © 2015 Carlos A. Iglesias" + "Department of Telematic Engineering Systems, Universidad Politécnica de Madrid, © 2016 Carlos A. Iglesias" ] }, { @@ -46,7 +46,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": null, "metadata": { "collapsed": false }, @@ -67,31 +67,13 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "<__main__.TV_Set object at 0x7fa0ec1a6c50> off\n" - ] - }, - { - "data": { - "text/plain": [ - "__main__.TV_Set" - ] - }, - "execution_count": 13, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ - "#€xample object instantiation\n", + "#Example object instantiation\n", "\n", "my_tv = TV_Set('Samsung')\n", "print(my_tv, my_tv.status)\n", @@ -110,21 +92,11 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "0\n", - "1\n", - "2\n" - ] - } - ], + "outputs": [], "source": [ "#Example class declaration\n", "class TV_Set:\n", @@ -162,19 +134,11 @@ }, { "cell_type": "code", - "execution_count": 29, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "<__main__.Person object at 0x7fa0ec14ec88>\n" - ] - } - ], + "outputs": [], "source": [ "class Person:\n", " def __init__(self, name, age):\n", @@ -188,19 +152,11 @@ }, { "cell_type": "code", - "execution_count": 30, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "name: Pedro, age: 0\n" - ] - } - ], + "outputs": [], "source": [ "# Example __str(self)__\n", "\n", @@ -239,19 +195,11 @@ }, { "cell_type": "code", - "execution_count": 28, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "name: Pedro, age: -1\n" - ] - } - ], + "outputs": [], "source": [ "# Now we could change the age of Pedro to a negative value\n", "p.age = -1\n", @@ -267,19 +215,11 @@ }, { "cell_type": "code", - "execution_count": 36, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "name: Pedro, age: 0\n" - ] - } - ], + "outputs": [], "source": [ "class Person:\n", " def __init__(self, name, age):\n", @@ -317,7 +257,7 @@ "source": [ "The notebook is freely licensed under under the [Creative Commons Attribution Share-Alike license](https://creativecommons.org/licenses/by/2.0/). \n", "\n", - "© 2015 Carlos A. Iglesias, Universidad Politécnica de Madrid." + "© 2016 Carlos A. Iglesias, Universidad Politécnica de Madrid." ] } ], diff --git a/python/1_9_Errors_Exceptions.ipynb b/python/1_9_Errors_Exceptions.ipynb index 1329fa3..091eab0 100644 --- a/python/1_9_Errors_Exceptions.ipynb +++ b/python/1_9_Errors_Exceptions.ipynb @@ -18,7 +18,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Department of Telematic Engineering Systems, Universidad Politécnica de Madrid, © 2015 Carlos A. Iglesias" + "Department of Telematic Engineering Systems, Universidad Politécnica de Madrid, © 2016 Carlos A. Iglesias" ] }, { @@ -39,20 +39,11 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "ename": "SyntaxError", - "evalue": "invalid syntax (, line 2)", - "output_type": "error", - "traceback": [ - "\u001b[1;36m File \u001b[1;32m\"\"\u001b[1;36m, line \u001b[1;32m2\u001b[0m\n\u001b[1;33m while True\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n" - ] - } - ], + "outputs": [], "source": [ "# Example SyntaxError - missing semicolon in while\n", "while True\n", @@ -69,23 +60,11 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "ename": "TypeError", - "evalue": "unsupported operand type(s) for +: 'int' and 'str'", - "output_type": "error", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", - "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;31m# Example TypeError - wrong use of '+' with different types\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[1;36m3\u001b[0m \u001b[1;33m+\u001b[0m \u001b[1;34m'a'\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[1;31mTypeError\u001b[0m: unsupported operand type(s) for +: 'int' and 'str'" - ] - } - ], + "outputs": [], "source": [ "# Example TypeError - wrong use of '+' with different types\n", "3 + 'a'" @@ -93,24 +72,12 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": null, "metadata": { "collapsed": false, "scrolled": true }, - "outputs": [ - { - "ename": "NameError", - "evalue": "name 'a' is not defined", - "output_type": "error", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", - "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;31m# Example NameError: variable not defined\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0ma\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[1;31mNameError\u001b[0m: name 'a' is not defined" - ] - } - ], + "outputs": [], "source": [ "# Example NameError: variable not defined\n", "a \n" @@ -130,21 +97,11 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Please enter a number: a\n", - "Oops! That was no valid number. Try again...\n", - "Please enter a number: 1\n" - ] - } - ], + "outputs": [], "source": [ "# Example\n", "# Try to enter a wrong input (e.g. a) and then a number (e.g. 0)\n", @@ -158,23 +115,11 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Please enter a number: a\n", - "Oops! That was no valid number. Try again...\n", - "End of input\n", - "Please enter a number: 1\n", - "End of input\n" - ] - } - ], + "outputs": [], "source": [ "# Example with finally\n", "while True:\n", @@ -189,24 +134,11 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Please enter a number: a\n", - "Oops! That was no valid number.\n", - "Number entered\n", - "Please enter a number: 1\n", - "No exception\n", - "Number entered\n" - ] - } - ], + "outputs": [], "source": [ "# Example with else and finally\n", "while True:\n", @@ -231,28 +163,11 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "ename": "TypeError", - "evalue": "Both arguments should be numbers or strings", - "output_type": "error", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", - "\u001b[1;32m\u001b[0m in \u001b[0;36madd\u001b[1;34m(a, b)\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[1;32mreturn\u001b[0m \u001b[0ma\u001b[0m \u001b[1;33m+\u001b[0m \u001b[0mb\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 4\u001b[0m \u001b[1;32mexcept\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", - "\u001b[1;31mTypeError\u001b[0m: unsupported operand type(s) for +: 'int' and 'str'", - "\nDuring handling of the above exception, another exception occurred:\n", - "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", - "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[1;32mraise\u001b[0m \u001b[0mTypeError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Both arguments should be numbers or strings\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 6\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 7\u001b[1;33m \u001b[0madd\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'a'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[1;32m\u001b[0m in \u001b[0;36madd\u001b[1;34m(a, b)\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[1;32mreturn\u001b[0m \u001b[0ma\u001b[0m \u001b[1;33m+\u001b[0m \u001b[0mb\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[1;32mexcept\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 5\u001b[1;33m \u001b[1;32mraise\u001b[0m \u001b[0mTypeError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Both arguments should be numbers or strings\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 6\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 7\u001b[0m \u001b[0madd\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'a'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", - "\u001b[1;31mTypeError\u001b[0m: Both arguments should be numbers or strings" - ] - } - ], + "outputs": [], "source": [ "def add(a, b):\n", " try:\n", @@ -276,7 +191,7 @@ "source": [ "The notebook is freely licensed under under the [Creative Commons Attribution Share-Alike license](https://creativecommons.org/licenses/by/2.0/). \n", "\n", - "© 2015 Carlos A. Iglesias, Universidad Politécnica de Madrid." + "© 2016 Carlos A. Iglesias, Universidad Politécnica de Madrid." ] } ], diff --git a/python/1__10_Modules_Packages.ipynb b/python/1__10_Modules_Packages.ipynb index cce1afc..dcae4b3 100644 --- a/python/1__10_Modules_Packages.ipynb +++ b/python/1__10_Modules_Packages.ipynb @@ -18,7 +18,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Department of Telematic Engineering Systems, Universidad Politécnica de Madrid, © 2015 Carlos A. Iglesias" + "Department of Telematic Engineering Systems, Universidad Politécnica de Madrid, © 2016 Carlos A. Iglesias" ] }, { @@ -45,23 +45,11 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "ename": "NameError", - "evalue": "name 'plurals' is not defined", - "output_type": "error", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", - "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;31m# We can import the module plural with import\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mbabel\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mmessages\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mplurals\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0mplurals\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mget_plural\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[1;31mNameError\u001b[0m: name 'plurals' is not defined" - ] - } - ], + "outputs": [], "source": [ "# We can import the module plural with import, but we should use the full name\n", "import babel.messages.plurals\n", @@ -70,22 +58,11 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "(2, '(n != 1)')" - ] - }, - "execution_count": 10, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "import babel.messages.plurals\n", "babel.messages.plurals.get_plural()" @@ -93,22 +70,11 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "(2, '(n != 1)')" - ] - }, - "execution_count": 12, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "from babel.messages import plurals # with from-import, we can use the short name\n", "plurals.get_plural()" @@ -116,22 +82,11 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/plain": [ - "(2, '(n != 1)')" - ] - }, - "execution_count": 14, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "from babel.messages.plurals import get_plural # now we can use directly get_plural()\n", "get_plural()" @@ -159,7 +114,7 @@ "source": [ "The notebook is freely licensed under under the [Creative Commons Attribution Share-Alike license](https://creativecommons.org/licenses/by/2.0/). \n", "\n", - "© 2015 Carlos A. Iglesias, Universidad Politécnica de Madrid." + "© 2016 Carlos A. Iglesias, Universidad Politécnica de Madrid." ] } ],