{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "

Chapter 16: Marching Towards C++

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 16.1, Page number: 537

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Read and display a string\n", "\n", "import sys\n", "\n", "#Initialize variable\n", "name = raw_input(\"Enter Your Name : \")\n", "\n", "#Result\n", "sys.stdout.write(\"Your name is %s\"%(name))" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Your Name : Amit\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Your name is Amit" ] } ], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 16.2, Page number: 540

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Display the value of a variable using reference variable\n", "\n", "import sys\n", "\n", "#Variable Initialization\n", "qty = 10\n", "qt = qty\n", "#in python, value tagging method is used for storage. so there is no reference concept\n", "\n", "#Result\n", "sys.stdout.write(\"qty\\tqt\\n\")\n", "sys.stdout.write(\"%d\\t%d\\n\"%(qty,qt))\n", "qt = qt + 1\n", "qty = qty + 1\n", "sys.stdout.write(\"%d\\t%d\\n\"%(qty,qt))\n", "qty = qty - 1\n", "qt = qt - 1\n", "sys.stdout.write(\"%d\\t%d\\n\"%(qty,qt))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "qty\tqt\n", "10\t10\n", "11\t11\n", "10\t10\n" ] } ], "prompt_number": 5 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 16.3, Page number: 541

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Program to use scope access operator.\n", "#Display the variaous values of the same variable declared at different scope levels.\n", "\n", "import sys\n", "\n", "#Variable Initialization\n", "a = 10\n", "\n", "def main():\n", " a = 20\n", " sys.stdout.write(\"::a = %d\"%(a))\n", " return\n", "\n", "#There is no scope operator. Intendation is used to define scope/block of statements\n", "main()\n", "sys.stdout.write(\" a = %d\"%(a))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "::a = 20 a = 10" ] } ], "prompt_number": 7 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 16.4, Page number: 542

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Program to read tow integers through the keyboard.\n", "#Declare the variables in C++ style.\n", "\n", "import sys\n", "\n", "#Read Variables\n", "num = int(raw_input(\"Enter Two numbers : \"))\n", "num1 = int(raw_input(\"Enter Two numbers : \"))\n", "#No variable declaration is needed in python\n", "\n", "#Result\n", "sys.stdout.write(\"Entered Numbers are : %d %d\"%(num,num1))\n", " " ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Two numbers : 8\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Two numbers : 9\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Entered Numbers are : 8 9" ] } ], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 16.5, Page number: 543

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Length of a string\n", "\n", "import sys\n", "\n", "#There is no variable declaration is needed in python\n", "#Read string\n", "name = raw_input(\"Enter Your Name : \")\n", "\n", "#Calculation\n", "len1 = len(name)\n", "\n", "#Result\n", "sys.stdout.write(\"The length of the string is : %d\"%(len1))" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Your Name : Santosh\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "The length of the string is : 7" ] } ], "prompt_number": 4 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 16.6, Page number: 544

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Sum of numbers using function\n", "\n", "import sys\n", "\n", "#Function definition\n", "def sum1(x,y):\n", " return x+y\n", "#Forward function definition is not possible in python.\n", "#Otherwise wrap the function within the main() function\n", "\n", "#Variable Initialization\n", "a = 20\n", "b = 2.5\n", "\n", "#Function call\n", "sys.stdout.write(\"Sum = %f\"%(sum1(a,b)))\n", "\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Sum = 22.500000" ] } ], "prompt_number": 15 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 16.7, Page number: 545

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Function with default arguments.\n", "\n", "import sys\n", "\n", "#Function definition\n", "def sum1(j,k=10,l=15,m=20):\n", " return j+k+l+m\n", "\n", "#Variable Initialization\n", "a = 2\n", "b = 3\n", "c = 4\n", "d = 5\n", "\n", "#Result\n", "sys.stdout.write(\"Sum = %d\"%(sum1(a,b,c,d)))\n", "sys.stdout.write(\"\\nSum = %d\"%(sum1(a,b,c)))\n", "sys.stdout.write(\"\\nSum = %d\"%(sum1(a,b)))\n", "sys.stdout.write(\"\\nSum = %d\"%(sum1(a)))\n", "sys.stdout.write(\"\\nSum = %d\"%(sum1(b,c,d)))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Sum = 14\n", "Sum = 29\n", "Sum = 40\n", "Sum = 47\n", "Sum = 32" ] } ], "prompt_number": 16 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 16.8, Page number: 547

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Square of integer and float number using function overloading\n", "\n", "import sys\n", "\n", "#Function definition/ There is no seperate function definition is needed.\n", "def sqr(s):\n", " return s*s\n", "\n", "#Variable iNitialization\n", "a = 15\n", "b = 2.5\n", "\n", "#Result\n", "sys.stdout.write(\"Square = %d\"%(sqr(a)))\n", "sys.stdout.write(\"\\nSquare = %f\"%(sqr(b)))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Square = 225\n", "Square = 6.250000" ] } ], "prompt_number": 17 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 16.9, Page number: 549

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Display private and public member variables of the class.\n", "\n", "import sys\n", "\n", "#Class declaration\n", "class num:\n", " def readdata(self,j,k):\n", " self.x = j\n", " self.y = k\n", " \n", " def display(self):\n", " sys.stdout.write(\"x = %d y = %f\"%(self.x,self.y))\n", "\n", "#Object declaration\n", "j = num()\n", "\n", "#Result\n", "j.z = 'C'\n", "j.readdata(10,10.5)\n", "j.display()\n", "sys.stdout.write(\" z = %c\"%(j.z))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "x = 10 y = 10.500000 z = C" ] } ], "prompt_number": 4 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 16.10, Page number: 550

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Private and public data member of a class.\n", "\n", "import sys\n", "\n", "#Class definition\n", "class player:\n", " def __init__(self,name=None,height=0,weight=0):\n", " self.name = name\n", " self.height = height\n", " self.weight = weight\n", " \n", "#Object declaration\n", "a = player()\n", "\n", "#Variable Initialization\n", "#There is no need/concept for private variables in python.\n", "a.name = \"Sanjay\"\n", "a.height = 5.5\n", "a.weight = 38\n", "\n", "#Result\n", "sys.stdout.write(\"Height : %d\"%(a.height))\n", "sys.stdout.write(\"\\nWeight : %d\"%(a.weight))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Height : 5\n", "Weight : 38" ] } ], "prompt_number": 5 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 16.11, Page number: 551

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Class with member variables and functions. \n", "\n", "import sys\n", "\n", "#Class definition\n", "class player:\n", " def __init__(self,name=None,height=0,weight=0):\n", " self.name = name\n", " self.height = height\n", " self.weight = weight\n", " \n", " def setdata(self):\n", " self.name = raw_input(\"Enter Name Age Height Weight\")\n", " self.age = int(raw_input(\"Enter Name Age Height Weight\"))\n", " self.height = float(raw_input(\"Enter Name Age Height Weight\"))\n", " self.weight = int(raw_input(\"Enter Name Age Height Weight\"))\n", " \n", " def show(self):\n", " sys.stdout.write(\"\\nName : %s\"%(self.name))\n", " sys.stdout.write(\"\\nAge : %d\"%(self.age))\n", " sys.stdout.write(\"\\nHeight : %f\"%(self.height))\n", " sys.stdout.write(\"\\nWeight : %d\"%(self.weight))\n", " \n", "#Object declaration\n", "a = player()\n", "\n", "#Function call\n", "a.setdata()\n", "a.show()" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Name Age Height WeightSanjay\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Name Age Height Weight24\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Name Age Height Weight5.5\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Name Age Height Weight54\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Name : Sanjay\n", "Age : 24\n", "Height : 5.500000\n", "Weight : 54" ] } ], "prompt_number": 5 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 16.12, Page number: 552

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Class with member variables and functions.\n", "\n", "import sys\n", "\n", "#Class definition\n", "class player:\n", " def __init__(self,name=None,height=0,weight=0):\n", " self.name = name\n", " self.height = height\n", " self.weight = weight\n", " \n", " def setdata(self):\n", " self.name = raw_input(\"Enter Name Age Height Weight\")\n", " self.age = int(raw_input(\"Enter Name Age Height Weight\"))\n", " self.height = float(raw_input(\"Enter Name Age Height Weight\"))\n", " self.weight = int(raw_input(\"Enter Name Age Height Weight\"))\n", " \n", " def show(self):\n", " sys.stdout.write(\"\\nName : %s\"%(self.name))\n", " sys.stdout.write(\"\\nAge : %d\"%(self.age))\n", " sys.stdout.write(\"\\nHeight : %f\"%(self.height))\n", " sys.stdout.write(\"\\nWeight : %d\"%(self.weight))\n", "#In python, member functions should be defined inside the class instance\n", "\n", "#Object declaration\n", "a = player()\n", "\n", "#Function call\n", "a.setdata()\n", "a.show() " ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Name Age Height WeightAjay\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Name Age Height Weight24\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Name Age Height Weight5.2\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Name Age Height Weight45\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Name : Ajay\n", "Age : 24\n", "Height : 5.200000\n", "Weight : 45" ] } ], "prompt_number": 6 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 16.13, Page number: 554

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Static data member. \n", "\n", "import sys\n", "\n", "#Class definition\n", "class sumnum:\n", " def __init__(self,num = 0):\n", " self.num = num\n", " \n", " def input1(self,c):\n", " self.num = int(raw_input(\"Enter Number : \"))\n", " c = c + self.num\n", " return c\n", " \n", " def sum1(self,c):\n", " sys.stdout.write(\"The sum of entered numbers is %d\"%(c))\n", " \n", "#Object declaration\n", "a = sumnum()\n", "b = sumnum()\n", "c1 = sumnum()\n", "c = 0\n", "\n", "#Function call\n", "#There is no static variable in python\n", "c = a.input1(c)\n", "c = b.input1(c)\n", "c = c1.input1(c)\n", "\n", "a.sum1(c)" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Number : 4\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Number : 5\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Number : 5\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "The sum of entered numbers is 14" ] } ], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 16.14, Page number: 555

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Static member functions \n", "\n", "import sys\n", "\n", "#Class definition\n", "class bita:\n", " def __initi__(self):\n", " self.c = 0\n", " \n", " @staticmethod \n", " def count(c):\n", " c = c + 1\n", " return c\n", " \n", " @staticmethod\n", " def display(k,c):\n", " k = k + 1\n", " sys.stdout.write(\"\\nCall Number : %d\"%(k))\n", " sys.stdout.write(\"\\nValue of c : %d\"%(c))\n", " return k\n", " \n", "#Object declaration\n", "b = bita()\n", "c1 = bita()\n", "\n", "c = 0\n", "k = 0\n", "\n", "#Function call\n", "k = bita.display(k,c)\n", "c = bita.count(c)\n", "c = b.count(c)\n", "c = c1.count(c)\n", "k = bita.display(k,c)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Call Number : 1\n", "Value of c : 0\n", "Call Number : 2\n", "Value of c : 3" ] } ], "prompt_number": 11 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 16.15, Page number: 557

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Array of objects to maintain records of bank \n", "\n", "import sys\n", "\n", "#Class definition\n", "class bank:\n", " def __init__(self):\n", " self.city = None\n", " self.bank = None\n", " self.branches = 0\n", " self.no_ac = 0\n", " \n", " def input1(self):\n", " self.city = raw_input(\"City Name : \")\n", " self.bank = raw_input(\"Bank Name : \")\n", " self.branches = int(raw_input(\"Total Branches : \"))\n", " self.no_ac = int(raw_input(\"Number of A/c : \"))\n", " \n", " def console(self):\n", " sys.stdout.write(\"\\n\\nCity Name : %s\"%(self.city))\n", " sys.stdout.write(\"\\nBank Name : %s\"%(self.bank))\n", " sys.stdout.write(\"\\nTotal Branches : %d\"%(self.branches))\n", " sys.stdout.write(\"\\nNumber of A/c : %d\"%(self.no_ac))\n", " \n", "#Object declaration\n", "ms = [bank() for i in range(0,2)]\n", "\n", "#Processing\n", "for k in range(0,2):\n", " ms[k].input1()\n", " \n", "for k in range(0,2):\n", " ms[k].console()" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "City Name : Nanded\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Bank Name : SNSB\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Total Branches : 10\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Number of A/c : 7500\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "City Name : Latur\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Bank Name : SNSB\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Total Branches : 8\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Number of A/c : 5400\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "\n", "City Name : Nanded\n", "Bank Name : SNSB\n", "Total Branches : 10\n", "Number of A/c : 7500\n", "\n", "City Name : Latur\n", "Bank Name : SNSB\n", "Total Branches : 8\n", "Number of A/c : 5400" ] } ], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 16.16, Page number: 559

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Accessing private data using non member function\n", "\n", "import sys\n", "\n", "#Class definition\n", "class ac:\n", " def __init__(self):\n", " self.name = None\n", " self.acno = 0\n", " self.bal = 0\n", " \n", " def read(self):\n", " self.name = raw_input(\"Name : \")\n", " self.acno = int(raw_input(\"A/c No :\"))\n", " self.bal = int(raw_input(\"Balance : \"))\n", " \n", "#Non member function\n", "def showbal(a):\n", " sys.stdout.write(\"\\nBalance of A/c no. %d is Rs. %d\"%(a.acno,a.bal))\n", " \n", "#object declaration\n", "k = ac()\n", "\n", "#Function call\n", "k.read()\n", "showbal(k)" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Name : Manoj\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "A/c No :474\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Balance : 40000\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Balance of A/c no. 474 is Rs. 40000" ] } ], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 16.17, Page number: 561

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Friend function in two classes. \n", "\n", "import sys\n", "\n", "#Class definition\n", "class first:\n", " def __init__(self):\n", " self.f = 0\n", " \n", " def getvalue(self):\n", " self.f = int(raw_input(\"Enter a number : \"))\n", " \n", "class second:\n", " def __init__(self):\n", " self.s = 0\n", " \n", " def getvalue(self):\n", " self.s = int(raw_input(\"Enter a number : \"))\n", " \n", " \n", "#Non member function\n", "def sum1(d,t):\n", " sys.stdout.write(\"\\nSum of two numbers : %d\"%(t.f+d.s))\n", " \n", "#Object declaration\n", "a = first()\n", "b = second()\n", "\n", "#Function call\n", "a.getvalue()\n", "b.getvalue()\n", "sum1(b,a)" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter a number : 7\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter a number : 8\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Sum of two numbers : 15" ] } ], "prompt_number": 4 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 16.18, Page number: 562

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#String Replacement\n", "\n", "import sys\n", "\n", "#Class definition\n", "class fandr:\n", " def __init__(self):\n", " self.str1 = None\n", " self.f = ''\n", " self.r = ''\n", " \n", " def accept(self):\n", " #Since string object does not support item assignment, initialize the string as character array\n", " self.str1 = ['P','r','o','g','r','a','_','e','r']\n", " self.f = raw_input(\"Find what (char) \")\n", " self.r = raw_input(\"Replace with (char) \")\n", " \n", " def display(self,d):\n", " sys.stdout.write(\"%c\"%(self.str1[d]))\n", " \n", " def len1(self):\n", " self.l = len(self.str1)\n", " return self.l\n", " \n", " def find(self,i):\n", " if self.str1[i] == self.f:\n", " self.replace(i)\n", " \n", " def replace(self,k):\n", " self.str1[k] = self.r\n", " \n", "#Object declaration\n", "b = fandr()\n", "\n", "#function call\n", "b.accept()\n", "l = b.len1()\n", "sys.stdout.write(\"\\nReplaced text : \")\n", "\n", "for i in range(0,l):\n", " b.find(i)\n", " b.display(i)" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Find what (char) _\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Replace with (char) m\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Replaced text : Programer" ] } ], "prompt_number": 5 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 16.19, Page number: 564

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Largest out of ten numbers.\n", "\n", "import sys\n", "\n", "#Class definition\n", "class num:\n", " def __init__(self):\n", " self.number = [0 for i in range(0,10)]\n", " \n", " def input1(self,i):\n", " self.number[i] = int(raw_input(\"Enter Number (%d) : \"%(i+1)))\n", " return self.number[i]\n", " \n", " def large(self,s,m):\n", " if self.number[s] == m:\n", " sys.stdout.write(\"\\nLargest number : %d\"%(self.number[s]))\n", " return 0\n", " else:\n", " return 1\n", " \n", "#Object declaration\n", "b = num()\n", "\n", "sum1 = 0\n", "c = 1\n", "\n", "#Sum\n", "for i in range(0,10):\n", " sum1 = sum1 + b.input1(i)\n", " \n", "#largest\n", "for k in range(sum1,0,-1):\n", " for i in range(0,10):\n", " if c == 1:\n", " c = b.large(i,k)\n", " else:\n", " break" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Number (1) : 125\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Number (2) : 654\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Number (3) : 246\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Number (4) : 945\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Number (5) : 258\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Number (6) : 159\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Number (7) : 845\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Number (8) : 940\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Number (9) : 944\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Number (10) : 485\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Largest number : 945" ] } ], "prompt_number": 6 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 16.20, Page number: 567

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Constructor to initialize the class member variables \n", "\n", "import sys\n", "\n", "#Class definition\n", "class num:\n", " #Constructor\n", " def __init__(self):\n", " sys.stdout.write(\"\\nConstructor called\")\n", " self.x = 5\n", " self.a = 0\n", " self.b = 1\n", " self.c = 2\n", " \n", " def show(self):\n", " sys.stdout.write(\"\\nx = %d a = %d b = %d c = %d\"%(self.x,self.a,self.b,self.c))\n", " \n", "#Object declaration\n", "x = num()\n", "\n", "#Function call\n", "x.show()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Constructor called\n", "x = 5 a = 0 b = 1 c = 2" ] } ], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 16.21, Page number: 569

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Constructor with arguments \n", "\n", "import sys\n", "\n", "#Class definition\n", "class num:\n", " #Constructor\n", " def __init__(self,m,j,k):\n", " self.a = m\n", " self.b = j\n", " self.c = k\n", " \n", " def show(self):\n", " sys.stdout.write(\"\\na = %d b = %d c = %d\"%(self.a,self.b,self.c))\n", " \n", " \n", "#Object declaration\n", "x = num(4,5,7)\n", "y = num(1,2,8)\n", "\n", "#Function call\n", "x.show()\n", "y.show()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "a = 4 b = 5 c = 7\n", "a = 1 b = 2 c = 8" ] } ], "prompt_number": 4 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 16.22, Page number: 571

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Constructor overloading\n", "\n", "import sys\n", "\n", "#Class definition\n", "class num:\n", " def __init__(self,m = None,j = None,k = None):\n", " if m and j and k is not None:\n", " sys.stdout.write(\"Constructor with three arguments\")\n", " self.a = m\n", " self.b = j\n", " self.c = k\n", " \n", " else:\n", " if m and j is not None:\n", " sys.stdout.write(\"Constructor with two arguments\")\n", " self.a = m\n", " self.b = j\n", " self.c = ''\n", " else:\n", " if m == j == k == None:\n", " sys.stdout.write(\"Constructor without arguments\")\n", " self.a = 0\n", " self.b = 0\n", " self.c = ''\n", " \n", " \n", " def show(self): \n", " sys.stdout.write(\"\\na = %d b = %f c = \"%(self.a,self.b))\n", " print self.c\n", " \n", " \n", "#Object declaration\n", "\n", "x = num(4,5.5,'A')\n", "x.show()\n", "\n", "y = num(1,2.2)\n", "y.show()\n", "\n", "z = num()\n", "z.show()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Constructor with three arguments\n", "a = 4 b = 5.500000 c = A\n", "Constructor with two arguments\n", "a = 1 b = 2.200000 c = \n", "Constructor without arguments\n", "a = 0 b = 0.000000 c = \n" ] } ], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 16.23, Page number: 573

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Default arguments in constructor. \n", "\n", "import sys\n", "\n", "#Class definition\n", "class power:\n", " def __init__(self,n = 9,p = 3):\n", " self.num = n\n", " self.power = p\n", " self.ans = pow(n,p)\n", " \n", " def show(self):\n", " sys.stdout.write(\"\\n%d raise to %d is %d\"%(self.num,self.power,self.ans))\n", " \n", " \n", "#Object declaration\n", "p1 = power()\n", "p2 = power(5)\n", "\n", "#Result\n", "p1.show()\n", "p2.show()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "9 raise to 3 is 729\n", "5 raise to 3 is 125" ] } ], "prompt_number": 4 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 16.24, Page number: 575

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Object with reference to constructor. \n", "\n", "import sys\n", "\n", "#Class definition\n", "class num:\n", " #There is no overloading concept in python\n", " def __init__(self,k=None,j=None):\n", " if k is not None:\n", " self.n = k\n", " return\n", " else:\n", " if j is not None:\n", " self.n = j.n\n", " \n", " def show(self):\n", " sys.stdout.write(\"%d\"%(self.n))\n", " \n", "#Object creation\n", "J = num(50)\n", "K = num(J.n)\n", "L = num()\n", "L = J\n", "M = num()\n", "M = J\n", "\n", "#Result\n", "sys.stdout.write(\"\\nObject J Value of n : \")\n", "J.show()\n", "sys.stdout.write(\"\\nObject K Value of n : \")\n", "K.show()\n", "sys.stdout.write(\"\\nObject L Value of n : \")\n", "L.show()\n", "sys.stdout.write(\"\\nObject M Value of n : \")\n", "M.show()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Object J Value of n : 50\n", "Object K Value of n : 50\n", "Object L Value of n : 50\n", "Object M Value of n : 50" ] } ], "prompt_number": 11 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 16.25, Page number: 576

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Destructors\n", "\n", "import sys\n", "\n", "c = 0\n", "\n", "#Class definition\n", "class num:\n", " def __init__(self,c):\n", " c = c + 1\n", " sys.stdout.write(\"\\nObject Created : Object(%d)\"%(c))\n", " \n", " \n", " def __exit__(self, type, value, traceback,c):\n", " self.package_obj.cleanup()\n", " sys.stdout.write(\"\\nObject Released : Object(%d)\"%(c))\n", " c = c - 1\n", " \n", " \n", "#Object creation\n", "sys.stdout.write(\"\\nIn main()\\n\")\n", "a = num(c)\n", "b = num(c)\n", "\n", "sys.stdout.write(\"\\nIn Block A()\\n\")\n", "c = num(c)\n", "\n", "sys.stdout.write(\"\\n\\nAgain in main()\\n\")\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "In main()\n", "\n", "Object Created : Object(1)\n", "Object Created : Object(1)\n", "In Block A()\n", "\n", "Object Created : Object(1)\n", "\n", "Again in main()\n" ] } ], "prompt_number": 15 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 16.26, Page number: 579

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Overload unary ++ operator\n", "\n", "import sys\n", "\n", "#Class definition\n", "class num:\n", " def input1(self):\n", " self.a = int(raw_input(\"Enter Values for a,b,c and d : \"))\n", " self.b = int(raw_input(\"Enter Values for a,b,c and d : \"))\n", " self.c = int(raw_input(\"Enter Values for a,b,c and d : \"))\n", " self.d = int(raw_input(\"Enter Values for a,b,c and d : \"))\n", " \n", " def show(self):\n", " sys.stdout.write(\"\\nA = %d B = %d C = %d D = %d\"%(self.a,self.b,self.c,self.d))\n", " \n", " def operatorplusplus(self):\n", " self.a = self.a + 1\n", " self.b = self.b + 1\n", " self.c = self.c + 1\n", " self.d = self.d + 1\n", " \n", "#Object creation\n", "X = num()\n", "\n", "X.input1()\n", "sys.stdout.write(\"\\nBefore Overloading X : \")\n", "X.show()\n", "\n", "#There is no operator overloading in python\n", "X.operatorplusplus()\n", "sys.stdout.write(\"\\nAfter Overloading X : \")\n", "X.show()" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Values for a,b,c and d : 4\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Values for a,b,c and d : 5\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Values for a,b,c and d : 8\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Values for a,b,c and d : 9\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Before Overloading X : \n", "A = 4 B = 5 C = 8 D = 9\n", "After Overloading X : \n", "A = 5 B = 6 C = 9 D = 10" ] } ], "prompt_number": 7 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 16.27, Page number: 581

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Overload + binary operator\n", "\n", "import sys\n", "\n", "#Class definition\n", "class num:\n", " def input1(self):\n", " self.a = int(raw_input(\"Enter Values for a,b,c and d : \"))\n", " self.b = int(raw_input(\"Enter Values for a,b,c and d : \"))\n", " self.c = int(raw_input(\"Enter Values for a,b,c and d : \"))\n", " self.d = int(raw_input(\"Enter Values for a,b,c and d : \"))\n", " \n", " def show(self):\n", " sys.stdout.write(\"A = %d B = %d C = %d D = %d\"%(self.a,self.b,self.c,self.d))\n", " \n", " def operatorplus(self,t):\n", " tmp = num()\n", " tmp.a = self.a + t.a\n", " tmp.b = self.b + t.b\n", " tmp.c = self.c + t.c\n", " tmp.d = self.d + t.d\n", " return tmp\n", " \n", "#Object creation\n", "X = num()\n", "Y = num()\n", "Z = num()\n", "\n", "#Result\n", "sys.stdout.write(\"\\nObject X \")\n", "X.input1()\n", "sys.stdout.write(\"\\nObject Y \")\n", "Y.input1()\n", "\n", "Z = X.operatorplus(Y)\n", "\n", "sys.stdout.write(\"\\nX : \")\n", "X.show()\n", "sys.stdout.write(\"\\nY : \")\n", "Y.show()\n", "sys.stdout.write(\"\\nZ : \")\n", "Z.show()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Object X " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Values for a,b,c and d : 1\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Values for a,b,c and d : 4\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Values for a,b,c and d : 2\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Values for a,b,c and d : 1\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Object Y " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Values for a,b,c and d : 2\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Values for a,b,c and d : 5\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Values for a,b,c and d : 4\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Values for a,b,c and d : 2\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "X : A = 1 B = 4 C = 2 D = 1\n", "Y : A = 2 B = 5 C = 4 D = 2\n", "Z : A = 3 B = 9 C = 6 D = 3" ] } ], "prompt_number": 8 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 16.28, Page number: 583

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Multiplication using an integer and object. \n", "\n", "import sys\n", "\n", "#Class definition\n", "class num:\n", " def input1(self):\n", " self.a = int(raw_input(\"Enter Values for a,b,c and d : \"))\n", " self.b = int(raw_input(\"Enter Values for a,b,c and d : \"))\n", " self.c = int(raw_input(\"Enter Values for a,b,c and d : \"))\n", " self.d = int(raw_input(\"Enter Values for a,b,c and d : \"))\n", " \n", " def show(self):\n", " sys.stdout.write(\"A = %d B = %d C = %d D = %d\"%(self.a,self.b,self.c,self.d))\n", " \n", "def operatormul(a,t):\n", " tmp = num()\n", " tmp.a = a * t.a\n", " tmp.b = a * t.b\n", " tmp.c = a * t.c\n", " tmp.d = a * t.d\n", " return tmp\n", "\n", "#Result\n", "X = num()\n", "Z = num()\n", "\n", "sys.stdout.write(\"\\nObject X \")\n", "X.input1()\n", "\n", "Z = operatormul(3,X)\n", "sys.stdout.write(\"\\nX : \")\n", "X.show()\n", "sys.stdout.write(\"\\nZ : \")\n", "Z.show()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Object X " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Values for a,b,c and d : 1\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Values for a,b,c and d : 2\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Values for a,b,c and d : 2\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Values for a,b,c and d : 3\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "X : A = 1 B = 2 C = 2 D = 3\n", "Z : A = 3 B = 6 C = 6 D = 9" ] } ], "prompt_number": 9 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 16.29, Page number: 585

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Inheritance\n", "\n", "import sys\n", "\n", "#Class declaration\n", "class one:\n", " def __init__(self):\n", " self.a = 0\n", " self.b = 0\n", " \n", "class two(one):\n", " def __init__(self):\n", " one.__init__(self)\n", " self.c = 0\n", " self.d = 0\n", " \n", " def input1(self):\n", " self.a = int(raw_input(\"Enter values for a,b,c,d : \"))\n", " self.b = int(raw_input(\"Enter values for a,b,c,d : \"))\n", " self.c = int(raw_input(\"Enter values for a,b,c,d : \"))\n", " self.d = int(raw_input(\"Enter values for a,b,c,d : \"))\n", " \n", " def display(self):\n", " sys.stdout.write(\"\\na = %d b = %d c = %d d = %d\"%(self.a,self.b,self.c,self.d))\n", " \n", "#Object declaration\n", "a = two()\n", "\n", "#Result\n", "a.input1()\n", "a.display()\n", " " ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter values for a,b,c,d : 2\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter values for a,b,c,d : 5\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter values for a,b,c,d : 4\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter values for a,b,c,d : 7\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "a = 2 b = 5 c = 4 d = 7" ] } ], "prompt_number": 10 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 16.30, Page number: 586

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Member function using pointer\n", "\n", "import sys\n", "\n", "#class declaration\n", "class super1:\n", " def display(self):\n", " sys.stdout.write(\"\\nIn function display() class super\")\n", " \n", " def show(self):\n", " sys.stdout.write(\"\\nIn function show() class super\")\n", " \n", "class sub(super1):\n", " def display(self):\n", " sys.stdout.write(\"\\nIn function display() class sub\")\n", " \n", " def show(self):\n", " sys.stdout.write(\"\\nIn function show() class sub\")\n", " \n", "#Object declaration\n", "S = super1()\n", "A = sub()\n", "\n", "#There is no pointer concept in python\n", "S.display()\n", "S.show()\n", "\n", "sys.stdout.write(\"\\n\\nNow Pointer point points to derived class sub\\n\")\n", "A.display()\n", "A.show()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "In function display() class super\n", "In function show() class super\n", "\n", "Now Pointer point points to derived class sub\n", "\n", "In function display() class sub\n", "In function show() class sub" ] } ], "prompt_number": 4 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }