{ "metadata": { "name": "", "signature": "sha256:6797b9444a439c70039a1e66c231ad4d494ff12505798711ee0fd43c452323b7" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Hour 15: Working with Functions" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 15.1, Page No 245" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def function_1(x,y):\n", " print \"Within Function_1.\"\n", " return (x+y)\n", "def function_2(x,y):\n", " print \"Within Function_2.\"\n", " return (x+y)\n", "\n", "x1=80\n", "y1=10\n", "x2=float\n", "y2=float\n", "x2=100.123456\n", "y2=10.123456\n", "print \"Pass function_1 %d\" % x1,\n", "print \" and %d\" % y1\n", "print \"Function_1 returns %d\" % function_1(x1,y1)\n", "print \"Pass function_2 %f\" % x2,\n", "print \" and %f\" % y2\n", "print \"Function_1 returns %f\" % function_2(x2,y2)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Pass function_1 80 and 10\n", "Within Function_1.\n", "Function_1 returns 90\n", "Pass function_2 100.123456 and 10.123456\n", "Within Function_2.\n", "Function_1 returns 110.246912\n" ] } ], "prompt_number": 9 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 15.2, Page No 248" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import time\n", "def GetDateTime():\n", " print \"Within GetDateTime().\"\n", " print \"Current date and time is %s\" % str(time.strftime(\"%a %b %d %X %Y\"))\n", "\n", "print \"Before the GetDateTime() function is called.\"\n", "GetDateTime()\n", "print \"After The GetDateTime() function is called.\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Before the GetDateTime() function is called.\n", "Within GetDateTime().\n", "Current date and time is Mon Nov 10 11:08:33 2014\n", "After The GetDateTime() function is called.\n" ] } ], "prompt_number": 23 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 15.3, Page No 253" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def AddDouble(*args):\n", " result=float\n", " result=0.0\n", " print \"The number of arguments is \"+ str(len(args))\n", " for i in range(len(args)):\n", " result= result + args[i]\n", " return result\n", "\n", "d1=float\n", "d2=float\n", "d3=float\n", "d4=float\n", "d1=1.5\n", "d2=2.5\n", "d3=3.5\n", "d4=4.5\n", "\n", "print \"Given an argument: %2.1f\" % d1\n", "print \"The result returned by AddDouble() is: %2.1f \\n\" % AddDouble(d1)\n", "\n", "print \"Given an argument: %2.1f\" % d1,\n", "print \" and %2.1f\" % d2\n", "print \"The result returned by AddDouble() is: %2.1f \\n\" % AddDouble(d1,d2)\n", "\n", "print \"Given an argument: %2.1f\" % d1,\n", "print \", %2.1f\" % d2,\n", "print \" and %2.1f\" % d3\n", "print \"The result returned by AddDouble() is: %2.1f \\n\" % AddDouble(d1,d2,d3)\n", "\n", "print \"Given an argument: %2.1f\" % d1,\n", "print \", %2.1f\" % d2,\n", "print \", %2.1f\" % d3,\n", "print \" and %2.1f\" % d4\n", "print \"The result returned by AddDouble() is: %2.1f \\n\" % AddDouble(d1,d2,d3,d4)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Given an argument: 1.5\n", "The number of arguments is 1\n", "The result returned by AddDouble() is: 1.5 \n", "\n", "Given an argument: 1.5 and 2.5\n", "The number of arguments is 2\n", "The result returned by AddDouble() is: 4.0 \n", "\n", "Given an argument: 1.5 , 2.5 and 3.5\n", "The number of arguments is 3\n", "The result returned by AddDouble() is: 7.5 \n", "\n", "Given an argument: 1.5 , 2.5 , 3.5 and 4.5\n", "The number of arguments is 4\n", "The result returned by AddDouble() is: 12.0 \n", "\n" ] } ], "prompt_number": 6 } ], "metadata": {} } ] }