{ "metadata": { "name": "", "signature": "sha256:f0e84b29896f17b0ff44f8bccf20cf041a408375d83b3bc2290848ec54364369" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Chapter 9 - Functions" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 9.1, page no. 179" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "def main(): #defining a function \n", " print \"Hello World!\"\n", "\n", "main()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Hello World!\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 9.2, page no. 180" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "def printMessage(): # defining function\n", " print \"Hello world!\"\n", "\n", "printMessage() # calling the function" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Hello world!\n" ] } ], "prompt_number": 4 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 9.3, page no. 181" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "printMessage();\n", "\n", "def printMessage():\n", " print \"Hello world!\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Hello world!\n" ] } ], "prompt_number": 5 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 9.4, page no. 182" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "def printMessage():\n", " print \"Hello world!\"\n", "\n", "printMessage()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Hello world!\n" ] } ], "prompt_number": 7 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 9.5, page no. 184" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "def printMessage():\n", " times = 0\n", " times += 1\n", " print \"This function called \", times, \" times\"\n", "\n", "\n", "\n", "choice = '1'\n", "while(choice != 'Q'):\n", " print \"Enter Q to quit, any other character to continue: \",\n", " choice = raw_input()\n", " if (choice == 'Q'):\n", " print \"Input stopped\"\n", " else:\n", " printMessage()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ " Enter Q to quit, any other character to continue: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "e\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " This function called 1 times\n", "Enter Q to quit, any other character to continue: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "e\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " This function called 1 times\n", "Enter Q to quit, any other character to continue: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "q\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " This function called 1 times\n", "Enter Q to quit, any other character to continue: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Q\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Input stopped\n" ] } ], "prompt_number": 4 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 9.6, page no. 185" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "def printMessage():\n", " times = 0\n", " times += 1\n", " print \"This function called \", times, \" times\"\n", "\n", "\n", "choice = '1'\n", "while(choice != 'Q'):\n", " print \"Enter Q to quit, any other character to continue: \",\n", " choice = raw_input()\n", " if (choice == 'Q'):\n", " print \"Input stopped\"\n", " else:\n", " printMessage()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ " Enter Q to quit, any other character to continue: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "w\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " This function called 1 times\n", "Enter Q to quit, any other character to continue: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "q\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " This function called 1 times\n", "Enter Q to quit, any other character to continue: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Q\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Input stopped\n" ] } ], "prompt_number": 9 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 9.7, page no. 186" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "times = 0\n", "def printMessage():\n", " global times\n", " times += 1\n", " print \"This function called \", times, \" times\"\n", "\n", "\n", "choice = '1'\n", "while(choice != 'Q'):\n", " print \"Enter Q to quit, any other character to continue: \",\n", " choice = raw_input()\n", " if (choice == 'Q'):\n", " print \"Input stopped\"\n", " else:\n", " printMessage()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter Q to quit, any other character to continue: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "w\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " This function called 1 times\n", "Enter Q to quit, any other character to continue: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "t\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " This function called 2 times\n", "Enter Q to quit, any other character to continue: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "u\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " This function called 3 times\n", "Enter Q to quit, any other character to continue: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Q\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Input stopped\n" ] } ], "prompt_number": 11 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 9.8, page no. 189" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "times = 0\n", "def printMessage():\n", " global times\n", " times += 1\n", " print \"This function called \", times, \" times\"\n", "\n", "\n", "choice = '1'\n", "while(choice != 'Q'):\n", " print \"Enter Q to quit, any other character to continue: \",\n", " choice = raw_input()\n", " if (choice == 'Q'):\n", " print \"Input stopped\"\n", " else:\n", " printMessage()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter Q to quit, any other character to continue: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "w\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " This function called 1 times\n", "Enter Q to quit, any other character to continue: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "r\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " This function called 2 times\n", "Enter Q to quit, any other character to continue: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "t\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " This function called 3 times\n", "Enter Q to quit, any other character to continue: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Q\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Input stopped\n" ] } ], "prompt_number": 12 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 9.9, page no. 190" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "def printMessage():\n", " print \"You inputted \", str\n", "\n", "print \"Enter a string: \",\n", "str = raw_input()\n", "printMessage()\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter a string: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Jeff\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " You inputted Jeff\n" ] } ], "prompt_number": 22 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 9.10, page no. 191" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "def printMessage(s):\n", " print \"You inputted \", s\n", "\n", "print \"Enter a string: \",\n", "str = raw_input()\n", "printMessage(str)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter a string: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Jeff\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " You inputted Jeff\n" ] } ], "prompt_number": 21 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 9.11, page no. 193" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "def printMessage(firstName, lastName):\n", " print \"Your name is \", firstName, \" \", lastName\n", "\n", "print \"Enter first name:\",\n", "name1 = raw_input()\n", "print \"Enter last name:\",\n", "name2 = raw_input()\n", "printMessage(name1, name2)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter first name:" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Jeff\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Enter last name:" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Kent\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Your name is Jeff Kent\n" ] } ], "prompt_number": 20 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 9.12, page no. 194" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "def printMessage(thename, theage):\n", " print \"Your name is \", thename, \" and your age is\", theage\n", "\n", "print \"Enter name:\",\n", "name = raw_input()\n", "print \"Enter age:\",\n", "age = int(raw_input())\n", "printMessage(name, age)\n", "printMessage(age, name)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter name:" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Jeff\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Enter age:" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "34\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Your name is Jeff and your age is 34\n", "Your name is 34 and your age is Jeff\n" ] } ], "prompt_number": 17 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 9.13, page no. 195" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "def doubleIt(x):\n", " print \"The number to be doubled is \", x\n", " x *= 2\n", " print \"The number doubled in doubleIt is \", x\n", "\n", "print \"Enter number: \",\n", "num = int(raw_input())\n", "doubleIt(num)\n", "print \"The number doubled outside the function is \", num," ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter number: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "34\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " The number to be doubled is 34\n", "The number doubled in doubleIt is 68\n", "The number doubled outside the function is 34\n" ] } ], "prompt_number": 18 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 9.14, page no. 196" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "li = [0]\n", "def doubleIt():\n", " print \"The number to be doubled is \", li[0]\n", " li[0] *= 2\n", " print \"The number doubled in doubleIt is \", li[0]\n", " \n", "print \"Enter number: \",\n", "num = int(raw_input())\n", "li[0] = num\n", "doubleIt()\n", "print \"The number doubled outside the function is \", li[0]" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter number: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "5\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " The number to be doubled is 5\n", "The number doubled in doubleIt is 10\n", "The number doubled outside the function is 10\n" ] } ], "prompt_number": 19 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 9.15, page no. 197" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "z = []\n", "def addNumbers (a, b):\n", " z.append(a + b)\n", "\n", "\n", "print \"Enter first number: \",\n", "firstNum = int(raw_input())\n", "print \"Enter second number: \",\n", "secondNum = int(raw_input())\n", "addNumbers(firstNum, secondNum)\n", "print firstNum, \" + \", secondNum, \" = \", z[0]" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter first number: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "3\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Enter second number: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "4\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " 3 + 4 = 7\n" ] } ], "prompt_number": 23 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 9.16, page no. 199" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "def addNumbers(x, y):\n", " return x + y\n", "\n", "print \"Enter first number: \",\n", "firstNum = int(raw_input())\n", "print \"Enter second number: \",\n", "secondNum = int(raw_input())\n", "sum = addNumbers(firstNum, secondNum)\n", "print firstNum, \" + \", secondNum, \" = \", sum\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter first number: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "4\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Enter second number: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "5\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " 4 + 5 = 9\n" ] } ], "prompt_number": 24 } ], "metadata": {} } ] }