{ "metadata": { "name": "", "signature": "sha256:057add51225e0c3d10e99246a561a5b527e19bd9c75972f8c282ffbd6b35fbb7" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "12:USER DEFINED FUNCTIONS" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "12.4.1, page number:194" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def get_a_value():\n", " return float(input())\n", "print \"Enter the Length..\"\n", "leng=get_a_value()\n", "print \"Enter the Width..\"\n", "width=get_a_value()\n", "print \"Area = \",leng*width" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter the Length..\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "16.8\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Enter the Width..\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "43.7\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Area = 734.16\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "12.5.1, page number:195" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def change_index():\n", " index=5\n", "index=3\n", "print \"index = \",index\n", "change_index()\n", "print \"index = \",index" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "index = 3\n", "index = 3\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "12.5.2, page number:196" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def get_a_value():\n", " return float(input())\n", "def calc_area():\n", " global leng,width\n", " return leng*width\n", "print \"Enter the Length..\"\n", "leng=get_a_value()\n", "print \"Enter the Width..\"\n", "width=get_a_value()\n", "print \"Area = \",calc_area()\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter the Length..\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "12.5\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Enter the Width..\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "23.7\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Area = 296.25\n" ] } ], "prompt_number": 3 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "12.6.1, page number:197" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def print_value(k):\n", " print k\n", "numb=5\n", "print \"Value of Expression : \",\n", "print_value(numb*3+2)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Value of Expression : 17\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "12.7.1, page number:198" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def TOL():\n", " return 0.0001\n", "def sqrt(n):\n", " if n>0.0:\n", " guess=0.0\n", " guess=n/2.0\n", " while True:\n", " if ((guess*guess-n) > TOL()) or ((guess*guess-n) < -TOL()):\n", " temp=guess+n/guess\n", " guess=temp/2.0\n", " else:\n", " break\n", " return guess\n", " else:\n", " return -1.0\n", " \n", "numb=float(input(\"Enter number..\"))\n", "print \"Square Root of\",numb,\"is\",sqrt(numb)\n" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter number..17.1\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Square Root of 17.1 is 4.13521500873\n" ] } ], "prompt_number": 4 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "12.8.1, page number:198" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "year=0\n", "def leap(n):\n", " return (n%4 == 0)and(n%100 != 0)or(n%400 == 0)\n", "year=int(input(\"Enter Year..\"))\n", "if leap(year):\n", " print year,\"is leap year\"\n", "else:\n", " print year,\"is not leap year\"" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "12.9.1, page number:199" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def input_value():\n", " return input()\n", "def area(l,w):\n", " return l*w\n", "print \"Length = \"\n", "length=input_value();\n", "print \"Width = \"\n", "width=input_value();\n", "print \"Area = \",area(length,width)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Length = \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "12.5\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Width = \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "23.7\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Area = 296.25\n" ] } ], "prompt_number": 6 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "12.9.2, page number:200" ] }, { "cell_type": "code", "collapsed": false, "input": [ "value=1\n", "def power(n,e):\n", " global value\n", " if(e<0):\n", " return 0\n", " for index in range(0,e):\n", " if(index < e):\n", " value*=n\n", " index+=1\n", " return value\n", " \n", "number=int(input(\"Enter Integer...\"))\n", "exponent=int(input(\"Enter Exponent...\"))\n", "while True:\n", " if (exponent<0):\n", " exponent=input(\"Enter a Non-Negative Integer :\")\n", " else:\n", " break\n", "print number,\"raised to \",exponent,\" = \",power(number,exponent)" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Integer...4\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Exponent...2\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "4 raised to 2 = 16\n" ] } ], "prompt_number": 7 } ], "metadata": {} } ] }