{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Chapter 6: Storage classes" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 6.1, Page number: 180" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Variable declaration\n", "n = int(raw_input('Enter any number '))\n", "\n", "# Function declaration, calculation and result\n", "def fact (n) :\n", " prod = 1\n", " while n :\n", " prod = prod * n\n", " n = n - 1\n", " return prod\n", "\n", "print ('Factorial of %d is %d' % (n, fact(n)))\n", "\n", "m = int(raw_input('Enter another number '))\n", "\n", "print ('Factorial of %d is %d' % (m, fact(m)))\n", "\n", "print ('Factorial of 6 is %d' % fact(6))" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter any number 5\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Factorial of 5 is 120\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter another number 3\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Factorial of 3 is 6\n", "Factorial of 6 is 720\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 6.2, Page number: 183" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Variable declaration\n", "a = int(raw_input('Enter first number '))\n", "b = int(raw_input('Enter second number '))\n", "\n", "# Function declaration, calculation and result\n", "def add (a, b) :\n", " global som\n", " som = a + b\n", "\n", "add (a, b)\n", "\n", "print ('The sum of %d + %d = %d' % (a, b, som))" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter first number 2\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter second number 3\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "The sum of 2 + 3 = 5\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 6.3, Page number: 187" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Variable declaration\n", "global y\n", "y = 90\n", "m = 10\n", "z = 2.18\n", "\n", "# Function declaration, calculation and result\n", "def fun1 (m, y, z) :\n", " z = 3.141\n", " print ('%f' % z)\n", "\n", "fun1 (m, y, z)\n", "\n", "print ('%d %d %f' % (m, y, z))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "3.141000\n", "10 90 2.180000\n" ] } ], "prompt_number": 3 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 6.4, Page number: 190" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Variable declaration\n", "global x, y, z, ch, tmp\n", "z = 2.18\n", "y = 90\n", "ch = '#'\n", "m = 10\n", "x = '*'\n", "tmp = '^'\n", "\n", "# Function declaration, calculation and result\n", "def fun1 (m, y, z) :\n", " z = 3.141\n", " num = 100\n", " tmp = '$'\n", " x = 50\n", " ch = '@'\n", " print ('In FUNCTION FORMAL PARAMETERS')\n", " print ('In the function value of m = %d address of m = %x' % (m, id(m)))\n", " print ('In the function value of y = %d address of y = %x' % (y, id(y)))\n", " print ('In the function value of z = %f address of z = %x' % (z, id(z)))\n", "\n", " print ('In FUNCTION LOCAL VARIABLES')\n", " print ('In the function value of z = %f address of z = %x' % (z, id(z)))\n", " print ('In the function value of num = %d address of num = %x' % (num, id(num)))\n", " print ('In the function value of tmp = %s address of tmp = %x' % (tmp, id(tmp)))\n", "\n", " print ('In FUNCTION GLOBAL VARIABLES')\n", " print ('In the function value of x = %s address of x = %x' % (x, id(x)))\n", " print ('In the function value of y = %d address of y = %x' % (y, id(y)))\n", " print ('In the function value of ch = %s address of ch = %x' % (ch, id(ch)))\n", "\n", "\n", "print ('In MAIN LOCAL VARIABLES')\n", "print ('In Main the value of m = %d address of m = %x' % (m, id(m)))\n", "print ('In Main the value of x = %s address of x = %s' % (x, id(x)))\n", "print ('In MAIN GLOBAL VARIABLES')\n", "print ('In Main the value of y = %d address of y = %x' % (y, id(y)))\n", "print ('In Main the value of z = %f address of z = %x' % (z, id(z)))\n", "print ('In Main the value of ch = %s address of ch = %s' % (ch, id(ch)))\n", "print ('In Main the value of tmp = %s address of tmp = %x' % (tmp, id(tmp)))\n", "\n", "fun1 (m, y, z)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "In MAIN LOCAL VARIABLES\n", "In Main the value of m = 10 address of m = 117d0c0\n", "In Main the value of x = * address of x = 140148786619376\n", "In MAIN GLOBAL VARIABLES\n", "In Main the value of y = 90 address of y = 117d8d0\n", "In Main the value of z = 2.180000 address of z = 16ca340\n", "In Main the value of ch = # address of ch = 140148787513184\n", "In Main the value of tmp = ^ address of tmp = 7f76eea46eb8\n", "In FUNCTION FORMAL PARAMETERS\n", "In the function value of m = 10 address of m = 117d0c0\n", "In the function value of y = 90 address of y = 117d8d0\n", "In the function value of z = 3.141000 address of z = 16ca2f8\n", "In FUNCTION LOCAL VARIABLES\n", "In the function value of z = 3.141000 address of z = 16ca2f8\n", "In the function value of num = 100 address of num = 117d7e0\n", "In the function value of tmp = $ address of tmp = 7f76eeb088a0\n", "In FUNCTION GLOBAL VARIABLES\n", "In the function value of x = 50 address of x = 117d4c8\n", "In the function value of y = 90 address of y = 117d8d0\n", "In the function value of ch = @ address of ch = 7f76ed6203f0\n" ] } ], "prompt_number": 4 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 6.5, Page number: 193" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Variable declaration\n", "a = int(raw_input('Enter first number '))\n", "b = int(raw_input('Enter second number '))\n", "\n", "# Function declaration, calculation and result\n", "def add (a, b) :\n", " global som\n", " som = a + b\n", "\n", "add (a, b)\n", "\n", "print ('The sum of %d + %d = %d' % (a, b, som))" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter first number 20\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter second number 30\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "The sum of 20 + 30 = 50\n" ] } ], "prompt_number": 5 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 6.6, Page number: 194" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Variable declaration\n", "a = int(raw_input('Enter first number '))\n", "b = int(raw_input('Enter second number '))\n", "\n", "# Function declaration, calculation and result\n", "def greater (a, b) :\n", " global big\n", " if a == b :\n", " big = 'equal'\n", " else :\n", " if a > b :\n", " big = 'first'\n", " else :\n", " big = 'second'\n", " return big\n", "\n", "greater (a, b)\n", "\n", "if big == 'equal' :\n", " print ('EQUAL')\n", "else :\n", " if big == 'first' :\n", " print ('First number is greater %d' % a)\n", " else :\n", " print ('Second number is greater %d' % b)" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter first number 20\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter second number 20\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "EQUAL\n" ] } ], "prompt_number": 6 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 6.7, Page number: 196" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Variable declaration\n", "a = int(raw_input('Enter first number '))\n", "b = int(raw_input('Enter second number '))\n", "c = int(raw_input('Enter third number '))\n", "\n", "# Function declaration, calculation and result\n", "def greater (a, b, c) :\n", " if a>b :\n", " if a>c :\n", " print ('The greatest number is %d ' % a)\n", " else :\n", " print ('The greatest number is %d ' % c)\n", "\n", " else :\n", " if b>c :\n", " print ('The greatest number is %d ' % b)\n", " else : \n", " print ('The greatest number is %d ' % c)\n", "\n", "greater (a, b, c)" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter first number 190\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter second number 128\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter third number 45\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "The greatest number is 190 \n" ] } ], "prompt_number": 7 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 6.8, Page number: 198" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Variable declaration\n", "n = int(raw_input('Enter any number '))\n", "\n", "# Function declaration, calculation and result\n", "def summate (n) :\n", " i = 1\n", " global total\n", " total = 0\n", " while i <= n :\n", " total = total + i\n", " i = i+1\n", " \n", "summate (n)\n", "\n", "print ('Sum of first %d natural numbers is %d' % (n, total))" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter any number 6\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Sum of first 6 natural numbers is 21\n" ] } ], "prompt_number": 8 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 6.9, Page number: 199" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Variable declaration\n", "n = int(raw_input('Enter any number '))\n", "\n", "# Function declaration, calculation and result\n", "def sumofdigit (n) :\n", " global sum\n", " sum = 0\n", " while n > 0 :\n", " sum = sum + n%10\n", " n = n/10\n", " return sum\n", "\n", "sumofdigit (n)\n", "\n", "print ('Sum of digits of %d is %d' % (n, sum))" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter any number 7612\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Sum of digits of 7612 is 16\n" ] } ], "prompt_number": 9 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 6.10, Page number: 201" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Variable declaration\n", "n = int(raw_input('Enter any number '))\n", "\n", "# Function declaration, calculation and result\n", "def checkprime (n) :\n", " flag = 1\n", " i = 2\n", " while i=10 :\n", " sum = 0\n", " while n>0 :\n", " sum = sum + n%10\n", " n = n/10\n", " n = sum\n", " return sum\n", "\n", "result = sumofdigit (n, sum)\n", "\n", "print ('Sum of digits of %d is %d' % (n, result))" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter any number 7612\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Sum of digits of 7612 is 7\n" ] } ], "prompt_number": 12 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 6.13, Page number: 207" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Variable declaration\n", "n = int(raw_input('Enter any number '))\n", "\n", "# Function declaration, calculation and result\n", "def prime (n) :\n", " for num in range (2, n) :\n", " flag = 1\n", " for i in range (2, num) :\n", " if num % i == 0 : \n", " flag = 0\n", " if flag :\n", " print ('%d ' % num),\n", "\n", "prime (n)" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter any number 20\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "2 3 5 7 11 13 17 19 \n" ] } ], "prompt_number": 14 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 6.14, Page number: 210" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Variable declaration\n", "m = int(raw_input('Enter any number '))\n", "\n", "# Function declaration, calculation and result\n", "def fun1 (m) :\n", " if not hasattr(fun1, \"temp\") : \n", " fun1.temp = 0\n", " print ('In function value of temp = %d' % fun1.temp)\n", " if fun1.temp < m :\n", " fun1.temp = m\n", " else :\n", " fun1.temp = fun1.temp + 1\n", "\n", "for i in range (1, 6) :\n", " fun1 (m)" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter any number 10\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "In function value of temp = 0\n", "In function value of temp = 10\n", "In function value of temp = 11\n", "In function value of temp = 12\n", "In function value of temp = 13\n" ] } ], "prompt_number": 15 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 6.15, Page number: 212" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Variable declaration\n", "m = 10\n", "i = 1\n", "\n", "# Function declaration, calculation and result\n", "def fun1 (m) :\n", " x = m\n", " if not hasattr(fun1, \"temp\") : \n", " fun1.temp = 0\n", "\n", " if fun1.temp == 0 :\n", " print ('\\nIn FUNCTION Global variable')\n", " print ('Value of m = %d address of m = %x' % (m, id(m)))\n", " print ('\\nIn FUNCTION Formal parameter variable')\n", " print ('Value of x = %d address of x = %x' % (x, id(x)))\n", " print ('\\nIn FUNCTION Static variable')\n", " print ('Value of temp = %d address of temp = %x' % (fun1.temp, id(fun1.temp)))\n", "\n", " if fun1.temp < m :\n", " fun1.temp = m\n", "\n", " else :\n", " print ('Value of temp = %d' % fun1.temp)\n", " fun1.temp = fun1.temp + 1\n", "\n", "print ('\\nIn MAIN Global variable')\n", "print ('Value of m = %d address of m = %x' % (m, id(m)))\n", "print ('\\nIn MAIN Local variable')\n", "print ('Value of i = %d address of i = %x' % (i, id(i)))\n", " \n", "for i in range (1, 6) :\n", " fun1 (m)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "In MAIN Global variable\n", "Value of m = 10 address of m = 117d0c0\n", "\n", "In MAIN Local variable\n", "Value of i = 1 address of i = 117d198\n", "\n", "In FUNCTION Global variable\n", "Value of m = 10 address of m = 117d0c0\n", "\n", "In FUNCTION Formal parameter variable\n", "Value of x = 10 address of x = 117d0c0\n", "\n", "In FUNCTION Static variable\n", "Value of temp = 0 address of temp = 117d1b0\n", "Value of temp = 10\n", "Value of temp = 11\n", "Value of temp = 12\n", "Value of temp = 13\n" ] } ], "prompt_number": 16 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 6.16, Page number: 215" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Variable declaration\n", "n = int(raw_input('Enter number of Fibonacci terms required '))\n", "\n", "# Function declaration, calculation and result\n", "def nextfibo () :\n", " if not hasattr(nextfibo, \"first\") : \n", " nextfibo.first = 0\n", " if not hasattr(nextfibo, \"second\") : \n", " nextfibo.second = 1\n", " if not hasattr(nextfibo, \"third\") : \n", " nextfibo.third = 0\n", " nextfibo.third = nextfibo.first + nextfibo.second\n", " nextfibo.first = nextfibo.second\n", " nextfibo.second = nextfibo.third\n", " return nextfibo.first\n", " \n", "print ('%d terms of Fibonacci series are' % n) \n", "print 0,\n", "for count in range (2, n+1) :\n", " result = nextfibo ()\n", " print ('%d' % result)," ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter number of Fibonacci terms required 10\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "10 terms of Fibonacci series are\n", "0 1 1 2 3 5 8 13 21 34\n" ] } ], "prompt_number": 17 } ], "metadata": {} } ] }