Updated to 2016 and clean cells

pull/1/head
cif2cif 8 years ago
parent 39897634b6
commit 4ffb1a1891

@ -18,7 +18,7 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "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", " 4. [Variables](1_7_Variables.ipynb)\n",
" 5. [Classes](1_8_Classes.ipynb)\n", " 5. [Classes](1_8_Classes.ipynb)\n",
" 6. [Errors and Exceptions](1_9_Errors_Exceptions.ipynb)\n", " 6. [Errors and Exceptions](1_9_Errors_Exceptions.ipynb)\n",
" 7. [Modules and Packages](1__10_Modules_Packages.ipynb)\n", " 7. [Modules and Packages](1__10_Modules_Packages.ipynb)"
" 8. Testing / Generadores / Decoradores"
] ]
}, },
{ {
@ -96,7 +95,7 @@
"source": [ "source": [
"The notebook is freely licensed under under the [Creative Commons Attribution Share-Alike license](https://creativecommons.org/licenses/by/2.0/). \n", "The notebook is freely licensed under under the [Creative Commons Attribution Share-Alike license](https://creativecommons.org/licenses/by/2.0/). \n",
"\n", "\n",
"© 2015 Carlos A. Iglesias, Universidad Politécnica de Madrid." "© 2016 Carlos A. Iglesias, Universidad Politécnica de Madrid."
] ]
} }
], ],

@ -18,7 +18,7 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "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": [ "source": [
"The notebook is freely licensed under under the [Creative Commons Attribution Share-Alike license](https://creativecommons.org/licenses/by/2.0/). \n", "The notebook is freely licensed under under the [Creative Commons Attribution Share-Alike license](https://creativecommons.org/licenses/by/2.0/). \n",
"\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", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython3", "pygments_lexer": "ipython3",
"version": "3.4.3+" "version": "3.5.1"
} }
}, },
"nbformat": 4, "nbformat": 4,

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

@ -18,7 +18,7 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "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", "cell_type": "code",
"execution_count": 413, "execution_count": null,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
"outputs": [ "outputs": [],
{
"data": {
"text/plain": [
"set()"
]
},
"execution_count": 413,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [ "source": [
"my_set = set() #create a set\n", "my_set = set() #create a set\n",
"my_set" "my_set"
@ -74,22 +63,11 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 414, "execution_count": null,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
"outputs": [ "outputs": [],
{
"data": {
"text/plain": [
"{1}"
]
},
"execution_count": 414,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [ "source": [
"my_set.add(1) # add an element\n", "my_set.add(1) # add an element\n",
"my_set" "my_set"
@ -97,7 +75,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 415, "execution_count": null,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
@ -108,44 +86,22 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 416, "execution_count": null,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
"outputs": [ "outputs": [],
{
"data": {
"text/plain": [
"{1, 2}"
]
},
"execution_count": 416,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [ "source": [
"my_set" "my_set"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 417, "execution_count": null,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
"outputs": [ "outputs": [],
{
"data": {
"text/plain": [
"{1, 2, 3}"
]
},
"execution_count": 417,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [ "source": [
"my_set.add(3) # add another one\n", "my_set.add(3) # add another one\n",
"my_set" "my_set"
@ -153,22 +109,11 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 418, "execution_count": null,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
"outputs": [ "outputs": [],
{
"data": {
"text/plain": [
"{1, 2, 3}"
]
},
"execution_count": 418,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [ "source": [
"my_set.add(1) #try to add a repeated element\n", "my_set.add(1) #try to add a repeated element\n",
"my_set" "my_set"
@ -176,22 +121,11 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 419, "execution_count": null,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
"outputs": [ "outputs": [],
{
"data": {
"text/plain": [
"{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}"
]
},
"execution_count": 419,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [ "source": [
"s2 = set(range(10)) # we can create a set from a range\n", "s2 = set(range(10)) # we can create a set from a range\n",
"s2" "s2"
@ -199,7 +133,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 420, "execution_count": null,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
@ -210,22 +144,11 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 421, "execution_count": null,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
"outputs": [ "outputs": [],
{
"data": {
"text/plain": [
"{'a', 'b', 'c'}"
]
},
"execution_count": 421,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [ "source": [
"s3 = set(l) # if we create a set from a list, elements are not repeated\n", "s3 = set(l) # if we create a set from a list, elements are not repeated\n",
"s3" "s3"
@ -233,88 +156,44 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 422, "execution_count": null,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
"outputs": [ "outputs": [],
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 422,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [ "source": [
"len(s3) " "len(s3) "
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 423, "execution_count": null,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
"outputs": [ "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"
}
],
"source": [ "source": [
"s3.union(s2) # we can use set methods: union(), intersection(), difference(), ..." "s3.union(s2) # we can use set methods: union(), intersection(), difference(), ..."
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 424, "execution_count": null,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
"outputs": [ "outputs": [],
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 424,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [ "source": [
"3 in my_set #check membership" "3 in my_set #check membership"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 425, "execution_count": null,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
"outputs": [ "outputs": [],
{
"data": {
"text/plain": [
"set"
]
},
"execution_count": 425,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [ "source": [
"type(s3)" "type(s3)"
] ]
@ -328,22 +207,11 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 426, "execution_count": null,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
"outputs": [ "outputs": [],
{
"data": {
"text/plain": [
"{'key1': 1, 'key2': 2, 'key3': 3}"
]
},
"execution_count": 426,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [ "source": [
"my_dictionary = {'key1': 1, 'key2': 2, 'key3': 3} # pairs of key-value mappings\n", "my_dictionary = {'key1': 1, 'key2': 2, 'key3': 3} # pairs of key-value mappings\n",
"my_dictionary" "my_dictionary"
@ -351,44 +219,22 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 427, "execution_count": null,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
"outputs": [ "outputs": [],
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 427,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [ "source": [
"my_dictionary['key1'] #retrieve a value given a key" "my_dictionary['key1'] #retrieve a value given a key"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 428, "execution_count": null,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
"outputs": [ "outputs": [],
{
"data": {
"text/plain": [
"{'key1': 1, 'key2': 2, 'key3': 3}"
]
},
"execution_count": 428,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [ "source": [
"my_dict = dict()\n", "my_dict = dict()\n",
"my_dict['key1'] = 1\n", "my_dict['key1'] = 1\n",
@ -399,44 +245,22 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 429, "execution_count": null,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
"outputs": [ "outputs": [],
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 429,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [ "source": [
"my_dict == my_dictionary # check if both dictionaries are equal" "my_dict == my_dictionary # check if both dictionaries are equal"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 430, "execution_count": null,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
"outputs": [ "outputs": [],
{
"data": {
"text/plain": [
"{'one': {'two': {'three': 'Nested dict'}}}"
]
},
"execution_count": 430,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [ "source": [
"my_dict2 = {'one': {'two': {'three': 'Nested dict'}}} #nested dictionary\n", "my_dict2 = {'one': {'two': {'three': 'Nested dict'}}} #nested dictionary\n",
"my_dict2" "my_dict2"
@ -444,22 +268,11 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 431, "execution_count": null,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
"outputs": [ "outputs": [],
{
"data": {
"text/plain": [
"'Nested dict'"
]
},
"execution_count": 431,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [ "source": [
"my_dict2['one']['two']['three'] #access the value" "my_dict2['one']['two']['three'] #access the value"
] ]
@ -475,110 +288,55 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 473, "execution_count": null,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
"outputs": [ "outputs": [],
{
"data": {
"text/plain": [
"dict_keys(['key2', 'key3', 'key1'])"
]
},
"execution_count": 473,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [ "source": [
"my_dict.keys() # in Python3 we get a View object that changes when the dictionary changes" "my_dict.keys() # in Python3 we get a View object that changes when the dictionary changes"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 478, "execution_count": null,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
"outputs": [ "outputs": [],
{
"data": {
"text/plain": [
"['key2', 'key3', 'key1']"
]
},
"execution_count": 478,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [ "source": [
"list(my_dict.keys()) # we can convert it to a list, we see dicionaries are unordered" "list(my_dict.keys()) # we can convert it to a list, we see dicionaries are unordered"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 474, "execution_count": null,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
"outputs": [ "outputs": [],
{
"data": {
"text/plain": [
"dict_values([2, 3, 1])"
]
},
"execution_count": 474,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [ "source": [
"my_dict.values()" "my_dict.values()"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 476, "execution_count": null,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
"outputs": [ "outputs": [],
{
"data": {
"text/plain": [
"[2, 3, 1]"
]
},
"execution_count": 476,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [ "source": [
"list(my_dict.values())" "list(my_dict.values())"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 479, "execution_count": null,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
"outputs": [ "outputs": [],
{
"data": {
"text/plain": [
"dict"
]
},
"execution_count": 479,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [ "source": [
"type(my_dict)" "type(my_dict)"
] ]
@ -596,7 +354,7 @@
"source": [ "source": [
"The notebook is freely licensed under under the [Creative Commons Attribution Share-Alike license](https://creativecommons.org/licenses/by/2.0/). \n", "The notebook is freely licensed under under the [Creative Commons Attribution Share-Alike license](https://creativecommons.org/licenses/by/2.0/). \n",
"\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", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython3", "pygments_lexer": "ipython3",
"version": "3.4.3+" "version": "3.5.1"
} }
}, },
"nbformat": 4, "nbformat": 4,

@ -18,7 +18,7 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "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", "cell_type": "code",
"execution_count": 22, "execution_count": null,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
"outputs": [ "outputs": [],
{
"data": {
"text/plain": [
"9"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [ "source": [
"import random # import random before using it\n", "import random # import random before using it\n",
"x = random.randrange(1, 10) # generate a random integer between [1, 10] (both included)\n", "x = random.randrange(1, 10) # generate a random integer between [1, 10] (both included)\n",
@ -92,19 +81,11 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 14, "execution_count": null,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
"outputs": [ "outputs": [],
{
"name": "stdout",
"output_type": "stream",
"text": [
"Too small\n"
]
}
],
"source": [ "source": [
"# Execute several times in order the previous cell and this one\n", "# Execute several times in order the previous cell and this one\n",
"if x < 5:\n", "if x < 5:\n",
@ -117,19 +98,11 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 15, "execution_count": null,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
"outputs": [ "outputs": [],
{
"name": "stdout",
"output_type": "stream",
"text": [
"Too small\n"
]
}
],
"source": [ "source": [
"# Only one branch\n", "# Only one branch\n",
"if x <= 5:\n", "if x <= 5:\n",
@ -140,19 +113,11 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 23, "execution_count": null,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
"outputs": [ "outputs": [],
{
"name": "stdout",
"output_type": "stream",
"text": [
"Too big\n"
]
}
],
"source": [ "source": [
"# Python has no switch statement for multiple branches\n", "# Python has no switch statement for multiple branches\n",
"if x <= 4:\n", "if x <= 4:\n",
@ -181,28 +146,11 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 27, "execution_count": null,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
"outputs": [ "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"
]
}
],
"source": [ "source": [
"# for with ranges\n", "# for with ranges\n",
"for i in range(10): \n", "for i in range(10): \n",
@ -211,22 +159,11 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 29, "execution_count": null,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
"outputs": [ "outputs": [],
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"2\n",
"3\n",
"4\n"
]
}
],
"source": [ "source": [
"# for with lists\n", "# for with lists\n",
"l = [1, 2, 3, 4]\n", "l = [1, 2, 3, 4]\n",
@ -236,21 +173,11 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 30, "execution_count": null,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
"outputs": [ "outputs": [],
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"a\n",
"b\n"
]
}
],
"source": [ "source": [
"# for with tuples\n", "# for with tuples\n",
"t = (1, 'a', 'b')\n", "t = (1, 'a', 'b')\n",
@ -260,21 +187,11 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 31, "execution_count": null,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
"outputs": [ "outputs": [],
{
"name": "stdout",
"output_type": "stream",
"text": [
"k2\n",
"k1\n",
"k3\n"
]
}
],
"source": [ "source": [
"# for with dictionaries\n", "# for with dictionaries\n",
"d = {'k1':'v1', 'k2':'v2', 'k3':'v3'}\n", "d = {'k1':'v1', 'k2':'v2', 'k3':'v3'}\n",
@ -284,21 +201,11 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 34, "execution_count": null,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
"outputs": [ "outputs": [],
{
"name": "stdout",
"output_type": "stream",
"text": [
"k2 v2\n",
"k1 v1\n",
"k3 v3\n"
]
}
],
"source": [ "source": [
"# We get only the keys. If we want the pairs we need to create a generator (we will see this later)\n", "# 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", "for k,v in d.items():\n",
@ -314,22 +221,11 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 56, "execution_count": null,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
"outputs": [ "outputs": [],
{
"name": "stdout",
"output_type": "stream",
"text": [
"5\n",
"4\n",
"3\n",
"2\n"
]
}
],
"source": [ "source": [
"x = 5\n", "x = 5\n",
"while x > 1:\n", "while x > 1:\n",
@ -339,21 +235,11 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 45, "execution_count": null,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
"outputs": [ "outputs": [],
{
"name": "stdout",
"output_type": "stream",
"text": [
"2\n",
"3\n",
"4\n"
]
}
],
"source": [ "source": [
"# Else is optional\n", "# Else is optional\n",
"x = 2\n", "x = 2\n",
@ -379,20 +265,11 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 1, "execution_count": null,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
"outputs": [ "outputs": [],
{
"name": "stdout",
"output_type": "stream",
"text": [
"Goal found at index c\n",
"End loop\n"
]
}
],
"source": [ "source": [
"# Example find an element, else executed at the end\n", "# Example find an element, else executed at the end\n",
"list = ['a', 'b', 'c']\n", "list = ['a', 'b', 'c']\n",
@ -406,19 +283,11 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 58, "execution_count": null,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
"outputs": [ "outputs": [],
{
"name": "stdout",
"output_type": "stream",
"text": [
"End loop\n"
]
}
],
"source": [ "source": [
"# Example else\n", "# Example else\n",
"list = []\n", "list = []\n",
@ -432,19 +301,11 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 60, "execution_count": null,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
"outputs": [ "outputs": [],
{
"name": "stdout",
"output_type": "stream",
"text": [
"Goal found at index c\n"
]
}
],
"source": [ "source": [
"# We improve above code with break\n", "# We improve above code with break\n",
"# Example else\n", "# Example else\n",
@ -460,19 +321,11 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 62, "execution_count": null,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
"outputs": [ "outputs": [],
{
"name": "stdout",
"output_type": "stream",
"text": [
"Goal not found\n"
]
}
],
"source": [ "source": [
"# We improve above code with break\n", "# We improve above code with break\n",
"# Example else\n", "# Example else\n",
@ -488,28 +341,11 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 50, "execution_count": null,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
"outputs": [ "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"
]
}
],
"source": [ "source": [
"# Print numbers from 0 to 15 which are not multiple of 3\n", "# Print numbers from 0 to 15 which are not multiple of 3\n",
"for i in range(15):\n", "for i in range(15):\n",
@ -520,19 +356,11 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 2, "execution_count": null,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
"outputs": [ "outputs": [],
{
"name": "stdout",
"output_type": "stream",
"text": [
"Found 4\n"
]
}
],
"source": [ "source": [
"# Find the first occurrence of an element in a list\n", "# Find the first occurrence of an element in a list\n",
"l = [1, 2, 3, 4, 1, 2]\n", "l = [1, 2, 3, 4, 1, 2]\n",
@ -547,19 +375,11 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 64, "execution_count": null,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
"outputs": [ "outputs": [],
{
"name": "stdout",
"output_type": "stream",
"text": [
"Big\n"
]
}
],
"source": [ "source": [
"# Example of pass, when we do not want to do anything\n", "# Example of pass, when we do not want to do anything\n",
"x = 4\n", "x = 4\n",
@ -586,22 +406,11 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 71, "execution_count": null,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
"outputs": [ "outputs": [],
{
"data": {
"text/plain": [
"[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]"
]
},
"execution_count": 71,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [ "source": [
"# Syntax: first what we want to include in the list (x) and then how to obtain x\n", "# 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", "# list = {x : x in {0 ... 9}}\n",
@ -611,22 +420,11 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 70, "execution_count": null,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
"outputs": [ "outputs": [],
{
"data": {
"text/plain": [
"[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]"
]
},
"execution_count": 70,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [ "source": [
"# list = {x² : x in {0 ... 9}}\n", "# list = {x² : x in {0 ... 9}}\n",
"list = [x*x for x in range(10)]\n", "list = [x*x for x in range(10)]\n",
@ -635,22 +433,11 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 72, "execution_count": null,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
"outputs": [ "outputs": [],
{
"data": {
"text/plain": [
"[0, 4, 16, 36, 64]"
]
},
"execution_count": 72,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [ "source": [
"# list = {x² : x in {0 ... 9}, x is even}\n", "# list = {x² : x in {0 ... 9}, x is even}\n",
"list = [x*x for x in range(10) if x % 2 == 0]\n", "list = [x*x for x in range(10) if x % 2 == 0]\n",
@ -670,7 +457,7 @@
"source": [ "source": [
"The notebook is freely licensed under under the [Creative Commons Attribution Share-Alike license](https://creativecommons.org/licenses/by/2.0/). \n", "The notebook is freely licensed under under the [Creative Commons Attribution Share-Alike license](https://creativecommons.org/licenses/by/2.0/). \n",
"\n", "\n",
"© 2015 Carlos A. Iglesias, Universidad Politécnica de Madrid." "© 2016 Carlos A. Iglesias, Universidad Politécnica de Madrid."
] ]
} }
], ],

@ -18,7 +18,7 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "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", "cell_type": "code",
"execution_count": 4, "execution_count": null,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
"outputs": [ "outputs": [],
{
"name": "stdout",
"output_type": "stream",
"text": [
"5\n"
]
}
],
"source": [ "source": [
"def sum(a, b):\n", "def sum(a, b):\n",
" return a + b\n", " return a + b\n",
@ -63,19 +55,11 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 17, "execution_count": null,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
"outputs": [ "outputs": [],
{
"name": "stdout",
"output_type": "stream",
"text": [
"5\n"
]
}
],
"source": [ "source": [
"#keyword parameters\n", "#keyword parameters\n",
"d = sum(a=2, b=3)\n", "d = sum(a=2, b=3)\n",
@ -84,21 +68,11 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 11, "execution_count": null,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
"outputs": [ "outputs": [],
{
"name": "stdout",
"output_type": "stream",
"text": [
"***\n",
"hi\n",
"***\n"
]
}
],
"source": [ "source": [
"def greetings():\n", "def greetings():\n",
" print('***')\n", " print('***')\n",
@ -110,21 +84,11 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 13, "execution_count": null,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
"outputs": [ "outputs": [],
{
"name": "stdout",
"output_type": "stream",
"text": [
"***\n",
"hi\n",
"***\n"
]
}
],
"source": [ "source": [
"# We can assign a function to a variable. Fun\n", "# We can assign a function to a variable. Fun\n",
"d = greetings()" "d = greetings()"
@ -132,44 +96,22 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 14, "execution_count": null,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
"outputs": [ "outputs": [],
{
"data": {
"text/plain": [
"NoneType"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [ "source": [
"type(d)" "type(d)"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 16, "execution_count": null,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
"outputs": [ "outputs": [],
{
"data": {
"text/plain": [
"function"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [ "source": [
"type(greetings)" "type(greetings)"
] ]
@ -184,20 +126,11 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 21, "execution_count": null,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
"outputs": [ "outputs": [],
{
"name": "stdout",
"output_type": "stream",
"text": [
"[3, 2, 1]\n",
"Receives a list and returns the list in reverse order\n"
]
}
],
"source": [ "source": [
"def reverse(l):\n", "def reverse(l):\n",
" \"\"\"Receives a list and returns the list in reverse order\"\"\"\n", " \"\"\"Receives a list and returns the list in reverse order\"\"\"\n",
@ -220,20 +153,11 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 22, "execution_count": null,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
"outputs": [ "outputs": [],
{
"name": "stdout",
"output_type": "stream",
"text": [
"2\n",
"5\n"
]
}
],
"source": [ "source": [
"def sum(a, b=0):\n", "def sum(a, b=0):\n",
" return a + b\n", " return a + b\n",
@ -250,20 +174,11 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 25, "execution_count": null,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
"outputs": [ "outputs": [],
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"6\n"
]
}
],
"source": [ "source": [
"#variable number of arguments: *\n", "#variable number of arguments: *\n",
"def sum(a, *l):\n", "def sum(a, *l):\n",
@ -278,20 +193,11 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 28, "execution_count": null,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
"outputs": [ "outputs": [],
{
"name": "stdout",
"output_type": "stream",
"text": [
"7\n",
"13\n"
]
}
],
"source": [ "source": [
"#Packing \n", "#Packing \n",
"l = [1, 2, 3]\n", "l = [1, 2, 3]\n",
@ -314,20 +220,11 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 8, "execution_count": null,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
"outputs": [ "outputs": [],
{
"name": "stdout",
"output_type": "stream",
"text": [
"c 27\n",
"c 27\n"
]
}
],
"source": [ "source": [
"def sq(x):\n", "def sq(x):\n",
" return x**x\n", " return x**x\n",
@ -366,21 +263,11 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 33, "execution_count": null,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
"outputs": [ "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"
]
}
],
"source": [ "source": [
"print(1, 2, 3, 4)\n", "print(1, 2, 3, 4)\n",
"print(1, 2, 3, 4, sep=',')\n", "print(1, 2, 3, 4, sep=',')\n",
@ -397,21 +284,11 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 41, "execution_count": null,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
"outputs": [ "outputs": [],
{
"name": "stdout",
"output_type": "stream",
"text": [
"Number: 1,2\n",
"PI #3.141592653589793#\n",
"PI # 3.14#\n"
]
}
],
"source": [ "source": [
"import math\n", "import math\n",
"print('Number: {},{}'.format(1, 2)) #replaces [] inside the string by the arguments of format\n", "print('Number: {},{}'.format(1, 2)) #replaces [] inside the string by the arguments of format\n",
@ -430,20 +307,11 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 45, "execution_count": null,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
"outputs": [ "outputs": [],
{
"name": "stdout",
"output_type": "stream",
"text": [
"Enter a number 5\n",
"5\n"
]
}
],
"source": [ "source": [
"num = input('Enter a number ')\n", "num = input('Enter a number ')\n",
"print(num)" "print(num)"
@ -462,7 +330,7 @@
"source": [ "source": [
"The notebook is freely licensed under under the [Creative Commons Attribution Share-Alike license](https://creativecommons.org/licenses/by/2.0/). \n", "The notebook is freely licensed under under the [Creative Commons Attribution Share-Alike license](https://creativecommons.org/licenses/by/2.0/). \n",
"\n", "\n",
"© 2015 Carlos A. Iglesias, Universidad Politécnica de Madrid." "© 2016 Carlos A. Iglesias, Universidad Politécnica de Madrid."
] ]
} }
], ],

@ -18,7 +18,7 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "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": [ "source": [
"The notebook is freely licensed under under the [Creative Commons Attribution Share-Alike license](https://creativecommons.org/licenses/by/2.0/). \n", "The notebook is freely licensed under under the [Creative Commons Attribution Share-Alike license](https://creativecommons.org/licenses/by/2.0/). \n",
"\n", "\n",
"© 2015 Carlos A. Iglesias, Universidad Politécnica de Madrid." "© 2016 Carlos A. Iglesias, Universidad Politécnica de Madrid."
] ]
} }
], ],

@ -18,7 +18,7 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "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", "cell_type": "code",
"execution_count": 12, "execution_count": null,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
@ -67,31 +67,13 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 13, "execution_count": null,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
"outputs": [ "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"
}
],
"source": [ "source": [
"#xample object instantiation\n", "#Example object instantiation\n",
"\n", "\n",
"my_tv = TV_Set('Samsung')\n", "my_tv = TV_Set('Samsung')\n",
"print(my_tv, my_tv.status)\n", "print(my_tv, my_tv.status)\n",
@ -110,21 +92,11 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 14, "execution_count": null,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
"outputs": [ "outputs": [],
{
"name": "stdout",
"output_type": "stream",
"text": [
"0\n",
"1\n",
"2\n"
]
}
],
"source": [ "source": [
"#Example class declaration\n", "#Example class declaration\n",
"class TV_Set:\n", "class TV_Set:\n",
@ -162,19 +134,11 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 29, "execution_count": null,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
"outputs": [ "outputs": [],
{
"name": "stdout",
"output_type": "stream",
"text": [
"<__main__.Person object at 0x7fa0ec14ec88>\n"
]
}
],
"source": [ "source": [
"class Person:\n", "class Person:\n",
" def __init__(self, name, age):\n", " def __init__(self, name, age):\n",
@ -188,19 +152,11 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 30, "execution_count": null,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
"outputs": [ "outputs": [],
{
"name": "stdout",
"output_type": "stream",
"text": [
"name: Pedro, age: 0\n"
]
}
],
"source": [ "source": [
"# Example __str(self)__\n", "# Example __str(self)__\n",
"\n", "\n",
@ -239,19 +195,11 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 28, "execution_count": null,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
"outputs": [ "outputs": [],
{
"name": "stdout",
"output_type": "stream",
"text": [
"name: Pedro, age: -1\n"
]
}
],
"source": [ "source": [
"# Now we could change the age of Pedro to a negative value\n", "# Now we could change the age of Pedro to a negative value\n",
"p.age = -1\n", "p.age = -1\n",
@ -267,19 +215,11 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 36, "execution_count": null,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
"outputs": [ "outputs": [],
{
"name": "stdout",
"output_type": "stream",
"text": [
"name: Pedro, age: 0\n"
]
}
],
"source": [ "source": [
"class Person:\n", "class Person:\n",
" def __init__(self, name, age):\n", " def __init__(self, name, age):\n",
@ -317,7 +257,7 @@
"source": [ "source": [
"The notebook is freely licensed under under the [Creative Commons Attribution Share-Alike license](https://creativecommons.org/licenses/by/2.0/). \n", "The notebook is freely licensed under under the [Creative Commons Attribution Share-Alike license](https://creativecommons.org/licenses/by/2.0/). \n",
"\n", "\n",
"© 2015 Carlos A. Iglesias, Universidad Politécnica de Madrid." "© 2016 Carlos A. Iglesias, Universidad Politécnica de Madrid."
] ]
} }
], ],

@ -18,7 +18,7 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "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", "cell_type": "code",
"execution_count": 1, "execution_count": null,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
"outputs": [ "outputs": [],
{
"ename": "SyntaxError",
"evalue": "invalid syntax (<ipython-input-1-16b482c78ccd>, line 2)",
"output_type": "error",
"traceback": [
"\u001b[1;36m File \u001b[1;32m\"<ipython-input-1-16b482c78ccd>\"\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"
]
}
],
"source": [ "source": [
"# Example SyntaxError - missing semicolon in while\n", "# Example SyntaxError - missing semicolon in while\n",
"while True\n", "while True\n",
@ -69,23 +60,11 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 2, "execution_count": null,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
"outputs": [ "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<ipython-input-2-fb8fe04af7b6>\u001b[0m in \u001b[0;36m<module>\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'"
]
}
],
"source": [ "source": [
"# Example TypeError - wrong use of '+' with different types\n", "# Example TypeError - wrong use of '+' with different types\n",
"3 + 'a'" "3 + 'a'"
@ -93,24 +72,12 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 3, "execution_count": null,
"metadata": { "metadata": {
"collapsed": false, "collapsed": false,
"scrolled": true "scrolled": true
}, },
"outputs": [ "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<ipython-input-3-82179588c5b9>\u001b[0m in \u001b[0;36m<module>\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"
]
}
],
"source": [ "source": [
"# Example NameError: variable not defined\n", "# Example NameError: variable not defined\n",
"a \n" "a \n"
@ -130,21 +97,11 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 4, "execution_count": null,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
"outputs": [ "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"
]
}
],
"source": [ "source": [
"# Example\n", "# Example\n",
"# Try to enter a wrong input (e.g. a) and then a number (e.g. 0)\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", "cell_type": "code",
"execution_count": 1, "execution_count": null,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
"outputs": [ "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"
]
}
],
"source": [ "source": [
"# Example with finally\n", "# Example with finally\n",
"while True:\n", "while True:\n",
@ -189,24 +134,11 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 2, "execution_count": null,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
"outputs": [ "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"
]
}
],
"source": [ "source": [
"# Example with else and finally\n", "# Example with else and finally\n",
"while True:\n", "while True:\n",
@ -231,28 +163,11 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 5, "execution_count": null,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
"outputs": [ "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<ipython-input-5-1e4048e7e923>\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<ipython-input-5-1e4048e7e923>\u001b[0m in \u001b[0;36m<module>\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<ipython-input-5-1e4048e7e923>\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"
]
}
],
"source": [ "source": [
"def add(a, b):\n", "def add(a, b):\n",
" try:\n", " try:\n",
@ -276,7 +191,7 @@
"source": [ "source": [
"The notebook is freely licensed under under the [Creative Commons Attribution Share-Alike license](https://creativecommons.org/licenses/by/2.0/). \n", "The notebook is freely licensed under under the [Creative Commons Attribution Share-Alike license](https://creativecommons.org/licenses/by/2.0/). \n",
"\n", "\n",
"© 2015 Carlos A. Iglesias, Universidad Politécnica de Madrid." "© 2016 Carlos A. Iglesias, Universidad Politécnica de Madrid."
] ]
} }
], ],

@ -18,7 +18,7 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "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", "cell_type": "code",
"execution_count": 9, "execution_count": null,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
"outputs": [ "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<ipython-input-9-1ca6b7751408>\u001b[0m in \u001b[0;36m<module>\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"
]
}
],
"source": [ "source": [
"# We can import the module plural with import, but we should use the full name\n", "# We can import the module plural with import, but we should use the full name\n",
"import babel.messages.plurals\n", "import babel.messages.plurals\n",
@ -70,22 +58,11 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 10, "execution_count": null,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
"outputs": [ "outputs": [],
{
"data": {
"text/plain": [
"(2, '(n != 1)')"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [ "source": [
"import babel.messages.plurals\n", "import babel.messages.plurals\n",
"babel.messages.plurals.get_plural()" "babel.messages.plurals.get_plural()"
@ -93,22 +70,11 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 12, "execution_count": null,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
"outputs": [ "outputs": [],
{
"data": {
"text/plain": [
"(2, '(n != 1)')"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [ "source": [
"from babel.messages import plurals # with from-import, we can use the short name\n", "from babel.messages import plurals # with from-import, we can use the short name\n",
"plurals.get_plural()" "plurals.get_plural()"
@ -116,22 +82,11 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 14, "execution_count": null,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
"outputs": [ "outputs": [],
{
"data": {
"text/plain": [
"(2, '(n != 1)')"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [ "source": [
"from babel.messages.plurals import get_plural # now we can use directly get_plural()\n", "from babel.messages.plurals import get_plural # now we can use directly get_plural()\n",
"get_plural()" "get_plural()"
@ -159,7 +114,7 @@
"source": [ "source": [
"The notebook is freely licensed under under the [Creative Commons Attribution Share-Alike license](https://creativecommons.org/licenses/by/2.0/). \n", "The notebook is freely licensed under under the [Creative Commons Attribution Share-Alike license](https://creativecommons.org/licenses/by/2.0/). \n",
"\n", "\n",
"© 2015 Carlos A. Iglesias, Universidad Politécnica de Madrid." "© 2016 Carlos A. Iglesias, Universidad Politécnica de Madrid."
] ]
} }
], ],

Loading…
Cancel
Save