{ "metadata": { "name": "", "signature": "sha256:245ea275fa7c7004a79eeeeb6b3fc9780b5a19707d8b95c7a675d2142428b630" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Chapter 7, The for loop" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 7-1 , Page number: 119" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# A simple illustration of for loop\n", "\n", "for i in range(1,11,1):\t\t# The loop starts with i=1 and continues until i<11 with its value increasing by 1 each time \n", "\tprint \"This is line number %d\"%i" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "This is line number 1\n", "This is line number 2\n", "This is line number 3\n", "This is line number 4\n", "This is line number 5\n", "This is line number 6\n", "This is line number 7\n", "This is line number 8\n", "This is line number 9\n", "This is line number 10\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 7-2 , Page number: 120" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Another simple illustration of a for loop\n", "\n", "print \"Countdown\"\n", "\n", "for i in range(10,-1,-1):\n", "\tprint i\n", "print \"\\n\\nBlastoff!\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Countdown\n", "10\n", "9\n", "8\n", "7\n", "6\n", "5\n", "4\n", "3\n", "2\n", "1\n", "0\n", "\n", "\n", "Blastoff!\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 7-3 , Page number: 121" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Calculating the squares of integers 1 through 10\n", "\n", "for i in range (1,11,1):\n", "\tprint \"%d squared = %d\"%(i,i*i)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1 squared = 1\n", "2 squared = 4\n", "3 squared = 9\n", "4 squared = 16\n", "5 squared = 25\n", "6 squared = 36\n", "7 squared = 49\n", "8 squared = 64\n", "9 squared = 81\n", "10 squared = 100\n" ] } ], "prompt_number": 3 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 7-4 , Page number: 121" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# A better looking program to calculate squares\n", "\n", "print \"Table of Results\\n\"\n", "print \" i i squared\"\n", "print \" - ---------\"\n", "\n", "for i in range(1,11,1):\n", "\tprint \"%4d%13d\"%(i,i*i)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Table of Results\n", "\n", " i i squared\n", " - ---------\n", " 1 1\n", " 2 4\n", " 3 9\n", " 4 16\n", " 5 25\n", " 6 36\n", " 7 49\n", " 8 64\n", " 9 81\n", " 10 100\n" ] } ], "prompt_number": 4 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 7-5 , Page number: 122" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Finding the sum of integers 1 through 100\n", "\n", "sum = 0\n", "n = 100\n", "\n", "for i in range(1,n+1,1):\t\t# The loop starts with i=1 and continues until i<11 with its value increasing by 1 each time \n", "\tsum += i\t\n", "\n", "print \"The sum of integers from 1 to %d is %d\"%(n,sum)\n", "print \"\\n\\nThis is confirmed by the Gauss Method: %d\"%(n*(n+1)/2)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The sum of integers from 1 to 100 is 5050\n", "\n", "\n", "This is confirmed by the Gauss Method: 5050\n" ] } ], "prompt_number": 5 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 7-6 , Page number: 124" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Finding the sum of the integers 1 through MAX_VAL\n", "\n", "# There is no #define command for defining constants\n", "# We can use a function that returns the value (although it is not very efficient to do so)\n", "\n", "def MAX_VAL():\n", "\treturn 100\t\t\t# The value of MAX_VAL\n", "\n", "sum = 0\n", "\n", "for i in range (0,MAX_VAL()+1,1):\n", "\tsum += i\n", "\n", "print \"The sum of the integers from 1 to %d is %d\"%(MAX_VAL(),sum)\n", "print \"\\n\\nThis is confirmed by the Gauss Method : %d\"%(MAX_VAL()*(MAX_VAL()+1)/2)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The sum of the integers from 1 to 100 is 5050\n", "\n", "\n", "This is confirmed by the Gauss Method : 5050\n" ] } ], "prompt_number": 6 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 7-7 , Page number: 125" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Calculating the sum of integers 1 through n\n", "\n", "sum = 0\n", "\n", "print \"Please type in a positive integer:\",\n", "n = 50\n", "print n\n", "\n", "for i in range (1,n+1,1):\n", "\tsum += i\n", "\n", "print \"\\nThe sum of the integers from 1 to %d is: %d\"%(n,sum)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Please type in a positive integer: 50\n", "\n", "The sum of the integers from 1 to 50 is: 1275\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 7-8 , Page number: 125" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Calculating the sum of integers low through high\n", "\n", "sum = 0\n", "\n", "print \"Please type in the low bound:\",\n", "low = 3\n", "print low\n", "print \"Please type in the high bound:\",\n", "high = 9\n", "print high\n", "\n", "for i in range(low,high+1,1):\n", "\tsum += i\n", "\n", "print \"\\nThe sum of integers from %d to %d is: %d\"%(low,high,sum)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Please type in the low bound: 3\n", "Please type in the high bound: 9\n", "\n", "The sum of integers from 3 to 9 is: 42\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 7-9 , Page number: 127" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Calculating the sum of integers low through high with an exchange if necessary\n", "\n", "sum = 0\n", "\n", "print \"Please type in the low bound:\",\n", "low = 30\n", "print low\n", "print \"Please type in the high bound:\",\n", "high = 15\n", "print high\n", "\n", "if high < low :\n", "\tprint \"Oops! Wrong order.\"\n", "\tprint \"Never mind, I'll switch them.\"\n", "\t# Code for swapping using temporary variable\n", "\ttemp = low\n", "\tlow = high\n", "\thigh = temp\n", "\n", "for i in range(low,high+1,1):\n", "\tsum += i\n", "\n", "print \"\\nThe sum of integers from %d to %d is: %d\"%(low,high,sum)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Please type in the low bound: 30\n", "Please type in the high bound: 15\n", "Oops! Wrong order.\n", "Never mind, I'll switch them.\n", "\n", "The sum of integers from 15 to 30 is: 360\n" ] } ], "prompt_number": 3 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 7-10 ,Page number: 128" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Illustration of a nest of for loops\n", "\n", "n = 5\n", "for i in range(1,7,1):\n", "\tprint \"\\n\\nPlease type in a positive integer: \",\n", "\tprint n\n", "\n", "\tsum = 0\n", "\tfor j in range(1,n+1,1):\n", "\t\tsum += j\n", "\tprint \"\\nThe sum of integers from 1 to %d is %d\"%(n,sum)\n", "\t\n", "\tn += 5\t\t\t# Just to match the exercise's values" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "\n", "Please type in a positive integer: 5\n", "\n", "The sum of integers from 1 to 5 is 15\n", "\n", "\n", "Please type in a positive integer: 10\n", "\n", "The sum of integers from 1 to 10 is 55\n", "\n", "\n", "Please type in a positive integer: 15\n", "\n", "The sum of integers from 1 to 15 is 120\n", "\n", "\n", "Please type in a positive integer: 20\n", "\n", "The sum of integers from 1 to 20 is 210\n", "\n", "\n", "Please type in a positive integer: 25\n", "\n", "The sum of integers from 1 to 25 is 325\n", "\n", "\n", "Please type in a positive integer: 30\n", "\n", "The sum of integers from 1 to 30 is 465\n" ] } ], "prompt_number": 4 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 7-11 , Page number: 130" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Another illustration of a nest of for loops\n", "\n", "print \"How many values of n do you wish to enter? \",\n", "runs = 3\n", "print runs\n", "print \"\\n\\n\"\n", "\n", "n = 9\n", "\n", "for i in range(1,runs+1,1) :\n", "\tprint \"\\n\\nPLease type in a positive integer: \",\n", "\tprint n\n", "\n", "\tsum = 0\n", "\tfor j in range(1,n+1,1) :\n", "\t\tsum += j\n", "\tprint \"\\nThe sum of the integers from 1 to %d is: %d\"%(n,sum)\n", "\tn *= 3\t\t\t# Just a random increment" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "How many values of n do you wish to enter? 3\n", "\n", "\n", "\n", "\n", "\n", "PLease type in a positive integer: 9\n", "\n", "The sum of the integers from 1 to 9 is: 45\n", "\n", "\n", "PLease type in a positive integer: 27\n", "\n", "The sum of the integers from 1 to 27 is: 378\n", "\n", "\n", "PLease type in a positive integer: 81\n", "\n", "The sum of the integers from 1 to 81 is: 3321\n" ] } ], "prompt_number": 5 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 7-12 , Page number: 131" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Newton-Rapshon method for calculating square roots\n", "\n", "def EPSILON() :\n", "\t\"\"\" USING AS A CONSTANT \"\"\"\n", "\treturn 0.0001\n", "\n", "print \"What number do you want to kate the square root of?\",\n", "n = 17.0\n", "print n\n", "\n", "while n < 0.0 :\n", "\tprint \"Please enter a non-negative number!\"\n", "\tn = 3 \t\t\t\t\t# Just a random positive number\n", "\n", "# The for loop statement given in the example(from the book) is not possible to code in python\n", "# So, breaking it down to while loop\n", "\n", "guess = n / 2.0\n", "while (guess * guess - n > EPSILON() or guess * guess - n < -EPSILON()) :\n", "\tguess = (guess + n/guess) / 2.0\n", "\t\n", "print \"I calculate the square root of %f to be %.4f\"%(n,guess)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "What number do you want to kate the square root of? 17.0\n", "I calculate the square root of 17.000000 to be 4.1231\n" ] } ], "prompt_number": 6 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 7-13 , Page number: 133" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# General addition table for a number n\n", "\n", "print \"What number do you want a table for?\",\n", "n = 10\n", "print n\n", "\n", "j = n\n", "for i in range (0,n+1,1) :\n", "\tprint \"%3d + %3d = %3d\"%(i,j,n)\n", "\tj -= 1" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "What number do you want a table for? 10\n", " 0 + 10 = 10\n", " 1 + 9 = 10\n", " 2 + 8 = 10\n", " 3 + 7 = 10\n", " 4 + 6 = 10\n", " 5 + 5 = 10\n", " 6 + 4 = 10\n", " 7 + 3 = 10\n", " 8 + 2 = 10\n", " 9 + 1 = 10\n", " 10 + 0 = 10\n" ] } ], "prompt_number": 7 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 7-14 , Page number: 135" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Generate addition table for a number n, shorter loop\n", "\n", "print \"What number do you want a table for?\",\n", "n = 10\n", "print n\n", "\n", "# Since the range() function supports at max 3 arguments, no possible shortening is possilbe in python\n", "j = n\n", "for i in range (0,n+1,1):\n", "\tprint \"%3d + %3d = %3d\"%(i,j,n)\n", "\tj -= 1\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "What number do you want a table for? 10\n", " 0 + 10 = 10\n", " 1 + 9 = 10\n", " 2 + 8 = 10\n", " 3 + 7 = 10\n", " 4 + 6 = 10\n", " 5 + 5 = 10\n", " 6 + 4 = 10\n", " 7 + 3 = 10\n", " 8 + 2 = 10\n", " 9 + 1 = 10\n", " 10 + 0 = 10\n" ] } ], "prompt_number": 8 } ], "metadata": {} } ] }