{ "metadata": { "name": "", "signature": "sha256:395f91f52f5165130592aef7f92afdbea52f690b93426a4caf3da2a8b3404bd6" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Chapter 3: Making Decisions" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 3.1, page no. 88" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "number = 0;\n", "print \"\\nEnter an integer between 1 and 10: \",\n", "number = int(raw_input())\n", " \n", "if(number > 5):\n", " print \"You entered %d which is greater than 5\\n\" % number\n", "if(number < 6):\n", " print \"You entered %d which is less than 6\\n\" % number" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter an integer between 1 and 10: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "6\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " You entered 6 which is greater than 5\n", "\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 3.2, page no. 91" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "unit_price = 3.50\n", "print \"Enter the number that you want to buy: \", \n", "quantity = int(raw_input())\n", "if(quantity > 10):\n", " total = quantity*unit_price*0.95 # 5% discount\n", " print \"The price for %d is $%.2f\\n\" % (quantity, total)\n", "else:\n", " total = quantity*unit_price #no discount\n", " print \"The price for %d is $%.2f\\n\" % (quantity, total)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter the number that you want to buy: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "20\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " The price for 20 is $66.50\n", "\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 3.3, page no. 95" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "test = 0.0\n", "print \"Enter an integer: \",\n", "test = int(raw_input())\n", " \n", "if(test % 2 == 0):\n", " print \"The number %ld is even\" %test\n", "if((test/2) % 2 == 0):\n", " print \"Half of %d is also even\" %test\n", " print \"That's interesting isn't it?\"\n", "else:\n", " print \"The number %d is odd\\n\" %test" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter an integer: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "20\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " The number 20 is even\n", "Half of 20 is also even\n", "That's interesting isn't it?\n" ] } ], "prompt_number": 3 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 3.4, page no. 98" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "print \"Enter an uppercase letter:\",\n", "letter = raw_input()\n", "if(ord(letter) >= ord('A')):\n", " if(ord(letter) <= ord('Z')):\n", " letter = chr( ord(letter) - ord('A') + ord('a'))\n", " print \"You entered an uppercase %c\" %letter\n", " else:\n", " print \"Try using the shift key! I want a capital letter.\\n\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter an uppercase letter:" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "G\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " You entered an uppercase g\n" ] } ], "prompt_number": 4 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 3.5, page no. 103" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "print \"Enter an uppercase letter:\",\n", "letter = raw_input()\n", "if( (ord(letter) >= ord('A')) and (ord(letter) <= ord('Z')) ):\n", " letter = chr( ord(letter) - ord('A') + ord('a'))\n", " print \"You entered an uppercase %c\" %letter\n", "else:\n", " print \"You did not enter an uppercase letter.\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter an uppercase letter:" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "j\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " You did not enter an uppercase letter.\n" ] } ], "prompt_number": 5 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 3.6, page no. 105" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "unit_price = 3.50\n", "discount1 = 0.05\n", "discount2 = 0.1\n", "discount3 = 0.15\n", "total_price = 0.0\n", " \n", "print \"Enter the number that you want to buy:\",\n", "quantity = int(raw_input())\n", " \n", "if(quantity >50):\n", " total_price = quantity*unit_price*(1.0-discount3)\n", "elif(quantity > 20):\n", " total_price = quantity*unit_price*(1.0-discount2)\n", "elif(quantity > 10):\n", " total_price = quantity*unit_price*(1.0-discount1)\n", "else:\n", " total_price = quantity*unit_price*(1.0-0.0)\n", "print \"The price for %d is $%.2f\\n\" %(quantity, total_price)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter the number that you want to buy:" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "60\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " The price for 60 is $178.50\n", "\n" ] } ], "prompt_number": 6 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 3.7, page no. 109" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "age = 0\n", "college = 0\n", "subject = 0\n", "interview = False # true for accept, false for reject\n", "\n", "print \"What college? 1 for Harvard, 2 for Yale, 3 for other: \",\n", "college = int(raw_input())\n", "print \"What subject? 1 for Chemistry, 2 for economics, 3 for other: \",\n", "subject = int(raw_input())\n", "print \"How old is the applicant? \",\n", "age = int(raw_input())\n", " \n", "if( (age>25 and subject==1) and (college==3 or college == 1) ):\n", " interview = True\n", "if(college == 2 and subject == 1):\n", " interview = True\n", "if(college == 1 and subject == 2 and not(age > 28)):\n", " interview = True\n", "if(college == 2 and (subject == 2 or subject == 3) and age > 25):\n", " interview = True\n", " \n", "if(interview):\n", " print \"Give 'em an interview\"\n", "else:\n", " print \"Reject 'em\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "What college? 1 for Harvard, 2 for Yale, 3 for other: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "2\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " What subject? 1 for Chemistry, 2 for economics, 3 for other: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "1\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " How old is the applicant? " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "24\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Give 'em an interview\n" ] } ], "prompt_number": 7 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 3.8, page no. 117" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "print \"Pick a number between 1 and 10 and you may win a prize! \",\n", "choice = int(raw_input())\n", "if((choice > 10) or (choice < 1)):\n", " choice = 11\n", "if choice == 7:\n", " print \"Congratulations!\"\n", " print \"You win the collected works of Amos Gruntfuttock.\"\n", "elif choice == 2:\n", " print \"You win the folding thermometer-pen-watch-umbrella.\"\n", "elif choice == 8:\n", " print \"You win the lifetime supply of aspirin tablets.\"\n", "elif choice == 11:\n", " print \"Try between 1 and 10. You wasted your guess.\",\n", "else:\n", " print \"Sorry, you lose.\\n\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Pick a number between 1 and 10 and you may win a prize! " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "7\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Congratulations!\n", "You win the collected works of Amos Gruntfuttock.\n" ] } ], "prompt_number": 9 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 3.9, page no. 119" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "print \"Enter Y or N: \",\n", "answer = raw_input()\n", "\n", "if answer =='y' or answer == 'Y':\n", " print \"You responded in the affirmative.\"\n", "elif answer == 'n' or answer == 'N':\n", " print \"You responded in the negative.\"\n", "else:\n", " print \"You did not respond correctly. . .\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter Y or N: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "y\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " You responded in the affirmative.\n" ] } ], "prompt_number": 10 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 3.10, page no. 128" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "original = 0xABC\n", "result = 0\n", "mask = 0xF\n", "\n", "print \"original = %X\" % original\n", "result |= original & mask\n", " \n", "original = original >> 4\n", "result = result << 4\n", "result |= original&mask\n", " \n", "original = original >> 4\n", "result = result << 4\n", "result |= original & mask\n", "print \"result = %X\" % result" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "original = ABC\n", "result = CBA\n" ] } ], "prompt_number": 11 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ " Program 3.11, page no. 132" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "print \"Enter the calculation\\n\",\n", "number1 = float(raw_input(\"Enter first number: \"))\n", "number2 = float(raw_input(\"Enter second number: \"))\n", "operation = raw_input(\"Enter operation '+' or '-' or '/' or '%' or '*': \")\n", " \n", "if operation == '+':\n", " print \"= %f\" %(number1 + number2)\n", "elif operation == '-':\n", " print \"= %f\" %(number1 - number2)\n", "elif operation == '*':\n", " print \"= %f\" %(number1 * number2);\n", "elif operation == '/':\n", " if(number2 == 0):\n", " print \"Division by zero error!\"\n", " else:\n", " print \"= %f\\n\" %(number1 / number2)\n", "elif operation == '%':\n", " if(number2 == 0):\n", " print \"Division by zero error!\"\n", " else:\n", " print \"= %f\\n\" %(number1 % number2)\n", "else:\n", " print \"Illegal operation!\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter the calculation\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter first number: 25\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter second number: 24\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter operation '+' or '-' or '/' or '%' or '*': *\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "= 600.000000\n" ] } ], "prompt_number": 13 } ], "metadata": {} } ] }