{ "metadata": { "name": "", "signature": "sha256:527aef20f7b40df168e6ff4a5ccb96d249aaf7c6743cc854ce1a221227ecfb75" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Chapter 5 - Making Decisions: if and switch Statements" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 5.1, page no. 100" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "a = 4\n", "b = 5\n", "print a, \" > \", b, \" is \", (a > b)\n", "print a, \" >= \", b, \" is \", (a >= b)\n", "print a, \" == \", b, \" is \", (a == b)\n", "print a, \" <= \", b, \" is \", (a <= b)\n", "print a, \" < \", b, \" is \", (a < b)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "4 > 5 is False\n", "4 >= 5 is False\n", "4 == 5 is False\n", "4 <= 5 is True\n", "4 < 5 is True\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 5.2, page no. 102" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "print \"Enter number of preregistered students: \",\n", "total = int(raw_input())\n", "print \"Enter number of students adding the course: \",\n", "added = int(raw_input())\n", "total = total + added\n", "print \"Total number of students: \", total" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter number of preregistered students: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "30\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Enter number of students adding the course: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "3\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Total number of students: 33\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 5.3, page no. 104" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "print \"Enter a whole number: \",\n", "num = int(raw_input())\n", "if (num % 2 == 0):\n", " print \"The number is even\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter a whole number: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "4\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " The number is even\n" ] } ], "prompt_number": 4 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 5.4, page no. 108" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "print \"Enter a whole number: \",\n", "num = int(raw_input())\n", "if (num % 2 == 0):\n", " print \"The number is even\"\n", "else:\n", " print \"The number is odd\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter a whole number: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "5\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " The number is odd\n" ] } ], "prompt_number": 5 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 5.4, page no. 109" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "print \"Enter a whole number: \",\n", "num = int(raw_input())\n", "print \"The number is\", (\"even\" if num %2 == 0 else \"odd\")" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter a whole number: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "6\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " The number is even\n" ] } ], "prompt_number": 6 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 5.6, page no. 112" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "print \"Enter your test score: \",\n", "testScore = int(raw_input())\n", "if (testScore >= 90 ):\n", " print \"Your grade is an A\"\n", "elif (testScore >= 80 ):\n", " print \"Your grade is a B\"\n", "elif (testScore >= 70 ):\n", " print \"Your grade is a C\"\n", "elif (testScore >= 60 ):\n", " print \"Your grade is a D\"\n", "else:\n", " print \"Your grade is an F\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter your test score: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "70\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Your grade is a C\n" ] } ], "prompt_number": 7 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 5.7, page no. 115" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "print \"Enter your grade: \",\n", "grade = raw_input()\n", "\n", "if grade == 'A':\n", " print \"Your average must be between 90 - 100\"\n", "elif grade == 'B':\n", " print \"Your average must be between 80 - 89\"\n", "elif grade == 'C':\n", " print \"Your average must be between 70 - 79\"\n", "elif grade == 'D':\n", " print \"Your average must be between 60 - 69\"\n", "else:\n", " print \"Your average must be below 60\" " ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter your grade: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "D\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Your average must be between 60 - 69\n" ] } ], "prompt_number": 9 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 5.8, page no. 118" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "\n", "print \"Enter your grade: \",\n", "grade = raw_input()\n", "\n", "if grade == 'A' or grade == 'a':\n", " print \"Your average must be between 90 - 100\"\n", "elif grade == 'B' or grade == 'b':\n", " print \"Your average must be between 80 - 89\"\n", "elif grade == 'C' or grade == 'c':\n", " print \"Your average must be between 70 - 79\"\n", "elif grade == 'D' or grade == 'd':\n", " print \"Your average must be between 60 - 69\"\n", "else:\n", " print \"Your average must be below 60\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter your grade: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "d\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Your average must be between 60 - 69\n" ] } ], "prompt_number": 10 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 5.9, page no. 119" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "\n", "print \"Choose your car\"\n", "print \"S for Standard\"\n", "print \"L for Leather Seats\"\n", "print \"D for Leather Seats + Chrome Wheels\"\n", "choice = raw_input()\n", "print \"Extra features purchased\"\n", "if choice == 'D':\n", " print \"Chrome wheels\\n\"\n", "elif choice == 'L':\n", " print \"Leather seats\"\n", "else:\n", " print \"None selected\"\n" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": "*" }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }