{ "metadata": { "name": "", "signature": "sha256:3993f457ea298742521e73510f9498127e21d06e98284c389cdf6b608a1b8c94" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Hour 5: Handling Standard Input and Output" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 5.1, Page No.73" ] }, { "cell_type": "code", "collapsed": false, "input": [ "ch=raw_input(\"Please type in one character\")\n", "print \"The Character you just entered is: \", ch" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Please type in one characterH\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "The Character you just entered is: H\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 5.2, Page No.74" ] }, { "cell_type": "code", "collapsed": false, "input": [ "ch1=raw_input(\"Please type in first character\")\n", "ch2=raw_input(\"Please type in second character\")\n", "\n", "print \"The first character you just entered is: \",ch1\n", "print \"The second character you just entered is: \",ch2" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Please type in first characterH\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Please type in second characteri\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "The first character you just entered is: H\n", "The second character you just entered is: i\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 5.3, Page No.76" ] }, { "cell_type": "code", "collapsed": false, "input": [ "ch=65\n", "print \"The character that has numeric value of 65 is:\",chr(ch)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The character that has numeric value of 65 is: A\n" ] } ], "prompt_number": 3 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 5.4, Page No.77" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print chr(65)\n", "print chr(10)\n", "print chr(66)\n", "print chr(10)\n", "print chr(67)\n", "print chr(10)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "A\n", "\n", "\n", "B\n", "\n", "\n", "C\n", "\n", "\n" ] } ], "prompt_number": 4 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 5.5, Page No.80" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print \"Hex(uppercase) Hex(lowercase) Decimal\"\n", "print \"{:01X} {:01x} {:d}\".format(0,0,0)\n", "print \"{:01X} {:01x} {:d}\".format(1,1,1)\n", "print \"{:01X} {:01x} {:d}\".format(2,2,2)\n", "print \"{:01X} {:01x} {:d}\".format(3,3,3)\n", "print \"{:01X} {:01x} {:d}\".format(4,4,4)\n", "print \"{:01X} {:01x} {:d}\".format(5,5,5)\n", "print \"{:01X} {:01x} {:d}\".format(6,6,6)\n", "print \"{:01X} {:01x} {:d}\".format(7,7,7)\n", "print \"{:01X} {:01x} {:d}\".format(8,8,8)\n", "print \"{:01X} {:01x} {:d}\".format(9,9,9)\n", "print \"{:01X} {:01x} {:d}\".format(10,10,10)\n", "print \"{:01X} {:01x} {:d}\".format(11,11,11)\n", "print \"{:01X} {:01x} {:d}\".format(12,12,12)\n", "print \"{:01X} {:01x} {:d}\".format(13,13,13)\n", "print \"{:01X} {:01x} {:d}\".format(14,14,14)\n", "print \"{:01X} {:01x} {:d}\".format(15,15,15)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Hex(uppercase) Hex(lowercase) Decimal\n", "0 0 0\n", "1 1 1\n", "2 2 2\n", "3 3 3\n", "4 4 4\n", "5 5 5\n", "6 6 6\n", "7 7 7\n", "8 8 8\n", "9 9 9\n", "A a 10\n", "B b 11\n", "C c 12\n", "D d 13\n", "E e 14\n", "F f 15\n" ] } ], "prompt_number": 31 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 5.6, Page No.82" ] }, { "cell_type": "code", "collapsed": false, "input": [ "num1=12\n", "num2=12345\n", "print num1\n", "print num2\n", "print \"{:5d}\".format(num1)\n", "print \"{:05d}\".format(num1)\n", "print \"{:2d}\".format(num2)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "12\n", "12345\n", " 12\n", "00012\n", "12345\n" ] } ], "prompt_number": 32 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 5.7, Page No.83" ] }, { "cell_type": "code", "collapsed": false, "input": [ "num1=1\n", "num2=12\n", "num3=123\n", "num4=1234\n", "num5=12345\n", "print \"{:8d}{:-8d}\".format(num1,num1)\n", "print \"{:8d}{:-8d}\".format(num2,num2)\n", "print \"{:8d}{:-8d}\".format(num3,num3)\n", "print \"{:8d}{:-8d}\".format(num4,num4)\n", "print \"{:8d}{:-8d}\".format(num5,num5)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ " 1 1\n", " 12 12\n", " 123 123\n", " 1234 1234\n", " 12345 12345\n" ] } ], "prompt_number": 35 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 5.8, Page No.84" ] }, { "cell_type": "code", "collapsed": false, "input": [ "int_num=123\n", "flt_num=123.456789\n", "print \"Default integer format: \",int_num\n", "print \"With Precision Specifier: \",format(int_num,'08d')\n", "print \"Default float format: \",flt_num\n", "print \"With Precision Specifier: \",format(flt_num,'6.2f')" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Default integer format: 123\n", "With Precision Specifier: 00000123\n", "Default float format: 123.456789\n", "With Precision Specifier: 123.46\n" ] } ], "prompt_number": 44 } ], "metadata": {} } ] }