{ "metadata": { "name": "", "signature": "sha256:67a316ecc3def0c634c382410d442033098ecf91839327bce17c0f2bf4424e27" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Chapter 10: Essential Input and Output" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 10.1, page no. 400 " ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "import sys\n", "\n", "def try_input(prompt, format):\n", " value_count = 5\n", " print prompt\n", " fp1 = float(raw_input(\"fp1: \"))\n", " i = int(raw_input(\"i: \"))\n", " j = int(raw_input(\"j: \"))\n", " word1 = raw_input()\n", " size_word1 = sys.getsizeof(word1)\n", " word2 = raw_input()\n", " size_word2 = sys.getsizeof(word2)\n", " print \"The input format string for scanf_s() is: \\\"%s\\\"\" %format\n", " print \"Count of bytes read = \", (size_word1 + size_word2)\n", " print \"Count of values read = %d\" % value_count\n", " print \"fp1 = %f i = %d j = %d\" %(fp1, i, j)\n", " print \"word1 = %s word2 = %s\" %(word1, word2)\n", " \n", "try_input(\"Enter as input: -2.35 15 25 ready2go \", \"%f %d %d %[abcdefghijklmnopqrstuvwxyz] %*1d %s%n\" )\n", "try_input(\"Enter the same input again: \", \"%4f %4d %d %*d %[abcdefghijklmnopqrstuvwxyz] %*1d %[^o]%n\")\n", "try_input(\"Enter as input: -2.3 15 25 ready2go\\n\",\"%4f %4d %d %*d %[abcdefghijklmnopqrstuvwxyz] %*1d %[^o]%n\")" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter as input: -2.35 15 25 ready2go \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "fp1: -2.35\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "i: 15\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "j: 25\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "ready2\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "go\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "The input format string for scanf_s() is: \"%f %d %d %[abcdefghijklmnopqrstuvwxyz] %*1d %s%n\"\n", "Count of bytes read = 82\n", "Count of values read = 5\n", "fp1 = -2.350000 i = 15 j = 25\n", "word1 = ready2 word2 = go\n", "Enter the same input again: \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "fp1: -2.35\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "i: 15\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "j: 25\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "ready 2go\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "2go\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "The input format string for scanf_s() is: \"%4f %4d %d %*d %[abcdefghijklmnopqrstuvwxyz] %*1d %[^o]%n\"\n", "Count of bytes read = 86\n", "Count of values read = 5\n", "fp1 = -2.350000 i = 15 j = 25\n", "word1 = ready 2go word2 = 2go\n", "Enter as input: -2.3 15 25 ready2go\n", "\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "fp1: -2.35\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "i: 15\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "j: 225\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "ready2\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "go\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "The input format string for scanf_s() is: \"%4f %4d %d %*d %[abcdefghijklmnopqrstuvwxyz] %*1d %[^o]%n\"\n", "Count of bytes read = 82\n", "Count of values read = 5\n", "fp1 = -2.350000 i = 15 j = 225\n", "word1 = ready2 word2 = go\n" ] } ], "prompt_number": 3 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 10.2, page no. 403" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "value_count = 2\n", "print \"Enter: fp1 = 3.14159 i = 7 j = 8\"\n", "print \"Input: \"\n", "fp1 = float(raw_input(\"fp1: \"))\n", "i = int(raw_input(\"i: \"))\n", "j = int(raw_input(\"j: \"))\n", "\n", "print \"Output:\"\n", "print \"Count of values read = \", value_count\n", "print \"fp1 = %f\\ti = %d\\tj = %d \" %(fp1, i, j)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter: fp1 = 3.14159 i = 7 j = 8\n", "Input: \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "fp1: 3.13159\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "i: 7\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "j: 8\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Output:\n", "Count of values read = 2\n", "fp1 = 3.131590\ti = 7\tj = 8 \n" ] } ], "prompt_number": 4 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 10.3, page no. 405" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "value_count = 3\n", "print \"Enter: 3.14 3.14 3.14\"\n", " \n", "print \"Input: \"\n", "\n", "fp1 = float(raw_input(\"fp1: \"))\n", "fp2 = float(raw_input(\"fp2: \"))\n", "fp3 = float(raw_input(\"fp3: \"))\n", "\n", "print \"Output: \"\n", "print \"Number of values read = \", value_count\n", "print \"fp1 = %f fp2 = %f fp3 = %f\" %(fp1, fp2, fp3)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter: 3.14 3.14 3.14\n", "Input: \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "fp1: 3.14\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "fp2: 3.14\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "fp3: 3.14\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Output: \n", "Number of values read = 3\n", "fp1 = 3.140000 fp2 = 3.140000 fp3 = 3.140000\n" ] } ], "prompt_number": 5 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 10.4, page no. 406" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "print \"Enter three integer values: \"\n", "n = 3\n", "i = int(raw_input())\n", "j = int(raw_input(), 16)\n", "k = int(raw_input(), 8)\n", " \n", "print \"Output:\"\n", "print \"%d values read.\" % n\n", "print \"i = %d j = %d k = %d \" %(i, j, k )" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter three integer values: \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "5\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "8\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "6\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Output:\n", "3 values read.\n", "i = 5 j = 8 k = 6 \n" ] } ], "prompt_number": 6 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 10.5, page no. 408" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "print \"Enter your first initial: \",\n", "initial = raw_input()\n", "print \"Enter your first name: \",\n", "name = raw_input()\n", "\n", "if(initial != name[0]):\n", " print \"%s, you got your initial wrong. \" % name\n", "else:\n", " print \"Hi, %s. Your initial is correct. Well done! \" %name \n", " print \"Enter your full name and your age separated by a comma: \", \n", " name_age = raw_input()\n", " print \"Your name is %s and you are %s years old.\" %(name_age.split(',')[0], name_age.split(',')[1])" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter your first initial: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "M\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Enter your first name: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Michael\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Hi, Michael. Your initial is correct. Well done! \n", "Enter your full name and your age separated by a comma: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Michael, 35\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Your name is Michael and you are 35 years old.\n" ] } ], "prompt_number": 9 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ " Program 10.6, page no. 410" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "print \"Enter your first initial: \",\n", "initial = raw_input()\n", "print \"Enter your first name: \",\n", "name = raw_input()\n", "\n", "if(initial != name[0]):\n", " print \"%s, you got your initial wrong. \" % name\n", "else:\n", " print \"Hi, %s. Your initial is correct. Well done! \" %name \n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter your first initial: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "M\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Enter your first name: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Nichael\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Nichael, you got your initial wrong. \n" ] } ], "prompt_number": 10 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 10.7, page no. 412" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "print \"Enter a sequence of integers and alphabetic names in a single line(separated by spaces): \",\n", "text = raw_input()\n", "split_text = text.split(' ')\n", "for element in split_text:\n", " if element.isdigit():\n", " print \"Integer Value: \", element\n", " elif element.isalpha():\n", " print \"Name: \", element\n", " else:\n", " pass " ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter a sequence of integers and alphabetic names in a single line(separated by spaces): " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Fred 22 34 Mary Jack 89 Jane\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Name: Fred\n", "Integer Value: 22\n", "Integer Value: 34\n", "Name: Mary\n", "Name: Jack\n", "Integer Value: 89\n", "Name: Jane\n" ] } ], "prompt_number": 11 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 10.8, page no. 419" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "i = 15\n", "j = 345\n", "k = 4567\n", "li = 56789\n", "lj = 67891234567\n", "lk = 23456789\n", "\n", "print \"i = %d j = %d k = %d li = %6.3d lj = %6.3d lk = %6.3d\" %(i ,j, k, i, j, k)\n", "print \"i = %-d j = %+d k = %-d i = %-6.3d j = %-6.3d k = %-6.3d \" %(i ,j, k, i, j, k)\n", "print \"li = %d lj = %d lk = %d \" %(li, lj, lk)\n", "print \"li = %d lj = %d lk = %d \" %(li, lj, lk)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "i = 15 j = 345 k = 4567 li = 015 lj = 345 lk = 4567\n", "i = 15 j = +345 k = 4567 i = 015 j = 345 k = 4567 \n", "li = 56789 lj = 67891234567 lk = 23456789 \n", "li = 56789 lj = 67891234567 lk = 23456789 \n" ] } ], "prompt_number": 12 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 10.9, page no. 421" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "k = \"678\"\n", "hex_k = int(k, 16)\n", "oct_k = int(oct(int(k)))\n", "k = 678\n", "print \"%%d %%o %%x %%X \"\n", "print \"%d %d %d %d \" %(k, oct_k, hex_k, hex_k)\n", "print \"|%%8d \\t|%%-8d \\t|%%+8d \\t|%%08d \\t|%%-+8d\"\n", "print \"|%8d |%-8d |%+8d |%08d |%-+8d |\" %(k, k, k, k, k)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "%%d %%o %%x %%X \n", "678 1246 1656 1656 \n", "|%%8d \t|%%-8d \t|%%+8d \t|%%08d \t|%%-+8d\n", "| 678 |678 | +678 |00000678 |+678 |\n" ] } ], "prompt_number": 13 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 10.10, page no. 422" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "fp1 = 345.678\n", "fp2 = 1.234E6\n", "fp3 = 234567898.0\n", "fp4 = 11.22334455e-6\n", " \n", "print \"%f %+f %-10.4f %6.4f\" %(fp1, fp2, fp1, fp2)\n", "print \"%e %+E\\n\" %(fp1, fp2)\n", "print \"%f %g %#+f %8.4f %10.4g \" %(fp3,fp3, fp3, fp3, fp4)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "345.678000 +1234000.000000 345.6780 1234000.0000\n", "3.456780e+02 +1.234000E+06\n", "\n", "234567898.000000 2.34568e+08 +234567898.000000 234567898.0000 1.122e-05 \n" ] } ], "prompt_number": 15 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 10.11, page no. 423" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "import string\n", "\n", "count = 0\n", "print \"The printable characters are the following: \"\n", "printable = string.printable\n", "for char in printable:\n", " if count % 32 == 0:\n", " print \"\"\n", " print char,\n", " count += 1" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The printable characters are the following: \n", "\n", "0 1 2 3 4 5 6 7 8 9 a b c d e f g h i j k l m n o p q r s t u v \n", "w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z ! \" \n", "# $ % & ' ( ) * + , - . / : ; < = > ? @ [ \\ ] ^ _ ` { | } ~ \t\n", "\n", "\r", "\u000b", "\f" ] } ], "prompt_number": 1 } ], "metadata": {} } ] }