{ "metadata": { "name": "", "signature": "sha256:beba487362ad72f7d0b5bcfa6d644d75a22e3d3f63f267f9db3f44d7a080a220" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Chapter 4: Input/Output Functions and Statements" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 1.0, Page number: IOF-5" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "#Variable Declaration and Initialization\n", "a = int(raw_input(\"\"))\n", "b = int(raw_input(\"\"))\n", "\n", "#Calculation\n", "sum1 = a + b\n", "product = a * b\n", "#Since sum is a keyword in python, sum1 is used\n", "\n", "#Output\n", "print sum1, product" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "5\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "3\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "8 15\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 1.1, Page number: IOF-6" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "#Variable initialization\n", "a = int(raw_input(\"Enter Value to A : \"))\n", "b = int(raw_input(\"Enter Value to B : \"))\n", "\n", "#Calculation\n", "sum1 = a + b\n", "product = a * b\n", "#Since sum is a keyword in python, sum1 is used\n", "\n", "#Output\n", "print \"SUM = \",sum1,\"\\nPRODUCT = \", product" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Value to A : 5\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Value to B : 3\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "SUM = 8 \n", "PRODUCT = 15\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 2, Page number: IOF-10" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "import sys\n", "\n", "#Variable Initialization\n", "f = int(raw_input(\"Degree fahrenheit ? \"))\n", "\n", "#Calculation\n", "c = 5.0/9.0 * (f-32)\n", "\n", "#Output\n", "sys.stdout.write(\"Degree centigrade = %6.2f\"%(c))" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Degree fahrenheit ? 105\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Degree centigrade = 40.56" ] } ], "prompt_number": 8 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 3, Page number: IOF-10" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "import sys\n", "import math\n", "\n", "#Variable Initialization\n", "a = int(raw_input(\"Enter three sides : \"))\n", "b = int(raw_input(\"Enter three sides : \"))\n", "c = int(raw_input(\"Enter three sides : \"))\n", "\n", "#Calculation\n", "s = (a+b+c)/2\n", "area = math.sqrt(s * (s-a) * (s-b) * (s-c))\n", "\n", "#Output\n", "sys.stdout.write(\"Area of Triangle = %6.2f Sq.units\"%(area))" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter three sides : 5\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter three sides : 4\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter three sides : 6\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Area of Triangle = 6.48 Sq.units" ] } ], "prompt_number": 14 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 4, Page number: IOF-16" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "#Variable Initialization\n", "ch = raw_input(\"Enter a character : \")\n", "\n", "#Output\n", "sys.stdout.write(\"\\n\\nASCII value of %c is %u\"%(ch,ord(ch)))\n", "sys.stdout.write(\"\\nPress any key to stop...\")" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter a character : A\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "\n", "ASCII value of A is 65\n", "Press any key to stop..." ] } ], "prompt_number": 17 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 5, Page number: IOF-16" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "import sys\n", "\n", "#Variable initialization\n", "sno = raw_input(\"Enter service number : \")\n", "pmr = int(raw_input(\"\\nPrevious month reading ? \"))\n", "cmr = int(raw_input(\"\\nCurrent month reading ? \"))\n", "\n", "#Calculation\n", "units = cmr - pmr\n", "amt = units * 1.50\n", "\n", "#Output\n", "sys.stdout.write(\"\\n Electricity Bill\")\n", "sys.stdout.write(\"\\n ________________\")\n", "sys.stdout.write(\"\\nService No. %s\"%(sno))\n", "sys.stdout.write(\"\\nUnits Consumed : %d\"%(units))\n", "sys.stdout.write(\"\\nElectricity Charges Rs. %0.2f\"%(amt))\n" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter service number : TMR65358\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Previous month reading ? 4305\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Current month reading ? 4410\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", " Electricity Bill\n", " ________________\n", "Service No. TMR65358\n", "Units Consumed : 105\n", "Electricity Charges Rs. 157.50" ] } ], "prompt_number": 18 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 6, Page number: IOF-17" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "import sys\n", "\n", "#Variable Initialization\n", "a = int(raw_input(\"Enter value to A : \"))\n", "b = int(raw_input(\"Enter value to B : \"))\n", "\n", "#Calculation\n", "temp = a\n", "a = b\n", "b = temp\n", "\n", "#Output\n", "sys.stdout.write(\"\\n\\nValue of A = %d\"%(a))\n", "sys.stdout.write(\"\\nValue of B = %d\"%(b))" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter value to A : 15\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter value to B : 250\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "\n", "Value of A = 250\n", "Value of B = 15" ] } ], "prompt_number": 19 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }