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

Chapter 5.1: Decision Statements

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

Example 5.1, Page number: 64

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Check whether the entered number is less than 10? if yes, display the same\n", "\n", "#Variable initialization\n", "v = raw_input(\"Enter the number : \")\n", "\n", "#In python, raw_input() is used to read values\n", "\n", "v = int(v)\n", "\n", "#Result\n", "if v < 10 :\n", " print '\\nNumber entered is less than 10'\n" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter the number : 9\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Number entered is less than 10\n" ] } ], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 5.2, Page number: 65

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Check equivalence of two numbers. Use if statement.\n", "\n", "#Variable initialization\n", "\n", "m = raw_input(\"Enter two numbers : \")\n", "n = raw_input(\"Enter two numbers : \")\n", "\n", "#In python, raw_input() is used to read values\n", "\n", "m = int(m)\n", "n = int(n)\n", "\n", "#Result\n", "if m - n == 0 :\n", " print '\\nTwo numbers are equal'\n", "\n", "\n" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter two numbers : 5\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter two numbers : 5\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Two numbers are equal\n" ] } ], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 5.3, Page number: 66

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Check whether the candidate's age is greater than 17 or not. If yes, display\n", "#message \"Eligible for Voting\"\n", "\n", "#Variable initialization\n", "age = raw_input(\"Enter age : \")\n", "age = int(age)\n", "\n", "#In python, raw_input() is used to read values\n", "\n", "#Result\n", "if age > 17 :\n", " print '\\nEligible for Voting.'\n", "\n", "\n" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter age : 20\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Eligible for Voting.\n" ] } ], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 5.4, Page number: 66

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Use curly brace in the if block. Enter only the three numbers and calculate\n", "#their sum and multiplication\n", "\n", "#Variable initialization\n", "#in python,the map(int,raw_input().split()) function splits the input characters\n", "#and converts it into integers and returns a value if all the values are integers\n", "#otherwise an exception will be generated and initializes v as 0.\n", "#using exception handling, this result can be achieved.\n", "\n", "#Exception handling\n", "try:\n", " #use space to separate the input characters\n", " v = raw_input(\"Enter Three Numbers : \")\n", " a,b,c = map(int,v.split())\n", " x = 3\n", "except:\n", " x = 0\n", "\n", "#Result\n", "#in python, block of statements are identified using indentation\n", " \n", "if x == 3:\n", " print \"Addition : %d\"%(a+b+c)\n", " print \"Multiplication : %d\"%(a*b*c)\n" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Three Numbers : 1 2 4\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Addition : 7\n", "Multiplication : 8\n" ] } ], "prompt_number": 5 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 5.5, Page number: 68

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Read the values of a,b,c through the keyboard. Add them and after addition\n", "#check if it is in the range of 100 & 200 or not. Print separate message for each.\n", "\n", "\n", "#Variable initialization\n", "a = raw_input(\"Enter Three Numbers a b c : \")\n", "b = raw_input(\"Enter Three Numbers a b c : \")\n", "c = raw_input(\"Enter Three Numbers a b c : \")\n", "\n", "a = int(a)\n", "b = int(b)\n", "c = int(c)\n", "\n", "print 'a = %d b = %d c = %d'%(a,b,c)\n", "#Calculation\n", "d = a + b + c\n", "\n", "#Condition evaluation and Result\n", "if d <= 200 and d >= 100 :\n", " print \"Sum is %d which is in between 100 & 200\"%(d)\n", "else:\n", " print \"\\nSum is %d which is out of range\"%(d)\n", "\n" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Three Numbers a b c : 50\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Three Numbers a b c : 52\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Three Numbers a b c : 54\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "a = 50 b = 52 c = 54\n", "Sum is 156 which is in between 100 & 200\n" ] } ], "prompt_number": 6 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 5.6, Page number: 68

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Find the roots of a quadratic equation by using if else condition\n", "\n", "#Variable initialization\n", "a = raw_input(\"Enter Values for a, b, c : \")\n", "b = raw_input(\"Enter Values for a, b, c : \")\n", "c = raw_input(\"Enter Values for a, b, c : \")\n", "\n", "a = int(a)\n", "b = int(b)\n", "c = int(c)\n", "\n", "#Condition evaluation and Result\n", "if b * b > 4 * a * c :\n", " x1 = b + sqrt(b*b - 4*a*c)/2*a\n", " x2 = b - sqrt(b*b - 4*a*c)/2*a\n", " print \"\\nx1 = %f x2 = %f\"%(x1,x2)\n", "else:\n", " print \"\\nRoots are Imaginary\"\n" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Values for a, b, c : 5\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Values for a, b, c : 1\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Values for a, b, c : 5\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Roots are Imaginary\n" ] } ], "prompt_number": 7 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 5.7, Page number: 69

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Calculate the square of those numbers only whose least significant digit is 5.\n", "\n", "\n", "#Variable initialization\n", "s = raw_input(\"Enter a Number : \")\n", "s = int(s)\n", "\n", "d = s % 10;\n", "\n", "#Condition evaluation and Result\n", "#There is no increment/decrement operator in python.\n", "if d==5 :\n", " s = s/10\n", " s += 1\n", " print \"\\nSquare = %d%d\"%(s*(s-1),d*d)\n", "else:\n", " print \"\\nInvalid Number\"\n" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter a Number : 25\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Square = 625\n" ] } ], "prompt_number": 8 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 5.8, Page number: 70

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Calculate the salary of medical representative based on the sales. Bonus and\n", "#incentive to be offered to him will be based on total sales. If the sale\n", "#exceeds Rs.100000/- follow the particulars of table (1) otherwise (2)\n", "\n", "# 1. TABLE 2. TABLE\n", "# Basic = Rs. 3000 Basic = Rs. 3000\n", "# HRA = 20% of basic HRA = 20% of basic\n", "# DA = 110% of basic DA = 110% of basic\n", "# Conveyance = Rs. 500 Conveyance = Rs. 500\n", "# Incentive = 10% of sales Incentive = 5% of sales\n", "# Bonus = Rs. 500 Bonus = Rs. 200\n", "\n", "\n", "#Variable initialization\n", "sale = raw_input(\"Enter Total Sales in Rs. : \")\n", "sale = int(sale)\n", "\n", "#Condition evaluation\n", "if sale >= 100000 :\n", " bs = 3000\n", " hra = 20 * bs/100\n", " da = 110 * bs/100\n", " cv = 500\n", " incentive = sale * 10/100\n", " bonus = 500\n", "else:\n", " bs = 3000\n", " hra = 20 * bs/100\n", " da = 110 * bs/100\n", " cv = 500\n", " incentive = sale * 5/100\n", " bonus = 200\n", "\n", "#Result\n", "ts = bs + hra + da + cv + incentive + bonus\n", "print \"\\nTotal Sales : %.2f\"%(sale)\n", "print \"\\nBasic Salary : %.2f\"%(bs)\n", "print \"\\nHra : %.2f\"%(hra)\n", "print \"\\nDa : %.2f\"%(da)\n", "print \"\\nConveyance : %.2f\"%(cv)\n", "print \"\\nIncentive : %.2f\"%(incentive)\n", "print \"\\nBonus : %.2f\"%(bonus)\n", "print \"\\nGross Salary : %.2f\"%(ts)\n" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Total Sales in Rs. : 100000\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Total Sales : 100000.00\n", "\n", "Basic Salary : 3000.00\n", "\n", "Hra : 600.00\n", "\n", "Da : 3300.00\n", "\n", "Conveyance : 500.00\n", "\n", "Incentive : 10000.00\n", "\n", "Bonus : 500.00\n", "\n", "Gross Salary : 17900.00\n" ] } ], "prompt_number": 9 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 5.9, Page number: 72

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Calculate energy bill using the starting and ending meter reading.\n", "#The charges are as follows:\n", "\n", "# No. of Units consumed Rates in (Rs.)\n", "# 200 - 500 3.50\n", "# 100 - 200 2.50\n", "# Less than 100 1.50\n", "\n", "#Variable initialization\n", "initial = raw_input(\"Initial & Final Readings : \")\n", "final = raw_input(\"Initial & Final Readings : \")\n", "\n", "consumed = int(final) - int(initial)\n", "\n", "#Condition evaluation\n", "if consumed >= 200 and consumed <= 500 :\n", " total = consumed * 3.50\n", "else:\n", " if consumed >= 100 and consumed <= 199:\n", " total = consumed * 2.50\n", " else :\n", " if consumed < 100 :\n", " total = consumed * 1.50\n", "\n", "#Result\n", "print \"Total bill for %d unit is %f\"%(consumed,total)\n" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Initial & Final Readings : 1200\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Initial & Final Readings : 1500\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Total bill for 300 unit is 1050.000000\n" ] } ], "prompt_number": 11 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 5.10, Page number: 73

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Calculate energy bill by reading the starting and ending meter reading.\n", "#If the consumed electricity energy is greater than or equal to 200 units\n", "#the rate should be 2.50/unit otherwise 1.50/unit.\n", "\n", "\n", "#Variable initialization\n", "initial = raw_input(\"Initial & Final Readings : \")\n", "final = raw_input(\"Initial & Final Readings : \")\n", "\n", "consumed = int(final) - int(initial)\n", "\n", "#Condition evaluation\n", "if consumed >= 200 :\n", " total = consumed * 2.50\n", "else:\n", " total = consumed * 1.50\n", "\n", "#Result\n", "print \"Total bill for %d unit is %f\"%(consumed,total)\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Total bill for 100 unit is 150.000000\n" ] } ], "prompt_number": 14 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 5.11, Page number: 73

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Sort six numbers and find the largest one by using ladder of if else\n", "\n", "#Variable initialization\n", "a = int(raw_input(\"Enter 1st number : \"))\n", "b = int(raw_input(\"Enter 2nd number : \"))\n", "c = int(raw_input(\"Enter 3rd number : \"))\n", "d = int(raw_input(\"Enter 4th number : \"))\n", "e = int(raw_input(\"Enter 5th number : \"))\n", "f = int(raw_input(\"Enter 6th number : \"))\n", "\n", "#Condition evaluation and Result\n", "if a > b and a > c and a > d and a > e and a > f :\n", " print \"Highest of six Number is : %d\"%(a)\n", "else:\n", " if b > a and b > c and b > d and b > e and b > f :\n", " print \"Highest of six Number is : %d\"%(b)\n", " else:\n", " if c > a and c > b and c > d and c > e and c > f :\n", " print \"Highest of six Number is : %d\"%(c)\n", " else:\n", " if d > a and d > b and d > c and d > e and d > f :\n", " print \"Highest of six Number is : %d\"%(d)\n", " else:\n", " if e > a and e > b and e > c and e > d and e > f :\n", " print \"Highest of six Number is : %d\"%(e)\n", " else:\n", " print \"Highest of six Number is : %d\"%(f)\n" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter 1st number : 52\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter 2nd number : 74\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter 3rd number : 90\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter 4th number : 45\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter 5th number : 10\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter 6th number : 22\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Highest of six Number is : 90\n" ] } ], "prompt_number": 12 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 5.12, Page number: 74

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Find largest number out of three numbers. Read the numbers through the keyboard\n", "\n", "\n", "#Variable initialization\n", "x = int(raw_input(\"\\nEnter Three Numbers x,y,z : \"))\n", "y = int(raw_input(\"\\nEnter Three Numbers x,y,z : \"))\n", "z = int(raw_input(\"\\nEnter Three Numbers x,y,z : \"))\n", "\n", "print \"\\nLargest out of Three Numbers is : \"\n", "\n", "#Condition evaluation and Result\n", "if x > y :\n", " if x > z:\n", " print \"x = %d\\n\"%(x)\n", " else:\n", " print \"z = %d\\n\"%(z)\n", "else:\n", " if z > y:\n", " print \"z = %d\\n\"%(z)\n", " else:\n", " print \"y = %d\\n\"%(y)\n" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter Three Numbers x,y,z : 10\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter Three Numbers x,y,z : 20\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter Three Numbers x,y,z : 30\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Largest out of Three Numbers is : \n", "z = 30\n", "\n" ] } ], "prompt_number": 13 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 5.13, Page number: 75

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Find the smallest out of the three numbers.\n", "\n", "\n", "#Variable initialization\n", "a = int(raw_input(\"\\nEnter Three Numbers : \"))\n", "b = int(raw_input(\"\\nEnter Three Numbers : \"))\n", "c = int(raw_input(\"\\nEnter Three Numbers : \"))\n", " \n", "#Condition evaluation and Result\n", "if a < b :\n", " if a < c:\n", " smallest = a\n", " else:\n", " smallest = c\n", "else:\n", " if b < c:\n", " smallest = b\n", " else:\n", " smallest = c\n", "\n", "#Result\n", "print \"The smallest of %d %d %d is %d\\n\"%(a,b,c,smallest)\n" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter Three Numbers : 1\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter Three Numbers : 5\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter Three Numbers : 8\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "The smallest of 1 5 8 is 1\n", "\n" ] } ], "prompt_number": 14 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 5.14, Page number: 76

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Calculate gross salary for the conditions given below\n", "\n", "# BASIC SALARY(Rs.) DA(Rs.) HRA(Rs.) CONVEYANCE(Rs.)\n", "# >= 5000 110% of basic 20% of basic 500\n", "# bs>=3000 & bs<5000 100% of basic 15% of basic 400\n", "# bs<3000 90% of basic 10% of basic 300\n", "\n", "\n", "#Variable initialization\n", "bs = int(raw_input(\"\\nEnter Basic Salary : \"))\n", "\n", "#Condition evaluation\n", "if bs >= 5000 :\n", " hra = 20 * bs/100\n", " da = 110 * bs/100\n", " cv = 500\n", "else:\n", " if bs >= 3000 and bs < 5000 :\n", " hra = 15 * bs/100\n", " da = 100 * bs/100\n", " cv = 400\n", " else:\n", " if bs < 3000:\n", " hra = 10 * bs/100\n", " da = 90 * bs/100\n", " cv = 300\n", "\n", "#Calculation\n", "ts = bs + hra + da + cv\n", "\n", "#Result\n", "print \"Basic Salary : %5.0f\"%(bs)\n", "print \"Hra : %5.0f\"%(hra)\n", "print \"Da : %5.0f\"%(da)\n", "print \"Conveyance : %5.0f\"%(cv)\n", "print \"Gross Salary : %5.0f\"%(ts)\n" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter Basic Salary : 5400\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Basic Salary : 5400\n", "Hra : 1080\n", "Da : 5940\n", "Conveyance : 500\n", "Gross Salary : 12920\n" ] } ], "prompt_number": 15 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 5.15, Page number: 77

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Calculate the total interest based on the following.\n", "\n", "#Principle Amount(Rs.) Rate of Interest(Rs.)\n", "# >= 10000 20%\n", "# >= 8000 & <= 9999 18%\n", "# < 8000 16%\n", "\n", "\n", "#Variable initialization\n", "princ = int(raw_input(\"\\nEnter Loan & No. of years :- \"))\n", "nyrs = int(raw_input(\"\\nEnter Loan & No. of years :- \"))\n", "\n", "#Condition evaluation\n", "if princ >= 10000 :\n", " rate = 20\n", "else:\n", " if princ >= 8000 and princ <= 9999 :\n", " rate = 18\n", " else:\n", " if princ < 8000:\n", " rate = 16\n", "\n", "#Calculation\n", "interest = princ * nyrs * rate/100\n", "\n", "#Result\n", "print \"Loan : %6.2f\"%(princ)\n", "print \"Years : %6.2f\"%(nyrs)\n", "print \"Rate : %6.2f\"%(rate)\n", "print \"Interest : %6.2f\"%(interest)\n" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter Loan & No. of years :- 5500\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter Loan & No. of years :- 3\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Loan : 5500.00\n", "Years : 3.00\n", "Rate : 16.00\n", "Interest : 2640.00\n" ] } ], "prompt_number": 16 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 5.16, Page number: 78

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Find the average of six subjects and display the results as follows.\n", "\n", "# AVERAGE RESULT\n", "# >34 & <50 Third Division\n", "# >49 & <60 Second Division\n", "# >60 & <75 First Division\n", "# >75 & <100 Distinction\n", "\n", "\n", "#Variable initialization\n", "a = int(raw_input(\"\\nEnter Marks\\nP C B M E H\\n\"))\n", "b = int(raw_input(\"\\nEnter Marks\\nP C B M E H\\n\"))\n", "c = int(raw_input(\"\\nEnter Marks\\nP C B M E H\\n\"))\n", "d = int(raw_input(\"\\nEnter Marks\\nP C B M E H\\n\"))\n", "e = int(raw_input(\"\\nEnter Marks\\nP C B M E H\\n\"))\n", "f = int(raw_input(\"\\nEnter Marks\\nP C B M E H\\n\"))\n", "\n", "#Calculation\n", "#here sum1 is used instead of sum because sum is a function in python.\n", "sum1 = a + b + c + d + e + f\n", "avg = sum1/6\n", "\n", "print \"Total : %d \\nAverage : %.2f\"%(sum1,avg)\n", "\n", "#Condition evaluation and Result\n", "if a < 35 or b < 35 or c < 35 or d < 35 or e < 35 :\n", " print \"Result : Fail\"\n", " exit\n", "if avg >= 34 and avg < 50 :\n", " print \"Result : Third Division \"\n", "else:\n", " if avg >= 49 and avg < 60:\n", " print \"Result : Second Division\"\n", " else:\n", " if avg >= 60 and avg > 75:\n", " print \"Result : First Division\"\n", " else:\n", " if avg >= 75 and avg <= 100:\n", " print \"Result : Distinction\"\n", " \n" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter Marks\n", "P C B M E H\n", "75\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter Marks\n", "P C B M E H\n", "75\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter Marks\n", "P C B M E H\n", "75\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter Marks\n", "P C B M E H\n", "75\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter Marks\n", "P C B M E H\n", "75\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter Marks\n", "P C B M E H\n", "75\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Total : 450 \n", "Average : 75.00\n", "Result : Distinction\n" ] } ], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 5.17, Page number: 80

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Detect the entered number as to whether it is even or odd. Use goto statement.\n", "\n", "\n", "#Variable initialization\n", "x = int(raw_input(\"\\nEnter a Number : \"))\n", "\n", "#Condition evaluation\n", "#There is no goto/label statement in python\n", "\n", "if x%2 == 0:\n", " print \"\\n%d is Even Number.\"%(x)\n", "else:\n", " print \"\\n%d is Odd Number.\"%(x)\n" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter a Number : 5\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "5 is Odd Number.\n" ] } ], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 5.18, Page number: 81

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Calculate telephone bill. Transfer controls at different places according\n", "#to number of calls and calculate the total charges. Follow rates as per\n", "#given in the table.\n", "\n", "# Telephone call Rate in Rs.\n", "# < 100 No charges\n", "# > 99 & < 200 1\n", "# > 199 & < 300 2\n", "# > 299 3\n", "\n", "\n", "#Variable initialization\n", "nc = int(raw_input(\"\\nEnter Number of Calls : \"))\n", "\n", "#Condition evaluation\n", "#There is no goto/label statement in python\n", "\n", "if nc < 100:\n", " print \"\\nNo charges\"\n", "else:\n", " if nc > 99 and nc < 200:\n", " print \"\\nTotal Charges : %d Rs.\"%(nc*1)\n", " else:\n", " if nc > 199 and nc < 300:\n", " print \"\\nTotal Charges : %d Rs.\"%(nc*2)\n", " else:\n", " print \"\\nTotal Charges : %d Rs.\"%(nc*3)\n" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter Number of Calls : 500\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Total Charges : 1500 Rs.\n" ] } ], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 5.19, Page number: 82

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Check whether the entered year is a leap year or not. Use goto statement.\n", "\n", "#Variable initialization\n", "leap = int(raw_input(\"\\nEnter Year : \"))\n", "\n", "#Condition evaluation\n", "#There is no goto/label statement in python\n", "\n", "if leap%4 == 0:\n", " print \"\\n%d is a leap year.\"%(leap)\n", "else:\n", " print \"%d is not a leap year.\"%(leap)\n" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter Year : 2000\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "2000 is a leap year.\n" ] } ], "prompt_number": 4 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 5.20, Page number: 84

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Print lines by selecting the choice.\n", "\n", "print \"\\n[1] ............\"\n", "print \"\\n[2] ____________\"\n", "print \"\\n[3] ************\"\n", "print \"\\n[4] ============\"\n", "print \"\\n[5] EXIT\"\n", "\n", "#Variable initialization\n", "ch = int(raw_input(\"\\nENTER YOUR CHOICE : \"))\n", "\n", "#Condition evaluation\n", "#there is no switch case statement in python, an alternative is to use\n", "#dictionary and functions\n", "\n", "def dot():\n", " print \"\\n......................................................\"\n", "\n", "def line():\n", " print \"\\n______________________________________________________\"\n", "\n", "def star():\n", " print \"\\n******************************************************\"\n", "\n", "def equal():\n", " print \"\\n======================================================\"\n", "\n", "def ex():\n", " exit\n", " \n", "options = {1 : dot,\n", " 2 : line,\n", " 3 : star,\n", " 4 : equal,\n", " 5 : ex\n", "}\n", "\n", "options[ch]()\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "[1] ............\n", "\n", "[2] ____________\n", "\n", "[3] ************\n", "\n", "[4] ============\n", "\n", "[5] EXIT\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "ENTER YOUR CHOICE : 1\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "......................................................\n" ] } ], "prompt_number": 5 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 5.21, Page number: 85

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Provide multiple functions such as 1. addition 2.Subtraction 3. Multiplication\n", "#4.Division 5.Remainder calculation 6.Larger out of two by using switch statement\n", "\n", "print \"\\n=============================\"\n", "print \"\\n\\t\\tMENU\"\n", "print \"\\n=============================\"\n", "print \"\\n\\t[1] ADDITION\"\n", "print \"\\n\\t[2] SUBTRACTION\"\n", "print \"\\n\\t[3] MULTIPLICATION\"\n", "print \"\\n\\t[4] DIVISION\"\n", "print \"\\n\\t[5] REMAINDER\"\n", "print \"\\n\\t[6] LARGER OUT OF TWO\"\n", "print \"\\n\\t[0] EXIT\"\n", "print \"\\n=============================\"\n", "\n", "#Variable initialization\n", "ch = int(raw_input(\"\\nENTER YOUR CHOICE : \"))\n", "\n", "#Condition evaluation\n", "#there is no switch case statement in python, an alternative is to use\n", "#dictionary and functions\n", "\n", "if ch <= 6 and ch > 0:\n", " a = int(raw_input(\"Enter Two Numbers : \"))\n", " b = int(raw_input(\"Enter Two Numbers : \"))\n", " \n", "def add():\n", " c = a + b\n", " print \"\\nAddition : %d\"%(c)\n", "\n", "def sub():\n", " c = a - b\n", " print \"\\nSubtraction : %d\"%(c)\n", "\n", "def mul():\n", " c = a * b\n", " print \"\\nMultiplication : %d\"%(c)\n", "\n", "def div():\n", " c = a/b\n", " print \"\\nDivision : %d\"%(c)\n", "\n", "def rem():\n", " c = a % b\n", " print \"\\nRemainder : %d\"%(c)\n", "\n", "def larger():\n", " if a > b:\n", " print \"\\n%d (a) is larger than %d (b)\"%(a,b)\n", " else:\n", " if b > a:\n", " print \"\\n%d (b) is larger than %d (a)\"%(b,a)\n", " else:\n", " print \"\\n%d (a) & %d (b) are same\"%(a,b)\n", "\n", "def ex():\n", " print \"\\nTerminated by choice\"\n", " exit\n", "\n", "def default():\n", " print \"\\nInvalid Choice\"\n", "\n", "options = { 1 : add,\n", " 2 : sub,\n", " 3 : mul,\n", " 4 : div,\n", " 5 : rem,\n", " 6 : larger,\n", " 7 : ex,\n", " 0 : default,\n", " 8 : default,\n", " 9 : default\n", "}\n", "\n", "options[ch]()\n", " \n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "=============================\n", "\n", "\t\tMENU\n", "\n", "=============================\n", "\n", "\t[1] ADDITION\n", "\n", "\t[2] SUBTRACTION\n", "\n", "\t[3] MULTIPLICATION\n", "\n", "\t[4] DIVISION\n", "\n", "\t[5] REMAINDER\n", "\n", "\t[6] LARGER OUT OF TWO\n", "\n", "\t[0] EXIT\n", "\n", "=============================\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "ENTER YOUR CHOICE : 6\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Two Numbers : 9\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Two Numbers : 8\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "9 (a) is larger than 8 (b)\n" ] } ], "prompt_number": 6 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 5.22, Page number: 87

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Convert years into Minutes, Hours, Days, Months and Seconds using switch()\n", "#statement\n", "\n", "\n", "print \"[1] MINUTES\"\n", "print \"[2] HOURS\"\n", "print \"[3] DAYS\"\n", "print \"[4] MONTHS\"\n", "print \"[5] SECONDS\"\n", "print \"[0] EXIT\"\n", "\n", "#Variable initialization\n", "ch = int(raw_input(\"\\nEnter Your Choice : \"))\n", "\n", "#Condition evaluation\n", "#there is no switch case statement in python, an alternative is to use\n", "#dictionary and functions\n", "\n", "if ch > 0 and ch < 6:\n", " yrs = int(raw_input(\"Enter Years : \"))\n", "\n", "#Calculation\n", "#since min is a keyword in python, min1 is used instead.\n", "mon = yrs * 12\n", "ds = mon * 30\n", "hrs = ds * 24\n", "min1 = hrs * 60\n", "se = min1 * 60\n", "\n", "def minute():\n", " print \"\\nMinutes : %ld\"%(min1)\n", "\n", "def hours():\n", " print \"\\nHours : %ld\"%(hrs)\n", "\n", "def days():\n", " print \"\\nDays : %ld\"%(ds)\n", "\n", "def months():\n", " print \"\\nMonths : %ld\"%(mon)\n", "\n", "def seconds():\n", " print \"\\nSeconds : %ld\"%(se)\n", "\n", "def ex():\n", " print \"\\nTerminated by choice\"\n", " exit\n", "\n", "def default():\n", " print \"\\nInvalid Choice\"\n", "\n", "options = { 1 : minute,\n", " 2 : hours,\n", " 3 : days,\n", " 4 : months,\n", " 5 : seconds,\n", " 6 : ex,\n", " 0 : default,\n", " 8 : default,\n", " 9 : default\n", "}\n", "\n", "options[ch]()\n", " \n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[1] MINUTES\n", "[2] HOURS\n", "[3] DAYS\n", "[4] MONTHS\n", "[5] SECONDS\n", "[0] EXIT\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter Your Choice : 4\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Years : 2\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Months : 24\n" ] } ], "prompt_number": 7 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 5.23, Page number: 89

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Display the names of the days of a week.\n", "\n", "\n", "#Variable initialization\n", "day = int(raw_input(\"\\nEnter a number between 1 to 7 : \"))\n", "\n", "#Condition evaluation\n", "#there is no switch case statement in python, an alternative is to use\n", "#dictionary and functions\n", "\n", "def first():\n", " print \"\\n1st day of week is Sunday\"\n", "\n", "def second():\n", " print \"\\n2nd day of week is Monday\"\n", "\n", "def third():\n", " print \"\\n3rd day of week is Tuesday\"\n", "\n", "def fourth():\n", " print \"\\n4th day of week is Wednesday\"\n", "\n", "def fifth():\n", " print \"\\n5th day of week is Thursday\"\n", "\n", "def sixth():\n", " print \"\\n6th day of week is Friday\"\n", "\n", "def seventh():\n", " print \"\\n7th day of week is Saturday\"\n", "\n", "def default():\n", " print \"\\nInvalid day\"\n", "\n", "options = { 1 : first,\n", " 2 : second,\n", " 3 : third,\n", " 4 : fourth,\n", " 5 : fifth,\n", " 6 : sixth,\n", " 7 : seventh,\n", " 0 : default,\n", " 8 : default,\n", " 9 : default\n", "}\n", "for i in range(1,day+1):\n", " options[i]()\n", " \n" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter a number between 1 to 7 : 4\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "1st day of week is Sunday\n", "\n", "2nd day of week is Monday\n", "\n", "3rd day of week is Tuesday\n", "\n", "4th day of week is Wednesday\n" ] } ], "prompt_number": 8 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 5.24, Page number: 90

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Perform following operations\n", "\n", "#1. Display any numbers or stars on the screen by using for loop\n", "#2. Display the menu containing the following\n", "#a) Whole screen b)Half screen c)Top 3 lines d)Bottom 3 lines\n", "\n", "import os\n", "import sys\n", "import turtle\n", "\n", "\n", "#Print numbers in the whole screen\n", "for i in range(1,700):\n", " sys.stdout.write(\"%d\"%i)\n", "\n", "print \"\\nCLEAR MENU\"\n", "print \"\\t1] Whole Screen\\t2] Half Screen\\t3]Top 3 lines\\t4] Bottom 3 lines\\t5] Exit\"\n", "\n", "#Variable initialization\n", "c = int(raw_input(\"Enter Your Choice : \"))\n", "\n", "#Condition evaluation\n", "#there is no switch case statement in python, an alternative is to use\n", "#dictionary and functions\n", "\n", "def one():\n", " os.system('cls')\n", "\n", "def two():\n", " for i in range(0,190):\n", " turtle.goto(i,1)\n", " sys.stdout.write(\"\\t\")\n", " \n", "def three():\n", " for i in range(1,100):\n", " turtle.goto(i,1)\n", " sys.stdout.write(\"\\t\")\n", "\n", "def four():\n", " for i in range(1,120):\n", " turtle.goto(i,21)\n", " sys.stdout.write(\"\\t\")\n", "\n", "def five():\n", " exit\n", "\n", "\n", "options = { 1 : one,\n", " 2 : two,\n", " 3 : three,\n", " 4 : four,\n", " 5 : five\n", "}\n", "options[c]()\n", " \n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699\n", "CLEAR MENU\n", "\t1] Whole Screen\t2] Half Screen\t3]Top 3 lines\t4] Bottom 3 lines\t5] Exit\n", "\t" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\t\t\t\t\t" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\t\t\t\t\t\t" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\t\t\t\t\t\t" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\t\t\t\t\t\t" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\t\t\t\t\t\t" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\t\t\t\t\t" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\t\t\t\t\t" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\t\t\t\t\t\t" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\t\t\t\t\t\t" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\t\t\t\t\t" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\t\t\t\t\t\t" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\t\t\t\t\t" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\t\t\t\t\t\t" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\t\t\t\t\t\t" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\t\t\t\t\t\t" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\t\t\t\t\t" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\t\t\t\t\t" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\t\t\t\t\t\t" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\t\t\t\t\t\t" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\t\t\t\t\t\t" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\t\t\t\t\t\t" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\t\t\t\t\t\t" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\t\t\t\t\t" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\t\t\t\t\t\t" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\t\t\t\t\t" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\t\t\t\t\t" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\t\t\t\t\t\t" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\t\t\t\t\t\t" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\t\t\t\t\t\t" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\t\t\t\t\t" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\t\t\t\t\t\t" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\t\t\t\t\t" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\t\t\t\t\t" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\t\t\t\t" ] } ], "prompt_number": 4 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 5.25, Page number: 91

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Display the files of current directory\n", "\n", "import os\n", "\n", "print \"\\nFILE LISTING MENU\"\n", "print \"1] .EXE\"\n", "print \"2] .BAT\"\n", "print \"3] .OBJ\"\n", "print \"4] .BAK\"\n", "#Variable initialization\n", "c = int(raw_input(\"Enter Your Choice -: \"))\n", "\n", "#Condition evaluation\n", "#there is no switch case statement in python, an alternative is to use\n", "#dictionary and functions\n", "\n", "def one():\n", " os.system('dir *.exe')\n", "\n", "def two():\n", " os.system('dir *.c')\n", "\n", "def three():\n", " os.system('dir *.obj')\n", "\n", "def four():\n", " os.system('dir *.bak')\n", "\n", "options = { 1 : one,\n", " 2 : two,\n", " 3 : three,\n", " 4 : four\n", "}\n", "options[c]()\n", "\n", " \n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "FILE LISTING MENU\n", "1] .EXE\n", "2] .BAT\n", "3] .OBJ\n", "4] .BAK\n" ] } ], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 5.26, Page number: 92

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Display number of days in calendar format of an entered month of year 2001.\n", "\n", "import sys\n", "\n", "#Variable initialization\n", "m = int(raw_input(\"Enter Month No. of Year 2001 : \"))\n", "\n", "#Condition evaluation\n", "#there is no switch case statement in python, an alternative is to use\n", "#else-if ladder.\n", "\n", "print \"\\nMonth - %d - 2001\"%(m)\n", "print \"\\n\\tSUN\\tMON\\tTUE\\tWED\\tTHU\\tFRI\\tSAT\"\n", "\n", "#Find a day any month of 2001\n", "\n", "if m == 1:\n", " a = 2\n", " j = 31\n", "else:\n", " if m == 2:\n", " a = 5\n", " j = 28\n", " else:\n", " if m == 3:\n", " a = 5\n", " j = 31\n", " else:\n", " if m == 4:\n", " a = 1\n", " j = 30\n", " else:\n", " if m == 5:\n", " a = 3\n", " j = 31\n", " else:\n", " if m == 6:\n", " a = 6\n", " j = 30\n", " else:\n", " if m == 7:\n", " a = 1\n", " j = 31\n", " else:\n", " if m == 8:\n", " a = 4\n", " j = 31\n", " else:\n", " if m == 9:\n", " a = 7\n", " j = 30\n", " else:\n", " if m == 10:\n", " a = 2\n", " j = 31\n", " else:\n", " if m == 11:\n", " a = 5\n", " j = 30\n", " else:\n", " if m == 12:\n", " a = 7\n", " j = 31\n", " else:\n", " print \"Invalid Month\"\n", " exit\n", "\n", "#Starting day is to be adjusted under the respective day\n", "#sys.stdout.write() function performs the same task as printf() function\n", " \n", "i = 1\n", "if a == 1:\n", " sys.stdout.write(\"\\t%d\"%(i))\n", "else:\n", " if a == 2:\n", " sys.stdout.write(\"\\t\\t%d\"%(i))\n", " else:\n", " if a == 3:\n", " sys.stdout.write(\"\\t\\t\\t%d\"%(i))\n", " else:\n", " if a == 4:\n", " sys.stdout.write(\"\\t\\t\\t\\t%d\"%(i))\n", " else:\n", " if a == 5:\n", " sys.stdout.write(\"\\t\\t\\t\\t\\t%d\"%(i))\n", " else:\n", " if a == 6:\n", " sys.stdout.write(\"\\t\\t\\t\\t\\t\\t%d\"%(i))\n", " else:\n", " if a == 7:\n", " sys.stdout.write(\"\\t\\t\\t\\t\\t\\t\\t%d\"%(i))\n", "\n", "\n", "h = 8 - a #The starting day is subtracted from 8\n", "for i in range(2,h+1): #to display the first row\n", " sys.stdout.write(\"\\t%d\"%(i))\n", "sys.stdout.write(\"\\n\")\n", "b = 1\n", "for i in range(h+1,j+1): #to continue with second row onwards\n", " if b == 8:\n", " sys.stdout.write(\"\\n\")\n", " b = 1\n", " sys.stdout.write(\"\\t%d\"%(i))\n", " b += 1\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Month - 1 - 2001\n", "\n", "\tSUN\tMON\tTUE\tWED\tTHU\tFRI\tSAT\n", "\t\t1\t2\t3\t4\t5\t6\n", "\t7\t8\t9\t10\t11\t12\t13\n", "\t14\t15\t16\t17\t18\t19\t20\n", "\t21\t22\t23\t24\t25\t26\t27\n", "\t28\t29\t30\t31" ] } ], "prompt_number": 4 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 5.27, Page number: 95

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Convert decimal to hexadecimal number.\n", "\n", "import sys\n", "import turtle\n", "\n", "#Variable initialization\n", "x = int(raw_input(\"Enter a number : \"))\n", "y = 30\n", "\n", "#Condition evaluation\n", "#there is no switch case statement in python, an alternative is to use\n", "#else-if ladder.\n", "\n", "print \"\\nConversion of Decimal to Hexadecimal Number\"\n", "\n", "#for loop without condition is not supported in python. so while loop is used.\n", "\n", "c = 1\n", "z = [0 for i in range(0,15)]\n", "\n", "while x != 0:\n", " z[c] = x % 16\n", " c = c + 1\n", " x = x/16\n", "\n", "\n", "for i in range(c-1,0,-1):\n", " if z[i] == 10:\n", " sys.stdout.write(\"A\")\n", " else:\n", " if z[i] == 11:\n", " sys.stdout.write(\"B\")\n", " else:\n", " if z[i] == 12:\n", " sys.stdout.write(\"C\")\n", " else:\n", " if z[i] == 13:\n", " sys.stdout.write(\"D\")\n", " else:\n", " if z[i] == 14:\n", " sys.stdout.write(\"E\")\n", " else:\n", " if z[i] == 15:\n", " sys.stdout.write(\"F\")\n", " else:\n", " sys.stdout.write(\"%d\"%(z[i]))\n", " \n", " \n", " \n" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter a number : 31\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Conversion of Decimal to Hexadecimal Number\n", "1F" ] } ], "prompt_number": 9 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 5.28, Page number: 97

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Detect whether the entered number is even or odd. use nested switch-case\n", "\n", "\n", "#Variable initialization\n", "x = int(raw_input(\"Enter a Number : \"))\n", "\n", "#Condition evaluation\n", "#there is no switch case statement in python, an alternative is to use\n", "#else-if ladder.\n", "\n", "if x == 0:\n", " print \"Number is Even\"\n", "else:\n", " if x == 1:\n", " print \"Number is odd\"\n", " else:\n", " y = x % 2\n", " if y == 0:\n", " print \"Number is Even\"\n", " else:\n", " print \"Number is odd\"\n" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter a Number : 5\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Number is odd\n" ] } ], "prompt_number": 10 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 5.29, Page number: 98

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Count number of 1s, 0s, blank spaces and others using nested\n", "#switch() statement in a given stream\n", "\n", "import sys\n", "\n", "#Variable initialization\n", "txt = raw_input(\"Enter Numbers : \")\n", "\n", "#Processing\n", "x = 0\n", "s = 0\n", "a = 0\n", "z = 0\n", "o = 0\n", "\n", "while x < len(txt):\n", " if txt[x] == ' ':\n", " s = s + 1\n", " else:\n", " if txt[x] == '1':\n", " a = a + 1\n", " else:\n", " if txt[x] == '0':\n", " z = z + 1\n", " else:\n", " o = o + 1\n", " x = x + 1\n", " \n", "#Result\n", "sys.stdout.write(\"\\nTotal Spaces : %d\"%(s))\n", "sys.stdout.write(\"\\nTotal 1s : %d\"%(a))\n", "sys.stdout.write(\"\\nTotal 0s : %d\"%(z))\n", "sys.stdout.write(\"\\nOthers : %d\"%(o))\n", "sys.stdout.write(\"\\nString Length : %d\"%(s+a+z+o))\n", " \n", " " ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Numbers : 1110022 222\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Total Spaces : 1\n", "Total 1s : 3\n", "Total 0s : 2\n", "Others : 5\n", "String Length : 11" ] } ], "prompt_number": 11 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 5.30, Page number: 99

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Convert integer to character using if condition\n", "\n", "\n", "#Variable initialization\n", "x = int(raw_input(\"Enter a Number : \"))\n", "\n", "#Condition evaluation\n", "#ord() function converts the character ASCII to integer\n", "\n", "if x == ord('A'):\n", " print \"%c\"%(x)\n" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter a Number : 65\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "A\n" ] } ], "prompt_number": 12 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 5.31, Page number: 100

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Use nested if..else statements in switch() statement. Also show the effect\n", "#of conversion of integer to character\n", "\n", "\n", "#Variable initialization\n", "i = int(raw_input(\"Enter any ASCII Number : \"))\n", "\n", "#Condition evaluation\n", "#there is no switch..case statement in python. alternative is to use\n", "#if..else and else..if ladder\n", "\n", "if i == ord('A'):\n", " print \"Capital A\"\n", "else:\n", " if i == ord('B'):\n", " print \"Capital B\"\n", " else:\n", " if i == ord('C'):\n", " print \"Capital C\"\n", " else:\n", " if i > 47 and i < 58:\n", " print \"Digit : [%c]\"%(i)\n", " else:\n", " if i >= 58 and i <= 64:\n", " print \"Symbol : [%c]\"%(i)\n", " else:\n", " if i > 64 and i < 91:\n", " print \"Capital : [%c]\"%(i)\n", " else:\n", " if i > 96 and i < 123:\n", " print \"Small : [%c]\"%(i)\n", " else:\n", " print \"Invalid Choice\"\n" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter any ASCII Number : 65\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Capital A\n" ] } ], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }