{
 "metadata": {
  "name": "",
  "signature": "sha256:62d8fc451396635cdeab5f4f92b23401a31f7b72236a9f3ecb8a6d1a87f20612"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Chapter 2: First Steps in Programming"
     ]
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Program 2.1, page no. 27"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "\n",
      "print \"My salary is $10000\""
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "My salary is $10000\n"
       ]
      }
     ],
     "prompt_number": 1
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Program 2.2, page no. 29"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      " \n",
      "salary = 10000; #Declare and store 10000 in variable called salary\n",
      "print \"My salary is %d.\" %salary"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "My salary is 10000.\n"
       ]
      }
     ],
     "prompt_number": 2
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Program 2.3, page no. 31"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "\n",
      "brothers = 7 #declaring variable & storing value\n",
      "brides = 7 #declaring variable & storing value\n",
      " \n",
      "print \"%d brides for %d brothers\" %(brides, brothers)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "7 brides for 7 brothers\n"
       ]
      }
     ],
     "prompt_number": 3
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Program 2.4, page no. 34"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "\n",
      "cats = 2\n",
      "dogs = 1\n",
      "ponies = 1\n",
      "others = 46\n",
      " \n",
      "total_pets = cats + dogs + ponies + others;\n",
      " \n",
      "print \"We have %d pets in total\\n\" %total_pets"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "We have 50 pets in total\n",
        "\n"
       ]
      }
     ],
     "prompt_number": 4
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Program 2.5, page no. 37"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "\n",
      "cookies = 5\n",
      "cookie_calories = 125\n",
      "total_eaten = 0\n",
      "eaten = 2\n",
      "cookies = cookies - eaten\n",
      "total_eaten = total_eaten + eaten\n",
      "print \"I have eaten %d cookies. There are %d cookies left\" %(eaten, cookies)\n",
      " \n",
      "eaten = 3\n",
      "cookies = cookies - eaten\n",
      "total_eaten = total_eaten + eaten\n",
      "print \"I have eaten %d more. Now there are %d cookies left\\n\" %(eaten, cookies)\n",
      "print \"Total energy consumed is %d calories.\\n\" %(total_eaten*cookie_calories)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "I have eaten 2 cookies. There are 3 cookies left\n",
        "I have eaten 3 more. Now there are 0 cookies left\n",
        "\n",
        "Total energy consumed is 625 calories.\n",
        "\n"
       ]
      }
     ],
     "prompt_number": 5
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Program 2.6, page no. 39"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "\n",
      "cookies = 45\n",
      "children = 7\n",
      "cookies_per_child = 0\n",
      "cookies_left_over = 0\n",
      " \n",
      "cookies_per_child = cookies/children\n",
      "print \"You have %d children and %d cookies\" %(children, cookies)\n",
      "print \"Give each child %d cookies.\\n\" %cookies_per_child\n",
      "cookies_left_over = cookies%children\n",
      "print \"There are %d cookies left over.\\n\" %cookies_left_over"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "You have 7 children and 45 cookies\n",
        "Give each child 6 cookies.\n",
        "\n",
        "There are 3 cookies left over.\n",
        "\n"
       ]
      }
     ],
     "prompt_number": 6
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Program 2.7, page no. 48"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "\n",
      "plank_length = 10.0\n",
      "piece_count = 4.0\n",
      "piece_length = 0.0\n",
      " \n",
      "piece_length = plank_length/piece_count;\n",
      "print \"A plank %f feet long can be cut into %f pieces %f feet long.\\n\" %(plank_length, piece_count, piece_length)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "A plank 10.000000 feet long can be cut into 4.000000 pieces 2.500000 feet long.\n",
        "\n"
       ]
      }
     ],
     "prompt_number": 7
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Program 2.8, page no. 51"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "\n",
      "radius = 0.0\n",
      "diameter = 0.0\n",
      "circumference = 0.0\n",
      "area = 0.0\n",
      "Pi = 3.14159265\n",
      " \n",
      "print \"Input the diameter of the table:\",\n",
      "diameter = float(raw_input()) \n",
      "radius = diameter/2.0\n",
      "circumference = 2.0*Pi*radius\n",
      "area = Pi*radius*radius\n",
      " \n",
      "print \"\\nThe circumference is %0.2f\" %circumference\n",
      "print \"\\nThe area is %0.2f\\n\" %area"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Input the diameter of the table:"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "6\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " \n",
        "The circumference is 18.85\n",
        "\n",
        "The area is 28.27\n",
        "\n"
       ]
      }
     ],
     "prompt_number": 8
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Program 2.9, page no. 54"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "\n",
      "PI = 3.14159\n",
      " \n",
      "radius = 0.0\n",
      "diameter = 0.0\n",
      "circumference = 0.0\n",
      "area = 0.0\n",
      " \n",
      "print \"Input the diameter of a table:\", \n",
      "diameter = float(raw_input())\n",
      " \n",
      "radius = diameter/2.0\n",
      "circumference = 2.0*PI*radius\n",
      "area = PI*radius*radius;\n",
      " \n",
      "print \"\\nThe circumference is %.2f. \" %circumference\n",
      "print \"\\nThe area is %.2f.\\n\" %area"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Input the diameter of a table:"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "6\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " \n",
        "The circumference is 18.85. \n",
        "\n",
        "The area is 28.27.\n",
        "\n"
       ]
      }
     ],
     "prompt_number": 9
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Program 2.10, page no. 55"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "\n",
      "diameter = 0.0\n",
      "radius = 0.0\n",
      "Pi = 3.14159\n",
      " \n",
      "print \"Input the diameter of the table:\",\n",
      "diameter = float(raw_input())\n",
      "radius = diameter/2.0\n",
      " \n",
      "print \"\\nThe circumference is %.2f.\" %(2.0*Pi*radius)\n",
      "print \"\\nThe area is %.2f.\\n\" %(Pi*radius*radius)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Input the diameter of the table:"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "6\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " \n",
        "The circumference is 18.85.\n",
        "\n",
        "The area is 28.27.\n",
        "\n"
       ]
      }
     ],
     "prompt_number": 10
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "example 2.12, page no. 59"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "\n",
      "import sys \n",
      "\n",
      "print \"Size of Integer is: \" + str(sys.getsizeof(int()))\n",
      "print \"Size of Float is: \" + str(sys.getsizeof(float()))\n",
      "print \"Size of Long is: \" + str(sys.getsizeof(long()))\n",
      "print \"Size of String is: \" + str(sys.getsizeof(str()))\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Size of Integer is: 24\n",
        "Size of Float is: 24\n",
        "Size of Long is: 24\n",
        "Size of String is: 37\n"
       ]
      }
     ],
     "prompt_number": 11
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Program 2.13, page no. 60"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "\n",
      "Revenue_Per_150 = 4.5\n",
      "JanSold = 23500\n",
      "FebSold = 19300\n",
      "MarSold = 21600\n",
      "RevQuarter = 0.0\n",
      " \n",
      "QuarterSold = JanSold + FebSold + MarSold\n",
      " \n",
      "print \"Stock sold in\\n Jan: %d\\n Feb: %d\\n Mar: %d\\n\" %(JanSold, FebSold, MarSold)\n",
      "print \"Total stock sold in first quarter: %d\\n\" %QuarterSold\n",
      " \n",
      "RevQuarter = QuarterSold/150*Revenue_Per_150\n",
      "print \"Sales revenue this quarter is:$%.2f\\n\" %RevQuarter"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Stock sold in\n",
        " Jan: 23500\n",
        " Feb: 19300\n",
        " Mar: 21600\n",
        "\n",
        "Total stock sold in first quarter: 64400\n",
        "\n",
        "Sales revenue this quarter is:$1930.50\n",
        "\n"
       ]
      }
     ],
     "prompt_number": 12
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Program 2.15, page no. 68"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "\n",
      "first = 'T'\n",
      "second = 63\n",
      " \n",
      "print \"The first example as a letter looks like this - \", first\n",
      "print \"The first example as a number looks like this - \", ord(first)\n",
      "print \"The second example as a letter looks like this - \", chr(second)\n",
      "print \"The second example as a number looks like this - %d\\n\" %second"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "The first example as a letter looks like this -  T\n",
        "The first example as a number looks like this -  84\n",
        "The second example as a letter looks like this -  ?\n",
        "The second example as a number looks like this - 63\n",
        "\n"
       ]
      }
     ],
     "prompt_number": 13
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Program 2.16, page no. 69"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "\n",
      "first = 'A'\n",
      "second = 'B'\n",
      "last = 'Z'\n",
      "number = 40\n",
      "ex1 = ord(first) + 2\n",
      "ex2 = ord(second) - 1\n",
      "ex3 = ord(last) + 2\n",
      " \n",
      "print \"Character values \\t %-5c%-5c%-5c\\n\" %(chr(ex1), chr(ex2), chr(ex3))\n",
      "print \"Numerical equivalents\\t %-5d%-5d%-5d\\n\" %(ex1, ex2, ex3)\n",
      "print \"The number %d is the code for the character %c\" %(number, chr(number))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Character values \t C    A    \\    \n",
        "\n",
        "Numerical equivalents\t 67   65   92   \n",
        "\n",
        "The number 40 is the code for the character (\n"
       ]
      }
     ],
     "prompt_number": 14
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Program 2.17 (combines 2.18 also), page no. 80"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "\n",
      "shorty = 0.0\n",
      "lofty = 0.0\n",
      "feet = 0.0\n",
      "inches = 0.0\n",
      "shorty_to_lofty = 0.0\n",
      "lofty_to_tree = 0.0\n",
      "inches_per_foot = 12.0\n",
      " \n",
      "print \"Enter Lofty's height to the top of his/her head, in whole feet: \",\n",
      "feet = float(raw_input())\n",
      "print \"...and then inches: \",\n",
      "inches = float(raw_input())\n",
      "lofty = feet*inches_per_foot + inches\n",
      "\n",
      "print \"Enter Shorty's height up to his/her eyes, in whole feet: \",\n",
      "feet = float(raw_input())\n",
      "print \"... and then inches: \",\n",
      "inches = float(raw_input())\n",
      "shorty = feet*inches_per_foot + inches\n",
      " \n",
      "print \"Enter the distance between Shorty and Lofty, in whole feet: \",\n",
      "feet = float(raw_input())\n",
      "print \"... and then inches: \",\n",
      "inches = float(raw_input())\n",
      "shorty_to_lofty = feet*inches_per_foot + inches\n",
      " \n",
      "print \"Finally enter the distance from Lofty to the tree to the nearest foot: \",\n",
      "feet = float(raw_input())\n",
      "lofty_to_tree = feet*inches_per_foot\n",
      "\n",
      "\n",
      "tree_height = shorty + (shorty_to_lofty + lofty_to_tree)*(lofty-shorty)/shorty_to_lofty\n",
      " \n",
      "print \"The height of the tree is %ld feet and %ld inches.\\n\" %(tree_height/inches_per_foot, tree_height% inches_per_foot)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter Lofty's height to the top of his/her head, in whole feet: "
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "6\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " ...and then inches: "
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "2\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " Enter Shorty's height up to his/her eyes, in whole feet: "
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "4\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " ... and then inches: "
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "6\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " Enter the distance between Shorty and Lofty, in whole feet: "
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "5\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " ... and then inches: "
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "0\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " Finally enter the distance from Lofty to the tree to the nearest foot: "
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "20\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " The height of the tree is 12 feet and 10 inches.\n",
        "\n"
       ]
      }
     ],
     "prompt_number": 15
    }
   ],
   "metadata": {}
  }
 ]
}