From 83c1bfceb1b681b4bb7253b47491be2d8b2014a1 Mon Sep 17 00:00:00 2001 From: debashisdeb Date: Fri, 20 Jun 2014 15:42:42 +0530 Subject: removing problem statements --- ANSI_C_Programming/chapter5.ipynb | 1939 ++++++++++++++++++------------------- 1 file changed, 969 insertions(+), 970 deletions(-) (limited to 'ANSI_C_Programming/chapter5.ipynb') diff --git a/ANSI_C_Programming/chapter5.ipynb b/ANSI_C_Programming/chapter5.ipynb index aadabff0..6e2f2667 100644 --- a/ANSI_C_Programming/chapter5.ipynb +++ b/ANSI_C_Programming/chapter5.ipynb @@ -1,971 +1,970 @@ -{ - "metadata": { - "name": "chapter5.ipynb" - }, - "nbformat": 3, - "nbformat_minor": 0, - "worksheets": [ - { - "cells": [ - { - "cell_type": "heading", - "level": 1, - "metadata": {}, - "source": [ - "CHAPTER 5: FUNCTIONS AND POINTERS" - ] - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "EXAMPLE ON PAGE:144" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "#Defining & calling a function\n", - "def message(): #function definition\n", - " print \"Smile,and the world smiles with you...\\n\"\n", - "message() #function call\n", - "print \"Cry, and you stop the monotony!\\n\"" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Smile,and the world smiles with you...\n", - "\n", - "Cry, and you stop the monotony!\n", - "\n" - ] - } - ], - "prompt_number": 1 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "EXAMPLE ON PAGE:146" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "#Calling of multiple functions\n", - "def italy():\n", - " print \"I am in italy\\n\"\n", - "def brazil():\n", - " print \"I am in brazil\\n\"\n", - "def argentina():\n", - " print \"I am in argentina\\n\"\n", - "print \"I am in main\\n\"\n", - "italy(); #italy() will be called\n", - "brazil(); #brazil() will be called\n", - "argentina(); #argentina() will be called" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "I am in main\n", - "\n", - "I am in italy\n", - "\n", - "I am in brazil\n", - "\n", - "I am in argentina\n", - "\n" - ] - } - ], - "prompt_number": 2 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "EXAMPLE ON PAGE:147" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "#Calling of function inside another function\n", - "def italy():\n", - " print \"I am in italy\\n\"\n", - " brazil(); #it will call brazil()\n", - " print \"I am back in italy\\n\"\n", - "def brazil():\n", - " print \"I am in brazil\\n\"\n", - " argentina(); #it will call argentina()\n", - "def argentina():\n", - " print \"I am in argentina\\n\"\n", - "print \"I am in main\\n\"\n", - "italy(); #italy() is called\n", - "print \"I am finally back in main\\n\"" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "I am in main\n", - "\n", - "I am in italy\n", - "\n", - "I am in brazil\n", - "\n", - "I am in argentina\n", - "\n", - "I am back in italy\n", - "\n", - "I am finally back in main\n", - "\n" - ] - } - ], - "prompt_number": 3 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "EXAMPLE ON PAGE:148" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "#Calling of main from another function\n", - "def message():\n", - " print \"Can't imagine life without C\\n\"\n", - " main() #it will call back main()\n", - "def main():\n", - " message()\n", - "main()" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "EXAMPLE ON PAGE:149" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "#Calling a function multiple number of times\n", - "def message():\n", - " print \"Jewel Thief!!\\n\"\n", - "message() #1st call\n", - "message() #2nd call" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Jewel Thief!!\n", - "\n", - "Jewel Thief!!\n", - "\n" - ] - } - ], - "prompt_number": 4 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "EXAMPLE ON PAGE:149" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "#Function calls doesn't depend on the order of functions definition\n", - "def message2():\n", - " print \"But the butter was bitter\\n\"\n", - "def message1():\n", - " print \"Mary bought some butter\\n\"\n", - "message1()\n", - "message2()" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Mary bought some butter\n", - "\n", - "But the butter was bitter\n", - "\n" - ] - } - ], - "prompt_number": 5 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "EXAMPLE ON PAGE:151-152" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "#Sending and receiving values between functions\n", - "def calsum(x,y,z): #parameterized function\n", - " d=x+y+z\n", - " return d\n", - "print \"Enter any three numbers\"\n", - "a=eval(raw_input())\n", - "b=eval(raw_input())\n", - "c=eval(raw_input())\n", - "sum=calsum(a,b,c) #passing values as arguments\n", - "print \"Sum=%d\\n\" % (sum)" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter any three numbers\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "10\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "20\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "30\n" - ] - }, - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Sum=60\n", - "\n" - ] - } - ], - "prompt_number": 6 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "EXAMPLE ON PAGE:153" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "#A function can have multiple return statements\n", - "def fun():\n", - " print \"Enter any number\"\n", - " n=eval(raw_input())\n", - " if (n>=10 and n<=90):\n", - " return n\n", - " else:\n", - " return n+32\n", - "fun()" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter any number\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "100\n" - ] - }, - { - "metadata": {}, - "output_type": "pyout", - "prompt_number": 7, - "text": [ - "132" - ] - } - ], - "prompt_number": 7 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "EXAMPLE ON PAGE:154" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "#Changing formal argument in function doesn't affect the actual argument\n", - "def fun(b):\n", - " b=60\n", - " print \"%d\\n\" % (b) #prints 60\n", - "a=30\n", - "fun(a) #prints 30\n", - "print \"%d\\n\" % (a)" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "60\n", - "\n", - "30\n", - "\n" - ] - } - ], - "prompt_number": 8 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "EXAMPLE ON PAGE:155" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "#Program explaining scope of a variable\n", - "def display(j):\n", - " k=35\n", - " print \"%d\\n\" % (j) #we can't print i directly here because scope of variable is local by default\n", - " print \"%d\\n\" % (k)\n", - "i=20\n", - "display(i)" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "20\n", - "\n", - "35\n", - "\n" - ] - } - ], - "prompt_number": 9 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "EXAMPLE ON PAGE:158-159" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "#Square of a floating point number using a function\n", - "def square(x):\n", - " y=x*x\n", - " return y\n", - "print \"Enter any number\"\n", - "a=eval(raw_input())\n", - "b=square(a)\n", - "print \"Square of %f is %f\\n\" % (a,b)" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter any number\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "2.5\n" - ] - }, - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Square of 2.500000 is 6.250000\n", - "\n" - ] - } - ], - "prompt_number": 10 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "EXAMPLE ON PAGE:161" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "#Printing address location of a variable\n", - "#Note:There is no concept of pointer in python thats why i am only writing it to get the similar output\n", - "i=3\n", - "print \"Address of i=%u\\n\" % (id(i)) #id() will return the loaction of a variable\n", - "print \"Value of i=%d\\n\" % (i)" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Address of i=20688560\n", - "\n", - "Value of i=3\n", - "\n" - ] - } - ], - "prompt_number": 12 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "EXAMPLE ON PAGE:161" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "#Program to print address & value\n", - "i=3\n", - "print \"Address of i=%u\\n\" % (id(i))\n", - "print \"Value of i=%d\\n\" % (i)\n", - "print \"Value of i=%d\\n\" % (i)" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Address of i=20688560\n", - "\n", - "Value of i=3\n", - "\n", - "Value of i=3\n", - "\n" - ] - } - ], - "prompt_number": 13 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "EXAMPLE ON PAGE:162" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "#Assigning address to other variable and printing them\n", - "i=3\n", - "j=id(i)\n", - "print \"Address of i=%u\\n\" % (id(i)) #print address of i\n", - "print \"Address of i=%u\\n\" % (j) #print address of i\n", - "print \"Address of j=%u\\n\" % (id(j)) #print address of j\n", - "print \"Value of j=%u\\n\" % (j) #print value of j\n", - "print \"Value of i=%d\\n\" % (i)\n", - "print \"Value of i=%d\\n\" % (i)\n", - "print \"Value of i=%d\\n\" % (i)" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Address of i=20688560\n", - "\n", - "Address of i=20688560\n", - "\n", - "Address of j=88986780\n", - "\n", - "Value of j=20688560\n", - "\n", - "Value of i=3\n", - "\n", - "Value of i=3\n", - "\n", - "Value of i=3\n", - "\n" - ] - } - ], - "prompt_number": 14 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "EXAMPLE ON PAGE:164" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "#Variable containg address of another variable\n", - "i=3\n", - "j=id(i)\n", - "k=id(j)\n", - "print \"Address of i=%u\\n\" % (id(i)) #print address of i\n", - "print \"Address of i=%u\\n\" % (j) #print address of i\n", - "print \"Address of i=%u\\n\" % (j) #print address of i\n", - "print \"Address of j=%u\\n\" % (id(j)) #print address of i\n", - "print \"Address of j=%u\\n\" % (k) #print address of j\n", - "print \"Address of k=%u\\n\" % (id(k)) #print address of k\n", - "print \"Value of j=%u\\n\" % (j) #print value of j\n", - "print \"Value of k=%u\\n\" % (k) #print value of k\n", - "print \"Value of i=%u\\n\" % (i) #print value of i\n", - "print \"Value of i=%u\\n\" % (i)\n", - "print \"Value of i=%u\\n\" % (i)\n", - "print \"Value of i=%u\\n\" % (i)" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Address of i=20688560\n", - "\n", - "Address of i=20688560\n", - "\n", - "Address of i=20688560\n", - "\n", - "Address of j=88986672\n", - "\n", - "Address of j=88986672\n", - "\n", - "Address of k=88986708\n", - "\n", - "Value of j=20688560\n", - "\n", - "Value of k=88986672\n", - "\n", - "Value of i=3\n", - "\n", - "Value of i=3\n", - "\n", - "Value of i=3\n", - "\n", - "Value of i=3\n", - "\n" - ] - } - ], - "prompt_number": 15 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "EXAMPLE ON PAGE:166" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "#Swapping of two variables\n", - "def swapv(x,y):\n", - " x,y=y,x\n", - " print \"x=%d y=%d\\n\" % (x,y)\n", - "a=10\n", - "b=20\n", - "swapv(a,b)\n", - "print \"a=%d b=%d\\n\" % (a,b)" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "x=20 y=10\n", - "\n", - "a=10 b=20\n", - "\n" - ] - } - ], - "prompt_number": 1 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "EXAMPLE ON PAGE-167" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "#program to swap 2 variables\n", - "#Note:There is no concept of pointer in python that's why i used global variables to get the same output\n", - "def swapr():\n", - " global a,b #global declaration\n", - " a,b=b,a\n", - "a=10\n", - "b=20\n", - "swapr()\n", - "print \"a=%d b=%d\\n\" % (a,b)" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "a=20 b=10\n", - "\n" - ] - } - ], - "prompt_number": 1 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "EXAMPLE ON PAGE:167-168" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "#Program to print area & perimeter of circle\n", - "def areaperi(r,a,p):\n", - " a=3.14*r*r #formula of area\n", - " p=2*3.14*r #formula of perimeter\n", - " print \"Area=%f\\n\" % (a)\n", - " print \"Perimeter=%f\\n\" % (p)\n", - "area=0\n", - "perimeter=0\n", - "print \"Enter radius of a circle\"\n", - "radius=eval(raw_input())\n", - "areaperi(radius,area,perimeter)" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter radius of a circle\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "5\n" - ] - }, - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Area=78.500000\n", - "\n", - "Perimeter=31.400000\n", - "\n" - ] - } - ], - "prompt_number": 22 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "EXAMPLE ON PAGE:169" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "#Finding factorial of a number without recursion\n", - "def factorial(x):\n", - " f=1\n", - " for i in range(x,1,-1):\n", - " f=f*i\n", - " return f\n", - "print \"Enter any number\"\n", - "a=eval(raw_input())\n", - "fact=factorial(a)\n", - "print \"Factorial value=%d\\n\" % (fact)" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter any number\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "3\n" - ] - }, - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Factorial value=6\n", - "\n" - ] - } - ], - "prompt_number": 23 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "EXAMPLE ON PAGE:170" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "#Finding factorial of a number with recursion\n", - "def rec(x):\n", - " if x==1:\n", - " return 1\n", - " else:\n", - " f=x*rec(x-1) \n", - " return f #will call back the rec() function\n", - "print \"Enter any number\"\n", - "a=eval(raw_input())\n", - "fact=rec(a)\n", - "print \"Factorial value=%d\\n\" % (fact)" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Enter any number\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "stream": "stdout", - "text": [ - "5\n" - ] - }, - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Factorial value=120\n", - "\n" - ] - } - ], - "prompt_number": 24 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "EXAMPLE ON PAGE:173" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "#Sum of two variables using function\n", - "def add(i,j):\n", - " sum=i+j\n", - " return sum\n", - "a=5\n", - "b=2\n", - "c=add(a,b) #Transfers control to add()\n", - "print \"sum=%d\\n\" % (c)" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "sum=7\n", - "\n" - ] - } - ], - "prompt_number": 25 - }, - { - "cell_type": "heading", - "level": 2, - "metadata": {}, - "source": [ - "EXAMPLE ON PAGE:175-176" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "def factorial(num):\n", - " f=1\n", - " for i in range(1,num+1,1):\n", - " f=f*i;\n", - " return f\n", - "f=factorial(5)\n", - "print \"%d\\n\" % f" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "120\n", - "\n" - ] - } - ], - "prompt_number": 3 - } - ], - "metadata": {} - } - ] +{ + "metadata": { + "name": "", + "signature": "sha256:2cf0a369761f6b9850c2adad43b1edd33ca8cbea4df08e2e06de2898a570851d" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "CHAPTER 5: FUNCTIONS AND POINTERS" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:144" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def message(): #function definition\n", + " print \"Smile,and the world smiles with you...\\n\"\n", + "message() #function call\n", + "print \"Cry, and you stop the monotony!\\n\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Smile,and the world smiles with you...\n", + "\n", + "Cry, and you stop the monotony!\n", + "\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:146" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def italy():\n", + " print \"I am in italy\\n\"\n", + "def brazil():\n", + " print \"I am in brazil\\n\"\n", + "def argentina():\n", + " print \"I am in argentina\\n\"\n", + "print \"I am in main\\n\"\n", + "italy(); #italy() will be called\n", + "brazil(); #brazil() will be called\n", + "argentina(); #argentina() will be called" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "I am in main\n", + "\n", + "I am in italy\n", + "\n", + "I am in brazil\n", + "\n", + "I am in argentina\n", + "\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:147" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def italy():\n", + " print \"I am in italy\\n\"\n", + " brazil(); #it will call brazil()\n", + " print \"I am back in italy\\n\"\n", + "def brazil():\n", + " print \"I am in brazil\\n\"\n", + " argentina(); #it will call argentina()\n", + "def argentina():\n", + " print \"I am in argentina\\n\"\n", + "print \"I am in main\\n\"\n", + "italy(); #italy() is called\n", + "print \"I am finally back in main\\n\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "I am in main\n", + "\n", + "I am in italy\n", + "\n", + "I am in brazil\n", + "\n", + "I am in argentina\n", + "\n", + "I am back in italy\n", + "\n", + "I am finally back in main\n", + "\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:148" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def message():\n", + " print \"Can't imagine life without C\\n\"\n", + " main() #it will call back main()\n", + "def main():\n", + " message()\n", + "main()" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:149" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def message():\n", + " print \"Jewel Thief!!\\n\"\n", + "message() #1st call\n", + "message() #2nd call" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Jewel Thief!!\n", + "\n", + "Jewel Thief!!\n", + "\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:149" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def message2():\n", + " print \"But the butter was bitter\\n\"\n", + "def message1():\n", + " print \"Mary bought some butter\\n\"\n", + "message1()\n", + "message2()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Mary bought some butter\n", + "\n", + "But the butter was bitter\n", + "\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:151-152" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def calsum(x,y,z): #parameterized function\n", + " d=x+y+z\n", + " return d\n", + "print \"Enter any three numbers\"\n", + "a=eval(raw_input())\n", + "b=eval(raw_input())\n", + "c=eval(raw_input())\n", + "sum=calsum(a,b,c) #passing values as arguments\n", + "print \"Sum=%d\\n\" % (sum)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter any three numbers\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "10\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "20\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "30\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Sum=60\n", + "\n" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:153" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def fun():\n", + " print \"Enter any number\"\n", + " n=eval(raw_input())\n", + " if (n>=10 and n<=90):\n", + " return n\n", + " else:\n", + " return n+32\n", + "fun()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter any number\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "100\n" + ] + }, + { + "metadata": {}, + "output_type": "pyout", + "prompt_number": 7, + "text": [ + "132" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:154" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def fun(b):\n", + " b=60\n", + " print \"%d\\n\" % (b) #prints 60\n", + "a=30\n", + "fun(a) #prints 30\n", + "print \"%d\\n\" % (a)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "60\n", + "\n", + "30\n", + "\n" + ] + } + ], + "prompt_number": 8 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:155" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def display(j):\n", + " k=35\n", + " print \"%d\\n\" % (j) #we can't print i directly here because scope of variable is local by default\n", + " print \"%d\\n\" % (k)\n", + "i=20\n", + "display(i)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "20\n", + "\n", + "35\n", + "\n" + ] + } + ], + "prompt_number": 9 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:158-159" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def square(x):\n", + " y=x*x\n", + " return y\n", + "print \"Enter any number\"\n", + "a=eval(raw_input())\n", + "b=square(a)\n", + "print \"Square of %f is %f\\n\" % (a,b)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter any number\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2.5\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Square of 2.500000 is 6.250000\n", + "\n" + ] + } + ], + "prompt_number": 10 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:161" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "i=3\n", + "print \"Address of i=%u\\n\" % (id(i)) #id() will return the loaction of a variable\n", + "print \"Value of i=%d\\n\" % (i)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Address of i=20688560\n", + "\n", + "Value of i=3\n", + "\n" + ] + } + ], + "prompt_number": 12 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:161" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "i=3\n", + "print \"Address of i=%u\\n\" % (id(i))\n", + "print \"Value of i=%d\\n\" % (i)\n", + "print \"Value of i=%d\\n\" % (i)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Address of i=20688560\n", + "\n", + "Value of i=3\n", + "\n", + "Value of i=3\n", + "\n" + ] + } + ], + "prompt_number": 13 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:162" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "i=3\n", + "j=id(i)\n", + "print \"Address of i=%u\\n\" % (id(i)) #print address of i\n", + "print \"Address of i=%u\\n\" % (j) #print address of i\n", + "print \"Address of j=%u\\n\" % (id(j)) #print address of j\n", + "print \"Value of j=%u\\n\" % (j) #print value of j\n", + "print \"Value of i=%d\\n\" % (i)\n", + "print \"Value of i=%d\\n\" % (i)\n", + "print \"Value of i=%d\\n\" % (i)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Address of i=20688560\n", + "\n", + "Address of i=20688560\n", + "\n", + "Address of j=88986780\n", + "\n", + "Value of j=20688560\n", + "\n", + "Value of i=3\n", + "\n", + "Value of i=3\n", + "\n", + "Value of i=3\n", + "\n" + ] + } + ], + "prompt_number": 14 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:164" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "i=3\n", + "j=id(i)\n", + "k=id(j)\n", + "print \"Address of i=%u\\n\" % (id(i)) #print address of i\n", + "print \"Address of i=%u\\n\" % (j) #print address of i\n", + "print \"Address of i=%u\\n\" % (j) #print address of i\n", + "print \"Address of j=%u\\n\" % (id(j)) #print address of i\n", + "print \"Address of j=%u\\n\" % (k) #print address of j\n", + "print \"Address of k=%u\\n\" % (id(k)) #print address of k\n", + "print \"Value of j=%u\\n\" % (j) #print value of j\n", + "print \"Value of k=%u\\n\" % (k) #print value of k\n", + "print \"Value of i=%u\\n\" % (i) #print value of i\n", + "print \"Value of i=%u\\n\" % (i)\n", + "print \"Value of i=%u\\n\" % (i)\n", + "print \"Value of i=%u\\n\" % (i)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Address of i=20688560\n", + "\n", + "Address of i=20688560\n", + "\n", + "Address of i=20688560\n", + "\n", + "Address of j=88986672\n", + "\n", + "Address of j=88986672\n", + "\n", + "Address of k=88986708\n", + "\n", + "Value of j=20688560\n", + "\n", + "Value of k=88986672\n", + "\n", + "Value of i=3\n", + "\n", + "Value of i=3\n", + "\n", + "Value of i=3\n", + "\n", + "Value of i=3\n", + "\n" + ] + } + ], + "prompt_number": 15 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:166" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def swapv(x,y):\n", + " x,y=y,x\n", + " print \"x=%d y=%d\\n\" % (x,y)\n", + "a=10\n", + "b=20\n", + "swapv(a,b)\n", + "print \"a=%d b=%d\\n\" % (a,b)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "x=20 y=10\n", + "\n", + "a=10 b=20\n", + "\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE-167" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def swapr():\n", + " global a,b #global declaration\n", + " a,b=b,a\n", + "a=10\n", + "b=20\n", + "swapr()\n", + "print \"a=%d b=%d\\n\" % (a,b)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "a=20 b=10\n", + "\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:167-168" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def areaperi(r,a,p):\n", + " a=3.14*r*r #formula of area\n", + " p=2*3.14*r #formula of perimeter\n", + " print \"Area=%f\\n\" % (a)\n", + " print \"Perimeter=%f\\n\" % (p)\n", + "area=0\n", + "perimeter=0\n", + "print \"Enter radius of a circle\"\n", + "radius=eval(raw_input())\n", + "areaperi(radius,area,perimeter)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter radius of a circle\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Area=78.500000\n", + "\n", + "Perimeter=31.400000\n", + "\n" + ] + } + ], + "prompt_number": 22 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:169" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def factorial(x):\n", + " f=1\n", + " for i in range(x,1,-1):\n", + " f=f*i\n", + " return f\n", + "print \"Enter any number\"\n", + "a=eval(raw_input())\n", + "fact=factorial(a)\n", + "print \"Factorial value=%d\\n\" % (fact)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter any number\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Factorial value=6\n", + "\n" + ] + } + ], + "prompt_number": 23 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:170" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def rec(x):\n", + " if x==1:\n", + " return 1\n", + " else:\n", + " f=x*rec(x-1) \n", + " return f #will call back the rec() function\n", + "print \"Enter any number\"\n", + "a=eval(raw_input())\n", + "fact=rec(a)\n", + "print \"Factorial value=%d\\n\" % (fact)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter any number\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Factorial value=120\n", + "\n" + ] + } + ], + "prompt_number": 24 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:173" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def add(i,j):\n", + " sum=i+j\n", + " return sum\n", + "a=5\n", + "b=2\n", + "c=add(a,b) #Transfers control to add()\n", + "print \"sum=%d\\n\" % (c)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "sum=7\n", + "\n" + ] + } + ], + "prompt_number": 25 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:175-176" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def factorial(num):\n", + " f=1\n", + " for i in range(1,num+1,1):\n", + " f=f*i;\n", + " return f\n", + "f=factorial(5)\n", + "print \"%d\\n\" % f" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "120\n", + "\n" + ] + } + ], + "prompt_number": 3 + } + ], + "metadata": {} + } + ] } \ No newline at end of file -- cgit