{ "metadata": { "name": "Chapter4_Loops" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "[Python for Developers](http://ricardoduarte.github.io/python-for-developers/#content)\n", "===================================\n", "First Edition\n", "-----------------------------------\n", "\n", "Chapter 4: Loops\n", "=============================\n", "_____________________________\n", "Loops are repetition structures, generally used to process data collections, such as lines of a file or records of a database that must be processed by the same code block.\n", "\n", "For\n", "---\n", "It is the repetition structure most often used in Python. The statement accepts not only static\u00a0sequences, but also sequences generated by iterators. Iterators are structures that allow iterations, i.e. access to items of a collection of elements, sequentially.\n", "\n", "![Loop Example](files/bpyfd_diags3.png)\n", "\n", "During the execution of a *for* loop, the reference points to an element in the sequence. At each iteration, the reference is updated, in order for the *for* code block to process the corresponding element.\n", "\n", "The clause *break*\u00a0stops the loop and *continue* passes it to the next iteration. The code inside the `else` is executed at the end of the loop, except if the loop has been interrupted by *break*.\n", "\n", "Syntax:\n", "\n", " for in :\n", " \n", " continue\n", " break\n", " else:\n", " \n", " \n", "Example:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Sum 0 to 99\n", "s = 0\n", "for x in range(1, 100):\n", " s = s + x\n", "print s" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "4950\n" ] } ], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "The function `range(m, n, p)`, is very useful in loops, as it returns a list of integers starting at `m` through smaller than\u00a0`n` in steps of length `p`, which can be used as the order for the loop.\n", "\n", "While\n", "-----\n", "Executes a block of code in response to a condition.\n", "\n", "Syntax:\n", "\n", " while :\n", " \n", " continue\n", " break\n", " else:\n", " \n", " \n", "The code block inside the *while* loop is repeated while the loop condition is evaluated as true.\n", " \n", "Example:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Sum 0 to 99\n", "s = 0\n", "x = 1\n", "\n", "while x < 100:\n", " s = s + x\n", " x = x + 1\n", "print s" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "4950\n" ] } ], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": {}, "source": [ "The *while* loop is appropriate when there is no way to determine how many iterations will occur and there is a sequence to follow." ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [ { "html": [ "\n", "" ], "output_type": "pyout", "prompt_number": 1, "text": [ "" ] } ], "prompt_number": 1 } ], "metadata": {} } ] }