{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "

Chapter 10: Functions

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 10.1, Page number: 320

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#user defined function\n", "\n", "import sys\n", "\n", "#Variable Initialization\n", "x = 1\n", "y = 2\n", "\n", "#Function definition\n", "def add(a,b):\n", " return a+b\n", "\n", "#Function call\n", "z = add(x,y)\n", "\n", "#Result\n", "sys.stdout.write(\"z = %d\"%(z))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "z = 3" ] } ], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 10.2, Page number: 321

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Call user-defined function at different places.\n", "\n", "import sys\n", "\n", "#Function definitions\n", "def y():\n", " sys.stdout.write(\" Y\")\n", " return\n", "\n", "def a():\n", " sys.stdout.write(\" A\")\n", " y()\n", " return\n", "\n", "def b():\n", " sys.stdout.write(\" B\")\n", " a()\n", " return\n", "\n", "def c():\n", " a()\n", " b()\n", " sys.stdout.write(\" C\")\n", " return\n", "\n", "def d():\n", " sys.stdout.write(\" D\")\n", " c()\n", " b()\n", " a()\n", " return\n", "\n", "#Function calls\n", "y()\n", "a()\n", "b()\n", "c()\n", "d()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ " Y A Y B A Y A Y B A Y C D A Y B A Y C B A Y A Y" ] } ], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 10.3, Page number: 323

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Using similar variable names in different functions.\n", "\n", "import sys\n", "\n", "#Function definition\n", "def fun():\n", " b = 20\n", " c = 10\n", " sys.stdout.write(\"\\nIn fun() B = %d C = %d\"%(b,c))\n", " return\n", "\n", "#Variable Initialization\n", "b = 10\n", "c = 5\n", "\n", "#Result\n", "sys.stdout.write(\"\\nIn main() B = %d C = %d\"%(b,c))\n", "fun()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "In main() B = 10 C = 5\n", "In fun() B = 20 C = 10" ] } ], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 10.4, Page number: 323

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Global variables on different functions.\n", "\n", "import sys\n", "\n", "#Global Variable Initialization\n", "b = 10\n", "c = 5\n", "\n", "def fun():\n", " global b \n", " b += 1\n", " global c \n", " c -= 1\n", " sys.stdout.write(\"\\nIn fun() B = %d C = %d\"%(b,c))\n", " return\n", "\n", "#Result\n", "sys.stdout.write(\"\\nIn main() B = %d C = %d\"%(b,c))\n", "fun()\n", "b += 1\n", "c -= 1\n", "sys.stdout.write(\"\\nAgain In main() B = %d C = %d\"%(b,c))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "In main() B = 10 C = 5\n", "In fun() B = 11 C = 4\n", "Again In main() B = 12 C = 3" ] } ], "prompt_number": 6 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 10.5, Page number: 325

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Using return statement in different ways\n", "\n", "import sys\n", "\n", "#Function definition\n", "def pass1(a):\n", " if a == 0:\n", " return;\n", " else:\n", " return a*a*a\n", "\n", "#Variable Initialization\n", "x = int(raw_input(\"Enter value of x : \"))\n", "\n", "#Function call & Result\n", "if x != 1 or x > 0:\n", " y = pass1(x)\n", "\n", "#There is no switch statement in python, so if..else statement\n", "if y == 1:\n", " sys.stdout.write(\"The value returned is %d\"%(y))\n", "else:\n", " sys.stdout.write(\"The Cube of %d is : %d\"%(x,y))\n", " " ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter value of x : 5\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "The Cube of 5 is : 125" ] } ], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 10.6, Page number: 327

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Display message using user defined function\n", "\n", "import sys\n", "\n", "#Function definition\n", "def message():\n", " sys.stdout.write(\"Have a nice day\")\n", "\n", "#function call\n", "message()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Have a nice day" ] } ], "prompt_number": 4 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 10.7, Page number: 328

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Display Alphabets 'A','B' and 'C' using functions\n", "\n", "import sys\n", "\n", "#Functions definitions\n", "def a():\n", " sys.stdout.write(\"\\nA\")\n", " \n", "def b():\n", " sys.stdout.write(\" B\")\n", " \n", "def c():\n", " sys.stdout.write(\" C\")\n", " \n", "#Function call\n", "a()\n", "b()\n", "c()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "A B C" ] } ], "prompt_number": 5 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 10.8, Page number: 329

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Send value to user defined function and display results\n", "\n", "import sys\n", "\n", "#Function Definition\n", "def dat(x,y,z):\n", " sys.stdout.write(\"Date = %d/%d/%d\"%(x,y,z))\n", " \n", "#Variable Initialization\n", "d = int(raw_input(\"Enter date dd/mm/yy\"))\n", "m = int(raw_input(\"Enter date dd/mm/yy\"))\n", "y = int(raw_input(\"Enter date dd/mm/yy\"))\n", "\n", "#function call & Result\n", "dat(d,m,y)" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter date dd/mm/yy12\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter date dd/mm/yy12\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter date dd/mm/yy2001\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Date = 12/12/2001" ] } ], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 10.9, Page number: 330

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Square of number using user defined function\n", "\n", "import sys\n", "\n", "#function definition\n", "def sqr(k):\n", " sys.stdout.write(\"\\n%d\"%(k*k))\n", " \n", "#Variable Initialization\n", "j = 0\n", "\n", "#Function call & Result\n", "for j in range(1,5):\n", " sqr(j)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "1\n", "4\n", "9\n", "16" ] } ], "prompt_number": 7 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 10.10, Page number: 330

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Pass the value to main() function\n", "\n", "import sys\n", "\n", "#There is no main function in python\n", "sys.stdout.write(\"\\nNumber of command line arguments J = %d\"%(len(sys.argv)))\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Number of command line arguments J = 6" ] } ], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 10.11, Page number: 331

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Pass and return values to user defined function. \n", "\n", "import sys\n", "\n", "#Function Definitions\n", "def dat(x,y,z):\n", " sys.stdout.write(\"\\nToday = %d/%d/%d\"%(x,y,z))\n", " x += 1\n", " return x\n", "\n", "#Variable Initialization\n", "d = int(raw_input(\"Enter Date dd/mm/yy : \"))\n", "m = int(raw_input(\"Enter Date dd/mm/yy : \"))\n", "y = int(raw_input(\"Enter Date dd/mm/yy : \"))\n", "\n", "#Function call\n", "t = dat(d,m,y)\n", "\n", "#Result\n", "sys.stdout.write(\"\\nTomorrow = %d/%d/%d\"%(t,m,y))" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Date dd/mm/yy : 12\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Date dd/mm/yy : 12\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Date dd/mm/yy : 2001\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Today = 12/12/2001\n", "Tomorrow = 13/12/2001" ] } ], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 10.12, Page number: 332

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Send and recieve values to user defined function \n", "\n", "import sys\n", "\n", "#Function definition\n", "def sum1(x,y,z):\n", " return x+y+z\n", "\n", "#Variable Initialization\n", "a = int(raw_input(\"Enter Three Numbers : \"))\n", "b = int(raw_input(\"Enter Three Numbers : \"))\n", "c = int(raw_input(\"Enter Three Numbers : \"))\n", "\n", "#Function call\n", "s = sum1(a,b,c)\n", "\n", "#Result\n", "sys.stdout.write(\"Sum = %d\"%(s))" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Three Numbers : 7\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Three Numbers : 5\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Three Numbers : 4\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Sum = 16" ] } ], "prompt_number": 4 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 10.13, Page number: 333

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Recieve values from user defined function without passing any \n", "#value through main().\n", "\n", "import sys\n", "\n", "#Function definition\n", "def sum1():\n", " x = int(raw_input(\"Enter Three Numbers : \"))\n", " y = int(raw_input(\"Enter Three Numbers : \"))\n", " z = int(raw_input(\"Enter Three Numbers : \"))\n", " return x+y+z\n", "\n", "#Function call\n", "s = sum1()\n", "\n", "#Result\n", "sys.stdout.write(\"Sum = %d\"%(s))\n" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Three Numbers : 3\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Three Numbers : 5\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Three Numbers : 4\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Sum = 12" ] } ], "prompt_number": 5 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 10.14, Page number: 333

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Return value in the form of address.\n", "\n", "import sys\n", "\n", "#Function definition\n", "def sum1():\n", " x = int(raw_input(\"Enter Three Values : \"))\n", " y = int(raw_input(\"Enter Three Values : \"))\n", " z = int(raw_input(\"Enter Three Values : \"))\n", " k = x + y + z\n", " return k\n", "\n", "#There is no pointer concept in python\n", "\n", "#Function call\n", "s = sum1()\n", "\n", "#Result\n", "sys.stdout.write(\"Sum = %d\"%(s))\n" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Three Values : 3\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Three Values : 5\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Three Values : 4\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Sum = 12" ] } ], "prompt_number": 6 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 10.15, Page number: 334

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Call by value\n", "\n", "import sys\n", "\n", "#Function Definition\n", "def change(a,b):\n", " k = a\n", " a = b\n", " b = k\n", " sys.stdout.write(\"\\nIn Change() X = %d Y = %d\"%(a,b))\n", " \n", "#Variable Initialization\n", "x = int(raw_input(\"Enter Values of X & Y : \"))\n", "y = int(raw_input(\"Enter Values of X & Y : \"))\n", "\n", "#Function call\n", "change(x,y)\n", "\n", "#Result\n", "sys.stdout.write(\"\\nIn main() X = %d Y = %d\"%(x,y))" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Values of X & Y : 5\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Values of X & Y : 4\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "In Change() X = 4 Y = 5\n", "In main() X = 5 Y = 4" ] } ], "prompt_number": 7 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 10.16, Page number: 335

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Call by reference \n", "\n", "import sys\n", "\n", "#Function Definition\n", "def change(a,b):\n", " k = a\n", " a = b\n", " b = k\n", " sys.stdout.write(\"\\nIn Change() X = %d Y = %d\"%(a,b))\n", " \n", "#There is no pointer concept in python\n", "\n", "#Variable Initialization\n", "x = int(raw_input(\"Enter Values of X & Y : \"))\n", "y = int(raw_input(\"Enter Values of X & Y : \"))\n", "\n", "#Function call\n", "change(x,y)\n", "\n", "#Result\n", "sys.stdout.write(\"\\nIn main() X = %d Y = %d\"%(y,x))" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Values of X & Y : 5\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Values of X & Y : 4\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "In Change() X = 4 Y = 5\n", "In main() X = 4 Y = 5" ] } ], "prompt_number": 8 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 10.17, Page number: 336

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Return by reference\n", "\n", "import sys\n", "\n", "#Function Definition\n", "def change(a,b):\n", " c = a + b\n", " d = a - b\n", " return c,d\n", "\n", "#Variable Initialization\n", "x = int(raw_input(\"Enter Values of X & Y : \"))\n", "y = int(raw_input(\"Enter Values of X & Y : \"))\n", "\n", "#Function call\n", "add,sub = change(x,y)\n", "\n", "#Result\n", "sys.stdout.write(\"\\nAddition : %d\"%(add))\n", "sys.stdout.write(\"\\nSubtraction : %d\"%(sub))" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Values of X & Y : 5\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Values of X & Y : 4\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Addition : 9\n", "Subtraction : 1" ] } ], "prompt_number": 9 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 10.18, Page number: 337

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Call by value and reference\n", "\n", "k = 0\n", "m = 0\n", "\n", "#Function Definition\n", "def other(k,m):\n", " sys.stdout.write(\"\\nAddress of k & m in other() : %u %u\"%(id(k),id(m)))\n", " \n", "#Result\n", "sys.stdout.write(\"\\nAddress of k & m in main() : %u %u\"%(id(k),id(m)))\n", "\n", "#Function call\n", "other(k,m)\n", "\n", "#there is no pointer concept in python and it uses value tagged method in data storage\n", "#instead of addressing the memory location, values of same variables are tagged together" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Address of k & m in main() : 30922996 30922996\n", "Address of k & m in other() : 30922996 30922996" ] } ], "prompt_number": 15 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 10.19, Page number: 338

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#User defined function as an argument to another function.\n", "\n", "#Variable Initialization\n", "y = 2\n", "\n", "#Function Definitions\n", "def double(m):\n", " return m*2\n", "\n", "def square(k):\n", " return k*k\n", "\n", "#Function call\n", "x = double(square(y))\n", "\n", "#Result\n", "sys.stdout.write(\"x = %d\"%(x))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "x = 8" ] } ], "prompt_number": 17 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 10.20, Page number: 338

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Two functions as arguments for another functions\n", "\n", "import sys\n", "\n", "#Function definitions\n", "def x(a,b):\n", " return abs(a-b)\n", "\n", "def y():\n", " y = int(raw_input(\"Enter First Number : \"))\n", " return y\n", "\n", "def z():\n", " z = int(raw_input(\"Enter Second Number : \"))\n", " return z\n", "\n", "#Function call\n", "d = x(y(),z())\n", "\n", "#Result\n", "sys.stdout.write(\"\\nz() - y() = %d\"%(d))" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter First Number : 25\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Second Number : 50\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "z() - y() = 25" ] } ], "prompt_number": 10 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 10.21, Page number: 339

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Return only absolute value like abs() function\n", "\n", "import sys\n", "\n", "#Function Definition\n", "def uabs(y):\n", " if y < 0:\n", " return y * -1\n", " else:\n", " return y\n", " \n", "#Variable Initialization\n", "x = int(raw_input(\"Enter a Negative Value : \"))\n", "\n", "#Function call\n", "x = uabs(x)\n", "\n", "#Result\n", "sys.stdout.write(\"\\nX = %d\"%(x))" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter a Negative Value : -5\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "X = 5" ] } ], "prompt_number": 11 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 10.22, Page number: 340

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Square and cube of an entered number. \n", "\n", "import sys\n", "\n", "#Function Definitions\n", "def input1(): #Since input() is a built in function in python, input1() is used\n", " k = int(raw_input(\"Number : \"))\n", " return k\n", "\n", "def sqr(m):\n", " sys.stdout.write(\"\\nSquare : %d\"%(m*m))\n", " return m\n", "\n", "def cube(m):\n", " return m*m*m\n", "\n", "#Function call and Result\n", "sys.stdout.write(\"\\nCube : %d\"%(cube(sqr(input1()))))" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Number : 2\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Square : 4\n", "Cube : 8" ] } ], "prompt_number": 12 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 10.23, Page number: 341

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Assign return value of a function to variable.\n", "\n", "import sys\n", "\n", "#Function Definition\n", "def input1():\n", " k = int(raw_input(\"Enter Value of x = \"))\n", " return k\n", "\n", "#Function call\n", "x = input1()\n", "\n", "#Result\n", "sys.stdout.write(\"\\nx = %d\"%(x))" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Value of x = 5\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "x = 5" ] } ], "prompt_number": 13 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 10.24, Page number: 342

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Addition and subtraction of numbers with return value of function.\n", "\n", "import sys\n", "\n", "#Function Definition\n", "def input1():\n", " k = int(raw_input(\"Enter Value of x = \"))\n", " return k\n", "\n", "def sqr(m):\n", " return pow(m,2)\n", "\n", "#Function call\n", "x = sqr(1 - input1() + 1)\n", "\n", "#Result\n", "sys.stdout.write(\"\\nSquare = %d\"%(x))" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Value of x = 5\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Square = 9" ] } ], "prompt_number": 14 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 10.25, Page number: 343

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Multiplication and division of numbers with return value of function.\n", "\n", "import sys\n", "\n", "#Function Definition\n", "def input1():\n", " k = int(raw_input(\"Enter Value of x = \"))\n", " return k\n", "\n", "def sqr(m):\n", " return pow(m,2)\n", "\n", "#Function call\n", "x = sqr(5 * input1()/2)\n", "\n", "#Result\n", "sys.stdout.write(\"\\nSquare = %d\"%(x))" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Value of x = 5\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Square = 144" ] } ], "prompt_number": 15 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 10.26, Page number: 344

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# ++ operator with return value of function\n", "\n", "import sys\n", "\n", "#Function Definition\n", "def input1():\n", " k = int(raw_input(\"Enter Value of x = \"))\n", " return k\n", "\n", "def sqr(m):\n", " return pow(m,2)\n", "\n", "#Function call\n", "#There is no ++ operator in python. so += operator is used\n", "y = input1()\n", "y += 1\n", "x = sqr(y)\n", "\n", "#Result\n", "sys.stdout.write(\"\\nSquare = %d\"%(x))" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Value of x = 7\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Square = 64" ] } ], "prompt_number": 16 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 10.27, Page number: 345

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Use mod(%) with function\n", "\n", "import sys\n", "\n", "#Function Definition\n", "def j():\n", " x = int(raw_input(\"Enter a Number : \"))\n", " return x\n", " \n", "#Function call & Result\n", "if j() %2 == 0:\n", " sys.stdout.write(\"\\nNumber is Even.\")\n", "else:\n", " sys.stdout.write(\"\\nNumber is Odd.\")" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter a Number : 5\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Number is Odd." ] } ], "prompt_number": 17 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 10.28, Page number: 346

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Conditional operator(?) with function.\n", "\n", "import sys\n", "\n", "#Function Definitions\n", "def sqr(x):\n", " sys.stdout.write(\"Square \")\n", " return pow(x,2)\n", "\n", "def cube(x):\n", " sys.stdout.write(\"Cube \")\n", " return pow(x,3)\n", "\n", "def y():\n", " return 10\n", "\n", "#Variable Initialization\n", "x = int(raw_input(\"Enter a Number : \"))\n", "\n", "#Function call\n", "z = sqr(x) if x > y() else cube(x)\n", "\n", "#Result\n", "sys.stdout.write(\" = %d\"%(z))" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter a Number : 5\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Cube = 125" ] } ], "prompt_number": 18 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 10.29, Page number: 346

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#compare two return values of functions\n", "\n", "import sys\n", "\n", "#Function Definitions\n", "def a():\n", " x = int(raw_input(\"Enter a Number a() : \"))\n", " return x\n", "\n", "def b():\n", " x = int(raw_input(\"Enter a Number b() : \"))\n", " return x\n", "\n", "#Function call and Result\n", "if a() == b():\n", " sys.stdout.write(\"\\nValue of a() & b() are equal\")\n", "else:\n", " sys.stdout.write(\"\\nValue of a() & b() are unique\")" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter a Number a() : 5\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter a Number b() : 5\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Value of a() & b() are equal" ] } ], "prompt_number": 19 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 10.30, Page number: 347

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Evaluate the equation s = sqr(a() + b()) using function\n", "\n", "import sys\n", "\n", "#Function Definitions\n", "def a():\n", " a = int(raw_input(\"Enter value of a : \"))\n", " return a\n", "\n", "def b():\n", " b = int(raw_input(\"Enter value of b : \"))\n", " return b\n", "\n", "def sqr(x):\n", " return x*x\n", "\n", "#Function call\n", "s = sqr(a() + b())\n", "\n", "#Result\n", "sys.stdout.write(\"\\nSquare of Sum = %d\"%(s))" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter value of a : 5\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter value of b : 3\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Square of Sum = 64" ] } ], "prompt_number": 20 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 10.31, Page number: 348

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Evaluate the equation y = x^1+x^2..x^n using function\n", "\n", "import sys\n", "\n", "#Function Definition\n", "def b(m):\n", " m += 1\n", " #sys.stdout.write(\"%d\"%(m))\n", " return m\n", "\n", "#Variable Initialization\n", "x = int(raw_input(\"Values of 'x' and 'n' : \"))\n", "n = int(raw_input(\"Values of 'x' and 'n' : \"))\n", "y = 0\n", "z = 1\n", "m = 0\n", "\n", "while(z <= n):\n", " m = b(m)\n", " y = y + pow(x,m)\n", " sys.stdout.write(\"%d + \"%(y))\n", " z += 1\n", " \n", "if z >= n:\n", " sys.stdout.write(\"\\nValue of y = %d\"%(y))" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Values of 'x' and 'n' : 3\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Values of 'x' and 'n' : 3\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "3 + 12 + 39 + \n", "Value of y = 39" ] } ], "prompt_number": 21 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 10.32, Page number: 350

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Call user defined function through if statement\n", "\n", "import sys\n", "\n", "#Function Definition\n", "def a():\n", " a = int(raw_input(\"Enter value of a :\"))\n", " return a\n", "\n", "#Function call and Result\n", "if a()%2 == 0:\n", " sys.stdout.write(\"\\nThe number is even.\")\n", "else:\n", " sys.stdout.write(\"\\nThe number is odd.\")" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter value of a :5\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "The number is odd." ] } ], "prompt_number": 22 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 10.33, Page number: 351

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Call user defined function through switch() statement\n", "\n", "import sys\n", "\n", "#Function Definition\n", "def a():\n", " c = raw_input(\"Enter Your Choice Square(s), Cube(c), Double(d) : \")\n", " c = c.lower()\n", " return c\n", "\n", "#Variable Initialization\n", "x = 5\n", "\n", "#There is no switch() statement in python.\n", "c = a()\n", "if c == 's':\n", " sys.stdout.write(\"\\nSquare of %d is %d\"%(x,pow(x,2)))\n", "else:\n", " if c == 'c':\n", " sys.stdout.write(\"\\nCube of %d is %d\"%(x,pow(x,3)))\n", " else:\n", " if c == 'd':\n", " sys.stdout.write(\"\\nDouble of %d is %d\"%(x,x*2))\n", " else:\n", " sys.stdout.write(\"\\nUnexpected Choice printed as it is : %d\"%(x))\n", " " ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Your Choice Square(s), Cube(c), Double(d) : D\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Double of 5 is 10" ] } ], "prompt_number": 23 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 10.34, Page number: 353

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Call function through the for loop\n", "\n", "import sys\n", "\n", "#Function Definition\n", "def plus(k):\n", " if k == 10:\n", " return 0\n", " else:\n", " return k\n", " \n", "#Variable Initialization\n", "m = 1\n", "\n", "#Function call & Result\n", "#in python, for loop iterates through a range of number. so while loop is used instead.\n", "while plus(m) != 0:\n", " sys.stdout.write(\"%3d\"%(m))\n", " m += 1\n", " " ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ " 1 2 3 4 5 6 7 8 9" ] } ], "prompt_number": 14 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 10.35, Page number: 354

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Call user defined function through while() loop\n", "\n", "import sys\n", "\n", "#Function Definition\n", "def y():\n", " x = int(raw_input(\"Enter a Number : \"))\n", " return x\n", "\n", "#Function call & Result\n", "while y() != 0:\n", " sys.stdout.write(\"Value enter is non-zero\\n\")" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter a Number : 5\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Value enter is non-zero\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter a Number : 0\n" ] } ], "prompt_number": 25 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 10.36, Page number: 355

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Call user defined function through do while() loop\n", "\n", "import sys\n", "\n", "#Function Definition\n", "def y():\n", " x = int(raw_input(\"Enter a Number : \"))\n", " return x\n", "\n", "#Function call and Result\n", "#There is no do-while loop in python\n", "\n", "while y() != 0:\n", " sys.stdout.write(\"\\nValue entered is non-zero\\n\")" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter a Number : 5\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Value entered is non-zero\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter a Number : 0\n" ] } ], "prompt_number": 27 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 10.37, Page number: 356

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Initialize an array using functions.\n", "\n", "import sys\n", "\n", "#Function Definition\n", "def c(m):\n", " n = int(raw_input(\"Enter Number d[%d]\"%(m+1)))\n", " return n\n", "\n", "#Variable Initialization\n", "d = [c(i) for i in range(0,5)]\n", "\n", "sys.stdout.write(\"\\nArray d[] elements are : \")\n", "for k in range(0,5):\n", " sys.stdout.write(\"%2d\"%d[k])" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Number d[1]4\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Number d[2]5\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Number d[3]6\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Number d[4]7\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Number d[5]8\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Array d[] elements are : 4 5 6 7 8" ] } ], "prompt_number": 28 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 10.38, Page number: 357

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Pass array element to the function using call by value method.\n", "\n", "import sys\n", "\n", "#Function Definition\n", "def show(m,u):\n", " sys.stdout.write(\"\\nnum[%d] = %d\"%(m+1,u))\n", " \n", "#Variable initialization\n", "num = [12,13,14,15,16,17,18]\n", "\n", "#Function call & Result\n", "for k in range(0,7):\n", " show(k,num[k])" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "num[1] = 12\n", "num[2] = 13\n", "num[3] = 14\n", "num[4] = 15\n", "num[5] = 16\n", "num[6] = 17\n", "num[7] = 18" ] } ], "prompt_number": 20 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 10.39, Page number: 358

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Pass array element to the function using call by reference\n", "\n", "import sys\n", "\n", "#Function Definition\n", "def show(u):\n", " m = 0\n", " sys.stdout.write(\"\\nnum[7] = {\")\n", " while m != 7:\n", " #There is no pointer concept in python\n", " sys.stdout.write(\"%2d,\"%(u[m]))\n", " m += 1\n", " sys.stdout.write(\"\\b}\")\n", " \n", "#Variable Initialization\n", "num = [12,13,14,15,16,17,18]\n", "\n", "#Function call\n", "show(num)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "num[7] = {12,13,14,15,16,17,18,\b}" ] } ], "prompt_number": 22 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 10.40, Page number: 359

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Array elements in reverse order.\n", "\n", "import sys\n", "\n", "#Function Definition\n", "def show(u):\n", " m = 6\n", " while m != -1:\n", " sys.stdout.write(\"\\nnum[%d] = %d\"%(m,u[m]))\n", " m -= 1\n", " \n", "#Variable Initialization\n", "num = [12,13,14,15,16,17,18]\n", "\n", "#Function call\n", "#There is no pointer concept in python\n", "show(num)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "num[6] = 18\n", "num[5] = 17\n", "num[4] = 16\n", "num[3] = 15\n", "num[2] = 14\n", "num[1] = 13\n", "num[0] = 12" ] } ], "prompt_number": 23 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 10.41, Page number: 360

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Copy array elements using user defined function.\n", "\n", "import sys\n", "\n", "#Function Definition\n", "def cpy(p,m):\n", " j = 0\n", " while j != 5:\n", " m[j] = p[j]\n", " j += 1\n", " \n", "#Variable Initialization\n", "a1 = [1,2,3,4,5]\n", "a2 = [0 for i in range(0,5)]\n", "\n", "#Function call\n", "cpy(a1,a2)\n", "\n", "#Result\n", "sys.stdout.write(\"Source Target\")\n", "for h in range(0,5):\n", " sys.stdout.write(\"\\n%5d\\t%d\"%(a1[h],a2[h]))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Source Target\n", " 1\t1\n", " 2\t2\n", " 3\t3\n", " 4\t4\n", " 5\t5" ] } ], "prompt_number": 24 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 10.42, Page number: 361

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Read array of other function in main()\n", "\n", "import sys\n", "\n", "#Function definition\n", "def arry(k):\n", " b = [1,2,3,4,5]\n", " return b[k]\n", "\n", "#main() function\n", "for k in range(0,5):\n", " sys.stdout.write(\"\\t%d\"%(arry(k)))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\t1\t2\t3\t4\t5" ] } ], "prompt_number": 29 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 10.43, Page number: 361

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Interchange array elements of two arrays using function.\n", "\n", "import sys\n", "global a\n", "global b\n", "\n", "#Function Definitions\n", "def read():\n", " x = int(raw_input(\"\"))\n", " return x\n", "\n", "def change(a,b):\n", "#Since there is no pointer concept in python, exchange is done in the function using global variables.\n", " for x in range(0,5):\n", " a[x] = a[x] + b[x]\n", " b[x] = a[x] - b[x]\n", " a[x] = a[x] - b[x]\n", " \n", "#Variable Initialization\n", "a = [0 for i in range(0,5)]\n", "b = [0 for i in range(0,5)]\n", "\n", "for x in range(0,10):\n", " if x < 5:\n", " a[x] = read()\n", " else:\n", " b[x-5] = read()\n", "\n", "#Swapping and Result\n", "sys.stdout.write(\"\\nArray A & B \")\n", "\n", "for x in range(0,5):\n", " sys.stdout.write(\"\\n%7d%8d\"%(a[x],b[x]))\n", " \n", "#There is no pointer concept in python.\n", "change(a,b)\n", " \n", "sys.stdout.write(\"\\nNow A & B\")\n", "for x in range(0,5):\n", " sys.stdout.write(\"\\n%7d%8d\"%(a[x],b[x]))" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "1\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "2\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "3\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "4\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "5\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "6\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "7\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "8\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "9\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "0\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Array A & B \n", " 1 6\n", " 2 7\n", " 3 8\n", " 4 9\n", " 5 0\n", "Now A & B\n", " 6 1\n", " 7 2\n", " 8 3\n", " 9 4\n", " 0 5" ] } ], "prompt_number": 30 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 10.44, Page number: 363

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Read array elements declared in different functions using global\n", "#pointer declaration\n", "\n", "import sys\n", "\n", "#Function Definition\n", "def call(j):\n", " m = 0\n", " u = [5,1,6,0,6]\n", " q = u\n", " while m != j:\n", " sys.stdout.write(\"%3d\"%(u[m]))\n", " m += 1\n", " sys.stdout.write(\"\\n\")\n", " \n", "#Variable Initialization\n", "m = 0\n", "k = [3,8,5,2,5]\n", "q = k\n", "\n", "\n", "\n", "#Result\n", "while m != 5:\n", " sys.stdout.write(\"%3d\"%(q[m]))\n", " m += 1\n", "\n", "sys.stdout.write(\"\\n\")\n", "call(5)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ " 3 8 5 2 5\n", " 5 1 6 0 6\n" ] } ], "prompt_number": 30 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 10.45, Page number: 364

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Sum of 1 to 5 numbers using recursion\n", "\n", "import sys\n", "global s\n", "\n", "s = 0\n", "#Function definition\n", "def main(x,s):\n", " s = s + x\n", " sys.stdout.write(\"\\nx = %d s = %d\"%(x,s))\n", " if x == 5:\n", " return\n", " x += 1\n", " main(x,s)\n", " \n", "main(1,s)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "x = 1 s = 1\n", "x = 2 s = 3\n", "x = 3 s = 6\n", "x = 4 s = 10\n", "x = 5 s = 15" ] } ], "prompt_number": 7 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 10.46, Page number: 365

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Calculate triangular number of a given number with recursion function method\n", "\n", "import sys\n", "\n", "#Function definition\n", "def tri_num(m):\n", " f = 0\n", " if m == 0:\n", " return f\n", " else:\n", " f = f + m + tri_num(m-1)\n", " return f\n", "\n", "#Variable Initialization\n", "n = int(raw_input(\"Enter a Number : \"))\n", "\n", "#Function call\n", "t = tri_num(n)\n", "\n", "#Result\n", "sys.stdout.write(\"\\nTriangular number of %d is %d\"%(n,t))\n", " " ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter a Number : 5\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Triangular number of 5 is 15" ] } ], "prompt_number": 31 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 10.47, Page number: 366

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Display the given string using recursion\n", "\n", "import sys\n", "import os\n", "global x\n", "\n", "#Variable Initialization\n", "x = 0\n", "str1 = \"Have a Good Day\"\n", "\n", "#Function Definition\n", "def main(x):\n", " if x == len(str1): #There is no null terminating character in python string\n", " return\n", " else:\n", " if str1[x] == 'H':\n", " os.system('cls')\n", " sys.stdout.write(\"%c\"%(str1[x]))\n", " else:\n", " sys.stdout.write(\"%c\"%(str1[x]))\n", " x += 1\n", " main(x)\n", " \n", "#Function call\n", "main(x)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Have a Good Day" ] } ], "prompt_number": 47 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 10.48, Page number: 367

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Display the given string 10 times using recursion\n", "\n", "import sys\n", "import os\n", "\n", "#Function definition\n", "def main(x):\n", " sys.stdout.write(\"\\n%.2d] %s\"%(x,str1))\n", " x += 1\n", " if x == 11:\n", " return\n", " else:\n", " if x == 1:\n", " os.system('cls')\n", " main(x)\n", " else:\n", " main(x)\n", " \n", " \n", "#Variable Initialization\n", "x = 0\n", "str1 = \"Have a Good Day\"\n", "\n", "#Function call\n", "main(x)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "00] Have a Good Day\n", "01] Have a Good Day" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "02] Have a Good Day\n", "03] Have a Good Day\n", "04] Have a Good Day\n", "05] Have a Good Day\n", "06] Have a Good Day\n", "07] Have a Good Day\n", "08] Have a Good Day\n", "09] Have a Good Day\n", "10] Have a Good Day" ] } ], "prompt_number": 51 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 10.49, Page number: 368

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Factorial using recursive function.\n", "\n", "import sys\n", "\n", "#Function Definition\n", "def fact(m):\n", " f = 1\n", " if m == 1:\n", " return 1\n", " else:\n", " f = m * fact(m-1)\n", " return f\n", " \n", "#Variable Initialization\n", "x = int(raw_input(\"Enter a Number : \"))\n", "\n", "#Function call\n", "f = fact (x)\n", "\n", "#Result\n", "sys.stdout.write(\"\\nFactorial of %d is %d\"%(x,f))" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter a Number : 5\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Factorial of 5 is 120" ] } ], "prompt_number": 32 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 10.50, Page number: 369

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Display address of user defined function\n", "\n", "import sys\n", "\n", "#Function definition\n", "def show():\n", " sys.stdout.write(\"\\nAddress of function show() is : \")\n", " \n", "#Function call\n", "show()\n", "\n", "#Result\n", "sys.stdout.write(\"%u\"%(id(show)))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Address of function show() is : 95041520" ] } ], "prompt_number": 54 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 10.51, Page number: 369

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Call function using pointer\n", "\n", "import sys\n", "\n", "#Function definition\n", "def show():\n", " sys.stdout.write(\"\\nAddress of function show() is : \")\n", " \n", "#There is no pointer concept in python\n", "p = id(show)\n", "show()\n", "sys.stdout.write(\"%u\"%(id(show)))\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Address of function show() is : 95041200" ] } ], "prompt_number": 55 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 10.52, Page number: 370

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Display the address of library function.\n", "\n", "import sys\n", "import os\n", "\n", "#Result\n", "sys.stdout.write(\"\\nAddress of printf() is %u\"%(id(sys.stdout.write)))\n", "sys.stdout.write(\"\\nAddress of scanf() is %u\"%(id(sys.stdout.read)))\n", "sys.stdout.write(\"\\nAddress of clrscr() is %u\"%(id(os.system('cls'))))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Address of printf() is 60743848\n", "Address of scanf() is 60743848\n", "Address of clrscr() is 4774132" ] } ], "prompt_number": 57 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 10.53, Page number: 371

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Call main() using pointer to main() function\n", "\n", "import sys\n", "\n", "#Variable Initilization\n", "x = 0\n", "\n", "#Function definition\n", "def main(x):\n", " p = id(main)\n", " x += 1\n", " sys.stdout.write(\"\\nCall %d Address of main() %u\"%(x,id(main)))\n", " if x == 3:\n", " return\n", " main(x)\n", " \n", "#function call\n", "main(x)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Call 1 Address of main() 95040880\n", "Call 2 Address of main() 95040880\n", "Call 3 Address of main() 95040880" ] } ], "prompt_number": 59 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }