{ "metadata": { "name": "", "signature": "sha256:4fe639aabf754893fb9526618dc70b095a7daac92032a0be83397e28da6d3261" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Chapter 3: Formatted I/O" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example on Page 38" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#variable declaration\n", "i=10\n", "j=20\n", "x=43.2892\n", "y=5527.0\n", "#print statement\n", "print \"i = %d, j = %d, x = %f, y = %f\" % (i,j,x,y)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "i = 10, j = 20, x = 43.289200, y = 5527.000000\n" ] } ], "prompt_number": 3 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example tprintf.c on Page 40" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def main():\n", " #variable declaration\n", " i=40\n", " x=839.21\n", " \n", " #formatted printing\n", " print \"|%d|%5d|%-5d|%5.3d|\" % (i,i,i,i)\n", " print \"|%10.3f|%10.3e|%-10g|\" % (x,x,x)\n", " \n", "if __name__=='__main__':\n", " main()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "|40| 40|40 | 040|\n", "| 839.210| 8.392e+02|839.21 |\n" ] } ], "prompt_number": 6 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example addfrac.c on Page 46" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def main():\n", "\n", " print \" Enter first fraction: \",\n", " #accepting numerator and denominator separated by '/'\n", " num1,denom1=map(int,raw_input().split('/')) \n", " print \"Enter second fraction: \",\n", " num2,denom2=map(int,raw_input().split('/'))\n", " #adding the fractions\n", " result_num = num1*denom2 + num2*denom1 \n", " result_denom = denom1*denom2\n", " print \"The sum is %d/%d\" % (result_num,result_denom)\n", "\n", "if __name__=='__main__':\n", " main()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter first fraction: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "5/6\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Enter second fraction: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "3/4\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " The sum is 38/24\n" ] } ], "prompt_number": 4 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }