{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "![](http://www.upm.es/sfs/Rectorado/Gabinete%20del%20Rector/Logos/UPM/EscPolitecnica/EscUpmPolit_p.gif \"UPM\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Course Notes for Learning Intelligent Systems" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Department of Telematic Engineering Systems, Universidad Politécnica de Madrid, © 2015 Carlos A. Iglesias" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Control Flow statements" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Python ends a statement with a colon (:) and blocks are denoted by the level of indentation. " ] }, { "cell_type": "raw", "metadata": {}, "source": [ "Example in Java:\n", "\n", "if (a < c) {\n", " a++;\n", " b++;\n", "}\n" ] }, { "cell_type": "raw", "metadata": {}, "source": [ "Example in Python: (observe: no parenthesis (), no ending semicolons (;) but a colon is used (:)\n", "if a < c:\n", " a++\n", " b++" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "## 1. Conditional statements: if, elif, else" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "9" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import random # import random before using it\n", "x = random.randrange(1, 10) # generate a random integer between [1, 10] (both included)\n", "x" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Too small\n" ] } ], "source": [ "# Execute several times in order the previous cell and this one\n", "if x < 5:\n", " print(\"Too small\")\n", "elif x == 5:\n", " print(\"Just in the middle\")\n", "else: \n", " print(\"Too big\")" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Too small\n" ] } ], "source": [ "# Only one branch\n", "if x <= 5:\n", " print(\"Too small\")\n", "else:\n", " print(\"Too big\")" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Too big\n" ] } ], "source": [ "# Python has no switch statement for multiple branches\n", "if x <= 4:\n", " print(\"Too small\")\n", "elif x <= 5:\n", " print(\"Small\")\n", "elif x <= 7:\n", " print (\"Big\")\n", "else:\n", " print(\"Too big\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 2. Loops: for, while" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2.1. For" ] }, { "cell_type": "code", "execution_count": 27, "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" ] } ], "source": [ "# for with ranges\n", "for i in range(10): \n", " print(i)" ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "3\n", "4\n" ] } ], "source": [ "# for with lists\n", "l = [1, 2, 3, 4]\n", "for i in l:\n", " print(i)" ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "a\n", "b\n" ] } ], "source": [ "# for with tuples\n", "t = (1, 'a', 'b')\n", "for i in t:\n", " print(i)" ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "k2\n", "k1\n", "k3\n" ] } ], "source": [ "# for with dictionaries\n", "d = {'k1':'v1', 'k2':'v2', 'k3':'v3'}\n", "for i in d:\n", " print(i)" ] }, { "cell_type": "code", "execution_count": 34, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "k2 v2\n", "k1 v1\n", "k3 v3\n" ] } ], "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", " print(k, v)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2.2. While" ] }, { "cell_type": "code", "execution_count": 56, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5\n", "4\n", "3\n", "2\n" ] } ], "source": [ "x = 5\n", "while x > 1:\n", " print(x)\n", " x -= 1 # Python does not have x--" ] }, { "cell_type": "code", "execution_count": 45, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2\n", "3\n", "4\n" ] } ], "source": [ "# Else is optional\n", "x = 2\n", "while x < 5: \n", " print(x)\n", " x += 1" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false }, "source": [ "### 2.3. Break, continue, pass\n", "\n", "* **break**: break out of the smallest enclosing loop (for or while)\n", "* **continue**: continue with the next iteration of the loop\n", "* **pass**: do nothing\n", "\n", "Both *for* and *while* can include an optional statement else which is executed always unless a break statement is reached" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Goal found at index c\n", "End loop\n" ] } ], "source": [ "# Example find an element, else executed at the end\n", "list = ['a', 'b', 'c']\n", "goal = 'c'\n", "for i in list:\n", " if i == goal:\n", " print('Goal found at index', i)\n", "else:\n", " print('End loop')" ] }, { "cell_type": "code", "execution_count": 58, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "End loop\n" ] } ], "source": [ "# Example else\n", "list = []\n", "goal = 'c'\n", "for i in list:\n", " if i == goal:\n", " print('Goal found at index', i)\n", "else:\n", " print('End loop')" ] }, { "cell_type": "code", "execution_count": 60, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Goal found at index c\n" ] } ], "source": [ "# We improve above code with break\n", "# Example else\n", "list = ['a', 'b', 'c']\n", "goal = 'c'\n", "for i in list:\n", " if i == goal:\n", " print('Goal found at index', i)\n", " break\n", "else:\n", " print('Goal not found')" ] }, { "cell_type": "code", "execution_count": 62, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Goal not found\n" ] } ], "source": [ "# We improve above code with break\n", "# Example else\n", "list = ['a', 'b', 'c']\n", "goal = 'e'\n", "for i in list:\n", " if i == goal:\n", " print('Goal found at index', i)\n", " break\n", "else:\n", " print('Goal not found')" ] }, { "cell_type": "code", "execution_count": 50, "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" ] } ], "source": [ "# Print numbers from 0 to 15 which are not multiple of 3\n", "for i in range(15):\n", " if i % 3 == 0:\n", " continue\n", " print(i)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Found 4\n" ] } ], "source": [ "# Find the first occurrence of an element in a list\n", "l = [1, 2, 3, 4, 1, 2]\n", "goal = 4\n", "for n in l:\n", " if n == 4:\n", " print(\"Found\", n)\n", " break\n", "else:\n", " print(\"Not found\")" ] }, { "cell_type": "code", "execution_count": 64, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Big\n" ] } ], "source": [ "# Example of pass, when we do not want to do anything\n", "x = 4\n", "if x < 2:\n", " print(\"Low\")\n", "elif x >= 2:\n", " print(\"Big\")\n", "else:\n", " pass" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 3. List comprehension\n", "\n", "List comprehension are a simple way to create lists like we describe vectors in mathematics.\n", "\n", "S = {x² : x in {0 ... 9}}\n", "\n", "M = {x | x in S and x even}" ] }, { "cell_type": "code", "execution_count": 71, "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" } ], "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", "list = [x for x in range(10)]\n", "list" ] }, { "cell_type": "code", "execution_count": 70, "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" } ], "source": [ "# list = {x² : x in {0 ... 9}}\n", "list = [x*x for x in range(10)]\n", "list" ] }, { "cell_type": "code", "execution_count": 72, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[0, 4, 16, 36, 64]" ] }, "execution_count": 72, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# list = {x² : x in {0 ... 9}, x is even}\n", "list = [x*x for x in range(10) if x % 2 == 0]\n", "list" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Licence" ] }, { "cell_type": "markdown", "metadata": {}, "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." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.5.1" } }, "nbformat": 4, "nbformat_minor": 0 }