{ "metadata": { "name": "for-loops" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "For Loops\n", "=========\n", "\n", "One of the most powerful aspects of computer programs is their ability to\n", "perform repetion, which is the ability to do something many times.\n", "One of the main forms of repetition is the *for loop*, which does something\n", "a certain number of times." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Looping over a list\n", "-------------------\n", "\n", "The most common use of for loops is to do the same thing to every item in a list.\n", "Python makes it easy to do this. The basic structure is simple\n", "\n", " for item in list_of_items:\n", " block\n", "\n", "where ``block`` is an indented list of Python commands.\n", "The first time through the loop the first object in ``list_of_items`` is assigned\n", "to ``item``. The second time through the loop the second object in the list is\n", "assigned to ``item`` and so on until the end of the this.\n", "\n", "For example, if we have a list of the nobel gases and want to print them out one\n", "at a time while mentioning that they are a nobel gas we would use a program like this." ] }, { "cell_type": "code", "collapsed": false, "input": [ "nobel_gases = ['He', 'Ne', 'Ar', 'Kr', 'Xe', 'Rn']\n", "for gas in nobel_gases:\n", " print \"%s is a nobel gas.\" % gas" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "He is a nobel gas.\n", "Ne is a nobel gas.\n", "Ar is a nobel gas.\n", "Kr is a nobel gas.\n", "Xe is a nobel gas.\n", "Rn is a nobel gas.\n" ] } ], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Looping over integers\n", "---------------------\n", "This most common approach to using for loops across different programming\n", "languages is to loop over a set of integers, often using those integers as\n", "indexes for items in a list. We can do this in Python using the ``range()``\n", "function.\n", "\n", "###Range\n", "``range()`` simple generates a list of integers when given starting and ending values.\n", "The list includes the starting value and goes up to, but does not include,\n", "the end value." ] }, { "cell_type": "code", "collapsed": false, "input": [ "range(1, 10)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 2, "text": [ "[1, 2, 3, 4, 5, 6, 7, 8, 9]" ] } ], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": {}, "source": [ "If we leave out the start value then Python just assumes that it is equal to zero." ] }, { "cell_type": "code", "collapsed": false, "input": [ "range(5)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 3, "text": [ "[0, 1, 2, 3, 4]" ] } ], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ "And we can also use integer steps that are greater than 1." ] }, { "cell_type": "code", "collapsed": false, "input": [ "range(0, 10, 2)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 5, "text": [ "[0, 2, 4, 6, 8]" ] } ], "prompt_number": 5 }, { "cell_type": "markdown", "metadata": {}, "source": [ "###Looping with Range\n", "\n", "Using range we can loop over integers/positions." ] }, { "cell_type": "code", "collapsed": false, "input": [ "positions = range(10)\n", "for i in positions:\n", " print i" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n", "5\n", "6\n", "7\n", "8\n", "9\n" ] } ], "prompt_number": 6 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Or, more simply" ] }, { "cell_type": "code", "collapsed": false, "input": [ "for i in range(10):\n", " print i" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n", "5\n", "6\n", "7\n", "8\n", "9\n" ] } ], "prompt_number": 7 }, { "cell_type": "markdown", "metadata": {}, "source": [ "We could use this to rewrite our nobel gas program as follows:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "nobel_gases = ['He', 'Ne', 'Ar', 'Kr', 'Xe', 'Rn']\n", "for i in range(len(nobel_gases)):\n", " print \"%s is a nobel gas.\" % nobel_gases[i]" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "He is a nobel gas.\n", "Ne is a nobel gas.\n", "Ar is a nobel gas.\n", "Kr is a nobel gas.\n", "Xe is a nobel gas.\n", "Rn is a nobel gas.\n" ] } ], "prompt_number": 8 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Looping over more than one list\n", "===============================\n", "\n", "Looping over the indexes of a list is most useful if we have more than\n", "one list that we want to access at the same time. For example, if\n", "we also have a list of the atomic numbers of the nobel gases and\n", "we want to print out each gas along with it's atomic number we would\n", "modify our program as follows." ] }, { "cell_type": "code", "collapsed": false, "input": [ "nobel_gases = ['He', 'Ne', 'Ar', 'Kr', 'Xe', 'Rn']\n", "atomic_numbers = [2, 10, 18, 36, 54, 86]\n", "for i in range(len(nobel_gases)):\n", " print \"%s is a nobel gas with an atomic number of %s.\" % (nobel_gases[i], atomic_numbers[i])" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "He is a nobel gas with an atomic number of 2.\n", "Ne is a nobel gas with an atomic number of 10.\n", "Ar is a nobel gas with an atomic number of 18.\n", "Kr is a nobel gas with an atomic number of 36.\n", "Xe is a nobel gas with an atomic number of 54.\n", "Rn is a nobel gas with an atomic number of 86.\n" ] } ], "prompt_number": 9 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Enumerate\n", "\n", "This is such a common idiom in programming that Python provides an alternative\n", "approach that is often easier to read using the ``enumerate()`` function. This\n", "function returns something like a list that includes both the first value in a \n", "list and its position. For example," ] }, { "cell_type": "code", "collapsed": false, "input": [ "nobel_gases = ['He', 'Ne', 'Ar', 'Kr', 'Xe', 'Rn']\n", "for i, gas in enumerate(nobel_gases):\n", " print i, gas" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "0 He\n", "1 Ne\n", "2 Ar\n", "3 Kr\n", "4 Xe\n", "5 Rn\n" ] } ], "prompt_number": 11 }, { "cell_type": "markdown", "metadata": {}, "source": [ "This alows us to rewrite out atomic number program as" ] }, { "cell_type": "code", "collapsed": false, "input": [ "nobel_gases = ['He', 'Ne', 'Ar', 'Kr', 'Xe', 'Rn']\n", "atomic_numbers = [2, 10, 18, 36, 54, 86]\n", "for i, gas in enumerate(nobel_gases):\n", " print \"%s is a nobel gas with an atomic number of %s.\" % (gas, atomic_numbers[i])" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "He is a nobel gas with an atomic number of 2.\n", "Ne is a nobel gas with an atomic number of 10.\n", "Ar is a nobel gas with an atomic number of 18.\n", "Kr is a nobel gas with an atomic number of 36.\n", "Xe is a nobel gas with an atomic number of 54.\n", "Rn is a nobel gas with an atomic number of 86.\n" ] } ], "prompt_number": 13 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Which is arguably easier to read." ] } ], "metadata": {} } ] }