{ "metadata": { "name": "", "signature": "sha256:9c1a031463da8a03e6f56a08f572c87969b01883476b123f1b468e43c2cd2b66" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Chapter 8: Structuring Your Programs" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 8.1, page no. 323" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "count1 = 1\n", "\n", "while(count1<=5):\n", " count1 += 1\n", " count2 = 0\n", " count2 += 1\n", " print \"count1 = %d count2 = %d \" %(count1, count2)\n", " \n", "print \"count1 = %d\\n\" %count1" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "count1 = 2 count2 = 1 \n", "count1 = 3 count2 = 1 \n", "count1 = 4 count2 = 1 \n", "count1 = 5 count2 = 1 \n", "count1 = 6 count2 = 1 \n", "count1 = 6\n", "\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 8.2, page no. 324" ] }, { "cell_type": "code", "collapsed": false, "input": [ " \n", "\n", "count = 0\n", "while(count<=5):\n", " count += 1\n", " print \"count = \", count\n", " \n", "print \"count = \", count" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "count = 1\n", "count = 2\n", "count = 3\n", "count = 4\n", "count = 5\n", "count = 6\n", "count = 6\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 8.3, page no. 330" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "def Sum(x, n):\n", " sum = 0.0\n", " for i in range(n):\n", " sum += x[i]\n", " return sum\n", " \n", "def Average(x, n):\n", " return Sum(x, n)/n\n", "\n", "def GetData(data, t):\n", " print \"How many values do you want to enter (Maximum 50)? \"\n", " nValues = int(raw_input())\n", " if(nValues > 50):\n", " print \"Maximum count exceeded. 50 items will be read.\"\n", " nValues = 50\n", " for i in range(nValues):\n", " data.append(float(raw_input()))\n", " return nValues\n", " \n", "samples = []\n", "sampleCount = GetData(samples, 50);\n", "average = Average(samples, sampleCount);\n", "print \"The average of the values you entered is: \", average" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "How many values do you want to enter (Maximum 50)? \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "5\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "1.0\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "2.0\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "3.0\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "4.0\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "5.0\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "The average of the values you entered is: 3.0\n" ] } ], "prompt_number": 3 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 8.4, page no. 339" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "def sort_string(text):\n", " dot_separated = text.split('.')\n", " text_sorted = sorted(dot_separated)\n", " return text_sorted\n", "\n", "\n", "print \"Enter strings to be sorted, separated by '.' Press Enter to end: \"\n", "text = raw_input()\n", "\n", "sorted_text = sort_string(text)\n", "\n", "for str in sorted_text:\n", " print str" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter strings to be sorted, separated by '.' Press Enter to end: \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Sample strings. These are samples. Another Sample\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Another Sample\n", " These are samples\n", "Sample strings\n" ] } ], "prompt_number": 4 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 8.5, page no. 344" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "def IncomePlus(pPay):\n", " pPay += 10000\n", " return pPay\n", "\n", "your_pay = 30000\n", "pold_pay = your_pay\n", "pnew_pay = IncomePlus(pold_pay)\n", "print \"Old pay = $\", pold_pay\n", "print \"New pay = $\", pnew_pay" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Old pay = $ 30000\n", "New pay = $ 40000\n" ] } ], "prompt_number": 5 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 8.6, page no. 345" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "def IncomePlus(pPay):\n", " pPay += 10000\n", " return id(pPay)\n", "\n", "your_pay = 30000\n", "pold_pay = your_pay\n", "pnew_pay = IncomePlus(pold_pay)\n", "print \"Old pay = $\", pold_pay\n", "print \"New pay = $\", pnew_pay" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Old pay = $ 30000\n", "New pay = $ 38417016\n" ] } ], "prompt_number": 6 } ], "metadata": {} } ] }