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

Chapter 11: Console Input/Output

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

Printf Example, Page number: 397

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "avg = 346 \n", "per = 69.2\n", "\n", "#Result\n", "print \"Average = %d\\nPercentage = %f\" %(avg, per )\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Average = 346\n", "Percentage = 69.200000\n" ] } ], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Format Specifications, Page number: 399

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "weight = 63\n", "\n", "#Result\n", "print \"weight is %d kg\" %( weight ) \n", "print \"weight is %2d kg\"%( weight ) \n", "print \"weight is %4d kg\" %( weight )\n", "print \"weight is %6d kg\" %(weight ) \n", "print \"weight is %-6d kg\" %( weight )\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "weight is 63 kg\n", "weight is 63 kg\n", "weight is 63 kg\n", "weight is 63 kg\n", "weight is 63 kg\n" ] } ], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

String Format Specifiers, Page number: 400

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "firstname1 = \"Sandy\" \n", "surname1 = \"Malya\" \n", "firstname2 = \"AjayKumar\" \n", "surname2 = \"Gurubaxani\" \n", "\n", "#Result\n", "print \"%20s%20s\" %( firstname1, surname1 )\n", "print \"%20s%20s\" %(firstname2, surname2 ) \n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ " Sandy Malya\n", " AjayKumar Gurubaxani\n" ] } ], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Escape Sequences, Page number: 401

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "print \"You\\tmust\\tbe\\tcrazy\\nto\\thate\\tthis\\tbook\" \n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "You\tmust\tbe\tcrazy\n", "to\thate\tthis\tbook\n" ] } ], "prompt_number": 4 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Format Conversions, Page number: 403

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "ch = 'z' \n", "i = 125 \n", "a = 12.55 \n", "s = \"hello there !\"\n", "\n", "#Result\n", "print \"%c %d %f\" %( ch, ord(ch), ord(ch )) \n", "print \"%s %d %f\"%( s, ord(s[1]), ord(s[1]) )\n", "print \"%c %d %f\"%(i ,i, i ) \n", "print \"%f %d\\n\"%( a, a )\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "z 122 122.000000\n", "hello there ! 101 101.000000\n", "} 125 125.000000\n", "12.550000 12\n", "\n" ] } ], "prompt_number": 5 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Sprintf Function, Page number: 404

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "i = 10 \n", "ch = 'A'\n", "a = 3.14 \n", "\n", "\n", "#Result\n", "print \"%d %c %f\" %(i, ch, a ) \n", "str = \"%d %c %f\" %(i, ch, a ) #sprintf\n", "print \"%s\" %(str )\n", "\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "10 A 3.140000\n", "10 A 3.140000\n" ] } ], "prompt_number": 6 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Unformatted Input, Page number: 406

\n" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "import msvcrt\n", "\n", "print \"Press any key to continue\" \n", "#msvcrt.getch( ) # will not echo the character \n", "print \"Type any character\" \n", "#ch = msvcrt.getche( ) # will echo the character typed \n", "ch = 'A'\n", "print ch\n", "#ch = input(\"Type any character\")#getchar( ) will echo character, must be followed by enter key \n", "print \"Type any character\"\n", "ch = 8\n", "print ch\n", "#ch = input( \"Continue Y/N\" ) #fgetchar( ) will echo character, must be followed by enter key\n", "print \"Continue Y/N\" \n", "ch = 'N'\n", "print ch\n", "\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Press any key to continue\n", "Type any character\n", "A\n", "Type any character\n", "8\n", "Continue Y/N\n", "N\n" ] } ], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Unformatted Output, Page number: 407

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "ch = 'A'\n", "\n", "#Result\n", "print ch #putch\n", "print ch #putchar\n", "print ch #fputchar\n", "print 'Z' #putch\n", "print 'Z' #putchar\n", "print 'Z' #fputchar\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "A\n", "A\n", "A\n", "Z\n", "Z\n", "Z\n" ] } ], "prompt_number": 7 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Unformatted String Input/Output, Page number: 408

\n" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "print \"Enter name\"\n", "footballer = \"Jonty Rhodes\"\n", "print footballer\n", "\n", "#Result\n", "print \"Happy footballing!\" \n", "print footballer " ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter name\n", "Jonty Rhodes\n", "Happy footballing!\n", "Jonty Rhodes\n" ] } ], "prompt_number": 9 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }