{ "metadata": { "name": "", "signature": "sha256:13a3347e13ed5ee09ae2833e49a517873801778756103a4da905a96a5997fa89" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Chapter 9, User-Defined Functions" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 9-1 , Page number: 187" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Using a function as an abbreviation\n", "\n", "# In python, the user defined functions have to defined before making a call to them\n", "\n", "def chorus() :\n", "\tprint \"%60s\"%\"With a knick-knack-paddy-whack,\"\n", "\tprint \"%60s\"%\"Give the dog a bone,\"\n", "\tprint \"%60s\\n\"%\"This old man went rolling home.\"\n", "\n", "print \"This old man, he played two,\"\n", "print \"He played knick-knack on my thumb,\"\n", "chorus()\n", "\n", "print \"This old man, he played one,\"\n", "print \"He played knick-knack on my shoe,\"\n", "chorus()\n", "\n", "print \"This old man, he played three,\"\n", "print \"He played knick-knack on my knee,\"\n", "chorus()\n", "\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "This old man, he played two,\n", "He played knick-knack on my thumb,\n", " With a knick-knack-paddy-whack,\n", " Give the dog a bone,\n", " This old man went rolling home.\n", "\n", "This old man, he played one,\n", "He played knick-knack on my shoe,\n", " With a knick-knack-paddy-whack,\n", " Give the dog a bone,\n", " This old man went rolling home.\n", "\n", "This old man, he played three,\n", "He played knick-knack on my knee,\n", " With a knick-knack-paddy-whack,\n", " Give the dog a bone,\n", " This old man went rolling home.\n", "\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 9-2 , Page number: 189" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Demonstration of one use of a function\n", "\n", "def get_an_int() :\n", "\t\"\"\"RETURNS AN INTEGER\"\"\"\t\t# This statement is just a description of the function (specific feature of python)\n", "\treturn 5\n", "\n", "print \"Enter the length of a rectangle:\",\n", "length = get_an_int()\n", "print length\n", "\n", "print \"Enter the width:\",\n", "width = get_an_int()\n", "print width\n", "\n", "print \"\\nThe area is %d\"%(length*width)\n", "\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter the length of a rectangle: 5\n", "Enter the width: 5\n", "\n", "The area is 25\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 9-3 , Page number: 191" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# An attempt at communication between functions\n", "\n", "def modify_i() :\n", "\ti = 3\n", "\t\n", "i = 1\n", "print \"i is now equal to %d\"%i\n", "\n", "modify_i()\n", "print \"i is now equal to %d\"%i\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "i is now equal to 1\n", "i is now equal to 1\n" ] } ], "prompt_number": 3 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 9-4 , Page number: 192" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Demonstration of the use of a global variable\n", "\n", "# Making variable global\n", "\n", "def get_an_int() :\n", "\treturn 5\n", "\n", "def calc_area() :\n", "\tarea = length*width\n", "\n", "globals()\t\t\t# The command simply makes all the variables declared so far as global variables\n", "print \"Enter the length of a rectangle:\",\n", "length = get_an_int()\n", "print length\n", "\n", "print \"Enter the width:\",\n", "width = get_an_int()\n", "print width\n", "\n", "calc_area()\n", "print \"\\nThe area is %d\"%(length*width)\n", "\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter the length of a rectangle: 5\n", "Enter the width: 5\n", "\n", "The area is 25\n" ] } ], "prompt_number": 4 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 9-5 , Page number: 194" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Introduction to parameters\n", "\n", "def print_an_int(i) :\n", "\t\"\"\"The function prints an integer passed as a parameter\"\"\"\n", "\tprint i\n", "\n", "\n", "n = 3\n", "\n", "print \"Passing a constant:\",\n", "print_an_int(5)\n", "\n", "print \"\\n\\nPassing a variable's value:\",\n", "print_an_int(n)\n", "\n", "print \"\\n\\nPassing the value of an expression:\",\n", "print_an_int(n * 4 + 1)\n", "\n", "\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Passing a constant: 5\n", "\n", "\n", "Passing a variable's value: 3\n", "\n", "\n", "Passing the value of an expression: 13\n" ] } ], "prompt_number": 5 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 9-6 , Page number: 196" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Demonstration of how a procedure cannot modify the contents of a variable passed as a parameter\n", "\n", "def modify(i) :\n", "\ti = 3\n", "\n", "i = 1\n", "\n", "print \"i is now equal to %d\"%i\n", "\n", "modify(i)\n", "\n", "print \"i is now equal to %d\"%i" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "i is now equal to 1\n", "i is now equal to 1\n" ] } ], "prompt_number": 6 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 9-7 , Page number: 197 " ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Hailstones, demonstrating the use of a parameter\n", "\n", "def step(i) :\n", "\t\"\"\"This function returns its parameter i divided by 2 if i is even else it returns i multiplied by 3 and incremented by 1\"\"\"\n", "\tif (i % 2) :\n", "\t\treturn (i * 3 + 1)\n", "\telse : \n", "\t\treturn (i / 2)\n", "\n", "counter = 0\n", "\n", "print \"Enter a positive integer\",\n", "n = 25\n", "print n\n", "\n", "nsave = n\n", "\n", "while n > 1 :\n", "\tn = step(n)\n", "\tcounter += 1\n", "\tprint \"Step %4d: n = %4d\"%(counter,n)\n", "\n", "print \"\\n\\n%d went to 1 in %d steps.\"%(nsave,counter)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter a positive integer 25\n", "Step 1: n = 76\n", "Step 2: n = 38\n", "Step 3: n = 19\n", "Step 4: n = 58\n", "Step 5: n = 29\n", "Step 6: n = 88\n", "Step 7: n = 44\n", "Step 8: n = 22\n", "Step 9: n = 11\n", "Step 10: n = 34\n", "Step 11: n = 17\n", "Step 12: n = 52\n", "Step 13: n = 26\n", "Step 14: n = 13\n", "Step 15: n = 40\n", "Step 16: n = 20\n", "Step 17: n = 10\n", "Step 18: n = 5\n", "Step 19: n = 16\n", "Step 20: n = 8\n", "Step 21: n = 4\n", "Step 22: n = 2\n", "Step 23: n = 1\n", "\n", "\n", "25 went to 1 in 23 steps.\n" ] } ], "prompt_number": 7 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 9-8 , Page number: 200" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Square root program demonstrating a non-integer function and parameter\n", "\n", "def EPSILON() :\n", "\t\"\"\"replacement of #define constant\"\"\"\n", "\treturn 0.0001\n", "\n", "def sqrt(n) :\n", "\t\"\"\"returns the square root of the number supplied as parameter using Newton-Raphson method\"\"\"\n", "\tif n > 0.0 :\n", "\t\t# The for loop statement given in the example(from the book) is not possible to code in python\n", "\t\t# So, breaking it down to while loop\n", "\t\tguess = n / 2.0\n", "\t\twhile (guess * guess - n > EPSILON() or guess * guess - n < -EPSILON()) :\n", "\t\t\tguess = (guess + n/guess) / 2.0\n", "\t\treturn guess\n", "\telse :\n", "\t\treturn (-1.0)\n", "\n", "print \"Enter the number whose square root you want:\",\n", "x = 17.1\n", "print x\n", "\n", "print \"The square root of %f is %f\"%(x,sqrt(x))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter the number whose square root you want: 17.1\n", "The square root of 17.100000 is 4.135215\n" ] } ], "prompt_number": 8 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 9-9 , Page number: 201" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Testing for leap years using a boolean function\n", "\n", "def is_leap(year) :\n", "\t\"\"\"A function that returns true if the given year is a leap year else returns false\"\"\"\n", "\treturn (year % 4 == 0 and year % 100 != 0 or year % 400 == 0)\n", "\n", "print \"Please enter a year:\",\n", "year = 2000\n", "print year\n", "\n", "if (is_leap(year)) :\n", "\tprint \"%d is a leap year.\"%year\n", "else :\n", "\tprint \"%d is not a leap year.\"%year" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Please enter a year: 2000\n", "2000 is a leap year.\n" ] } ], "prompt_number": 9 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 9-10 , Page number: 203" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Demonstration of the use of multiple parameters\n", "\n", "def get_an_int() :\n", "\t\"\"\"returns a random integer\"\"\"\n", "\treturn 7\n", "\n", "def area(length,width) :\n", "\t\"\"\"returns the area of the rectangle using its parameters length and width\"\"\"\n", "\treturn length * width\n", "\n", "print \"Enter the length of a rectangle:\",\n", "length = get_an_int()\n", "print length\n", "\n", "print \"Enter the width:\",\n", "width = get_an_int()\n", "print width\n", "\n", "print \"\\nThe area is %d.\"%area(length,width)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter the length of a rectangle: 7\n", "Enter the width: 7\n", "\n", "The area is 49.\n" ] } ], "prompt_number": 10 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 9-11 , Page number: 204" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Illustration of a 2-parameter function: raising a number to a power \n", "\n", "def power(i,x) :\n", "\t\"\"\"returns the values of i to the power of x\"\"\"\n", "\taccum = 1\n", "\tif x < 0 :\n", "\t\treturn 0\n", "\tfor index in range (0,x,1) :\n", "\t\taccum *= i\n", "\treturn accum\n", "\n", "answer = \"y\"\n", "\n", "while answer == \"y\" or answer == \"Y\" :\n", "\tprint \"\\nPlease enter an integer:\",\n", "\tnumber = 4\n", "\tprint number\n", "\n", "\tprint \"Please enter a non-negative integral power\"\n", "\tprint \" to which the first number is to be raised\",\n", "\texponent = 2\n", "\tprint exponent\n", "\n", "\twhile exponent < 0 :\n", "\t\tprint \"***\\7Please make that a non-negative integer\"\n", "\t\texponent = 2 \t\t\t\t# randomly making the exponent non-negative\n", "\n", "\tprint \"%d raised to the power %d is %ld.\"%(number,exponent,power(number,exponent))\n", "\n", "\t# see if the user wants to keep going\n", "\n", "\tprint \"\\nAnother calculation? (y/n)\",\n", "\tanswer = \"n\"\n", "\tprint answer" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Please enter an integer: 4\n", "Please enter a non-negative integral power\n", " to which the first number is to be raised 2\n", "4 raised to the power 2 is 16.\n", "\n", "Another calculation? (y/n) n\n" ] } ], "prompt_number": 11 } ], "metadata": {} } ] }