{ "metadata": { "name": "", "signature": "sha256:78ef4056dbbb5cf9345972f218d0d597af5ce86eb47594e9fba3eaa74e6511a9" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Chapter 3 : Making statements" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 3.1, Page No 45" ] }, { "cell_type": "code", "collapsed": false, "input": [ "num = 8\n", "letter = 'A'\n", "if num > 5:\n", " print \"Number exceeds five\"\n", "else: \n", " \"Number is five or less\"\n", "if letter == 'A':\n", " print \"Letter is A\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Number exceeds five\n", "Letter is A\n" ] } ], "prompt_number": 3 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 3.2, Page No 47" ] }, { "cell_type": "code", "collapsed": false, "input": [ "num = 3\n", "if num == 1:\n", " print num,\" : Monday\"\n", "elif num == 2:\n", " print num,\" : Tuesday\"\n", "elif num == 3:\n", " print num,\" : Wednesday\"\n", "elif num == 4:\n", " print num,\" : Thursday\"\n", "elif num == 5:\n", " print num,\" : Friday\"\n", "else:\n", " print \"Weekend day\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "3 : Wednesday\n" ] } ], "prompt_number": 4 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 3.3, Page No 48" ] }, { "cell_type": "code", "collapsed": false, "input": [ "for i in range(1,4):\n", " print \"Loop iteration: \",i\n", " for j in range(1,4):\n", " print \" Inner loop iteration: \",j" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Loop iteration: 1\n", " Inner loop iteration: 1\n", " Inner loop iteration: 2\n", " Inner loop iteration: 3\n", "Loop iteration: 2\n", " Inner loop iteration: 1\n", " Inner loop iteration: 2\n", " Inner loop iteration: 3\n", "Loop iteration: 3\n", " Inner loop iteration: 1\n", " Inner loop iteration: 2\n", " Inner loop iteration: 3\n" ] } ], "prompt_number": 11 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 3.4, Page No 50" ] }, { "cell_type": "code", "collapsed": false, "input": [ "vec = range(10)\n", "i = 0\n", "print \n", "while i < len(vec):\n", " i = i + 1\n", " if i == 3:\n", " print \"| Skipped\",\n", " continue\n", " if i == 8:\n", " print \"Done\"\n", " break\n", " vec[i-1] = i\n", " print \"|\",vec[i-1]," ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "| 1 | 2 | Skipped | 4 | 5 | 6 | 7 Done\n" ] } ], "prompt_number": 17 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 3.5, Page No 53" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def bodyTempC():\n", " temperature = 37.0\n", " return temperature\n", "def bodyTempF():\n", " temperature = 98.6\n", " return temperature\n", "print \"Centigrade: \",bodyTempC()\n", "print \"Fahrenheit: \",bodyTempF()\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Centigrade: 37.0\n", "Fahrenheit: 98.6\n" ] } ], "prompt_number": 18 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 3.6, Page No 54" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def fToC(degreesF = 32.0):\n", " degreesC = ((5.0/9.0) * (degreesF - 32.0))\n", " return degreesC\n", "fahrenheit = float(raw_input(\"Enter a Fahrenheit temperature:\\t\"))\n", "centigrade = fToC(fahrenheit)\n", "print fahrenheit,\"F is \",centigrade,\"C\"\n", "print \"Freezing point: \",fToC(),\"C\"" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter a Fahrenheit temperature:\t98.6\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " 98.6 F is 37.0 C\n", "Freezing point: 0.0 C\n" ] } ], "prompt_number": 21 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 3.7, Page No 56" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def computeArea(diameter):\n", " radius = (diameter/2)\n", " return (3.141593 * (radius * radius))\n", "def computeArea(width,height):\n", " return width * height\n", "def computeArea(letter,width,height):\n", " return ((width/2) * height)\n", "num = float(raw_input(\"Enter dimension in feet: \"))\n", "# python does not support overriding in this way\n", "#area = computeArea(num)\n", "#print \"Circle: Area = \",area,\" sq.ft.\"\n", "#area= computeArea(num,num)\n", "#print\"Square: Area = \",area,\" sq.ft.\"\n", "area = computeArea('T',num,num)\n", "print \"Triangle: Area = \",area,\" sq.ft.\"" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter dimension in feet: 4\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Triangle: Area = 8.0 sq.ft.\n" ] } ], "prompt_number": 4 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 3.8, Page No 58" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def computeFactorials(num,max1):\n", " print \"Factorial of \",num,\":\",\n", " print factorial(num)\n", " num = num + 1\n", " if num > max1:\n", " return 0\n", " else :\n", " computeFactorials(num,max1)\n", "def factorial(n):\n", " if n == 1:\n", " result = 1\n", " else:\n", " result = (factorial(n - 1) * n)\n", " return result\n", "computeFactorials( 1, 8) ;" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Factorial of 1 : 1\n", "Factorial of 2 : 2\n", "Factorial of 3 : 6\n", "Factorial of 4 : 24\n", "Factorial of 5 : 120\n", "Factorial of 6 : 720\n", "Factorial of 7 : 5040\n", "Factorial of 8 : 40320\n" ] } ], "prompt_number": 28 } ], "metadata": {} } ] }