{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "

Chapter 4: Data Input and Output

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 4.4, Page number: 4.4

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "ch='a'\n", "print 'equivalent uppercase character is ',ch.upper()\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "equivalent uppercase character is A\n" ] } ], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 4.16, Page number: 4.14

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "import math\n", "\n", "i,j=2.0,3.0\n", "print \"%f %f %f %f\" %(i,i,i+j,math.sqrt(i+j))\n", "\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "2.000000 2.000000 5.000000 2.236068\n" ] } ], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 4.18, Page number: 4.15

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "x,y=5000.0,0.0025\n", "\n", "print \"%f %f %f %f\\n\\n\" %(x,y,x*y,x/y)\n", "print \"%e %e %e %e\" %(x,y,x*y,x/y)\n", "\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "5000.000000 0.002500 12.500000 2000000.000000\n", "\n", "\n", "5.000000e+03 2.500000e-03 1.250000e+01 2.000000e+06\n" ] } ], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 4.19, Page number: 4.16

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "line=\"The PITTSBURG STEELERS is one of America's favorite football teams!\"\n", "print \"%s\" %(line)\n", "\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The PITTSBURG STEELERS is one of America's favorite football teams!\n" ] } ], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 4.20, Page number: 4.17

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "i=12345\n", "x=345.678\n", "\n", "print \"%3d %5d %8d\\n\\n\" %(i,i,i)\n", "print \"%3f %10f %13f\" %(x,x,x)\n", "print \"%3e %13e %16e\" %(x,x,x)\n", "\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "12345 12345 12345\n", "\n", "\n", "345.678000 345.678000 345.678000\n", "3.456780e+02 3.456780e+02 3.456780e+02\n" ] } ], "prompt_number": 4 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 4.21, Page number: 4.17

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "i=12345\n", "x=345.678\n", "\n", "print \"%3d %5d %8d\\n\\n\" %(i,i,i)\n", "print \"%3g %10g %13g\" %(x,x,x)\n", "print \"%3g %13g %16g\" %(x,x,x)\n", "\n", "\n", "\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "12345 12345 12345\n", "\n", "\n", "345.678 345.678 345.678\n", "345.678 345.678 345.678\n" ] } ], "prompt_number": 5 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 4.22, Page number: 4.18

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "x=123.456\n", "\n", "print \"%7f %7.3f %7.1f\" %(x,x,x)\n", "print \"%12e %12.5e %12.3e\" %(x,x,x)\n", "\n", "\n", "\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "123.456000 123.456 123.5\n", "1.234560e+02 1.23456e+02 1.235e+02\n" ] } ], "prompt_number": 6 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 4.23, Page number: 4.19

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "x=123.456\n", "\n", "print \"%f %.3f %.1f\" %(x,x,x)\n", "print \"%e %.5e %.3e\" %(x,x,x)\n", "\n", "\n", "\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "123.456000 123.456 123.5\n", "1.234560e+02 1.23456e+02 1.235e+02\n" ] } ], "prompt_number": 7 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 4.24, Page number: 4.20

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "line=\"hexadecimal\"\n", "\n", "print \"%10s %15s %15.5s %.5s\" %(line,line,line,line)\n", "\n", "\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "hexadecimal hexadecimal hexad hexad\n" ] } ], "prompt_number": 8 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 4.26, Page number 4.21

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "a=0x80ec\n", "b=0.3e-12\n", "\n", "print \"%4x %10.2e\\n\\n\" %(a,b)\n", "print \"%4X %10.2E\" %(a,b)\n", "\n", "\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "80ec 3.00e-13\n", "\n", "\n", "80EC 3.00E-13\n" ] } ], "prompt_number": 9 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 4.27, Page number: 4.22

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "i,x,y=123,12.0,-3.3\n", "\n", "print \":%6d %7.0f %10.1e: \\n\" %(i,x,y)\n", "print \":%-6d %-7.0f %-10.1e: \\n\" %(i,x,y)\n", "print \":%+6d %+7.0f %+10.1e: \\n\" %(i,x,y)\n", "print \":%-+6d %-+7.0f %-+10.1e: \\n\" %(i,x,y)\n", "\n", "\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ ": 123 12 -3.3e+00: \n", "\n", ":123 12 -3.3e+00 : \n", "\n", ": +123 +12 -3.3e+00: \n", "\n", ":+123 +12 -3.3e+00 : \n", "\n", ": 12 12. -3.3 -3.30000: \n", "\n" ] } ], "prompt_number": 10 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 4.28, Page number: 4.23

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "i,j,k=1234,01777,0xa08c\n", "\n", "print \":%8u %8o %8x:\\n\" %(i,j,k)\n", "print \":%-8u %-8o %-8x:\\n\" %(i,j,k)\n", "print \":%08u %08o %08x:\\n\" %(i,j,k)\n", "\n", "\n", "\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ ": 1234 1777 a08c:\n", "\n", ":1234 1777 a08c :\n", "\n", ": 1234 01777 0xa08c:\n", "\n", ":00001234 00001777 0000a08c:\n", "\n" ] } ], "prompt_number": 11 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 4.29, Page number: 4.24

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "line=\"lower-case\"\n", "\n", "print \":%15s %15.5s %.5s:\" %(line,line,line)\n", "print \":%-15s %-15.5s %-.5s:\" %(line,line,line)\n", "\n", "\n", "\n", "\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ ": lower-case lower lower:\n", ":lower-case lower lower:\n" ] } ], "prompt_number": 12 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 4.30, Page number: 4.24

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "a,b,x1,x2=2.2,-6.2,0.005,-12.88\n", "\n", "print \"$%4.2f %7.1f%%\\n\" %(a,b)\n", "print \"x1=%7.3f x2=%7.3f\" %(x1,x2)\n", "\n", "\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "$2.20 -6.2%\n", "\n", "x1= 0.005 x2=-12.880\n" ] } ], "prompt_number": 13 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 4.32, Page number: 4.26

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "name=\"Robert Smith\"\n", "score1=88\n", "score2=62.5\n", "score3=90\n", "\n", "average=(float)(score1+score2+score3)/3\n", "print \"Name: %-s\" %(name)\n", "print \"Score1: %-5.1f\" %(score1)\n", "print \"Score2: %-5.1f\" %(score2)\n", "print \"Score3: %-5.1f\" %(score3)\n", "print \"Average: %-5.1f\" %(average)\n", "\n", "\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Name: Robert Smith\n", "Score1: 88.0 \n", "Score2: 62.5 \n", "Score3: 90.0 \n", "Average: 80.2 \n" ] } ], "prompt_number": 15 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }