{
 "metadata": {
  "name": "",
  "signature": "sha256:cf6bdce268b89df5f08741df390466a4ba8e95dbdba72ee30279eb61d19897af"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "9 : DECISION MAKING,BREAKING BRANCHING AND LOOPING"
     ]
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "9.1.1,page number:156"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "print \"Type an Integer\"\n",
      "numb=int(input())\n",
      "if numb<=5:\n",
      "    print \"Good Choice!\"\n",
      "print \"Thank You!\""
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Type an Integer\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "3\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Good Choice!\n",
        "Thank You!\n"
       ]
      }
     ],
     "prompt_number": 1
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "9.2.1,page number:157"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "print \"Enter year : \"\n",
      "year= int(input())\n",
      "if year%4 == 0 and year%100 != 0 or year%400 == 0:\n",
      "    print year,\"is a leap year\"\n",
      "else:\n",
      "    print year,\"is not a leap year\""
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter year : \n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "1990\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "1990 is not a leap year\n"
       ]
      }
     ],
     "prompt_number": 5
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "9.3.1,page number:157"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "print \"Enter Year :\"\n",
      "year=int(input())\n",
      "if year%4 == 0 :\n",
      "    if year % 100 != 0:\n",
      "        print year,\"is leap year\"\n",
      "    else:\n",
      "        if year%400 == 0:\n",
      "            print year,\" is leap year\"\n",
      "else:\n",
      "    print year,\"is not a leap year\""
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter Year :\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "1990\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "1990 is not a leap year\n"
       ]
      }
     ],
     "prompt_number": 5
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "9.3.2,page number:158"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "print \"Enter the number\"\n",
      "numb =int(input())\n",
      "if numb<=100:\n",
      "    if numb>=30:\n",
      "        print numb,\"lies between 30-100\"\n",
      "    else:\n",
      "        print numb,\"is outside 30-100\""
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter the number\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "40\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "40 lies between 30-100\n"
       ]
      }
     ],
     "prompt_number": 6
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "9.4.1,page number : 158"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "print \"Enter choice of city(1-4)\"\n",
      "city = int(input())\n",
      "if city == 1:\n",
      "    print \"Madurai\"\n",
      "elif city == 2:\n",
      "    print \"Chennai\"\n",
      "elif city == 3:\n",
      "    print \"Mumbai\"\n",
      "elif city == 4:\n",
      "    print \"Calcutta\""
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter choice of city(1-4)\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "3\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Mumbai\n"
       ]
      }
     ],
     "prompt_number": 6
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "9.5.1,page number:159"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "numb=int(input(\"Enter the number\"))\n",
      "if numb == 0:\n",
      "    print \"prints case0\"\n",
      "elif numb == 1:\n",
      "    print \"prints case1\"\n",
      "elif numb == 2:\n",
      "    print \"prints case2\" \n",
      "else:\n",
      "    print"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter the number2\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "prints case2\n"
       ]
      }
     ],
     "prompt_number": 10
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "9.6.1,page number : 161"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import array\n",
      "i =20\n",
      "variable =array.array('i',[])\n",
      "size = 0\n",
      "\n",
      "def get_array_size():\n",
      "    print \"Array-size :\"\n",
      "    global size\n",
      "    size=int(input())\n",
      "    if size<1 or size>i :\n",
      "        get_array_size()\n",
      "    \n",
      "def input_fetch():\n",
      "    global size\n",
      "    global variabless\n",
      "    for k in range(0,size):\n",
      "        m=int(input())\n",
      "        variable.append(m)\n",
      "    return\n",
      "\n",
      "def sort_list():\n",
      "    global variable\n",
      "    variable= array.array('i',sorted(variable))\n",
      "    \n",
      "    return\n",
      "    \n",
      "def output():\n",
      "    for k in range(0,size):\n",
      "        print variable[k],\n",
      "    return\n",
      "\n",
      "def even(number):\n",
      "    if number/2 == 0:\n",
      "        return 1\n",
      "    else:\n",
      "        return 0\n",
      "\n",
      "get_array_size()\n",
      "input_fetch()\n",
      "sort_list()\n",
      "print \"The sorted list is :\"\n",
      "output()\n",
      "print \"\\nMedian : \",\n",
      "\n",
      "median=round((variable[(size/2)-1]+variable[size/2])/2,3) if even(size)  else float(variable[size/2])\n",
      "print median"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Array-size :\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "10\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "7\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "6\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "8\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "-4\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "0\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "5\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "13\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "17\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "25\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "6\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "The sorted list is :\n",
        "-4 0 5 6 6 7 8 13 17 25 \n",
        "Median :  7.0\n"
       ]
      }
     ],
     "prompt_number": 7
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "9.8.1,page number:163"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "index = 1\n",
      "while index<12:\n",
      "    print \"This is line \",index\n",
      "    index+=1\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "This is line  1\n",
        "This is line  2\n",
        "This is line  3\n",
        "This is line  4\n",
        "This is line  5\n",
        "This is line  6\n",
        "This is line  7\n",
        "This is line  8\n",
        "This is line  9\n",
        "This is line  10\n",
        "This is line  11\n"
       ]
      }
     ],
     "prompt_number": 8
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "9.9.1,page number:164"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "print \"Enter Integer\"\n",
      "total_sum=0\n",
      "numb=int(input())\n",
      "while numb>0:\n",
      "    digit=numb %10\n",
      "    total_sum+=digit\n",
      "    numb/=10\n",
      "print \"Sum is \",total_sum"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter Integer\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "1776\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Sum is  21\n"
       ]
      }
     ],
     "prompt_number": 9
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "9.9.2,page number:165"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "print \"Enter Integer:\"\n",
      "numb=int(input())\n",
      "print numb,\"is reversed as \",\n",
      "while numb>0:\n",
      "    digit=numb%10\n",
      "    print digit,\n",
      "    numb/=10\n",
      "print \"\\n\""
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter Integer:\n"
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "1234\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "1234 is reversed as  4 3 2 1 \n",
        "\n"
       ]
      }
     ],
     "prompt_number": 10
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "9.10.1,page number:166"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "for index in range(1,11):\n",
      "    print \" line \",index"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " line  1\n",
        " line  2\n",
        " line  3\n",
        " line  4\n",
        " line  5\n",
        " line  6\n",
        " line  7\n",
        " line  8\n",
        " line  9\n",
        " line  10\n"
       ]
      }
     ],
     "prompt_number": 2
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "9.10.2,page number:166"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "print \"Result\"\n",
      "print \"Index     Index Squared\"\n",
      "print \"_____     _____________\"\n",
      "for  index in range(1,11):\n",
      "    print index,\"         \",index*index"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Result\n",
        "Index     Index Squared\n",
        "_____     _____________\n",
        "1           1\n",
        "2           4\n",
        "3           9\n",
        "4           16\n",
        "5           25\n",
        "6           36\n",
        "7           49\n",
        "8           64\n",
        "9           81\n",
        "10           100\n"
       ]
      }
     ],
     "prompt_number": 3
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "9.10.3,page number:167"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "total_sum=0\n",
      "value=100\n",
      "for index in range(1,value+1):\n",
      "    total_sum+=index\n",
      "print \"Sum of 1 to \",value,\" = \",total_sum"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Sum of 1 to  100  =  5050\n"
       ]
      }
     ],
     "prompt_number": 4
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "9.11.1,page number:167"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def exceptive():\n",
      "    for index in range(1,90):\n",
      "        if index%5 == 0:\n",
      "            continue\n",
      "        print index\n",
      "exceptive()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "1\n",
        "2\n",
        "3\n",
        "4\n",
        "6\n",
        "7\n",
        "8\n",
        "9\n",
        "11\n",
        "12\n",
        "13\n",
        "14\n",
        "16\n",
        "17\n",
        "18\n",
        "19\n",
        "21\n",
        "22\n",
        "23\n",
        "24\n",
        "26\n",
        "27\n",
        "28\n",
        "29\n",
        "31\n",
        "32\n",
        "33\n",
        "34\n",
        "36\n",
        "37\n",
        "38\n",
        "39\n",
        "41\n",
        "42\n",
        "43\n",
        "44\n",
        "46\n",
        "47\n",
        "48\n",
        "49\n",
        "51\n",
        "52\n",
        "53\n",
        "54\n",
        "56\n",
        "57\n",
        "58\n",
        "59\n",
        "61\n",
        "62\n",
        "63\n",
        "64\n",
        "66\n",
        "67\n",
        "68\n",
        "69\n",
        "71\n",
        "72\n",
        "73\n",
        "74\n",
        "76\n",
        "77\n",
        "78\n",
        "79\n",
        "81\n",
        "82\n",
        "83\n",
        "84\n",
        "86\n",
        "87\n",
        "88\n",
        "89\n"
       ]
      }
     ],
     "prompt_number": 6
    }
   ],
   "metadata": {}
  }
 ]
}