mirror of
				https://github.com/gsi-upm/sitc
				synced 2025-11-04 01:18:16 +00:00 
			
		
		
		
	
		
			
				
	
	
		
			1106 lines
		
	
	
		
			20 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
	
	
			
		
		
	
	
			1106 lines
		
	
	
		
			20 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
	
	
{
 | 
						|
 "cells": [
 | 
						|
  {
 | 
						|
   "cell_type": "markdown",
 | 
						|
   "metadata": {},
 | 
						|
   "source": [
 | 
						|
    ""
 | 
						|
   ]
 | 
						|
  },
 | 
						|
  {
 | 
						|
   "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": [
 | 
						|
    "# Built-in Types: Sequences"
 | 
						|
   ]
 | 
						|
  },
 | 
						|
  {
 | 
						|
   "cell_type": "markdown",
 | 
						|
   "metadata": {},
 | 
						|
   "source": [
 | 
						|
    "**Sequences** are ordered collections of elements. The basic built-int sequence types are **lists**, **tuples** and **range** objets. There other sequence types for text strings (str, Strings) or binary data.\n",
 | 
						|
    "\n",
 | 
						|
    "Sequences can be mutable or inmutable, if their value can be changed or not, respectively.\n",
 | 
						|
    "* **lists** are mutable ordered sequences of values\n",
 | 
						|
    "* **tuples** are inmutable ordered sequences of values\n",
 | 
						|
    "* **ranges** are  immutable sequences of numbers"
 | 
						|
   ]
 | 
						|
  },
 | 
						|
  {
 | 
						|
   "cell_type": "markdown",
 | 
						|
   "metadata": {
 | 
						|
    "collapsed": true
 | 
						|
   },
 | 
						|
   "source": [
 | 
						|
    "## 1. Lists"
 | 
						|
   ]
 | 
						|
  },
 | 
						|
  {
 | 
						|
   "cell_type": "code",
 | 
						|
   "execution_count": 137,
 | 
						|
   "metadata": {
 | 
						|
    "collapsed": false
 | 
						|
   },
 | 
						|
   "outputs": [],
 | 
						|
   "source": [
 | 
						|
    "l = [1, 2, 3, 4, 5, 6]"
 | 
						|
   ]
 | 
						|
  },
 | 
						|
  {
 | 
						|
   "cell_type": "code",
 | 
						|
   "execution_count": 138,
 | 
						|
   "metadata": {
 | 
						|
    "collapsed": false
 | 
						|
   },
 | 
						|
   "outputs": [
 | 
						|
    {
 | 
						|
     "data": {
 | 
						|
      "text/plain": [
 | 
						|
       "[1, 2, 3, 4, 5, 6]"
 | 
						|
      ]
 | 
						|
     },
 | 
						|
     "execution_count": 138,
 | 
						|
     "metadata": {},
 | 
						|
     "output_type": "execute_result"
 | 
						|
    }
 | 
						|
   ],
 | 
						|
   "source": [
 | 
						|
    "l"
 | 
						|
   ]
 | 
						|
  },
 | 
						|
  {
 | 
						|
   "cell_type": "code",
 | 
						|
   "execution_count": 139,
 | 
						|
   "metadata": {
 | 
						|
    "collapsed": false
 | 
						|
   },
 | 
						|
   "outputs": [
 | 
						|
    {
 | 
						|
     "data": {
 | 
						|
      "text/plain": [
 | 
						|
       "[1, 2, 3]"
 | 
						|
      ]
 | 
						|
     },
 | 
						|
     "execution_count": 139,
 | 
						|
     "metadata": {},
 | 
						|
     "output_type": "execute_result"
 | 
						|
    }
 | 
						|
   ],
 | 
						|
   "source": [
 | 
						|
    "l[0:3] # we can use slicing in sequence types"
 | 
						|
   ]
 | 
						|
  },
 | 
						|
  {
 | 
						|
   "cell_type": "code",
 | 
						|
   "execution_count": 140,
 | 
						|
   "metadata": {
 | 
						|
    "collapsed": true
 | 
						|
   },
 | 
						|
   "outputs": [],
 | 
						|
   "source": [
 | 
						|
    "other_list = [1, 0.0, \"hola\"] #lists can have elements of different types"
 | 
						|
   ]
 | 
						|
  },
 | 
						|
  {
 | 
						|
   "cell_type": "code",
 | 
						|
   "execution_count": 141,
 | 
						|
   "metadata": {
 | 
						|
    "collapsed": false
 | 
						|
   },
 | 
						|
   "outputs": [
 | 
						|
    {
 | 
						|
     "data": {
 | 
						|
      "text/plain": [
 | 
						|
       "[1, 0.0, 'hola']"
 | 
						|
      ]
 | 
						|
     },
 | 
						|
     "execution_count": 141,
 | 
						|
     "metadata": {},
 | 
						|
     "output_type": "execute_result"
 | 
						|
    }
 | 
						|
   ],
 | 
						|
   "source": [
 | 
						|
    "other_list"
 | 
						|
   ]
 | 
						|
  },
 | 
						|
  {
 | 
						|
   "cell_type": "code",
 | 
						|
   "execution_count": 142,
 | 
						|
   "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"
 | 
						|
    }
 | 
						|
   ],
 | 
						|
   "source": [
 | 
						|
    "l + other_list # we can add lists (append)"
 | 
						|
   ]
 | 
						|
  },
 | 
						|
  {
 | 
						|
   "cell_type": "code",
 | 
						|
   "execution_count": 143,
 | 
						|
   "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"
 | 
						|
    }
 | 
						|
   ],
 | 
						|
   "source": [
 | 
						|
    "l * 3 # we can add n times a list"
 | 
						|
   ]
 | 
						|
  },
 | 
						|
  {
 | 
						|
   "cell_type": "code",
 | 
						|
   "execution_count": 144,
 | 
						|
   "metadata": {
 | 
						|
    "collapsed": false
 | 
						|
   },
 | 
						|
   "outputs": [
 | 
						|
    {
 | 
						|
     "data": {
 | 
						|
      "text/plain": [
 | 
						|
       "6"
 | 
						|
      ]
 | 
						|
     },
 | 
						|
     "execution_count": 144,
 | 
						|
     "metadata": {},
 | 
						|
     "output_type": "execute_result"
 | 
						|
    }
 | 
						|
   ],
 | 
						|
   "source": [
 | 
						|
    "len(l) # length of a list (as Strings)"
 | 
						|
   ]
 | 
						|
  },
 | 
						|
  {
 | 
						|
   "cell_type": "code",
 | 
						|
   "execution_count": 145,
 | 
						|
   "metadata": {
 | 
						|
    "collapsed": true
 | 
						|
   },
 | 
						|
   "outputs": [],
 | 
						|
   "source": [
 | 
						|
    "l.append(7) #append at the end of the list. Check help with Shift-tab, and methods with tab"
 | 
						|
   ]
 | 
						|
  },
 | 
						|
  {
 | 
						|
   "cell_type": "code",
 | 
						|
   "execution_count": 146,
 | 
						|
   "metadata": {
 | 
						|
    "collapsed": false
 | 
						|
   },
 | 
						|
   "outputs": [
 | 
						|
    {
 | 
						|
     "data": {
 | 
						|
      "text/plain": [
 | 
						|
       "[1, 2, 3, 4, 5, 6, 7]"
 | 
						|
      ]
 | 
						|
     },
 | 
						|
     "execution_count": 146,
 | 
						|
     "metadata": {},
 | 
						|
     "output_type": "execute_result"
 | 
						|
    }
 | 
						|
   ],
 | 
						|
   "source": [
 | 
						|
    "l"
 | 
						|
   ]
 | 
						|
  },
 | 
						|
  {
 | 
						|
   "cell_type": "code",
 | 
						|
   "execution_count": 147,
 | 
						|
   "metadata": {
 | 
						|
    "collapsed": false
 | 
						|
   },
 | 
						|
   "outputs": [
 | 
						|
    {
 | 
						|
     "data": {
 | 
						|
      "text/plain": [
 | 
						|
       "7"
 | 
						|
      ]
 | 
						|
     },
 | 
						|
     "execution_count": 147,
 | 
						|
     "metadata": {},
 | 
						|
     "output_type": "execute_result"
 | 
						|
    }
 | 
						|
   ],
 | 
						|
   "source": [
 | 
						|
    "l.pop() # remove last element"
 | 
						|
   ]
 | 
						|
  },
 | 
						|
  {
 | 
						|
   "cell_type": "code",
 | 
						|
   "execution_count": 148,
 | 
						|
   "metadata": {
 | 
						|
    "collapsed": false
 | 
						|
   },
 | 
						|
   "outputs": [
 | 
						|
    {
 | 
						|
     "data": {
 | 
						|
      "text/plain": [
 | 
						|
       "[1, 2, 3, 4, 5, 6]"
 | 
						|
      ]
 | 
						|
     },
 | 
						|
     "execution_count": 148,
 | 
						|
     "metadata": {},
 | 
						|
     "output_type": "execute_result"
 | 
						|
    }
 | 
						|
   ],
 | 
						|
   "source": [
 | 
						|
    "l"
 | 
						|
   ]
 | 
						|
  },
 | 
						|
  {
 | 
						|
   "cell_type": "code",
 | 
						|
   "execution_count": 149,
 | 
						|
   "metadata": {
 | 
						|
    "collapsed": false
 | 
						|
   },
 | 
						|
   "outputs": [
 | 
						|
    {
 | 
						|
     "data": {
 | 
						|
      "text/plain": [
 | 
						|
       "3"
 | 
						|
      ]
 | 
						|
     },
 | 
						|
     "execution_count": 149,
 | 
						|
     "metadata": {},
 | 
						|
     "output_type": "execute_result"
 | 
						|
    }
 | 
						|
   ],
 | 
						|
   "source": [
 | 
						|
    "l.pop(2) # remove element at index 2"
 | 
						|
   ]
 | 
						|
  },
 | 
						|
  {
 | 
						|
   "cell_type": "code",
 | 
						|
   "execution_count": 150,
 | 
						|
   "metadata": {
 | 
						|
    "collapsed": false
 | 
						|
   },
 | 
						|
   "outputs": [
 | 
						|
    {
 | 
						|
     "data": {
 | 
						|
      "text/plain": [
 | 
						|
       "[1, 2, 4, 5, 6]"
 | 
						|
      ]
 | 
						|
     },
 | 
						|
     "execution_count": 150,
 | 
						|
     "metadata": {},
 | 
						|
     "output_type": "execute_result"
 | 
						|
    }
 | 
						|
   ],
 | 
						|
   "source": [
 | 
						|
    "l"
 | 
						|
   ]
 | 
						|
  },
 | 
						|
  {
 | 
						|
   "cell_type": "code",
 | 
						|
   "execution_count": null,
 | 
						|
   "metadata": {
 | 
						|
    "collapsed": true
 | 
						|
   },
 | 
						|
   "outputs": [],
 | 
						|
   "source": []
 | 
						|
  },
 | 
						|
  {
 | 
						|
   "cell_type": "code",
 | 
						|
   "execution_count": 151,
 | 
						|
   "metadata": {
 | 
						|
    "collapsed": false
 | 
						|
   },
 | 
						|
   "outputs": [],
 | 
						|
   "source": [
 | 
						|
    "l.insert(2,3) # insert at index 2 the value 3"
 | 
						|
   ]
 | 
						|
  },
 | 
						|
  {
 | 
						|
   "cell_type": "code",
 | 
						|
   "execution_count": 152,
 | 
						|
   "metadata": {
 | 
						|
    "collapsed": false
 | 
						|
   },
 | 
						|
   "outputs": [
 | 
						|
    {
 | 
						|
     "data": {
 | 
						|
      "text/plain": [
 | 
						|
       "[1, 2, 3, 4, 5, 6]"
 | 
						|
      ]
 | 
						|
     },
 | 
						|
     "execution_count": 152,
 | 
						|
     "metadata": {},
 | 
						|
     "output_type": "execute_result"
 | 
						|
    }
 | 
						|
   ],
 | 
						|
   "source": [
 | 
						|
    "l"
 | 
						|
   ]
 | 
						|
  },
 | 
						|
  {
 | 
						|
   "cell_type": "code",
 | 
						|
   "execution_count": 153,
 | 
						|
   "metadata": {
 | 
						|
    "collapsed": true
 | 
						|
   },
 | 
						|
   "outputs": [],
 | 
						|
   "source": [
 | 
						|
    "l.reverse()"
 | 
						|
   ]
 | 
						|
  },
 | 
						|
  {
 | 
						|
   "cell_type": "code",
 | 
						|
   "execution_count": 154,
 | 
						|
   "metadata": {
 | 
						|
    "collapsed": false
 | 
						|
   },
 | 
						|
   "outputs": [
 | 
						|
    {
 | 
						|
     "data": {
 | 
						|
      "text/plain": [
 | 
						|
       "[6, 5, 4, 3, 2, 1]"
 | 
						|
      ]
 | 
						|
     },
 | 
						|
     "execution_count": 154,
 | 
						|
     "metadata": {},
 | 
						|
     "output_type": "execute_result"
 | 
						|
    }
 | 
						|
   ],
 | 
						|
   "source": [
 | 
						|
    "l"
 | 
						|
   ]
 | 
						|
  },
 | 
						|
  {
 | 
						|
   "cell_type": "code",
 | 
						|
   "execution_count": 155,
 | 
						|
   "metadata": {
 | 
						|
    "collapsed": true
 | 
						|
   },
 | 
						|
   "outputs": [],
 | 
						|
   "source": [
 | 
						|
    "l.sort()"
 | 
						|
   ]
 | 
						|
  },
 | 
						|
  {
 | 
						|
   "cell_type": "code",
 | 
						|
   "execution_count": 156,
 | 
						|
   "metadata": {
 | 
						|
    "collapsed": false
 | 
						|
   },
 | 
						|
   "outputs": [
 | 
						|
    {
 | 
						|
     "data": {
 | 
						|
      "text/plain": [
 | 
						|
       "[1, 2, 3, 4, 5, 6]"
 | 
						|
      ]
 | 
						|
     },
 | 
						|
     "execution_count": 156,
 | 
						|
     "metadata": {},
 | 
						|
     "output_type": "execute_result"
 | 
						|
    }
 | 
						|
   ],
 | 
						|
   "source": [
 | 
						|
    "l"
 | 
						|
   ]
 | 
						|
  },
 | 
						|
  {
 | 
						|
   "cell_type": "code",
 | 
						|
   "execution_count": 157,
 | 
						|
   "metadata": {
 | 
						|
    "collapsed": true
 | 
						|
   },
 | 
						|
   "outputs": [],
 | 
						|
   "source": [
 | 
						|
    "l.remove(3) # remove first ocurrence of 3 from l. Remember: remove (element) vs pop(index)"
 | 
						|
   ]
 | 
						|
  },
 | 
						|
  {
 | 
						|
   "cell_type": "code",
 | 
						|
   "execution_count": 158,
 | 
						|
   "metadata": {
 | 
						|
    "collapsed": false
 | 
						|
   },
 | 
						|
   "outputs": [
 | 
						|
    {
 | 
						|
     "data": {
 | 
						|
      "text/plain": [
 | 
						|
       "[1, 2, 4, 5, 6]"
 | 
						|
      ]
 | 
						|
     },
 | 
						|
     "execution_count": 158,
 | 
						|
     "metadata": {},
 | 
						|
     "output_type": "execute_result"
 | 
						|
    }
 | 
						|
   ],
 | 
						|
   "source": [
 | 
						|
    "l"
 | 
						|
   ]
 | 
						|
  },
 | 
						|
  {
 | 
						|
   "cell_type": "code",
 | 
						|
   "execution_count": 159,
 | 
						|
   "metadata": {
 | 
						|
    "collapsed": false
 | 
						|
   },
 | 
						|
   "outputs": [],
 | 
						|
   "source": [
 | 
						|
    "l[0] = 0 # lists are mutable"
 | 
						|
   ]
 | 
						|
  },
 | 
						|
  {
 | 
						|
   "cell_type": "code",
 | 
						|
   "execution_count": 160,
 | 
						|
   "metadata": {
 | 
						|
    "collapsed": false
 | 
						|
   },
 | 
						|
   "outputs": [
 | 
						|
    {
 | 
						|
     "data": {
 | 
						|
      "text/plain": [
 | 
						|
       "[0, 2, 4, 5, 6]"
 | 
						|
      ]
 | 
						|
     },
 | 
						|
     "execution_count": 160,
 | 
						|
     "metadata": {},
 | 
						|
     "output_type": "execute_result"
 | 
						|
    }
 | 
						|
   ],
 | 
						|
   "source": [
 | 
						|
    "l"
 | 
						|
   ]
 | 
						|
  },
 | 
						|
  {
 | 
						|
   "cell_type": "code",
 | 
						|
   "execution_count": 161,
 | 
						|
   "metadata": {
 | 
						|
    "collapsed": false
 | 
						|
   },
 | 
						|
   "outputs": [
 | 
						|
    {
 | 
						|
     "data": {
 | 
						|
      "text/plain": [
 | 
						|
       "True"
 | 
						|
      ]
 | 
						|
     },
 | 
						|
     "execution_count": 161,
 | 
						|
     "metadata": {},
 | 
						|
     "output_type": "execute_result"
 | 
						|
    }
 | 
						|
   ],
 | 
						|
   "source": [
 | 
						|
    "2 in l # check if an element is in a list"
 | 
						|
   ]
 | 
						|
  },
 | 
						|
  {
 | 
						|
   "cell_type": "code",
 | 
						|
   "execution_count": 162,
 | 
						|
   "metadata": {
 | 
						|
    "collapsed": false
 | 
						|
   },
 | 
						|
   "outputs": [
 | 
						|
    {
 | 
						|
     "data": {
 | 
						|
      "text/plain": [
 | 
						|
       "False"
 | 
						|
      ]
 | 
						|
     },
 | 
						|
     "execution_count": 162,
 | 
						|
     "metadata": {},
 | 
						|
     "output_type": "execute_result"
 | 
						|
    }
 | 
						|
   ],
 | 
						|
   "source": [
 | 
						|
    "7 in l # check if an element is in a list "
 | 
						|
   ]
 | 
						|
  },
 | 
						|
  {
 | 
						|
   "cell_type": "code",
 | 
						|
   "execution_count": 163,
 | 
						|
   "metadata": {
 | 
						|
    "collapsed": false
 | 
						|
   },
 | 
						|
   "outputs": [
 | 
						|
    {
 | 
						|
     "data": {
 | 
						|
      "text/plain": [
 | 
						|
       "False"
 | 
						|
      ]
 | 
						|
     },
 | 
						|
     "execution_count": 163,
 | 
						|
     "metadata": {},
 | 
						|
     "output_type": "execute_result"
 | 
						|
    }
 | 
						|
   ],
 | 
						|
   "source": [
 | 
						|
    "4 not in l # check if an element is not in a list"
 | 
						|
   ]
 | 
						|
  },
 | 
						|
  {
 | 
						|
   "cell_type": "code",
 | 
						|
   "execution_count": 164,
 | 
						|
   "metadata": {
 | 
						|
    "collapsed": false
 | 
						|
   },
 | 
						|
   "outputs": [
 | 
						|
    {
 | 
						|
     "data": {
 | 
						|
      "text/plain": [
 | 
						|
       "2"
 | 
						|
      ]
 | 
						|
     },
 | 
						|
     "execution_count": 164,
 | 
						|
     "metadata": {},
 | 
						|
     "output_type": "execute_result"
 | 
						|
    }
 | 
						|
   ],
 | 
						|
   "source": [
 | 
						|
    "l.index(4) # search for an item"
 | 
						|
   ]
 | 
						|
  },
 | 
						|
  {
 | 
						|
   "cell_type": "code",
 | 
						|
   "execution_count": 165,
 | 
						|
   "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<ipython-input-165-f8d9d96086c8>\u001b[0m in \u001b[0;36m<module>\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"
 | 
						|
     ]
 | 
						|
    }
 | 
						|
   ],
 | 
						|
   "source": [
 | 
						|
    "l.index(-1) # search for an item, error since it is not in the list"
 | 
						|
   ]
 | 
						|
  },
 | 
						|
  {
 | 
						|
   "cell_type": "code",
 | 
						|
   "execution_count": 166,
 | 
						|
   "metadata": {
 | 
						|
    "collapsed": false
 | 
						|
   },
 | 
						|
   "outputs": [],
 | 
						|
   "source": [
 | 
						|
    "matrix = [[1,2], [3,4]] # matrix"
 | 
						|
   ]
 | 
						|
  },
 | 
						|
  {
 | 
						|
   "cell_type": "code",
 | 
						|
   "execution_count": 167,
 | 
						|
   "metadata": {
 | 
						|
    "collapsed": false
 | 
						|
   },
 | 
						|
   "outputs": [
 | 
						|
    {
 | 
						|
     "data": {
 | 
						|
      "text/plain": [
 | 
						|
       "[[1, 2], [3, 4]]"
 | 
						|
      ]
 | 
						|
     },
 | 
						|
     "execution_count": 167,
 | 
						|
     "metadata": {},
 | 
						|
     "output_type": "execute_result"
 | 
						|
    }
 | 
						|
   ],
 | 
						|
   "source": [
 | 
						|
    "matrix"
 | 
						|
   ]
 | 
						|
  },
 | 
						|
  {
 | 
						|
   "cell_type": "code",
 | 
						|
   "execution_count": 168,
 | 
						|
   "metadata": {
 | 
						|
    "collapsed": false
 | 
						|
   },
 | 
						|
   "outputs": [
 | 
						|
    {
 | 
						|
     "data": {
 | 
						|
      "text/plain": [
 | 
						|
       "1"
 | 
						|
      ]
 | 
						|
     },
 | 
						|
     "execution_count": 168,
 | 
						|
     "metadata": {},
 | 
						|
     "output_type": "execute_result"
 | 
						|
    }
 | 
						|
   ],
 | 
						|
   "source": [
 | 
						|
    "matrix[0][0]"
 | 
						|
   ]
 | 
						|
  },
 | 
						|
  {
 | 
						|
   "cell_type": "code",
 | 
						|
   "execution_count": 169,
 | 
						|
   "metadata": {
 | 
						|
    "collapsed": false
 | 
						|
   },
 | 
						|
   "outputs": [
 | 
						|
    {
 | 
						|
     "data": {
 | 
						|
      "text/plain": [
 | 
						|
       "2"
 | 
						|
      ]
 | 
						|
     },
 | 
						|
     "execution_count": 169,
 | 
						|
     "metadata": {},
 | 
						|
     "output_type": "execute_result"
 | 
						|
    }
 | 
						|
   ],
 | 
						|
   "source": [
 | 
						|
    "matrix[0][1]"
 | 
						|
   ]
 | 
						|
  },
 | 
						|
  {
 | 
						|
   "cell_type": "code",
 | 
						|
   "execution_count": 170,
 | 
						|
   "metadata": {
 | 
						|
    "collapsed": false
 | 
						|
   },
 | 
						|
   "outputs": [
 | 
						|
    {
 | 
						|
     "data": {
 | 
						|
      "text/plain": [
 | 
						|
       "list"
 | 
						|
      ]
 | 
						|
     },
 | 
						|
     "execution_count": 170,
 | 
						|
     "metadata": {},
 | 
						|
     "output_type": "execute_result"
 | 
						|
    }
 | 
						|
   ],
 | 
						|
   "source": [
 | 
						|
    "type(matrix)"
 | 
						|
   ]
 | 
						|
  },
 | 
						|
  {
 | 
						|
   "cell_type": "markdown",
 | 
						|
   "metadata": {},
 | 
						|
   "source": [
 | 
						|
    "## 2. Tuples"
 | 
						|
   ]
 | 
						|
  },
 | 
						|
  {
 | 
						|
   "cell_type": "markdown",
 | 
						|
   "metadata": {},
 | 
						|
   "source": [
 | 
						|
    "Tuples are *inmutable* sequences, tipically used to store collections of heterogeneous data.\n",
 | 
						|
    "\n",
 | 
						|
    "The main difference from lists is that lists are *mutable* while tuples are *inmutable*. Thus, tuples have not methods such as insert(), pop(), remove() or append() to change them.\n",
 | 
						|
    "\n",
 | 
						|
    "Tuples are faster than lists. Its main usage is when the collection is constant, or you do not want it can be changed (write protected). \n",
 | 
						|
    "\n",
 | 
						|
    "Tuples can be converted into lists and vice-versa, with the methods list() and tuple()."
 | 
						|
   ]
 | 
						|
  },
 | 
						|
  {
 | 
						|
   "cell_type": "code",
 | 
						|
   "execution_count": 171,
 | 
						|
   "metadata": {
 | 
						|
    "collapsed": false
 | 
						|
   },
 | 
						|
   "outputs": [],
 | 
						|
   "source": [
 | 
						|
    "tuple = ('a', 1)"
 | 
						|
   ]
 | 
						|
  },
 | 
						|
  {
 | 
						|
   "cell_type": "code",
 | 
						|
   "execution_count": 172,
 | 
						|
   "metadata": {
 | 
						|
    "collapsed": false
 | 
						|
   },
 | 
						|
   "outputs": [
 | 
						|
    {
 | 
						|
     "data": {
 | 
						|
      "text/plain": [
 | 
						|
       "('a', 1)"
 | 
						|
      ]
 | 
						|
     },
 | 
						|
     "execution_count": 172,
 | 
						|
     "metadata": {},
 | 
						|
     "output_type": "execute_result"
 | 
						|
    }
 | 
						|
   ],
 | 
						|
   "source": [
 | 
						|
    "tuple"
 | 
						|
   ]
 | 
						|
  },
 | 
						|
  {
 | 
						|
   "cell_type": "markdown",
 | 
						|
   "metadata": {
 | 
						|
    "collapsed": false
 | 
						|
   },
 | 
						|
   "source": [
 | 
						|
    "Tuples implement all the common [sequence operators](https://docs.python.org/3/library/stdtypes.html#typesseq-common), such as slicing, concatenation, len, etc."
 | 
						|
   ]
 | 
						|
  },
 | 
						|
  {
 | 
						|
   "cell_type": "code",
 | 
						|
   "execution_count": 173,
 | 
						|
   "metadata": {
 | 
						|
    "collapsed": false
 | 
						|
   },
 | 
						|
   "outputs": [
 | 
						|
    {
 | 
						|
     "data": {
 | 
						|
      "text/plain": [
 | 
						|
       "(1, 'a')"
 | 
						|
      ]
 | 
						|
     },
 | 
						|
     "execution_count": 173,
 | 
						|
     "metadata": {},
 | 
						|
     "output_type": "execute_result"
 | 
						|
    }
 | 
						|
   ],
 | 
						|
   "source": [
 | 
						|
    "tuple[::-1]"
 | 
						|
   ]
 | 
						|
  },
 | 
						|
  {
 | 
						|
   "cell_type": "code",
 | 
						|
   "execution_count": 174,
 | 
						|
   "metadata": {
 | 
						|
    "collapsed": false
 | 
						|
   },
 | 
						|
   "outputs": [
 | 
						|
    {
 | 
						|
     "data": {
 | 
						|
      "text/plain": [
 | 
						|
       "2"
 | 
						|
      ]
 | 
						|
     },
 | 
						|
     "execution_count": 174,
 | 
						|
     "metadata": {},
 | 
						|
     "output_type": "execute_result"
 | 
						|
    }
 | 
						|
   ],
 | 
						|
   "source": [
 | 
						|
    "len(tuple)"
 | 
						|
   ]
 | 
						|
  },
 | 
						|
  {
 | 
						|
   "cell_type": "code",
 | 
						|
   "execution_count": 175,
 | 
						|
   "metadata": {
 | 
						|
    "collapsed": false
 | 
						|
   },
 | 
						|
   "outputs": [
 | 
						|
    {
 | 
						|
     "data": {
 | 
						|
      "text/plain": [
 | 
						|
       "('a', 1, 'a', 1, 'b', 'c', 2.1, True)"
 | 
						|
      ]
 | 
						|
     },
 | 
						|
     "execution_count": 175,
 | 
						|
     "metadata": {},
 | 
						|
     "output_type": "execute_result"
 | 
						|
    }
 | 
						|
   ],
 | 
						|
   "source": [
 | 
						|
    "tuple * 2 + ('b', 'c', 2.1, True)"
 | 
						|
   ]
 | 
						|
  },
 | 
						|
  {
 | 
						|
   "cell_type": "code",
 | 
						|
   "execution_count": 176,
 | 
						|
   "metadata": {
 | 
						|
    "collapsed": false
 | 
						|
   },
 | 
						|
   "outputs": [
 | 
						|
    {
 | 
						|
     "data": {
 | 
						|
      "text/plain": [
 | 
						|
       "1"
 | 
						|
      ]
 | 
						|
     },
 | 
						|
     "execution_count": 176,
 | 
						|
     "metadata": {},
 | 
						|
     "output_type": "execute_result"
 | 
						|
    }
 | 
						|
   ],
 | 
						|
   "source": [
 | 
						|
    "tuple[1]"
 | 
						|
   ]
 | 
						|
  },
 | 
						|
  {
 | 
						|
   "cell_type": "code",
 | 
						|
   "execution_count": 177,
 | 
						|
   "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<ipython-input-177-8dc8d238809d>\u001b[0m in \u001b[0;36m<module>\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"
 | 
						|
     ]
 | 
						|
    }
 | 
						|
   ],
 | 
						|
   "source": [
 | 
						|
    "tuple[1] = 2 # Error, tuples are inmutable"
 | 
						|
   ]
 | 
						|
  },
 | 
						|
  {
 | 
						|
   "cell_type": "code",
 | 
						|
   "execution_count": 178,
 | 
						|
   "metadata": {
 | 
						|
    "collapsed": false
 | 
						|
   },
 | 
						|
   "outputs": [
 | 
						|
    {
 | 
						|
     "data": {
 | 
						|
      "text/plain": [
 | 
						|
       "tuple"
 | 
						|
      ]
 | 
						|
     },
 | 
						|
     "execution_count": 178,
 | 
						|
     "metadata": {},
 | 
						|
     "output_type": "execute_result"
 | 
						|
    }
 | 
						|
   ],
 | 
						|
   "source": [
 | 
						|
    "type(tuple)"
 | 
						|
   ]
 | 
						|
  },
 | 
						|
  {
 | 
						|
   "cell_type": "markdown",
 | 
						|
   "metadata": {},
 | 
						|
   "source": [
 | 
						|
    "## 3. Range"
 | 
						|
   ]
 | 
						|
  },
 | 
						|
  {
 | 
						|
   "cell_type": "markdown",
 | 
						|
   "metadata": {
 | 
						|
    "collapsed": true
 | 
						|
   },
 | 
						|
   "source": [
 | 
						|
    "A [range](https://docs.python.org/3/library/stdtypes.html#range) represents an immutable sequence of numbers. Ranges are created with two constructors: *range(stop)* or *range(start, stop, [step])*. \n",
 | 
						|
    "\n",
 | 
						|
    "The default *start* is 0 and the default *step* is 1. It generates a sequence [start, stop)"
 | 
						|
   ]
 | 
						|
  },
 | 
						|
  {
 | 
						|
   "cell_type": "code",
 | 
						|
   "execution_count": 179,
 | 
						|
   "metadata": {
 | 
						|
    "collapsed": false
 | 
						|
   },
 | 
						|
   "outputs": [],
 | 
						|
   "source": [
 | 
						|
    "r = range(10)"
 | 
						|
   ]
 | 
						|
  },
 | 
						|
  {
 | 
						|
   "cell_type": "code",
 | 
						|
   "execution_count": 180,
 | 
						|
   "metadata": {
 | 
						|
    "collapsed": false
 | 
						|
   },
 | 
						|
   "outputs": [
 | 
						|
    {
 | 
						|
     "data": {
 | 
						|
      "text/plain": [
 | 
						|
       "range(0, 10)"
 | 
						|
      ]
 | 
						|
     },
 | 
						|
     "execution_count": 180,
 | 
						|
     "metadata": {},
 | 
						|
     "output_type": "execute_result"
 | 
						|
    }
 | 
						|
   ],
 | 
						|
   "source": [
 | 
						|
    "r"
 | 
						|
   ]
 | 
						|
  },
 | 
						|
  {
 | 
						|
   "cell_type": "code",
 | 
						|
   "execution_count": 181,
 | 
						|
   "metadata": {
 | 
						|
    "collapsed": false
 | 
						|
   },
 | 
						|
   "outputs": [
 | 
						|
    {
 | 
						|
     "data": {
 | 
						|
      "text/plain": [
 | 
						|
       "True"
 | 
						|
      ]
 | 
						|
     },
 | 
						|
     "execution_count": 181,
 | 
						|
     "metadata": {},
 | 
						|
     "output_type": "execute_result"
 | 
						|
    }
 | 
						|
   ],
 | 
						|
   "source": [
 | 
						|
    "5 in r # check if a number is in a range"
 | 
						|
   ]
 | 
						|
  },
 | 
						|
  {
 | 
						|
   "cell_type": "code",
 | 
						|
   "execution_count": 182,
 | 
						|
   "metadata": {
 | 
						|
    "collapsed": false
 | 
						|
   },
 | 
						|
   "outputs": [
 | 
						|
    {
 | 
						|
     "data": {
 | 
						|
      "text/plain": [
 | 
						|
       "2"
 | 
						|
      ]
 | 
						|
     },
 | 
						|
     "execution_count": 182,
 | 
						|
     "metadata": {},
 | 
						|
     "output_type": "execute_result"
 | 
						|
    }
 | 
						|
   ],
 | 
						|
   "source": [
 | 
						|
    "r[2] # Get a value"
 | 
						|
   ]
 | 
						|
  },
 | 
						|
  {
 | 
						|
   "cell_type": "code",
 | 
						|
   "execution_count": 183,
 | 
						|
   "metadata": {
 | 
						|
    "collapsed": false
 | 
						|
   },
 | 
						|
   "outputs": [
 | 
						|
    {
 | 
						|
     "data": {
 | 
						|
      "text/plain": [
 | 
						|
       "range"
 | 
						|
      ]
 | 
						|
     },
 | 
						|
     "execution_count": 183,
 | 
						|
     "metadata": {},
 | 
						|
     "output_type": "execute_result"
 | 
						|
    }
 | 
						|
   ],
 | 
						|
   "source": [
 | 
						|
    "type(r)"
 | 
						|
   ]
 | 
						|
  },
 | 
						|
  {
 | 
						|
   "cell_type": "code",
 | 
						|
   "execution_count": 184,
 | 
						|
   "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"
 | 
						|
    }
 | 
						|
   ],
 | 
						|
   "source": [
 | 
						|
    "list(range(10))"
 | 
						|
   ]
 | 
						|
  },
 | 
						|
  {
 | 
						|
   "cell_type": "code",
 | 
						|
   "execution_count": 185,
 | 
						|
   "metadata": {
 | 
						|
    "collapsed": false
 | 
						|
   },
 | 
						|
   "outputs": [
 | 
						|
    {
 | 
						|
     "data": {
 | 
						|
      "text/plain": [
 | 
						|
       "[1, 3, 5, 7, 9]"
 | 
						|
      ]
 | 
						|
     },
 | 
						|
     "execution_count": 185,
 | 
						|
     "metadata": {},
 | 
						|
     "output_type": "execute_result"
 | 
						|
    }
 | 
						|
   ],
 | 
						|
   "source": [
 | 
						|
    "list(range(1,10,2))"
 | 
						|
   ]
 | 
						|
  },
 | 
						|
  {
 | 
						|
   "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.4.3+"
 | 
						|
  }
 | 
						|
 },
 | 
						|
 "nbformat": 4,
 | 
						|
 "nbformat_minor": 0
 | 
						|
}
 |