diff options
Diffstat (limited to 'sample_notebooks/Abu BakkerSiddik/Functions.ipynb')
-rwxr-xr-x | sample_notebooks/Abu BakkerSiddik/Functions.ipynb | 1441 |
1 files changed, 1441 insertions, 0 deletions
diff --git a/sample_notebooks/Abu BakkerSiddik/Functions.ipynb b/sample_notebooks/Abu BakkerSiddik/Functions.ipynb new file mode 100755 index 00000000..4a23de26 --- /dev/null +++ b/sample_notebooks/Abu BakkerSiddik/Functions.ipynb @@ -0,0 +1,1441 @@ +{
+ "metadata": {
+ "name": "",
+ "signature": "sha256:63dae7729725476213c038c75f708a0afd5386cd85264fbfba4fc7a16437b94f"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Chapter 6: Functions"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 1 page 217"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def power(x,n): #Function header\n",
+ " # Function body starts here....\n",
+ " result=1.0 #declaration of variable result \n",
+ " for i in range(1,n+1,1):\n",
+ " result*=x #computing X to power n\n",
+ " return result #returning result\n",
+ "#Function body ends here....\n",
+ "\n",
+ "print \"result=%f\" % (power(2,2)) #function call\n",
+ "print \"result=%f\" % (power(3,3)) #function call"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "result=4.000000\n",
+ "result=27.000000\n"
+ ]
+ }
+ ],
+ "prompt_number": 23
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 2 page 218"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def FtoC(faren): #Function header\n",
+ " # Function body starts here....\n",
+ " factor =5.0/9.0\n",
+ " freezing=32.0 \n",
+ " celcius=factor*(faren-freezing) #computing temp in celcius\n",
+ " return celcius #returning result\n",
+ "#Function body ends here....\n",
+ "\n",
+ "print \"result=%f\" % (FtoC(32.0)) #function call"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "result=0.000000\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 3 page 218"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#prototype not present in Python def sum_(a,b);\n",
+ "def main():\n",
+ " a=5\n",
+ " b=10\n",
+ " print \"sum=%d\" % (sum_(a,b))\n",
+ " return\n",
+ "#Main ends here....\n",
+ "\n",
+ "def sum_(x,y):\n",
+ "# Function body starts here....\n",
+ " return x+y #returning result\n",
+ "#Function body ends here....\n",
+ "main() #Calling main function"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "sum=15\n"
+ ]
+ }
+ ],
+ "prompt_number": 12
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 4 page 219"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def leap_yr(yr): #Function Defination\n",
+ "# Function body starts here....\n",
+ " if((yr%4==0) and (yr%100!=0) or yr%400==0):\n",
+ " return 1 #returning result\n",
+ " else:\n",
+ " return 0 #returning result\n",
+ "#Function body ends here.... \n",
+ "print leap_yr(2016) #Calling function"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "1\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 5 page 220"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def main(): #main starts here\n",
+ " print \"Temperature in Fahrenheit scale: \"\n",
+ " tempInF=eval(raw_input())\n",
+ " tempInC=FtoC(tempInF) #function FtoC() called from In[2]\n",
+ " print \"%f Fahrenheit equals %f Celcius \\n \" % (tempInF,tempInC)\n",
+ " return\n",
+ " #main ends here\n",
+ "main()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Temperature in Fahrenheit scale: \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "32\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "32.000000 Fahrenheit equals 0.000000 Celcius \n",
+ " \n"
+ ]
+ }
+ ],
+ "prompt_number": 18
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 6 page 221"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#main starts here\n",
+ "def main(): \n",
+ " num=3\n",
+ " print \"\\n num = %d before function call.\" % (num) #taking input from user\n",
+ " result=mul_by_10(num) #function call\n",
+ " print \"\\n result = %d, after return from function.\" % (result) #Displaying result\n",
+ " print \"\\n num = %d\" % (num)\n",
+ " return\n",
+ "#main ends here\n",
+ "def mul_by_10(num): #Function header\n",
+ " num*=10; #Function body starts here\n",
+ " return num #returning result\n",
+ " #Function body ends here\n",
+ "main() #calling main function"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ " num = 3 before function call.\n",
+ "\n",
+ " result = 30, after return from function.\n",
+ "\n",
+ " num = 3\n"
+ ]
+ }
+ ],
+ "prompt_number": 22
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 7 page 222"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def main(): #main starts here\n",
+ " print \"Enter two numbers: \"\n",
+ " nOne=eval(raw_input()) #taking input from user\n",
+ " nTwo=eval(raw_input()) #taking input from user\n",
+ " n=GCD(nOne,nTwo)\n",
+ " print \"\\nGCD of %d and %d is %d \\n\" %(nOne,nTwo,n) #main ends here\n",
+ " return\n",
+ "def GCD(x,y): #function starts here\n",
+ " result=1\n",
+ " k=2\n",
+ " while((k<=x) and (k<=y)): #checking if x and y are greater than k\n",
+ " if((x%k==0) and (y%k==0)):\n",
+ " result=k\n",
+ " k+=1\n",
+ " return result #function ends here\n",
+ "main() #function call"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter two numbers: \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "4\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "8\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "GCD of 4 and 8 is 4 \n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 8 page 222"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def main(): #main starts here\n",
+ " print \"\\nEnter the Number: \"\n",
+ " n=eval(raw_input()) #taking input from user\n",
+ " print \"\\nPrime factors on %d is...\\n \" % (n) \n",
+ " for d in range(2,(n/2)+1,1):\n",
+ " if((n%d==0) and isPrime(d)): #calculating prime factors, isPrime() function\n",
+ " print \"%d\" % (d)\n",
+ " return #main ends here \n",
+ "def isPrime(x): #isPrime() function starts here\n",
+ " for d in range(2,(x/2)+1,1):\n",
+ " if(x%d==0):\n",
+ " return 0\n",
+ " return 1 #isPrime() function ends here\n",
+ "main() #calling main"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Enter the Number: \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "2\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Prime factors on 2 is...\n",
+ " \n"
+ ]
+ }
+ ],
+ "prompt_number": 12
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 9 page 223"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def main(): #main starts here\n",
+ " arr=[1,2,3] #Array declaration and initialisation\n",
+ " change(arr) #calling function\n",
+ " print \"Elements are %d, %d and %d\" % (arr[0],arr[1],arr[2])\n",
+ " return #main starts here\n",
+ "def change(my_array):\n",
+ " my_array[0]=10 #Changing values\n",
+ " my_array[1]=20\n",
+ " return\n",
+ "main()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Elements are 10, 20 and 3\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 10 page 223 and 224"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#main starts here\n",
+ "def main():\n",
+ " arr=[3,2,7,0,6,4,9,8,1,5] #array declaration and initializtion\n",
+ " print \"The array before sort is: \\n\" \n",
+ " for i in range(0,10,1): #displaying array values before changing\n",
+ " print arr[i],\n",
+ " sort(arr,10)\n",
+ " print \"\\nThe array after sort is: \\n\"\n",
+ " for i in range(0,10,1): #displaying array values after changing\n",
+ " print arr[i],\n",
+ " return\n",
+ "#main ends here\n",
+ "#functions starts here\n",
+ "def sort(a,n):\n",
+ " for i in range(0,n-1,1): #sorting array values\n",
+ " for j in range(0,n-i-1,1):\n",
+ " if(a[j]>a[j+1]):\n",
+ " temp=a[j]\n",
+ " a[j]=a[j+1]\n",
+ " a[j+1]=temp\n",
+ " return\n",
+ "#functions ends here\n",
+ "main()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The array before sort is: \n",
+ "\n",
+ "3 2 7 0 6 4 9 8 1 5 \n",
+ "The array after sort is: \n",
+ "\n",
+ "0 1 2 3 4 5 6 7 8 9\n"
+ ]
+ }
+ ],
+ "prompt_number": 7
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 11 page 224"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#main starts here\n",
+ "def main():\n",
+ " myInts=[1,2,3,4,5,6,7,8,9,10] #Array initialisation and declaration\n",
+ " size=10\n",
+ " print \"\\n\\nThe given numbers are:\"\n",
+ " for i in range(0,size,1): #displaying array values before changing\n",
+ " print myInts[i],\n",
+ " doubleThem(myInts,size) #calling function\n",
+ " print \"\\nThe double numbers are: \"\n",
+ " for i in range(0,size,1): #displaying array values after changing\n",
+ " print myInts[i],\n",
+ " return\n",
+ "#main ends here\n",
+ "#*********function defination***********#\n",
+ "def doubleThem(a,size):\n",
+ " for i in range(0,size,1): #multplying array values by 2\n",
+ " a[i]=2*a[i]\n",
+ " return\n",
+ "main()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "\n",
+ "The given numbers are:\n",
+ "1 2 3 4 5 6 7 8 9 10 \n",
+ "The double numbers are: \n",
+ "2 4 6 8 10 12 14 16 18 20\n"
+ ]
+ }
+ ],
+ "prompt_number": 11
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 12 page 224 and 225"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#main starts here\n",
+ "def main():\n",
+ " b=list() #array declaration\n",
+ " n=input(\"How many students?\")\n",
+ " print \"Enter the age of students\\n\"\n",
+ " for i in range(0,n,1): #taking input from user\n",
+ " val=input()\n",
+ " b.append(val)\n",
+ " average=avg_age(b,n) #calling function\n",
+ " print \"\\nThe avg age of students is = %f\" % average\n",
+ " return\n",
+ "#main ends here\n",
+ "def avg_age(a,n):\n",
+ " sum=0.0\n",
+ " for j in range(0,n,1): #calculating avg age\n",
+ " sum=sum+a[j]\n",
+ " return sum/n\n",
+ "main()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "How many students?5\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the age of students\n",
+ "\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "10\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "10\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "10\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "10\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "10\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "The avg age of students is = 10.000000\n"
+ ]
+ }
+ ],
+ "prompt_number": 16
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 13 page 225"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#main starts here\n",
+ "def main():\n",
+ " values=list() #array declaration\n",
+ " print \"Enter 5 numbers\"\n",
+ " for i in range(0,5,1): #taking input from user\n",
+ " val=input()\n",
+ " values.append(val)\n",
+ " max1=maximum(values,5) #calling function\n",
+ " print \"Maximum value is %d\\n\" % max1\n",
+ " return\n",
+ "#main ends here\n",
+ "#function begins here\n",
+ "def maximum(values,n):\n",
+ " max_value=values[0]\n",
+ " for i in range(0,n,1): #finding maximum value\n",
+ " if(values[i]>max_value):\n",
+ " max_value=values[i]\n",
+ " return max_value\n",
+ "#function ends here\n",
+ "main()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter 5 numbers\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "125\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "365\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "552\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "001\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "-568\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Maximum value is 552\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 20
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 14 page 225 and 226"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def main():\n",
+ " a=list() #source string\n",
+ " b=list() #destination string\n",
+ " print \"Input source string: \"\n",
+ " a=raw_input() #read input string\n",
+ " b=string_copy(b,a) #function call\n",
+ " print \"\\nDestination string: %s\\n\" % b\n",
+ " return\n",
+ "#function call\n",
+ "def string_copy(d,s):\n",
+ " print \"\\nSource string: %s\" % s \n",
+ " #copying the string\n",
+ " d=s\n",
+ " return d\n",
+ "main()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Input source string: \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "i love python\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Source string: i love python\n",
+ "\n",
+ "Destination string: i love python\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 15 page 226"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "ROW=2 #define row 3\n",
+ "COLUMN=3 #define col 3\n",
+ "def main(): \n",
+ " global ROW,COLUMN\n",
+ " a=[[0 for row in range(0,COLUMN)] for col in range(0,ROW)]\n",
+ " b=[[0 for row in range(0,COLUMN)] for col in range(0,ROW)]\n",
+ " print \"\\nEnter elements of the first matrix.\\n\"\n",
+ " #Read first matrix elements\n",
+ " for i in range(ROW):\n",
+ " for j in range(COLUMN):\n",
+ " a[i][j]=input()\n",
+ " print \"\\nEnter elements of the second matrix.\\n\"\n",
+ " #Read second matrix elements\n",
+ " for i in range(ROW):\n",
+ " for j in range(COLUMN):\n",
+ " b[i][j]=input()\n",
+ " mat_arith(a,b) #function call\n",
+ "def mat_arith(a,b):\n",
+ " global ROW,COLUMN\n",
+ " c=[[0 for row in range(0,COLUMN)] for col in range(0,ROW)]\n",
+ " print \"\\nFor addition enter: 1\\n\"\n",
+ " print \"\\nFor subtraction enter: 2\\n\"\n",
+ " print \"\\nEnter your choice\\n\"\n",
+ " choice=eval(raw_input())\n",
+ " for i in range(ROW):\n",
+ " for j in range(COLUMN):\n",
+ " if choice == 1: #checking for addation and subtraction and performing it\n",
+ " c[i][j]=a[i][j]+b[i][j]\n",
+ " elif choice == 2:\n",
+ " c[i][j]=a[i][j]-b[i][j]\n",
+ " else:\n",
+ " print \"Invalid choice. Task not done.\"\n",
+ " return\n",
+ " print \"\\nThe resulting matrix is:\\n\"\n",
+ " for i in range(ROW):\n",
+ " for j in range(COLUMN):\n",
+ " print c[i][j],\n",
+ " print(\"\\n\")\n",
+ "main()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Enter elements of the first matrix.\n",
+ "\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "2\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "4\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": [
+ "10\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Enter elements of the second matrix.\n",
+ "\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "1\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "3\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "5\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "7\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "9\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "11\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "For addition enter: 1\n",
+ "\n",
+ "\n",
+ "For subtraction enter: 2\n",
+ "\n",
+ "\n",
+ "Enter your choice\n",
+ "\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "1\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "The resulting matrix is:\n",
+ "\n",
+ "3 7 11 \n",
+ "\n",
+ "14 17 21 \n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 16 page 227 and 228"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#main strats here\n",
+ "def main():\n",
+ " a=5\n",
+ " b=7\n",
+ " print \"In main: a= %d, b= %d\\n\" % (a,b)\n",
+ " exchange(a,b) #calling function\n",
+ " print\"\\nBack in main:\"\n",
+ " print\"a= %d, b= %d\" % (a,b)\n",
+ " return\n",
+ "#main ends here\n",
+ "#function starts here\n",
+ "def exchange(a,b):\n",
+ " print\"In function exchange() before change: just received from main... a=%d and b=%d \" % (a,b)\n",
+ " #exchanging values\n",
+ " temp=a\n",
+ " a=b\n",
+ " b=temp\n",
+ " print\"In function exchange() after change:\"\n",
+ " print\"a=%d and b=%d \" % (a,b)\n",
+ " return\n",
+ "main()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "In main: a= 5, b= 7\n",
+ "\n",
+ "In function exchange() before change: just received from main... a=5 and b=7 \n",
+ "In function exchange() after change:\n",
+ "a=7 and b=5 \n",
+ "\n",
+ "Back in main:\n",
+ "a= 5, b= 7\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 17 page 228"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "a=5 #declaration of global variables\n",
+ "b=7\n",
+ "#main starts here\n",
+ "def main():\n",
+ " global a\n",
+ " global b\n",
+ " print \"In main: a= %d, b= %d\\n\" % (a,b)\n",
+ " exchange() #calling function\n",
+ " print\"\\nBack in main:\",\n",
+ " print\"a= %d, b= %d\" % (a,b)\n",
+ "#main ends here\n",
+ "#function starts here\n",
+ "def exchange():\n",
+ " global a\n",
+ " global b\n",
+ " print\"In function exchange() before change: just received from main... a=%d and b=%d \" % (a,b)\n",
+ " temp=a\n",
+ " a=b\n",
+ " b=temp\n",
+ " print\"In function exchange() after change:\",\n",
+ " print\"a=%d and b=%d \" % (a,b)\n",
+ " return\n",
+ "#function ends here\n",
+ "main()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "In main: a= 5, b= 7\n",
+ "\n",
+ "In function exchange() before change: just received from main... a=5 and b=7 \n",
+ "In function exchange() after change: a=7 and b=5 \n",
+ "\n",
+ "Back in main: a= 7, b= 5\n"
+ ]
+ }
+ ],
+ "prompt_number": 9
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 18 page 229"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def fn1():\n",
+ " #variable declaration in inner block\n",
+ " x=45\n",
+ " print\"\\nin inner block x= %d\" % x\n",
+ "#variable declaration in outer block\n",
+ "x=3\n",
+ "print \"\\nin outer block x = %d before executing inner block\" % x\n",
+ "fn1()\n",
+ "print \"\\nin outer block x = %d after executing inner block\" % x"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "in outer block x = 3 before executing inner block\n",
+ "\n",
+ "in inner block x= 45\n",
+ "\n",
+ "in outer block x = 3 after executing inner block\n"
+ ]
+ }
+ ],
+ "prompt_number": 7
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 19 page 231"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#function starts here\n",
+ "def fn1():\n",
+ " a=10\n",
+ " i=4199232\n",
+ " print \"a=%d\" % a\n",
+ " print \"i=%d\" % i\n",
+ "#function ends here\n",
+ "a=5\n",
+ "print \"a=%d\" % a\n",
+ "fn1() #function call\n",
+ "print \"a=%d\" % a"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "a=5\n",
+ "a=10\n",
+ "i=4199232\n",
+ "a=5\n"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 20 page 232"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "i = 0\n",
+ "#function starts here\n",
+ "def show():\n",
+ " global i\n",
+ " print 'i =',i\n",
+ " i+=1\n",
+ "#function ends here\n",
+ "print\"\\nFirst call of show()\"\n",
+ "show() #function call\n",
+ "print\"\\nSecond call of show()\"\n",
+ "show() #function call\n",
+ "print\"\\nThird call of show()\"\n",
+ "show() #function call"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "First call of show()\n",
+ "i = 0\n",
+ "\n",
+ "Second call of show()\n",
+ "i = 1\n",
+ "\n",
+ "Third call of show()\n",
+ "i = 2\n"
+ ]
+ }
+ ],
+ "prompt_number": 18
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 21 page 231"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "i = None\n",
+ "#function starts here\n",
+ "def show():\n",
+ " print 'Value of i in pgm2.c =',i\n",
+ "#function ends here\n",
+ "i = 10\n",
+ "#function call\n",
+ "show()\n",
+ "print 'Value of i in pgm1.c =',i"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Value of i in pgm2.c = 10\n",
+ "Value of i in pgm1.c = 10\n"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 22 page 233"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "i = None\n",
+ "#function starts here\n",
+ "def show():\n",
+ " global i\n",
+ " i = 20\n",
+ " print \"value of i in pgm2.c = \", i\n",
+ "#function ends here\n",
+ "show() #function call\n",
+ "print\"Value of i in pgm1.c =\",i"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "value of i in pgm2.c = 20\n",
+ "Value of i in pgm1.c = 20\n"
+ ]
+ }
+ ],
+ "prompt_number": 15
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 23 page 236"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "import sys, traceback\n",
+ "#main starts here\n",
+ "def main():\n",
+ " print \"\\nEnter the number of terms: \"\n",
+ " i=eval(raw_input()) #taking input\n",
+ " if (i<0): \n",
+ " print\"\\nError - Number of terms cannot be negative\\n\"\n",
+ " sys.exit(0)\n",
+ " print\"Fibonacci sequence for %d terms is: \" % i\n",
+ " for j in range(1,i+1,1):\n",
+ " print\"%d\" % fib(j) #function call\n",
+ "#function ends here\n",
+ "#function starts here\n",
+ "def fib(val):\n",
+ " if(val<=2):\n",
+ " return 1\n",
+ " else:\n",
+ " return(fib(val-1)+fib(val-2))\n",
+ "#function ends here\n",
+ "main()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Enter the number of terms: \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "5\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Fibonacci sequence for 5 terms is: \n",
+ "1\n",
+ "1\n",
+ "2\n",
+ "3\n",
+ "5\n"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 24 page 237"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#main starts here\n",
+ "def main():\n",
+ " print\"Enter the numbers: \"\n",
+ " i=eval(raw_input())\n",
+ " j=eval(raw_input())\n",
+ " print\"The GCD of %d and %d is %d\" % (i,j,gcd(i,j)) #calling gcd function\n",
+ " return\n",
+ "#function ends here\n",
+ "#function starts here\n",
+ "def gcd(a,b):\n",
+ " remainder=a%b\n",
+ " if(remainder==0):\n",
+ " return b\n",
+ " else:\n",
+ " return gcd(b,remainder) #recursive call to itself, gcd function\n",
+ "#function ends here\n",
+ "main()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the numbers: \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "4\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "8\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The GCD of 4 and 8 is 4\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file |