{ "metadata": { "name": "", "signature": "sha256:3541583e6368ad0fd94057f7e595031de565be72b75fadedd8abb1168ccd2728" }, "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 6.1, Page No 126" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def dAverage(x1,x2):\n", " av = (x1+x2)/2\n", " return av\n", "\n", "print \"Enter two numbers:\"\n", "dNo1= float(raw_input(\"\"))\n", "dNo2= float(raw_input(\"\"))\n", "dAvg= dAverage(dNo1,dNo2)\n", "print \"The average is: \",dAvg" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter two numbers:\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "30\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "20\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "The average is: 25.0\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 6.2, Page No 127" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def min(x,y):\n", " if (x1000):\n", " return 0.15\n", " elif(dLinePrice>500 or iQty>10):\n", " return 0.10\n", " else:\n", " return 0\n", "\n", "def dPrice(iNo,dUnitPrice):\n", " dTax=0.25\n", " dLinePr=iNo*dUnitPrice\n", " dDiscPerc=dDiscount(iNo,dLinePr)\n", " return dLinePr*(1-dDiscPerc)*(1+dTax)\n", "\n", "print \"Enter quantity and unit price: \"\n", "iQuantity=int(raw_input(\"\"))\n", "dUnitPrice=float(raw_input(\"\"))\n", "\n", "print \"To be paid\", dPrice(iQuantity,dUnitPrice)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter quantity and unit price: \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "30\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "50\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "To be paid 1593.75\n" ] } ], "prompt_number": 8 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 6.6, Page No 137" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def replace1(s,c,cnew):\n", " n=0\n", " s = list(s)\n", " while(s[n]!='\\0'):\n", " if(s[n]==c):\n", " s[n]=cnew\n", " n=n+1\n", " print \"\".join(map(str,s))\n", "a=\"C:/Mydocumnets/Sheets\\0\"\n", "print a\n", "replace1(a,'/','-')" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "C:/Mydocumnets/Sheets\u0000\n", "C:-Mydocumnets-Sheets\u0000\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 6.7, Page No 139" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def word(s):\n", " i=0\n", " j=len(s)\n", " while(i='a' and s[i]<='z')or(s[i]>='A' and s[i]<='Z')):\n", " i=i+1\n", " else:\n", " return 0;\n", " return 1;\n", "\n", "str=range(100)\n", "str=raw_input(\"Enter word: \")\n", "while(str):\n", " if(word(str)):\n", " print \"A word\"\n", " else:\n", " print \"No word\"\n", " str=raw_input(\"Enter word: \")" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter word: john\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "A word\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter word: smith\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "A word\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter word: john smith\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "No word\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter word: \n" ] } ], "prompt_number": 9 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 6.8, Page No 143" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#import myfunc we have created this header file and its going to work as header file user has to make it\n", "print \"To be paid: \" + str(dPrice(10,700))\n", "\n", "# Code for myfunc Header file is here\n", "\n", "\"\"\"\n", "def underline(n):\n", " for i in range(n):\n", " print \"=\",\n", "def dDiscount(iQty,dLinePrice):\n", " if(dLinePrice > 1000):\n", " return 0.15\n", " elif(dLinePrice>500 or iQty>10):\n", " return 0.10\n", " else:\n", " return 0\n", "def dPrice(iNo,dUnitPrice):\n", " dTax = 0.25\n", " dLinePr = iNo * dUnitPrice\n", " dDiscPerc = dDiscount(iNo,dLinePr)\n", " return dLinePr * (1-dDiscPerc) * (1+dTax)\n", "\"\"\"" ], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }