{ "metadata": { "name": "", "signature": "sha256:985e0e6b7e8ae27035cca1419b7775a3f9a570b5ac018168350c0febbe31d7b4" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Chapter 4 - Arithmetic Operators" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 4.1, page no. 82" ] }, { "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": [ "35\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: 38\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 4.2, page no. 84" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "firstName = \"Jeff\"\n", "lastName = \"Kent\"\n", "print \"Your name is \", firstName + lastName" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Your name is JeffKent\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 4.3, page no. 84" ] }, { "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 \"How many students dropped? \",\n", "dropped = int(raw_input())\n", "total -= dropped\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": [ " How many students dropped? " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "5\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Total number of students: 28\n" ] } ], "prompt_number": 3 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "exampe 4.4, page no. 85" ] }, { "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 \"How many students dropped? \",\n", "dropped = int(raw_input())\n", "total -= dropped\n", "print \"Total number of students: \", total\n", "print \"Total tuition owed: $\",((total + dropped) * 72)" ], "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": [ " How many students dropped? " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "5\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Total number of students: 28\n", "Total tuition owed: $ 2376\n" ] } ], "prompt_number": 5 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 4.5, page no. 88" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "firstOp = 10\n", "secondOp = 4\n", "result = firstOp / secondOp\n", "print firstOp, \" / \", secondOp, \" = \", result" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "10 / 4 = 2\n" ] } ], "prompt_number": 6 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 4.6, page no. 88" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "firstOp = 10.0\n", "secondOp = 4.0\n", "result = firstOp / secondOp\n", "print firstOp, \" / \", secondOp, \" = \", result" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "10.0 / 4.0 = 2.5\n" ] } ], "prompt_number": 7 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 4.7, page no. 89" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "firstOp = 10\n", "secondOp = 4\n", "result = float(firstOp) / float(secondOp) #converting both the variables to float\n", "print firstOp, \" / \", secondOp, \" = \", result" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "10 / 4 = 2.5\n" ] } ], "prompt_number": 8 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 4.8, page no. 89" ] }, { "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 \"How many students dropped? \",\n", "dropped = int(raw_input())\n", "total -= dropped\n", "print \"Total number of students: \", total\n", "tuition = (total + dropped) * 72\n", "print \"Total tuition owed: $\",tuition\n", "print \"Average tuition per enrolled student: $\", (float(tuition)/float(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": [ " How many students dropped? " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "5\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Total number of students: 28\n", "Total tuition owed: $ 2376\n", "Average tuition per enrolled student: $ 84.8571428571\n" ] } ], "prompt_number": 9 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 4.9, page no. 91" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "print \"Enter radius of circle: \",\n", "radius = float(raw_input())\n", "area = 3.14159 * pow(radius, 2);\n", "print \"The area is \", area" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter radius of circle: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "6\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " The area is 113.09724\n" ] } ], "prompt_number": 11 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 4.10, page no. 92" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "print \"Enter number of pennies to make change for: \",\n", "total = int(raw_input())\n", "dollars = total / 100\n", "leftover = total % 100\n", "quarters = leftover / 25\n", "leftover %= 25\n", "dimes = leftover / 10\n", "leftover %= 10\n", "nickels = leftover / 5\n", "leftover %= 5\n", "print \"Dollars: \", dollars\n", "print \"Quarters: \", quarters\n", "print \"Dimes: \", dimes\n", "print \"Nickels: \", nickels\n", "print \"Pennies: \", leftover" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter number of pennies to make change for: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "387\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Dollars: 3\n", "Quarters: 3\n", "Dimes: 1\n", "Nickels: 0\n", "Pennies: 2\n" ] } ], "prompt_number": 12 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }