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

Chapter 2: The Decision Control Structure

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

If Demo , Page number: 52

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "print \"Enter a number less than 10: \"\n", "num = 8\n", "print num\n", "\n", "#if statement\n", "if num <= 10:\n", " print(\"What an obedient servant you are !\") #display result\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter a number less than 10: \n", "8\n", "What an obedient servant you are !\n" ] } ], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 2.1 , Page number: 53

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "dis = 0 #Initial Discount (%0)\n", "\n", "#Input from the user\n", "#qty,rate = raw_input(\"Enter quantity and rate: \").split()\n", "print \"Enter quantity and rate: \"\n", "qty = 1200 # Quantity of item\n", "rate = 15.50 # Rate of item (Rs)\n", "print qty , rate\n", "\n", "#discount of 10% if quantity > 1000\n", "if qty > 1000:\n", " dis = 10\n", "\n", "#Calculation\n", "tot = (qty * rate) - (qty * rate * dis / 100 ) # total expenses (Rs)\n", "\n", "#Result\n", "print \"Total expenses = Rs. \", tot \n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter quantity and rate: \n", "1200 15.5\n", "Total expenses = Rs. 16740.0\n" ] } ], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 2.2, Page number: 57

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "#cy,yoj = raw_input(\"Enter current year and year of joining: \").split() \n", "print \"Enter current year and year of joining: \"\n", "cy = 2013 # Current year\n", "yoj = 1990 # Year of joining\n", "print cy, yoj \n", "#Calculation\n", "yr_of_ser = cy - yoj # number of years of service\n", "\n", "#Assign bonus if years of service > 3\n", "if yr_of_ser > 3:\n", " bonus = 2500 # Bonus of Rs. 2500\n", " print \"Bonus = Rs.\", bonus #display result\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter current year and year of joining: \n", "2013 1990\n", "Bonus = Rs. 2500\n" ] } ], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 2.3 , Page number: 58

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "print \"Enter basic salary: \"\n", "bs = 2561.1 #Basic salary (Rs)\n", "print bs\n", "\n", "#Calculation\n", "if bs < 1500: # if basic salary is less than Rs.1500\n", " hra = bs * 10 / 100 # HRA (Rs)\n", " da = bs * 90 / 100 #DA (Rs)\n", "else: #if basic salary is greater than or equal to Rs.1500\n", " hra = 500 # HRA (Rs)\n", " da = bs * 98 / 100 # DA (Rs)\n", "\n", "gs = bs + hra + da # gross salary (Rs)\n", "\n", "#Result\n", "print \"gross salary = Rs. \", gs \n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter basic salary: \n", "2561.1\n", "gross salary = Rs. 5570.978\n" ] } ], "prompt_number": 4 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Nested If-else , Page number: 61

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "print \"Enter either 1 or 2: \"\n", "i = 1\n", "print i\n", "\n", "#nested if-else\n", "if i == 1 :\n", " print \"You would go to heaven !\" \n", "else:\n", " if i == 2 :\n", " print \"Hell was created with you in mind\" \n", " else:\n", " print \"How about mother earth !\" " ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter either 1 or 2: \n", "1\n", "You would go to heaven !\n" ] } ], "prompt_number": 5 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 2.4 (Method 1), Page number: 64

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "print \"Enter marks in five subjects: \"\n", "m1 = 88 #Marks in 1st subject\n", "m2 = 92 #Marks in 2nd subject\n", "m3 = 87 #Marks in 3rd subject\n", "m4 = 66 #Marks in 4th subject\n", "m5 = 56 #Marks in 5th subject\n", "print m1,m2,m3,m4,m5\n", "\n", "#Calculation\n", "per = ( m1 + m2 + m3 + m4 + m5 ) / 5 #Percentage\n", "\n", "#check for different cases and display appropriate result\n", "if per >= 60:\n", " print \"First division\"\n", "else:\n", " if per >= 50:\n", " print \"Second division\"\n", " else:\n", " if per >= 40:\n", " print \"Third division\"\n", " else:\n", " print \"Fail\"\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ " Enter marks in five subjects: \n", "88 92 87 66 56\n", "First division\n" ] } ], "prompt_number": 7 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 2.4 (Method 2), Page number: 65

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "print \"Enter marks in five subjects: \"\n", "m1 = 88 #Marks in 1st subject\n", "m2 = 92 #Marks in 2nd subject\n", "m3 = 87 #Marks in 3rd subject\n", "m4 = 66 #Marks in 4th subject\n", "m5 = 56 #Marks in 5th subject\n", "print m1,m2,m3,m4,m5\n", "\n", "#Calculation\n", "per = ( m1 + m2 + m3 + m4 + m5 ) / 5 #Percentage\n", "\n", "#check for different cases and display appropriate result\n", "if per >= 60:\n", " print \"First division\"\n", "\n", "if (per >= 50) and (per <60):\n", " print\"Second division\"\n", "\n", "if (per >= 40) and (per <50):\n", " print\"Third division\"\n", "\n", "if per < 40 :\n", " print\"Fail\"\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter marks in five subjects: \n", "88 92 87 66 56\n", "First division\n" ] } ], "prompt_number": 8 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 2.4 (Method 3), Page number: 67

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "print \"Enter marks in five subjects: \"\n", "m1 = 88 #Marks in 1st subject\n", "m2 = 92 #Marks in 2nd subject\n", "m3 = 87 #Marks in 3rd subject\n", "m4 = 66 #Marks in 4th subject\n", "m5 = 56 #Marks in 5th subject\n", "print m1,m2,m3,m4,m5\n", "\n", "#Calculation\n", "per = ( m1 + m2 + m3 + m4 + m5 ) / 5 #Percentage\n", "\n", "#check for different cases and display appropriate result\n", "if per >= 60:\n", " print\"First division\"\n", "elif per >= 50:\n", " print\"Second division\"\n", "elif per >= 40:\n", " print\"Third division\"\n", "else:\n", " print\"Fail\"\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter marks in five subjects: \n", "88 92 87 66 56\n", "First division\n" ] } ], "prompt_number": 9 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 2.5 (Method 1) , Page number: 68

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "print \"Enter age, sex, marital status: \"\n", "age = 43 # Age of driver (years)\n", "sex = 'M'\n", "ms = 'M'\n", "print age,sex,ms\n", "#check for different cases and display appropriate result\n", "if ms == 'M':\n", " print(\"Driver is insured\")\n", "else:\n", " if sex == 'M':\n", " if age > 30:\n", " print (\"Driver is insured\")\n", " else:\n", " print (\"Driver is not insured\")\n", " else:\n", " if age > 25:\n", " print (\"Driver is insured\")\n", " else:\n", " print (\"Driver is not insured\")\n", " \n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter age, sex, marital status: \n", "43 M M\n", "Driver is insured\n" ] } ], "prompt_number": 11 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 2.5 (Method 2) , Page number: 69

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "print \"Enter age, sex, marital status: \"\n", "age = 43 # Age of driver (years)\n", "sex = 'M'\n", "ms = 'M'\n", "print age,sex,ms\n", "\n", "#check for different cases and display appropriate result\n", "if ((ms == 'M') or (ms == 'U' and sex == 'M' and age > 30) or (ms == 'U' and sex == 'F' and age >25) ) :\n", " print\"Driver is insured\"\n", "else:\n", " print\"Driver is not insured\"\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter age, sex, marital status: \n", "43 M M\n", "Driver is insured\n" ] } ], "prompt_number": 12 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 2.6, Page number: 71

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "print \"Enter Gender, Years of Service and Qualifications ( 0 = G, 1 = PG ):\"\n", "g = 'f'\n", "yos = 8 # Years of service(years)\n", "qual = 1 # Qualification ( 0=G, 1=PG)\n", "print g,yos,qual\n", "\n", "# Assign salary depending upon the conditions\n", "if (g == 'm') and (yos >= 10) and (qual == 1):\n", " sal = 15000 #salary\n", "elif ((g == 'm' and yos >= 10 and qual == 0) or ( g == 'm' and yos < 10 and qual == 1 )):\n", " sal = 10000 #salary\n", "elif ( g == 'm' and yos < 10 and qual == 0 ):\n", " sal = 7000 #salary\n", "elif ( g == 'f' and yos >= 10 and qual == 1 ):\n", " sal = 12000 #salary\n", "elif ( g == 'f' and yos >= 10 and qual == 0 ):\n", " sal = 9000 #salary\n", "elif ( g == 'f' and yos < 10 and qual == 1 ):\n", " sal = 10000 #salary\n", "elif ( g == 'f' and yos < 10 and qual == 0 ):\n", " sal = 6000 #salary\n", "\n", "#Result\n", "print \"Salary of Employee = \", sal " ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter Gender, Years of Service and Qualifications ( 0 = G, 1 = PG ):\n", "f 8 1\n", "Salary of Employee = 10000\n" ] } ], "prompt_number": 13 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }