{
 "metadata": {
  "name": "",
  "signature": "sha256:ab47bb393809bc88589e7e92320813b90077813594dc417229af49780e24b53b"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h1>Chapter 7: Functions,Part One: The Fundamentals<h1>"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 7.1, Page Number: 129<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "def f1():\n",
      "    print \"Enter something: \"\n",
      "    str= \"Hello\"           #User-input\n",
      "    print str\n",
      "    \n",
      "#Variable decleration\n",
      "str=\"This is str in main\"\n",
      "\n",
      "print str\n",
      "f1()        #function call\n",
      "print str"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "This is str in main\n",
        "Enter something: \n",
        "Hello\n",
        "This is str in main\n"
       ]
      }
     ],
     "prompt_number": 1
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 7.2, Page Number: 130<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "#Variable decleration\n",
      "choice=0\n",
      "\n",
      "print \"(1) add numbers or (2) concatenate strings?: \"\n",
      "choice=2                           #User Input taken as 2\n",
      "\n",
      "if choice==1:\n",
      "    print \"Enter two numbers: \"\n",
      "    a=5            #Variable decleration; User Input\n",
      "    b=7\n",
      "    print a+b      #Result\n",
      "else :\n",
      "    print \"Enter two strings: \"\n",
      "    s1=\"Hello\"     #Variable decleration; User Input\n",
      "    s2=\"World\"\n",
      "    s1+=s2         #Concatenation\n",
      "    print s1       #Result"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "(1) add numbers or (2) concatenate strings?: \n",
        "Enter two strings: \n",
        "HelloWorld\n"
       ]
      }
     ],
     "prompt_number": 1
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 7.3, Page Number: 131<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "#Variable decleration\n",
      "i=10\n",
      "j=100\n",
      "\n",
      "if j>0:\n",
      "    i=None  \n",
      "    i= j/2\n",
      "    print \"inner i: \",i   #Result\n",
      "\n",
      "print \"outer i: \",i       #Result"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "inner i:  50\n",
        "outer i:  50\n"
       ]
      }
     ],
     "prompt_number": 2
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 7.4, Page Number: 132<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "\n",
      "a=5                      #Variable decleration; user input\n",
      "\n",
      "b=10                     #declaration of another variable; user input\n",
      "\n",
      "#Result\n",
      "print \"Product: \",a*b"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Product:  50\n"
       ]
      }
     ],
     "prompt_number": 4
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 7.5, Page Number: 134<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "\n",
      "import random\n",
      "\n",
      "#Variable decleration\n",
      "count=None\n",
      "num_right=0\n",
      "\n",
      "#Function for the drill\n",
      "def drill():\n",
      "    #Generate two numbers between 0 and 99.\n",
      "    a=random.randint(0,99)\n",
      "    b=random.randint(0,99)\n",
      "    #The user gets three tries to get it right.\n",
      "    for count in range(3):\n",
      "        print \"What is \",a,\" + \",b,\"? \"\n",
      "        ans = random.randint(0,200)            #user input\n",
      "        if ans==a+b:\n",
      "            print \"Right\"\n",
      "            num_right+=1\n",
      "    print \"You've used up all your tries.\"\n",
      "    print \"The answer is \",a+b\n",
      "    \n",
      "#Main function    \n",
      "print \"How many practice problems: \"\n",
      "count=2                              #User input taken as 2\n",
      "num_right=0\n",
      "while True:\n",
      "    drill()\n",
      "    count-=1\n",
      "    if(count==0):\n",
      "        break\n",
      "        \n",
      "#Result\n",
      "print \"You got \",num_right,\" right. \"  \n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "How many practice problems: \n",
        "What is  9  +  89 ? \n",
        "What is  9  +  89 ? \n",
        "What is  9  +  89 ? \n",
        "You've used up all your tries.\n",
        "The answer is  98\n",
        "What is  85  +  98 ? \n",
        "What is  85  +  98 ? \n",
        "What is  85  +  98 ? \n",
        "You've used up all your tries.\n",
        "The answer is  183\n",
        "You got  0  right. \n"
       ]
      }
     ],
     "prompt_number": 5
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 7.6, Page Number: 136<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "from ctypes import *\n",
      " \n",
      "def f(j):\n",
      "    j[0]=100          #j is assigned 100\n",
      "\n",
      "#Variable decleration\n",
      "i=c_int(1)\n",
      "p=pointer(i)\n",
      "\n",
      "#Calling the function\n",
      "f(p)      \n",
      "\n",
      "print i"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "c_long(100)\n"
       ]
      }
     ],
     "prompt_number": 5
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 7.7, Page Number: 137<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "from ctypes import *\n",
      "\n",
      " \n",
      "def f(j):\n",
      "    j[0]=100          #j is assigned 100\n",
      "\n",
      "#Variable decleration\n",
      "i=c_int(1)\n",
      "p=pointer(i)\n",
      "\n",
      "#Calling the function\n",
      "f(p)      \n",
      "\n",
      "print i"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "c_long(100)\n"
       ]
      }
     ],
     "prompt_number": 6
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 7.8, Page Number: 137<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      " \n",
      "def display(num):\n",
      "    for i in range(10):\n",
      "        print num[i],\n",
      "    \n",
      "#Variable declaration\n",
      "t=[]\n",
      "\n",
      "for i in range(10):\n",
      "    t.append(i)\n",
      "#Pass list to a function\n",
      "display(t)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "0 1 2 3 4 5 6 7 8 9\n"
       ]
      }
     ],
     "prompt_number": 7
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 7.9, Page Number: 138<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      " \n",
      "def display(num):\n",
      "    print num,\n",
      "\n",
      "#Variable declaration\n",
      "t=[]\n",
      "\n",
      "for i in range(10):\n",
      "    t.append(i)\n",
      "    \n",
      "#Printing without passing entire list\n",
      "for i in range(10):\n",
      "    display(t[i])"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "0 1 2 3 4 5 6 7 8 9\n"
       ]
      }
     ],
     "prompt_number": 8
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 7.10, Page Number: 139<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      " \n",
      "def cube(n,num):\n",
      "    num-=1\n",
      "    while num:\n",
      "        n[num]=n[num]*n[num]*n[num]\n",
      "        num-=1\n",
      "\n",
      "#Variable declaration\n",
      "nums=[]\n",
      "\n",
      "for i in range(10):\n",
      "    nums.append(i+1)\n",
      "    \n",
      "print \"Original contents: \",\n",
      "for i in range(10):\n",
      "    print nums[i],\n",
      "print\n",
      "\n",
      "cube(nums,10)           #Compute cubes\n",
      "\n",
      "#Result\n",
      "print \"Altered contents: \",\n",
      "for i in range(10):\n",
      "    print nums[i],"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Original contents:  1 2 3 4 5 6 7 8 9 10\n",
        "Altered contents:  1 8 27 64 125 216 343 512 729 1000\n"
       ]
      }
     ],
     "prompt_number": 10
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 7.11, Page Number: 140<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "import string\n",
      "\n",
      "def stringupper(str):\n",
      "    str=string.upper(str) #convert to uppercase\n",
      "    return str\n",
      "\n",
      "#Variable declaration    \n",
      "str=\"this is a test\"\n",
      "\n",
      "#Calling the function\n",
      "str=stringupper(str)\n",
      "\n",
      "#Result\n",
      "print str"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "THIS IS A TEST\n"
       ]
      }
     ],
     "prompt_number": 7
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 7.12, Page Number: 141<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      " \n",
      "def mystrlen(str):\n",
      "    l=len(str)\n",
      "    return l\n",
      "\n",
      "#Result\n",
      "print \"Length of Hello There is: \",\n",
      "print mystrlen(\"Hello There\")"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Length of Hello There is:  11\n"
       ]
      }
     ],
     "prompt_number": 11
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 7.13, Page Number: 142<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "import sys\n",
      "\n",
      "def main():\n",
      "    if len(sys.argv)!=2:\n",
      "        print \"You forgot to type your name.\"                                            #CHECK!!!!\n",
      "        return\n",
      "    #Result\n",
      "    print \"Hello \",sys.argv[1]\n",
      "\n",
      "main()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "You forgot to type your name.\n"
       ]
      }
     ],
     "prompt_number": 2
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 7.14, Page Number: 143<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import sys\n",
      "\n",
      "#Result\n",
      "for t in range(len(sys.argv)):\n",
      "    i=0\n",
      "    print sys.argv[t]  "
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "-c\n",
        "-f\n",
        "C:\\Users\\Anandi\\.ipython\\profile_default\\security\\kernel-6e851974-75ff-4911-bdf9-e089a03e5741.json\n",
        "--KernelApp.parent_appname='ipython-notebook'\n",
        "--interrupt=904\n",
        "--parent=876\n"
       ]
      }
     ],
     "prompt_number": 2
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 7.15, Page Number: 144<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "import sys\n",
      "\n",
      "if len(sys.argv)!=3:\n",
      "    print \"usage: add num num\"    \n",
      "else :\n",
      "    #Variable Decleration\n",
      "    a=sys.argv[1]\n",
      "    b=sys.argv[2]\n",
      "    #Result\n",
      "    print a+b\n",
      "    \n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "usage: add num num\n"
       ]
      }
     ],
     "prompt_number": 3
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 7.16, Page Number: 145<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "import string\n",
      "\n",
      "#Variable Decleration\n",
      "i=string.atoi(\"100\")\n",
      "j=string.atoi(\"100000\")\n",
      "k=string.atof(\"-0.123\")\n",
      "\n",
      "#Result\n",
      "print i,\" \",j,\" \",k"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "100   100000   -0.123\n"
       ]
      }
     ],
     "prompt_number": 13
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 7.17, Page Number: 147<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "#Variable Decleration\n",
      "i=abs(-10)\n",
      "\n",
      "#Result\n",
      "print abs(-23)\n",
      "abs(100)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "23\n"
       ]
      },
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 5,
       "text": [
        "100"
       ]
      }
     ],
     "prompt_number": 5
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 7.18, Page Number: 148<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      " \n",
      "def find_substr(sub,str):\n",
      "    l=str.find(sub)\n",
      "    return l\n",
      "\n",
      "#Variable decleration;Calling the function\n",
      "index=find_substr(\"three\",\"one two three four\")\n",
      "\n",
      "#Result\n",
      "print \"Index of three is \",index"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Index of three is  8\n"
       ]
      }
     ],
     "prompt_number": 6
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 7.19, Page Number: 149<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "\n",
      "import sys\n",
      "\n",
      "def print_vertical(str):\n",
      "    l=len(str)\n",
      "    for i in range(l):\n",
      "        print str[i],\n",
      "        \n",
      "print_vertical(sys.argv[1])\n",
      "    "
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "- f\n"
       ]
      }
     ],
     "prompt_number": 14
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 7.20, Page Number: 150<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      " \n",
      "def find_substr(sub,str):\n",
      "    return str.find(sub)\n",
      "\n",
      "#Variable declaration\n",
      "s=\"one two three four\"\n",
      "\n",
      "substr = find_substr(\"three\",s)\n",
      "\n",
      "#Result\n",
      "print \"substring found:\",s[substr:]\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "substring found: three four\n"
       ]
      }
     ],
     "prompt_number": 2
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 7.21, Page Number: 154<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      " \n",
      "def factr(n):\n",
      "    if n==1:\n",
      "        return 1\n",
      "    answer=factr(n-1)*n\n",
      "    return answer\n",
      "\n",
      "#Iterative version\n",
      "def fact(n):\n",
      "    answer=1\n",
      "    for t in range(n):\n",
      "        answer=answer*(t+1)\n",
      "    return answer\n",
      "\n",
      "#Using recursion version\n",
      "print \"4 factorial is \",factr(4)\n",
      "\n",
      "#Using iterative version\n",
      "print \"4 factorial is \",fact(4)\n",
      "\n",
      " "
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "4 factorial is  24\n",
        "4 factorial is  24\n"
       ]
      }
     ],
     "prompt_number": 3
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Example 7.22, Page Number: 155<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      " \n",
      "def reverse(s):\n",
      "     r = \"\"\n",
      "     for c in s:\n",
      "            r=c+r\n",
      "     print r\n",
      "     \n",
      "#VariabDecleration      \n",
      "str=\"this is a test\"\n",
      "\n",
      "#Calling function\n",
      "reverse(str)\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "tset a si siht\n"
       ]
      }
     ],
     "prompt_number": 9
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}