{ "metadata": { "name": "", "signature": "sha256:48110bf73660634c54f30da35ad71ab95825c100a9ad798c30a92b3d23049e8e" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Chapter 3: Basic Input-Output" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example getchar.c, page no. 79" ] }, { "cell_type": "code", "collapsed": false, "input": [ "c = raw_input(\"Type one character: \")\n", "print \"The character you typed is: %c\" %c" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Type one character: w\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "The character you typed is: w\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example gets.c, page no. 80" ] }, { "cell_type": "code", "collapsed": false, "input": [ "s = raw_input(\"Type a string (Less than 80 characters):\")\n", "print \"The string typed is: %s\" %s" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Type a string (Less than 80 characters):C Book\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "The string typed is: C Book\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 3.1, page no. 81" ] }, { "cell_type": "code", "collapsed": false, "input": [ "f_var = 10.12576893\n", "print \"Output 1: %f\" %f_var\n", "print \"Output 2: %20f\" %f_var\n", "print \"Output 3: %-20f\" %f_var\n", "print \"Output 4: %+f\" %f_var\n", "wid_prec = int(raw_input(\"Input the width: \"))\n", "print \"Output 5: %*f\" %(wid_prec,f_var)\n", "print \"Output 6: %.5f\" %f_var\n", "wid_prec = int(raw_input(\"Input the precision: \"))\n", "print \"Output 7: %20.*f\" %(wid_prec,f_var)\n", "print \"Output 8: %-+20.5f\" %(f_var)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Output 1: 10.125769\n", "Output 2: 10.125769\n", "Output 3: 10.125769 \n", "Output 4: +10.125769\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Input the width: 13\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Output 5: 10.125769\n", "Output 6: 10.12577\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Input the precision: 8\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Output 7: 10.12576893\n", "Output 8: +10.12577 \n" ] } ], "prompt_number": 4 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example scanf1.c, page no. 84" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print \"Input an integer followed by a floating-point number:\"\n", "int_var = int(raw_input(\"integer:\"))\n", "float_var=float(raw_input(\"float:\"))\n", "print \"The Integer: %d\" %int_var\n", "print \"The floating-point number: %f\" %float_var" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Input an integer followed by a floating-point number:\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "integer:34\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "float:56.57\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "The Integer: 34\n", "The floating-point number: 56.570000\n" ] } ], "prompt_number": 5 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example scanf2.c, page no. 85" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#counting the inputs from user using raw_input() is not possible in Python. Hence, skipping the example" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 1 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example scanf3.c, page no. 86" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print \"Input the day, followed by the date (dd-mm-yyyy):\"\n", "day = raw_input(\"day:\")\n", "date = int(raw_input(\"date:\"))\n", "month = int(raw_input(\"month:\"))\n", "year = int(raw_input(\"year:\"))\n", "print \"Day: %s\" %day\n", "print \"Date: %s\" %date\n", "print \"Month: %s\" %month\n", "print \"Year: %s\" %year" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Input the day, followed by the date (dd-mm-yyyy):\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "day:Sunday\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "date:27\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "month:1\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "year:1974\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Day: Sunday\n", "Date: 27\n", "Month: 1\n", "Year: 1974\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example scanf4.c, page no. 87" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print \"Enter two strings: \"\n", "string1 = raw_input(\"string1:\")\n", "string2 = raw_input(\"string2:\")\n", "print \"You have input:\"\n", "print string1\n", "print string2" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter two strings: \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "string1:Hello\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "string2:world\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "You have input:\n", "Hello\n", "world\n" ] } ], "prompt_number": 7 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example scanf5.c, page no. 88" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#getting input from user the way it is given in textbook is not possible in Python.\n", "#Hence, output will be different compared to textbook.\n", "\n", "print \"Input two string: \"\n", "string1 = raw_input(\"String 1: \")\n", "string2 = raw_input(\"String 2: \")\n", "\n", "print \"The two strings are: %4s\" %string1\n", "print \"The two strings are: %5s\" %string2" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Input two string: \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "String 1: Morning\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "String 2: Comes\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "The two strings are: Morning\n", "The two strings are: Comes\n" ] } ], "prompt_number": 5 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example scanf6.c, page no. 89" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print \"Input the date:\"\n", "date = int(raw_input(\"date:\"))\n", "separator1 = raw_input(\"separator:\")\n", "month = int(raw_input(\"month:\"))\n", "separator2 = raw_input(\"separator:\")\n", "year = int(raw_input(\"year:\"))\n", "print \"Date: %d\" %date\n", "print \"Month: %d\" %month\n", "print \"Year: %d\" %year" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Input the date:\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "date:31\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "separator:-\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "month:12\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "separator:/\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "year:1999\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Date: 31\n", "Month: 12\n", "Year: 1999\n" ] } ], "prompt_number": 7 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example scanf7.c, page no. 90" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print \"Input the date:\"\n", "date = int(raw_input(\"date:\"))\n", "month = int(raw_input(\"month:\"))\n", "year = int(raw_input(\"year:\"))\n", "print \"Date: %d\" %date\n", "print \"Month: %d\" %month\n", "print \"Year: %d\" %year" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Input the date:\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "date:31\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "month:12\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "year:1999\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Date: 31\n", "Month: 12\n", "Year: 1999\n" ] } ], "prompt_number": 9 } ], "metadata": {} } ] }