diff options
Diffstat (limited to 'Let_us_C/chapter-2.ipynb')
-rw-r--r-- | Let_us_C/chapter-2.ipynb | 1075 |
1 files changed, 522 insertions, 553 deletions
diff --git a/Let_us_C/chapter-2.ipynb b/Let_us_C/chapter-2.ipynb index e2379edc..b7409b44 100644 --- a/Let_us_C/chapter-2.ipynb +++ b/Let_us_C/chapter-2.ipynb @@ -1,554 +1,523 @@ -{
- "metadata": {
- "name": "chapter-2.ipynb"
- },
- "nbformat": 3,
- "nbformat_minor": 0,
- "worksheets": [
- {
- "cells": [
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "<h1>Chapter 2: The Decision Control Structure <h1>"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "<h3>If Demo , Page number: 52<h3>"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "\n",
- "#taking in input from the user\n",
- "#num = raw_input(\"Enter a number less than 10: \")\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": [
- "<h3>Example 2.1 , Page number: 53<h3>"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "\n",
- "#Variable declaration\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": [
- "<h3>Example 2.2, Page number: 57<h3>"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "If the number of years for which the employee has served the organization is greater than 3 then a bonus of Rs. 2500/- is given to the employee.\n",
- "\n",
- "#input from user\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": [
- "<h3>Example 2.3 , Page number: 58<h3>"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "If his basic salary is less than Rs. 1500, then HRA = 10% of basic salary and\n",
- "DA = 90% of basic salary. If his salary is either equal to or above Rs. 1500, then HRA = Rs. 500 and DA = 98% of basic salary.\n",
- "\n",
- "#input from user\n",
- "#bs = raw_input(\"Enter basic salary: \")\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": [
- "<h3>Nested If-else , Page number: 61<h3>"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "\n",
- "#input from user\n",
- "#i = raw_input(\"Enter either 1 or 2: \")\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": [
- "<h3>Example 2.4 (Method 1), Page number: 64<h3>"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "Percentage above or equal to 60 - First division\n",
- "Percentage between 50 and 59 - Second division\n",
- "Percentage between 40 and 49 - Third division\n",
- "Percentage less than 40 - Fail\n",
- "Write a program to calculate the division obtained by the student.\n",
- "\n",
- "#input from user\n",
- "#m1,m2,m3,m4,m5 = raw_input(\"Enter marks in five subjects: \").split() \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": [
- "<h3>Example 2.4 (Method 2), Page number: 65<h3>"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "\n",
- "#input from user\n",
- "#m1,m2,m3,m4,m5 = raw_input(\"Enter marks in five subjects: \").split() \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": [
- "<h3>Example 2.4 (Method 3), Page number: 67<h3>"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "\n",
- "#input from user\n",
- "#m1,m2,m3,m4,m5 = raw_input(\"Enter marks in five subjects: \").split() \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": [
- "<h3>Example 2.5 (Method 1) , Page number: 68<h3>"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "\u2212 If the driver is married.\n",
- "\u2212 If the driver is unmarried, male & above 30 years of age.\n",
- "\u2212 If the driver is unmarried, female & above 25 years of age.\n",
- "In all other cases the driver is not insured.\n",
- "\n",
- "#input from user\n",
- "#age,sex,ms = raw_input(\"Enter age, sex, marital status: \").split() # Age , sex and marital status of the driver\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": [
- "<h3>Example 2.5 (Method 2) , Page number: 69<h3>"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "\n",
- "#input from user\n",
- "#age,sex,ms = raw_input(\"Enter age, sex, marital status: \").split() # Age , sex and marital status of the driver\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": [
- "<h3>Example 2.6, Page number: 71<h3>"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "\n",
- "#Input gender( m/f), years of service and qualification from the user\n",
- "#g,yos,qual = raw_input(\"Enter Gender, Years of Service and Qualifications ( 0 = G, 1 = PG ):\").split()\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": {}
- }
- ]
+{ + "metadata": { + "name": "", + "signature": "sha256:2995e92c707106426a9edd38d59fc05e81394353c5d1739975459edb98accea9" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "<h1>Chapter 2: The Decision Control Structure <h1>" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "<h3>If Demo , Page number: 52<h3>" + ] + }, + { + "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": [ + "<h3>Example 2.1 , Page number: 53<h3>" + ] + }, + { + "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": [ + "<h3>Example 2.2, Page number: 57<h3>" + ] + }, + { + "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": [ + "<h3>Example 2.3 , Page number: 58<h3>" + ] + }, + { + "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": [ + "<h3>Nested If-else , Page number: 61<h3>" + ] + }, + { + "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": [ + "<h3>Example 2.4 (Method 1), Page number: 64<h3>" + ] + }, + { + "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": [ + "<h3>Example 2.4 (Method 2), Page number: 65<h3>" + ] + }, + { + "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": [ + "<h3>Example 2.4 (Method 3), Page number: 67<h3>" + ] + }, + { + "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": [ + "<h3>Example 2.5 (Method 1) , Page number: 68<h3>" + ] + }, + { + "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": [ + "<h3>Example 2.5 (Method 2) , Page number: 69<h3>" + ] + }, + { + "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": [ + "<h3>Example 2.6, Page number: 71<h3>" + ] + }, + { + "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": {} + } + ] }
\ No newline at end of file |