{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Loops and lists" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We want to print out a conversion table with degree Celsius in the first column and corresponding degree Farheneit in the second one\n", "\n", "**The formula for conversion**: $F = \\frac{9}{5}C + 32$\n", "\n", "We want to use it repeatedly for temperatures from -20C, with steps of 5C, up to +40C included " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [], "source": [ "C = -20; F = 9.0/5*C + 32; print C, F\n", "C = -15; F = 9.0/5*C + 32; print C, F\n", "C = -10; F = 9.0/5*C + 32; print C, F\n", "C = -5; F = 9.0/5*C + 32; print C, F\n", "C = 0; F = 9.0/5*C + 32; print C, F\n", "C = 5; F = 9.0/5*C + 32; print C, F\n", "C = 10; F = 9.0/5*C + 32; print C, F\n", "C = 15; F = 9.0/5*C + 32; print C, F\n", "C = 20; F = 9.0/5*C + 32; print C, F\n", "C = 25; F = 9.0/5*C + 32; print C, F\n", "C = 30; F = 9.0/5*C + 32; print C, F\n", "C = 35; F = 9.0/5*C + 32; print C, F\n", "C = 40; F = 9.0/5*C + 32; print C, F" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Identical statements are repeated, a task that should be automated using appropriate constructs\n", "\n", "* **WHILE loops **\n", "* **FOR loops**\n", "\n", "For storing and processing collections of data we use **lists**\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## WHILE loops\n", "\n", "A **WHILE loops** is used to repeat a set of statements, as long as some user-defined condition is verified\n", "\n", "We use the C2F example, with properly formatted output for the table entries and with lines of dashes above and below the table\n", "\n", "* Print dashed line\n", "* Let C = 20\n", "* WHILE C <= 40
\n", " - F = 9/5*C + 32
\n", " - PRINT C, PRINT F
\n", " - Increment C by 5
\n", "* Print dashed line\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "scrolled": false }, "outputs": [], "source": [ "print '------------------' # table heading\n", "C = -20 # start value for C\n", "dC = 5 # increment of C in loop\n", "while C <= 40: # loop heading with condition\n", " F = (9.0/5)*C + 32 # 1st statement inside loop\n", " print C, F # 2nd statement inside loop\n", " C = C + dC # 3rd statement inside loop\n", "print '------------------' # end of table line (after loop)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The **block of statements** is executed at each pass \n", "* The commands in the block are executed sequentially\n", "* The commands in the block must have the same indentation\n", "* Also the colon after is important to mark the beginning of the block" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**STEP-by-STEP**\n", "\n", "* We print a dashed line\n", "* We assign a start value to $C$ ($-20$) and we assign a value to the increment $dC$ ($5$)\n", "* We check (first time) the loop condition ($C = -20 <= 40$, satisfied)\n", "* Because the condition is satisfied, we enter the loop and start executing its commands (indented)
\n", " - We compute F for current $C$ ($-20$), $F = \\frac{9}{5}C + 32$ ($\\frac{9}{5}(-20) + 32 = -4$)\n", " - We print current $C$ ($-20$) and current $F$ ($-4$)\n", " - We increment current $C$ by $dC$, $C = C + dC$ ($-15 = -20 + 5$)\n", "* We exit the loop block and we check (second time) the loop condition ($C = -15 <= 40$, satisfied)\n", "* Because the condition is satisfied, we enter the loop and start executing its commands (indented)
\n", " - We compute F for current $C$ ($-15$), $F = \\frac{9}{5}C + 32$ ($\\frac{9}{5}(-15) + 32 = -5$)\n", " - We print current $C$ ($-15$) and current $F$ ($-5$)\n", " - We increment current $C$ by $dC$, $C = C + dC$ ($-10 = -15 + 5$)\n", "* We exit the loop block and we check (third time) the loop condition ($C = -10 <= 40$, satisfied)\n", "* Because the condition is satisfied, we enter the loop and start executing its commands (indented)
\n", " - ...\n", " - ... \n", " - ...\n", "* ...\n", "* ...
\n", " - ...\n", " - ... \n", " - We increment current $C$ by $dC$, $C = C + dC$ ($45 = 40 + 5$)\n", "* We exit the loop block and we check (n-th time) the loop condition ($C = 45 <= 40$, not satisfied)\n", "* Because the condition is not satisfied, we do not enter the loop\n", "* We execute the first unindented command, we print a dashed line\n", " " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Short-hand notations" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "C = 0; dC = 5;\n", "print C\n", "\n", "C += dC # equivalent to C = C + dC\n", "print C\n", "\n", "C -= dC # equivalent to C = C - dC\n", "print C\n", "C *= dC # equivalent to C = C*dC\n", "print C\n", "\n", "C /= dC # equivalent to C = C/dC\n", "print C" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What if, ... we indent the last command, too" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [], "source": [ "print '------------------' # table heading\n", "C = -20 # start value for C\n", "dC = 5 # increment of C in loop\n", "while C <= 40: # loop heading with condition\n", " F = (9.0/5)*C + 32 # 1st statement inside loop\n", " print C, F # 2nd statement inside loop\n", " C = C + dC # 3rd statement inside loop\n", " print '------------------' # end of table line (after loop)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* This is not what we wanted for our table" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Short-hand notations" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "C=0; print C # Example\n", "\n", "print C == 40 # C equals 40 (?) \n", "print C != 40 # C does not equal 40 (?)\n", "print C >= 40 # C is greater than or equal to 40 (?)\n", "print C > 40 # C is greater than 40 (?)\n", "print C < 40 # C is less than 40 (?)\n", "print C <= 40 # C is less than or equal to 40 (?)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The comparison expressions returned either TRUE or FALSE as their output\n", "* Logical or Boolean expressions\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Boolean expressions\n", "\n", "Any expression with boolean (True or False) value can be used\n", "\n", "The keyword NOT can be inserted in front of a Boolean expression to chenge its value\n", "* TRUE to FALSE, if originally TRUE\n", "* FALSE to TRUE, if originally FALSE" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print '---//---\\n'\n", "\n", "C = 1; print 'C = %d' %C \n", "print C == 40 # Is C = 40? Print the result: FALSE\n", "print not C == 40 # Is C = 40? Invert the result and print it: TRUE\n", "\n", "print '\\n---//---\\n'\n", "\n", "C = 40; print 'C = %d' %C \n", "print C == 40 # Is C = 40? Print the result: TRUE\n", "print not C == 40 # Is C = 40? Invert the result and print it: FALSE\n", "print C != 40 # Is C =/= 40? Print the result: FALSE\n", "\n", "print '\\n---//---'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It is also possible to combine boolena expressions using AND and OR\n", "* The goal is to define more complex expressions\n", "\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "x = 0; y = 1.2\n", "print 'x = %g' % x; \n", "print 'y = %g' % y\n", "\n", "print 'Are conditions x >= 0 AND y < 1 verified?', x >= 0 and y < 1 # Are x >= 0 AND y < 1?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print 'Are conditions x >= 0 OR y < 1 verified?', x >= 0 or y < 1 # Are x >=0 OR y < 1?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print 'Are conditions x > 0 OR y > 1 verified?' , x > 0 or y > 1 # Are x > 0 OR y > 1?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print 'Are conditions x > 0 OR (NOT y < 1) verified?', x > 0 or not y > 1 # Are x > 0 OR NOT y > 1?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print 'Does x belong to the interval (-1,0]?', -1 < x <= 0" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print '', not (x > 0 or y > 0)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "s = 'some string' # some string\n", "print s\n", "print bool(s)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "s = '' # empty string\n", "bool(s)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "L = [1, 4, 6]; print L\n", "print bool(L)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "L = []; print L\n", "bool(L)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "a = 88.0\n", "bool(a)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [], "source": [ "a = 0.0\n", "bool(a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Summation" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$\\sin{(x)} \\approx x - \\cfrac{x^3}{3!} + \\cfrac{x^5}{5!} - \\cfrac{x^7}{7!} + \\cdots$" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import math\n", "import time\n", "\n", "x = math.pi # assign some value\n", "N = 25 # maximum power in sum\n", "\n", "k = 1\n", "s = x\n", "sign = 1.0\n", "\n", "while k < N:\n", " print 'Partial summation for k={k} is {s}' .format(k=k, s=s)\n", " sign = - sign\n", " k = k + 2\n", " \n", " term = sign*x**k/math.factorial(k)\n", " \n", " s = s + term\n", " time.sleep(0.5) # Rests 0.5 seconds before next pass\n", "\n", "print 'sin(%g) = %g (approximation with %d terms)' % (x, s, N)\n", "print 'sin(x={x})= {sinx} (machine calculation)' .format(x=x, sinx=math.sin(x))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Lists" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Basic operations" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "C = [-20, -15, -10, -5, 0, 5, 10, 15, 20, 25, 30, 35, 40]; print C\n", "\n", "print '\\nC[3] is the 4-th element of list C: \\nThat is, C[3] equals %d' % C[3] " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "C = [-10, -5, 0, 5, 10, 15, 20, 25, 30] # create list\n", "C.append(35) # add new element 35 at the end\n", "\n", "print C # print list C\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "C = C + [40, 45] # extend C at the end\n", "print C" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "C.insert(0, -15) # insert new element -15 as index 0\n", "print C" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "del C[2] # delete 3rd element\n", "print C\n", "\n", "del C[2] # delete what is now 3rd element\n", "print C\n", "\n", "print '\\nlen(C) is the number of elements (length) of list C: \\nThat is, len(C) equals %d' % len(C) # length of list" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [], "source": [ "print C\n", "C.index(10) # find index for an element (10)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print C\n", "10 in C # is element (10) in list C?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "#print C # Uncomment to run and check\n", "#C.index(11) # is element (11) in list C?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "C = [-15, -10, -5, 0, 5, 10, 15, 20, 25, 30, 35, 40, 45]; print C; \n", "\n", "print '\\nElement C[-1] (the last one) is equal to %d' % C[-1]\n", "print '\\nElement C[-2] (the last but one) is equal to %d' % C[-2]\n", " " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Build a list of degrees from -10 to +10 degrees, in steps of 2.5 degrees" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "C = []\n", "C_value = -10\n", "C_max = +10\n", "\n", "while C_value <= C_max:\n", " print 'List C: {C} \\nValue C_value (to be added next): {C_value}\\n' .format(C=C,C_value=C_value)\n", " C.append(C_value)\n", " C_value += 2.5\n", " time.sleep(0.5)\n", "print 'Final list C: {C}' .format(C=C)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## FOR-loops" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "degrees = [0, 10, 20, 40, 100]\n", "\n", "for C in degrees:\n", " print 'list element:', C\n", " time.sleep(0.5)\n", "print 'The degrees list has', len(degrees), 'elements'" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "degrees = [0, 10, 20, 40, 100]; print 'List degrees: {degrees}\\n' .format(degrees=degrees)\n", "\n", "iC=0;\n", "for C in degrees:\n", " print 'List element degrees[{iC}]: {C}' .format(iC=iC, C=C)\n", " iC += 1\n", " time.sleep(0.5)\n", "print '\\nThe list degrees has', len(degrees), 'elements' " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Alternative implementations of lists and loops\n", "\n", "There are usually many alternative ways of write a program to perform a specified task" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## WHILE-loops with FOR-loops\n", "Any **FOR-loops** can be implemented as a **WHILE-loops**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "Cdegrees = [-20, -15, -10, -5, 0, 5, 10, 15, 20, 25, 30, 35, 40]\n", "\n", "index = 0\n", "print ' C F'\n", "print '-----------'\n", "\n", "while index < len(Cdegrees):\n", " C = Cdegrees[index]\n", " F = (9./5)*C + 32\n", " print '%5d %5.1f' % (C,F)\n", " index += 1\n", "print '-----------'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Range construction\n", "\n", "The **range** construction is useful for writing many elements in a list\n", "* **for i in range (start,stop,step)**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We want to create a **Cdegrees** list of values -20, -25, ..., 40 " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Way number 1 (long)\n", "Cdegrees = []\n", "for C in range(-20,45,5): # Upper limit must greater than 40 (41, 42, ...)\n", " Cdegrees.append(C)\n", "print Cdegrees" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [], "source": [ "# Way number 2 (compact)\n", "Cdegrees = range(-20,45,5); print Cdegrees # Upper limit must greater than 40 (41, 42, ...)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We want to create a **Cdegrees** list of values -10, -7.5, -5, ..., 40\n", "\n", "* We cannot use **range** directly\n", "* **range** can only create integers\n", "\n", "We use the formula $C = -10 + 2.5i$ with $i=1,2, ..., 20$" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "Cdegrees = []\n", "for i in range(0,21):\n", " C = -10 + i*2.5\n", " Cdegrees.append(C)\n", "print Cdegrees" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## For-loops with list indexes\n", "\n", "* **for i in range(len(somelist))**\n", "* **for element in somelist**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Iterating over loops indexes is useful when we need to process two lists simulataneously" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import time\n", "n = 21\n", "\n", "Cmin = -10\n", "Cmax = +40\n", "dC = (Cmax - Cmin)/float(n-1)\n", "\n", "Cdegrees = []\n", "for i in range(0,n):\n", " C = -10 + i*dC\n", " Cdegrees.append(C)\n", " \n", "print 'Cdegrees: {C}\\n' .format(C=Cdegrees)\n", "\n", "Fdegrees = []\n", "for C in Cdegrees:\n", " F = (9./5)*C + 32\n", " Fdegrees.append(F)\n", "print 'Fdegrees: {F}\\n' .format(F=Fdegrees)\n", "\n", "for i in range(len(Cdegrees)):\n", " C = Cdegrees[i]\n", " F = Fdegrees[i]\n", " print '%5.1f %5.1f' % (C,F)\n", " time.sleep(0.5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The previous implementation is based on appending new elements to an existing list, initially empty\n", "\n", "An alternative implementation starts with a list of appropriate dimentions filled with zeros (**somelist = [0]*n**) and then iteratively replaces the exiting values\n", "* The construction **[0]*n** is necessary to access index **[i]**\n", "* **[0]*n** must be of the right size" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "n = 9\n", "\n", "C_min = -2\n", "C_max = +2\n", "dC = (C_max - C_min)/float(n-1)\n", "\n", "Cdegrees = [0]*n\n", "print 'Cdegrees of zeros: {C}\\n' .format(C=Cdegrees)\n", "for i in range(len(Cdegrees)):\n", " Cdegrees[i] = C_min + i*dC\n", " print 'Cdegrees: {C}\\n' .format(C=Cdegrees)\n", " time.sleep(0.5) \n", "print 'Cdegrees: {C}\\n' .format(C=Cdegrees)\n", "\n", "Fdegrees = [0]*n\n", "print '\\nFdegrees of zeros: {F}\\n' .format(F=Fdegrees) \n", "for i in range(len(Cdegrees)):\n", " Fdegrees[i] = (9./5)*Cdegrees[i] + 32\n", " print 'Fdegrees: {F}\\n' .format(F=Fdegrees)\n", " time.sleep(0.5)\n", "print 'Fdegrees: {F}\\n' .format(F=Fdegrees)\n", "\n", "print '\\n-- // Using len(Cdegrees) // --\\n'\n", "\n", "for i in range(len(Cdegrees)):\n", " print '%5.1f %5.1f' % (Cdegrees[i], Fdegrees[i])\n", "\n", "print '\\n-- // Using len(Fdegrees) // --\\n'\n", "\n", "for i in range(len(Fdegrees)):\n", " print '%5.1f %5.1f' % (Cdegrees[i], Fdegrees[i])" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "## Change list elements\n", "\n", "It is useful to be able to modify part or all of the values in a given list" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print 'List Cdegrees: {C}\\n' .format(C=Cdegrees)\n", "for i in range(len(Cdegrees)):\n", " Cdegrees[i] += 5 # Adds 5C to all elements in list Cdegrees \n", "print 'New list Cdegrees: {C}\\n' .format(C=Cdegrees)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## List comprehension\n", "\n", "AS general syntax for running through a list and for each element creating a new element in another list\n", "* **newlist = [E(e) for e in oldlist]**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "n=21\n", "\n", "Cdegrees = [-5 + i*0.5 for i in range(n)] \n", "print 'Create list Cdegrees: {C}\\n' .format(C=Cdegrees)\n", "\n", "Fdegrees = [(9./5)*C + 32 for C in Cdegrees] \n", "print 'Use list Cdegrees to create list Fdegrees: {F}\\n' .format(F=Fdegrees)\n", "\n", "C_plus_5 = [C+5 for C in Cdegrees] \n", "print 'Modify list Cdegrees: {C}\\n' .format(C=Cdegrees)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Multiple lists\n", "\n", "How to traverse two or more lists simultaneously" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print '\\nA FOR loop over integer indexes:\\n'\n", "for i in range(len(Cdegrees)):\n", " print '%5d %5.1f' % (Cdegrees[i],Fdegrees[i])\n", "\n", "print '\\nA FOR-loop over list elements:\\n'\n", "for C,F in zip(Cdegrees,Fdegrees):\n", " print '%5d %5.1f' % (C,F)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Nested lists" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Lists of rows and columns\n", "\n", "The table of temperatures is constructed as a list of two columns, and each column is a list on numbers" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [], "source": [ "Cdegrees = range(-20,41,5) # -20, -15, ..., -35, 40\n", "Fdegrees = [(9./5)*C + 32 for C in Cdegrees]\n", "\n", "print '\\nThis is the list of C temperatures: \\n {Cdegrees}' .format(Cdegrees=Cdegrees)\n", "print '\\nThis is the list of corresponding F temperatures: \\n {Fdegrees}' .format(Fdegrees=Fdegrees)\n", "\n", "table = [Cdegrees, Fdegrees]\n", "\n", "print '\\nThis is the list of two columns with C and F temperatures: \\n {table}' .format(table=table)\n", "print '\\nThe first element in table (table[0]) is the Cdegrees list: \\n{t0}' .format(t0 = table[0])\n", "print '\\nThe third element in the first element of table (table[0][2] = {t02}) corresponds the third element of Cdegrees (Cdegrees[2] = {c2})' .format(t02 = table[0][2], c2=Cdegrees[2])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For tabular data with rows and columns, the convention is that the underlying data is a nested list\n", "* The first index counts the rows\n", "* The second index counts the columns\n", "\n", "To have table in such a form, we must construct it as a list of [C,F] pairs" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "\n", "print 'Cdegrees: {C}' .format(C=Cdegrees)\n", "print 'Fdegrees: {F}' .format(F=Fdegrees)\n", "\n", "table = []; print 'Table: {T}' .format(T=table)\n", "for C, F in zip(Cdegrees, Fdegrees):\n", " table.append([C,F])\n", " # print 'Table: {T}' .format(T=table); time.sleep(0.5) # Uncomment to see step-by-step\n", "print '\\nFirst, long, construct: {table}\\n' .format(table=table)\n", "\n", "print 'Subscript table[0] is the first [C,F] pair: {t0}' .format(t0=table[0])\n", "print 'Subscript table[0][0] is the C value: {t00}' .format(t00=table[0][0])\n", "print 'Subscript table[0][1] is the C value: {t01}\\n' .format(t01=table[0][1])\n", "print 'Subscript table[1] is the second [C,F] pair: {t1}' .format(t1=table[1])\n", "print 'Subscript table[1][0] is the F value: {t10}' .format(t10=table[1][0])\n", "print 'Subscript table[1][1] is the F value: {t11}' .format(t11=table[1][1])\n", "\n", "table = [[C,F] for C, F in zip(Cdegrees, Fdegrees)]\n", "print '\\nSecond, compact, construct: {table}\\n' .format(table=table)\n", "\n", "print 'Subscript table[0] is the first [C,F] pair: {t0}' .format(t0=table[0])\n", "print 'Subscript table[0][0] is the C value: {t00}' .format(t00=table[0][0])\n", "print 'Subscript table[0][1] is the C value: {t01}\\n' .format(t01=table[0][1])\n", "print 'Subscript table[1] is the second [C,F] pair: {t1}' .format(t1=table[1])\n", "print 'Subscript table[1][0] is the F value: {t10}' .format(t10=table[1][0])\n", "print 'Subscript table[1][1] is the F value: {t11}' .format(t11=table[1][1])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Printing \n", "\n", "We used simple '**print table**' commands to view the content of a nested list\n", "* This is valid for viewing any object in Python\n", "* Objects can be visualised with '**print obj_name**'\n", "\n", "The **pprint module** produces a visually nicer output (pprint stands for pretty print)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import pprint \n", "pprint.pprint(table)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Alternatively, manual formatting can be used to to get two aligned columns\n", "1. Loop over each row of the table\n", "2. Extract elements C and F in each row\n", "3. Print using printf formatting" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "for C, F in table:\n", " print '%5d %5.1f' % (C,F)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Function **pformat** works as the **pprint** function and it returns formatted string" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "somelist = [15.8, [0.2, 1.7]]\n", "pprint.pprint(somelist)\n", "\n", "s = pprint.pformat(somelist)\n", "print s" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Sublists\n", "\n", "Parts of a list structure are names **sublists** or **slices**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* **A[i:]** is the sublist of list A whose elements have **index i** to **END**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "A = [2, 3.5, 8, 10]\n", "\n", "print 'A[1:] is {Afrom1toE}' .format(Afrom1toE=A[1:])\n", "print 'A[2:] is {Afrom2toE}' .format(Afrom2toE=A[2:])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* **A[i:j]** is the sublist of list A with **index i** to **index j-1**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print 'A[1:3] is {Afrom1to2}' .format(Afrom1to2=A[1:3])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* **A[:i]** is the sublist of list A with **index 0** to **index i-1**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print 'A[:3] is {A0to2}' .format(A0to2=A[:3])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* **A[1:-1]** is the sublist of list A with all elements, except the first and the last" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print 'A[1:-1] is {A1toE1}' .format(A1toE1=A[1:-1])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* **A[:]** is the sublist of all elements of list A" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print 'A[:] is {A0toE}' .format(A0toE=A[:])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In a nested list, we may use indexes in the fisrt list" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "pprint.pprint(table)\n", "\n", "print '\\ntable[4:] is a slice of the nested list table, with indexes 4 to END:\\n'\n", "pprint.pprint(table[4:])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The second index can be *sliced* too" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print '\\ntable[4:7][0:2] is a slice of the nested list table, with indexes 4 to 7 and then indexes 0 to 2'\n", "print '\\ntable[4:7][0:2] first makes a list of 3 elements:'; pprint.pprint(table[4:7])\n", "print '\\nThe slice [0:2] acts on this sublist ands pick elements 0 to 1:'; pprint.pprint(table[4:7][0:2])\n", "\n", "print '\\nSlices, of slices, of slices, ...:\\n'\n", "print table\n", "print table[4:]\n", "print table[4:7]\n", "print table[4:7][0:2]\n", "print table[4:7][0:2][0]\n", "print table[4:7][0:2][0][0]\n", "# print table[4:7][0:2][0][0][0]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Sublists are copies of the original list, modifying the sublists does not alter the original list and vice versa" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "l1 = [1, 4, 3]; pprint.pprint(l1) # List definition (and print)\n", "l2 = l1[:-1]; pprint.pprint(l2) # Sublist definition (and print)\n", "\n", "l1[0] = 100; pprint.pprint(l1) # List modification (and print)\n", "pprint.pprint(l2) # Sublist is unchanged\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "Slicing corresponds to making copies" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "B = A[:] # Create a sublist (make B refer to a copy of list A)\n", "C = A # Make C refer to the same object named A (not a sublist/copy)\n", "\n", "print B == A # TRUE if all elements of B are equal to corresponding elements of A\n", "print B is A # TRUE if A and B are names for the same list\n", "print C is A # TRUE if A and C are names for the same list" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Example\n", "\n", "Write out the part of the **table** list of **[C,F]** rows in which the **C** temperature is between 10 and 35 (35 excluded)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "for C, F in table[Cdegrees.index(10):Cdegrees.index(35)]:\n", " print '%5.0f %5.1f' %(C, F)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Nested lists" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We use a nested list **scores** to record the scores of players in a game\n", "* **scores[p]** holds a list of scores for **player p**\n", "* Different players have played a different number of times\n", "* The length of **scores[p]** varies" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [], "source": [ "scores = []; print scores\n", "\n", "scores.append([12, 16, 11, 12]); print scores # Player 0: scores\n", "scores.append([9]); print scores # Player 1: scores\n", "scores.append([6, 9, 111, 14, 17, 15, 14, 20]); print scores # Player 2: scores" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "List **scores** has three elements, each corresponding to a player\n", "* **Element g** in the list **scores[p]** corresponds to the scores obtained in **game number g** by **player number p**\n", "* The length of **scores[p]** are **4**, **8** and **1** for **p** equal **0**, **1** and **2**, respectively" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "How to traverse a the scores list and write it out?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Method number 1 (Use integers indexes to iterate over the elements of the list and its sublists)\n", "\n", "for p in range(len(scores)): # For each player p in list scores\n", " for g in range(len(scores[p])): # For each game g in list p \n", " score = scores[p][g] # Define object score\n", " print '%4d' % score, # Print object score (the comma avoids a new line)\n", " print # Add a new line after each table" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Method number 2 (Use variables to iterate over the lements of the list and its sublists)\n", "\n", "for player in scores: # For each player \n", " for game in player: # For each game\n", " print '%4d' % game, # Print\n", " print # Add new line" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "# Tuples\n", "\n", "A tuple can be understood as a constant list, standard parenthesis are used instead of square brackets " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "t = (2, 4, 6, 'temp.pdf') # Definition of a tuple t\n", "print t\n", "\n", "t = 2, 4, 6, 'temp.pdf' # Parenthesis can often be dropped\n", "print t" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "for element in 'myfile.txt', 'urfile.txt', 'herfile.txt':\n", " print element, # The comma suppresses the final 'new line'\n", " # automatically added by the print command" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Tuple share many of the functionalities available for lists" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print 'Variable t, before: {t}' .format(t=t)\n", "t = t + (-1.0, -2.0) # add two tuples\n", "print 'Variable t, after: {t}\\n' .format(t=t)\n", "\n", "print t[1] # Indexing\n", "print t[2:] # Subtuple/slice\n", "\n", "print 6 in t # Membership" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Since a tuple can be understood as a constant list, list operation that aim at changing the list and some simple methods will not work " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "#t[1] = -1 # Uncomment to 'run' and get the error message\n", "\n", "#t.append(0) # Uncomment to 'run' and get the error message\n", " \n", "#del t[1] # Uncomment to 'run' and get the error message" ] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python [default]", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.12" } }, "nbformat": 4, "nbformat_minor": 1 }