{ "metadata": { "name": "", "signature": "sha256:dc3f15d93cb383d5df54c3f7eecdad38d50217ab8d7454c3152fde2edbe2eca1" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Chapter2, Using C++ Operators" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "C8NEG, Page number:166" ] }, { "cell_type": "code", "collapsed": false, "input": [ "temp=-12\n", "\n", "#Result\n", "print -temp" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "12\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "C8DIV, Page number:167" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#To compute weekly pay\n", "#Get input\n", "#yearly=input(\"What is your annual pay?\")\n", "\n", "yearly=38000.00\n", "weekly=yearly/52\n", "\n", "#Result\n", "print \"\\n\\nYour weekly pay is \",float(weekly)\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "\n", "Your weekly pay is 730.769230769\n" ] } ], "prompt_number": 18 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "C8AVG1, Page number:172" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Compute the average of three grades\n", "\n", "grade1=87.5\n", "grade2=92.4\n", "grade3=79.6\n", "\n", "#Average calculation\n", "avg=grade1+grade2+grade3/3.0\n", "\n", "#Result\n", "print \"The average is: \",avg" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The average is: 206.433333333\n" ] } ], "prompt_number": 19 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "C8DATA, Page number:179" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Declaration\n", "bonus=50\n", "salary=1400.50\n", "\n", "#Calculation\n", "total=salary+bonus\n", "\n", "#Result\n", "print \"The total is \",\"%.2f\" %total" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The total is 1450.50\n" ] } ], "prompt_number": 16 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "C8INT1, Page number:181" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Calculate interest\n", "\n", "days=45\n", "principle=3500.00\n", "interest_rate=0.155\n", "\n", "#daily interest rate\n", "daily_interest=interest_rate/365 \n", "daily_interest=principle*daily_interest*days\n", "\n", "#Update principle\n", "principle+=daily_interest \n", "\n", "#Result\n", "print \"The balance you owe is:\",\"%.2f\" %principle" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The balance you owe is: 3566.88\n" ] } ], "prompt_number": 23 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "C9PAY1, Page number:193" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Calculate salesperson's pay based on his or her sales\n", "\n", "print \"\\n\\n\"\n", "print \"Payroll Calculation\\n\"\n", "print \"------------------------\\n\"\n", "\n", "#Get input\n", "sal_name=raw_input(\"What is salesperson's last name? \")\n", "hours=input(\"How many hours did the salesperson work? \")\n", "total_sales=input(\"What were the total sales? \")\n", "bonus=0\n", "\n", "#Compute base pay\n", "pay=4.10*float(hours) \n", "\n", "if total_sales>8500.00:\n", " bonus=500.00\n", "\n", "#Result\n", "print sal_name,\"made $\",\"%.2f\" %pay, \"\\n and got a bonus of $\",\"%.2f\" %bonus" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "\n", "\n", "Payroll Calculation\n", "\n", "------------------------\n", "\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "What is salesperson's last name? Harrison\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "How many hours did the salesperson work? 40\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "What were the total sales? 6050.64\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Harrison made $ 164.00 \n", " and got a bonus of $ 0.00\n" ] } ], "prompt_number": 29 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "C9AGE, Page number:195" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Get input\n", "age=input(\"What is the student's age?\")\n", "\n", "if age<10:\n", " print \"\\n*** The age cannot be less than 10 ***\\n\"\n", " print \"Try again...\\n\"\n", " age=input(\"What is the student's age?\")\n", "\n", "#Result\n", "print \"\\nThank you. You entered a valid age.\"" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "What is the student's age?3\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "*** The age cannot be less than 10 ***\n", "\n", "Try again...\n", "\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "What is the student's age?21\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Thank you. You entered a valid age.\n" ] } ], "prompt_number": 26 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "C9SQR1, Page number:197" ] }, { "cell_type": "code", "collapsed": false, "input": [ "##Print square of the input value if it is lessthan 180\n", "\n", "#Get input\n", "num=input(\"What number do you want to see the square of?\")\n", "\n", "if num<=180:\n", " square=num*num\n", " print \"The square of \",num,\"is \",square,\"\\n\"\n", " print \"\\nThank you for requesting square roots.\\n\"\n", " \n", "num=input(\"What number do you want to see the square of?\")\n", "if num>180:\n", " import os\n", " os.system('\\a')\n", " print \"\\n* Square is not allowed for numbers over 180 *\"\n", " print \"\\nRun this program again trying a smaller value.\"\n", "\n", "print \"\\nThank you for requesting square roots.\\n\"" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "What number do you want to see the square of?45\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "The square of 45 is 2025 \n", "\n", "\n", "Thank you for requesting square roots.\n", "\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "What number do you want to see the square of?212\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "* Square is not allowed for numbers over 180 *\n", "\n", "Run this program again trying a smaller value.\n", "\n", "Thank you for requesting square roots.\n", "\n" ] } ], "prompt_number": 32 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "C9IFEL1, Page number:200" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Get input\n", "#num=input(\"What is your answer?\\n\")\n", "num=1\n", "if num>0:\n", " print \"\\nMore than 0\\n\"\n", "else:\n", " print \"\\nLess or equal to 0\\n\"\n", "\n", "print \"\\nThanks for your time.\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "More than 0\n", "\n", "\n", "Thanks for your time.\n" ] } ], "prompt_number": 33 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "C9IFEL2, Page number:200" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Test user's first initial and prints a message.\n", "\n", "#Get input\n", "#last=raw_input(\"\\nWhat is your last name?\\n\")\n", "\n", "last=\"Praveen\"\n", "\n", "#test the initial\n", "if last[0] <= 'P':\n", " print \"Your name is early in the alphabet.\\n\"\n", "else:\n", " print \"You have to wait a while for Your name to be called\\n\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Your name is early in the alphabet.\n", "\n" ] } ], "prompt_number": 34 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "C9PAY2, Page number:201" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Get input\n", "#hours=input(\"\\nHow many hours were worked?\")\n", "#rate=input(\"\\nWhat is the regular hourly pay?\")\n", "\n", "hours=44\n", "rate=0.20\n", "\n", "#Compute pay\n", "if hours>50:\n", " dt=2.0*rate*float(hours-50)\n", " ht=1.5*rate*10.0\n", "else:\n", " dt=0.0\n", " \n", "#Time and a half\n", "if hours>40:\n", " ht=1.5*rate*float(hours-40)\n", "\n", "#Regular pay\n", "if hours>=40:\n", " rp=40*rate\n", "else:\n", " rp=float(hours)*rate\n", "\n", "#Payroll\n", "pay=dt+ht+rp \n", "\n", "#Result\n", "print \"\\nThe pay is \",\"%.2f\" %pay\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "The pay is 9.20\n" ] } ], "prompt_number": 36 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "C9SERV, Page number:202" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#if...else...if\n", "\n", "#yrs=input(\"\\nHow many years of service?\")\n", "yrs=25\n", "\n", "if yrs>20:\n", " print \"\\nGive a gold watch\"\n", "else:\n", " if yrs>10:\n", " print \"\\nGive a paper weight\"\n", " else:\n", " print \"\\nGive a pat on the back\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Give a gold watch\n" ] } ], "prompt_number": 37 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "C10YEAR, Page number:212" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#To determine if it is Summer Olympics year\n", "\n", "#year=input(\"\\nWhat is a year for the test?\")\n", "\n", "year=2004\n", "\n", "#Test the Year\n", "if year%4==0 and year%10==0:\n", " print \"\\nBoth Olympics and U.S. Census!\"\n", " exit(0)\n", " \n", "if year%4==0:\n", " print \"\\nSummer Olympics only\"\n", "else:\n", " if year%10==0:\n", " print \"\\nU.S. Census only\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Summer Olympics only\n" ] } ], "prompt_number": 41 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "C10AGE, Page number:213" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Get input\n", "#age=input(\"\\nWhat is your age?\\n\")\n", "age=20\n", "if age<10 or age>100:\n", " print \"*** The age must be between 10 and 100 ***\\n\"\n", "else:\n", " print \"You entered a valid age.\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "You entered a valid age.\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "C10VIDEO, Page number:214" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# -*- coding: cp1252 -*-\n", "#Program that computes video rental amounts and gives\n", "# appropriate discounts based on the day or customer status.\n", "\n", "print \"\\n *** Video Rental Computation ***\\n\"\n", "print \"--------------------------------------\\n\"\n", "\n", "tape_charge=2.00 #Before-discount tape fee-per tape.\n", "\n", "first_name=raw_input(\"\\nWhat is customer's first name? \")\n", "last_name=raw_input(\"\\nWhat is customer's last name? \")\n", "num_tapes=input(\"\\nHow many tapes are being rented? \")\n", "val_day=raw_input(\"Is this a Value day (Y/N)?\")\n", "sp_stat=raw_input(\"Is this a Special Status customer (Y/N)?\")\n", "\n", "# Calculate rental amount.\n", "\n", "discount=0.0\n", "if val_day=='Y' or sp_stat=='Y':\n", " discount=0.5\n", " x=num_tapes*tape_charge\n", " y=discount*num_tapes\n", " rental_amt=x-y\n", "\n", "\n", "print \"\\n** Rental club **\\n\"\n", "print first_name,last_name,\"rented \",num_tapes,\" tapes \"\n", "print \"The total was \",\"%.2f\" %rental_amt\n", "print \"The discount was \",\"%.2f\" %discount,\"per tape\\n\"\n", "\n", "if sp_stat=='Y':\n", " print \"\\nThank them for being a special Status customer\\n\"\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", " *** Video Rental Computation ***\n", "\n", "--------------------------------------\n", "\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "What is customer's first name? Jerry\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "What is customer's last name? Parker\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "How many tapes are being rented? 3\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Is this a Value day (Y/N)?Y\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Is this a Special Status customer (Y/N)?Y\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "** Rental club **\n", "\n", "Jerry Parker rented 3 tapes \n", "The total was 4.50\n", "The discount was 0.50 per tape\n", "\n", "\n", "Thank them for being a special Status customer\n", "\n" ] } ], "prompt_number": 5 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }