From fba055ce5aa0955e22bac2413c33493b10ae6532 Mon Sep 17 00:00:00 2001 From: hardythe1 Date: Tue, 5 May 2015 14:21:39 +0530 Subject: add books --- .../Chapter_11_Inheritance_1.ipynb | 2636 +++++++++++++++++++ .../Chapter_11_Inheritance_2.ipynb | 2661 +++++++++++++++++++ .../Chapter_11_Inheritance_3.ipynb | 2669 ++++++++++++++++++++ sample_notebooks/Abu BakkerSiddik/Functions_.ipynb | 1441 +++++++++++ sample_notebooks/AkshayShende/chapter2.ipynb | 65 + .../Chapter_6_Objects_and_Classes_1.ipynb | 934 +++++++ sample_notebooks/ApurvaBhushan/Chapter_3_1.ipynb | 212 ++ sample_notebooks/ChaitanyaPotti/Chapter2.ipynb | 286 +++ sample_notebooks/Dileep KumarShakya/chapter1.ipynb | 1 + .../Dileep KumarShakya/chapter1_1.ipynb | 1 + sample_notebooks/HeminChheda/chapter1.ipynb | 281 +++ ...nciples_of_electronic_Instrumentation_Ch1.ipynb | 406 +++ .../MohdAsif/Chapter2_-_Measurement_Errors.ipynb | 1549 ++++++++++++ ..._BY_U.A_BAKSHI,_A.V_BAKSHI,_K.A_BAKSHI_10.ipynb | 462 ++++ ..._BY_U.A_BAKSHI,_A.V_BAKSHI,_K.A_BAKSHI_11.ipynb | 462 ++++ ..._BY_U.A_BAKSHI,_A.V_BAKSHI,_K.A_BAKSHI_12.ipynb | 462 ++++ ...N_BY_U.A_BAKSHI,_A.V_BAKSHI,_K.A_BAKSHI_5.ipynb | 462 ++++ ...N_BY_U.A_BAKSHI,_A.V_BAKSHI,_K.A_BAKSHI_6.ipynb | 462 ++++ ...N_BY_U.A_BAKSHI,_A.V_BAKSHI,_K.A_BAKSHI_7.ipynb | 462 ++++ ...N_BY_U.A_BAKSHI,_A.V_BAKSHI,_K.A_BAKSHI_8.ipynb | 462 ++++ ...N_BY_U.A_BAKSHI,_A.V_BAKSHI,_K.A_BAKSHI_9.ipynb | 462 ++++ sample_notebooks/NirenNegandhi/ch2_1.ipynb | 505 ++++ .../NitamoniDas/Chapter_8_Data_Abstraction.ipynb | 809 ++++++ ...rough_Classes_and_User-Defined_Data_Types.ipynb | 809 ++++++ ...rough_Classes_and_User_Defined_Data_Types.ipynb | 809 ++++++ ...ugh_Classes_and_User_Defined_Data_Types_1.ipynb | 809 ++++++ ...ugh_Classes_and_User_Defined_Data_Types_2.ipynb | 809 ++++++ ...ugh_Classes_and_User_Defined_Data_Types_3.ipynb | 809 ++++++ ...ugh_Classes_and_User_Defined_Data_Types_4.ipynb | 809 ++++++ ...ugh_Classes_and_User_Defined_Data_Types_5.ipynb | 809 ++++++ ...ugh_Classes_and_User_Defined_Data_Types_6.ipynb | 809 ++++++ ...hapter_8_Data_Abstraction_through_Classes.ipynb | 809 ++++++ ...h_Classes_and_User-Defined_Data_Types_(1).ipynb | 809 ++++++ ...gh_Classes_and_User-Defined_Data_Types_10.ipynb | 809 ++++++ ...ugh_Classes_and_User-Defined_Data_Types_2.ipynb | 809 ++++++ ...ugh_Classes_and_User-Defined_Data_Types_3.ipynb | 809 ++++++ ...ugh_Classes_and_User-Defined_Data_Types_4.ipynb | 809 ++++++ ...ugh_Classes_and_User-Defined_Data_Types_5.ipynb | 809 ++++++ ...ugh_Classes_and_User-Defined_Data_Types_6.ipynb | 809 ++++++ ...ugh_Classes_and_User-Defined_Data_Types_7.ipynb | 809 ++++++ ...ugh_Classes_and_User-Defined_Data_Types_8.ipynb | 809 ++++++ ...ugh_Classes_and_User-Defined_Data_Types_9.ipynb | 809 ++++++ .../Prashanth Kumar Immady/Chapter_1.ipynb | 307 +++ .../Prashanth Kumar Immady/Chapter_1_1.ipynb | 307 +++ .../Prashanth Kumar Immady/Chapter_1_2.ipynb | 307 +++ sample_notebooks/SPANDANAARROJU/Chapter4.ipynb | 553 ++++ sample_notebooks/SUMITRATHI/chapterno1.ipynb | 126 + ..._11-_Object_Initialization_and_Clean-Up_2.ipynb | 1779 +++++++++++++ sample_notebooks/Tarun KumarDas/Chapter9_1.ipynb | 227 ++ sample_notebooks/Tarun KumarDas/Chapter9_2.ipynb | 227 ++ sample_notebooks/Tarun KumarDas/Chapter9_3.ipynb | 227 ++ sample_notebooks/Tarun KumarDas/Chapter9_4.ipynb | 227 ++ sample_notebooks/Tarun KumarDas/Chapter9_5.ipynb | 227 ++ sample_notebooks/VidyaSri/Chapter_1.ipynb | 389 +++ sample_notebooks/VikasPrasad/Chapter_01.ipynb | 170 ++ sample_notebooks/VikasPrasad/Chapter_01_1.ipynb | 170 ++ sample_notebooks/VikasPrasad/Chapter_01_2.ipynb | 170 ++ sample_notebooks/VikasPrasad/chapter1_2.ipynb | 165 ++ sample_notebooks/VikasPrasad/chapter1_3.ipynb | 165 ++ sample_notebooks/VikasPrasad/chapter1_4.ipynb | 165 ++ sample_notebooks/VikasPrasad/chapter1_5.ipynb | 165 ++ sample_notebooks/VikasPrasad/chapter1_6.ipynb | 165 ++ sample_notebooks/VikasPrasad/chapter1_7.ipynb | 165 ++ sample_notebooks/VikasPrasad/chapter1_8.ipynb | 170 ++ sample_notebooks/VikasPrasad/chapter1_9.ipynb | 170 ++ sample_notebooks/Vishwajith VRao/Chapter2.ipynb | 350 +++ sample_notebooks/Vishwajith VRao/Chapter2_1.ipynb | 348 +++ sample_notebooks/YogeshPatil/Chapter_11_1.ipynb | 494 ++++ sample_notebooks/abhishekchauhan/chapter13.ipynb | 109 + sample_notebooks/nitinkumar/Chapter4.ipynb | 613 +++++ sample_notebooks/sriragap/CHAPTER_2.ipynb | 294 +++ sample_notebooks/sriragap/CHAPTER_2_1.ipynb | 294 +++ 72 files changed, 42768 insertions(+) create mode 100755 sample_notebooks/AJEET KUMARSINGH/Chapter_11_Inheritance_1.ipynb create mode 100755 sample_notebooks/AJEET KUMARSINGH/Chapter_11_Inheritance_2.ipynb create mode 100755 sample_notebooks/AJEET KUMARSINGH/Chapter_11_Inheritance_3.ipynb create mode 100755 sample_notebooks/Abu BakkerSiddik/Functions_.ipynb create mode 100755 sample_notebooks/AkshayShende/chapter2.ipynb create mode 100755 sample_notebooks/Aman KumarJain/Chapter_6_Objects_and_Classes_1.ipynb create mode 100755 sample_notebooks/ApurvaBhushan/Chapter_3_1.ipynb create mode 100755 sample_notebooks/ChaitanyaPotti/Chapter2.ipynb create mode 100755 sample_notebooks/Dileep KumarShakya/chapter1.ipynb create mode 100755 sample_notebooks/Dileep KumarShakya/chapter1_1.ipynb create mode 100755 sample_notebooks/HeminChheda/chapter1.ipynb create mode 100755 sample_notebooks/LaxmanSole/Pinciples_of_electronic_Instrumentation_Ch1.ipynb create mode 100755 sample_notebooks/MohdAsif/Chapter2_-_Measurement_Errors.ipynb create mode 100755 sample_notebooks/NIKHILESH DAMLE/ANTENNAS_AND_WAVE_PROPAGATION_BY_U.A_BAKSHI,_A.V_BAKSHI,_K.A_BAKSHI_10.ipynb create mode 100755 sample_notebooks/NIKHILESH DAMLE/ANTENNAS_AND_WAVE_PROPAGATION_BY_U.A_BAKSHI,_A.V_BAKSHI,_K.A_BAKSHI_11.ipynb create mode 100755 sample_notebooks/NIKHILESH DAMLE/ANTENNAS_AND_WAVE_PROPAGATION_BY_U.A_BAKSHI,_A.V_BAKSHI,_K.A_BAKSHI_12.ipynb create mode 100755 sample_notebooks/NIKHILESH DAMLE/ANTENNAS_AND_WAVE_PROPAGATION_BY_U.A_BAKSHI,_A.V_BAKSHI,_K.A_BAKSHI_5.ipynb create mode 100755 sample_notebooks/NIKHILESH DAMLE/ANTENNAS_AND_WAVE_PROPAGATION_BY_U.A_BAKSHI,_A.V_BAKSHI,_K.A_BAKSHI_6.ipynb create mode 100755 sample_notebooks/NIKHILESH DAMLE/ANTENNAS_AND_WAVE_PROPAGATION_BY_U.A_BAKSHI,_A.V_BAKSHI,_K.A_BAKSHI_7.ipynb create mode 100755 sample_notebooks/NIKHILESH DAMLE/ANTENNAS_AND_WAVE_PROPAGATION_BY_U.A_BAKSHI,_A.V_BAKSHI,_K.A_BAKSHI_8.ipynb create mode 100755 sample_notebooks/NIKHILESH DAMLE/ANTENNAS_AND_WAVE_PROPAGATION_BY_U.A_BAKSHI,_A.V_BAKSHI,_K.A_BAKSHI_9.ipynb create mode 100755 sample_notebooks/NirenNegandhi/ch2_1.ipynb create mode 100755 sample_notebooks/NitamoniDas/Chapter_8_Data_Abstraction.ipynb create mode 100755 sample_notebooks/NitamoniDas/Chapter_8_Data_Abstraction_through_Classes_and_User-Defined_Data_Types.ipynb create mode 100755 sample_notebooks/NitamoniDas/Chapter_8_Data_Abstraction_through_Classes_and_User_Defined_Data_Types.ipynb create mode 100755 sample_notebooks/NitamoniDas/Chapter_8_Data_Abstraction_through_Classes_and_User_Defined_Data_Types_1.ipynb create mode 100755 sample_notebooks/NitamoniDas/Chapter_8_Data_Abstraction_through_Classes_and_User_Defined_Data_Types_2.ipynb create mode 100755 sample_notebooks/NitamoniDas/Chapter_8_Data_Abstraction_through_Classes_and_User_Defined_Data_Types_3.ipynb create mode 100755 sample_notebooks/NitamoniDas/Chapter_8_Data_Abstraction_through_Classes_and_User_Defined_Data_Types_4.ipynb create mode 100755 sample_notebooks/NitamoniDas/Chapter_8_Data_Abstraction_through_Classes_and_User_Defined_Data_Types_5.ipynb create mode 100755 sample_notebooks/NitamoniDas/Chapter_8_Data_Abstraction_through_Classes_and_User_Defined_Data_Types_6.ipynb create mode 100755 sample_notebooks/NitamoniDas/chapter_8_Data_Abstraction_through_Classes.ipynb create mode 100755 sample_notebooks/NitamoniDas/chapter_8_Data_Abstraction_through_Classes_and_User-Defined_Data_Types_(1).ipynb create mode 100755 sample_notebooks/NitamoniDas/chapter_8_Data_Abstraction_through_Classes_and_User-Defined_Data_Types_10.ipynb create mode 100755 sample_notebooks/NitamoniDas/chapter_8_Data_Abstraction_through_Classes_and_User-Defined_Data_Types_2.ipynb create mode 100755 sample_notebooks/NitamoniDas/chapter_8_Data_Abstraction_through_Classes_and_User-Defined_Data_Types_3.ipynb create mode 100755 sample_notebooks/NitamoniDas/chapter_8_Data_Abstraction_through_Classes_and_User-Defined_Data_Types_4.ipynb create mode 100755 sample_notebooks/NitamoniDas/chapter_8_Data_Abstraction_through_Classes_and_User-Defined_Data_Types_5.ipynb create mode 100755 sample_notebooks/NitamoniDas/chapter_8_Data_Abstraction_through_Classes_and_User-Defined_Data_Types_6.ipynb create mode 100755 sample_notebooks/NitamoniDas/chapter_8_Data_Abstraction_through_Classes_and_User-Defined_Data_Types_7.ipynb create mode 100755 sample_notebooks/NitamoniDas/chapter_8_Data_Abstraction_through_Classes_and_User-Defined_Data_Types_8.ipynb create mode 100755 sample_notebooks/NitamoniDas/chapter_8_Data_Abstraction_through_Classes_and_User-Defined_Data_Types_9.ipynb create mode 100755 sample_notebooks/Prashanth Kumar Immady/Chapter_1.ipynb create mode 100755 sample_notebooks/Prashanth Kumar Immady/Chapter_1_1.ipynb create mode 100755 sample_notebooks/Prashanth Kumar Immady/Chapter_1_2.ipynb create mode 100755 sample_notebooks/SPANDANAARROJU/Chapter4.ipynb create mode 100755 sample_notebooks/SUMITRATHI/chapterno1.ipynb create mode 100755 sample_notebooks/SrutiGoyal/Chapter_11-_Object_Initialization_and_Clean-Up_2.ipynb create mode 100755 sample_notebooks/Tarun KumarDas/Chapter9_1.ipynb create mode 100755 sample_notebooks/Tarun KumarDas/Chapter9_2.ipynb create mode 100755 sample_notebooks/Tarun KumarDas/Chapter9_3.ipynb create mode 100755 sample_notebooks/Tarun KumarDas/Chapter9_4.ipynb create mode 100755 sample_notebooks/Tarun KumarDas/Chapter9_5.ipynb create mode 100755 sample_notebooks/VidyaSri/Chapter_1.ipynb create mode 100755 sample_notebooks/VikasPrasad/Chapter_01.ipynb create mode 100755 sample_notebooks/VikasPrasad/Chapter_01_1.ipynb create mode 100755 sample_notebooks/VikasPrasad/Chapter_01_2.ipynb create mode 100755 sample_notebooks/VikasPrasad/chapter1_2.ipynb create mode 100755 sample_notebooks/VikasPrasad/chapter1_3.ipynb create mode 100755 sample_notebooks/VikasPrasad/chapter1_4.ipynb create mode 100755 sample_notebooks/VikasPrasad/chapter1_5.ipynb create mode 100755 sample_notebooks/VikasPrasad/chapter1_6.ipynb create mode 100755 sample_notebooks/VikasPrasad/chapter1_7.ipynb create mode 100755 sample_notebooks/VikasPrasad/chapter1_8.ipynb create mode 100755 sample_notebooks/VikasPrasad/chapter1_9.ipynb create mode 100755 sample_notebooks/Vishwajith VRao/Chapter2.ipynb create mode 100755 sample_notebooks/Vishwajith VRao/Chapter2_1.ipynb create mode 100755 sample_notebooks/YogeshPatil/Chapter_11_1.ipynb create mode 100755 sample_notebooks/abhishekchauhan/chapter13.ipynb create mode 100755 sample_notebooks/nitinkumar/Chapter4.ipynb create mode 100755 sample_notebooks/sriragap/CHAPTER_2.ipynb create mode 100755 sample_notebooks/sriragap/CHAPTER_2_1.ipynb (limited to 'sample_notebooks') diff --git a/sample_notebooks/AJEET KUMARSINGH/Chapter_11_Inheritance_1.ipynb b/sample_notebooks/AJEET KUMARSINGH/Chapter_11_Inheritance_1.ipynb new file mode 100755 index 00000000..4f69b243 --- /dev/null +++ b/sample_notebooks/AJEET KUMARSINGH/Chapter_11_Inheritance_1.ipynb @@ -0,0 +1,2636 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:e85379c4218575d4b069259557cffbbc2d0259e3ba5d0e030c11dd77aae5e38d" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.1, Page Number:444" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#A simple classA having a public data member x\n", + "class A:\n", + " def __init__(self):\n", + " self.x=None\n", + "\n", + "#A simple classA having a public data member y \n", + "class B(A): #derived class\n", + " def __init__(self):\n", + " self.y=None\n", + " \n", + "b=B() #create a instance b of Derived class B\n", + "b.x=20\n", + "b.y=30\n", + "\n", + "print 'member of A:',b.x\n", + "print 'Member of B:',b.y" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "member of A: 20\n", + "Member of B: 30\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.2, Page Number:445" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class A:\n", + " def __init__(self): #class A having x as a private data member\n", + " self.__x=20\n", + " \n", + " def showx(self):\n", + " print \"x=\",self.__x\n", + " \n", + " \n", + "class B(A): #Derived class\n", + " def __init__(self):\n", + " self.y=30 #class B having y as a public data member\n", + " \n", + " def show(self):\n", + " a=A()\n", + " a.showx()\n", + " print \"y=\",self.y\n", + " \n", + " \n", + "b=B() #declaration of object\n", + " #class the method of derived class object by a derived class instance\n", + "b.show()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "x= 20\n", + "y= 30\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.3, Page Number:447" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#base class\n", + "class A:\n", + " def __init__(self):\n", + " self.x=None #x is a public member\n", + " \n", + " \n", + "#derived class\n", + "class B(A):\n", + " def __init__(self):\n", + " self.y=40 \n", + " A.__x=20 #since it is privately inherites base class ,x become private member of it\n", + " \n", + " def show(self):\n", + " print \"x=\",A.__x\n", + " print \"y=\",self.y\n", + " \n", + "b=B()\n", + "b.show()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "x= 20\n", + "y= 40\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.4, Page Number:448" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class A:\n", + " def __init__(self):\n", + " self.__x=20 #x is a privet member of it\n", + " \n", + " def showx(self): \n", + " print \"x=\",self.__x\n", + " \n", + " \n", + "class B(A):\n", + " def __init__(self):\n", + " self.y=40 #y is a public member of it\n", + " \n", + " def show(self):\n", + " a=A()\n", + " a.showx() #call the base class method\n", + " print \"y=\",self.y\n", + " \n", + " \n", + "b=B()\n", + "b.show()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "x= 20\n", + "y= 40\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.5, Page Number:449" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class A:\n", + " def __init__(self):\n", + " self._x=None #x is a protected member of the base class\n", + " \n", + " \n", + "class B(A): #private inheritance,x become a private member of the derived class\n", + " def __init__(self):\n", + " self.y=40\n", + " self.__x=30\n", + " \n", + " \n", + " def show(self):\n", + " print \"x=\",self.__x\n", + " print \"y=\",self.y\n", + " \n", + " \n", + "b=B()\n", + "b.show()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "x= 30\n", + "y= 40\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.6, Page Number:456" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class ABC: #Base class\n", + " def __init__(self):\n", + " self._name=None #these 2 are protected data member\n", + " self._age=None\n", + " \n", + "class abc(ABC): #Derived class ,Public derivation\n", + " def __init__(self):\n", + " self.height=None\n", + " self.weight=None\n", + " \n", + " def getdata(self):\n", + " \n", + " self.name=raw_input(\"Enter a name: \") #take inputes to all the data members \n", + " self.age=raw_input(\"Enter a age: \") \n", + " self._height=raw_input(\"Enter a Height: \") \n", + " self._weight=raw_input(\"Enter a Weight: \") \n", + " \n", + " def show(self): #display the value of data members\n", + " print 'Name:',self.name \n", + " print 'Age:',self.age,\"years\"\n", + " print 'Height:',self._height,\"Feets\"\n", + " print 'Weight:',self._weight,\"kg.\"\n", + " \n", + " \n", + "x=abc()\n", + "x.getdata()\n", + "x.show()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter a name: Santosh\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter a age: 24\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter a Height: 4.5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter a Weight: 50\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Name: Santosh\n", + "Age: 24 years\n", + "Height: 4.5 Feets\n", + "Weight: 50 kg.\n" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.7, Page Number:458" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class A1: #super Base class,have 2 protected data members\n", + " def __init__(self):\n", + " self._name=None\n", + " self._age=None\n", + "\n", + " \n", + "class A2(A1): #Public derivation\n", + " def __init(self):\n", + " self._height=None\n", + " self._weight=None\n", + "\n", + "class A3(A2): #public Derivation\n", + " def __init__(self):\n", + " self._sex=None\n", + " \n", + " \n", + " def get(self): #get input \n", + " self._name=raw_input(\"Name: \")\n", + " self._age=raw_input(\"Age: \")\n", + " self._sex=raw_input(\"Sex: \")\n", + " \n", + " self._height=raw_input(\"Height: \")\n", + " self._weight=raw_input(\"Weight: \")\n", + " \n", + " def show(self): #Display values of all the data members\n", + " print \"Name:\",self._name\n", + " print \"Age:\",self._age ,\"years\"\n", + " print \"Sex:\",self._sex\n", + " print \"Height:\",self._height ,\"Feet\"\n", + " print \"Weight:\",self._weight ,\"Kg.\"\n", + " \n", + "\n", + "x=A3()\n", + "x.get()\n", + "x.show()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Name: Balaji\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Age: 26\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Sex: M\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Height: 4\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Weight: 49.5\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Name: Balaji\n", + "Age: 26 years\n", + "Sex: M\n", + "Height: 4 Feet\n", + "Weight: 49.5 Kg.\n" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.8, Page Number:459" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Example of multiple Inheritance\n", + "\n", + "class A:\n", + " def __init__(self):\n", + " self._a=None\n", + " \n", + "class B:\n", + " def __init__(self):\n", + " self._b=None\n", + " \n", + " \n", + "class C:\n", + " def __init__(self):\n", + " self._c=None\n", + " \n", + "class D:\n", + " def __init__(self):\n", + " self._d=None\n", + " \n", + "class E(A,B,C,D): #inherites all the base classes publically\n", + " def __init__(self):\n", + " self.e=None\n", + " \n", + " def getdata(self):\n", + " print \"Enter the value of a,b,c &d &e:\"\n", + " self._a=input()\n", + " self._b=input()\n", + " self._c=input()\n", + " self._d=input()\n", + " self._e=input()\n", + " \n", + " def show(self):\n", + " print\"a=\",self._a,\"b=\",self._b,\"c=\",self._c,\"d=\",self._d,\"e=\",self._e\n", + " \n", + " \n", + "x=E() #x is the instance of the derived class\n", + "x.getdata() #call the methods of derived class through x \n", + "x.show()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the value of a,b,c &d &e:\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "4\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "8\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "16\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "a= 1 b= 2 c= 4 d= 8 e= 16\n" + ] + } + ], + "prompt_number": 8 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.9, Page Number:461" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class red: #these three base class\n", + " def __init__(self):\n", + " print \"Red\",\n", + " \n", + "class yellow:\n", + " def __init__(self):\n", + " print \"Yellow\",\n", + " \n", + "class blue:\n", + " def __init__(self):\n", + " print \"Blue\",\n", + " \n", + "class orange(red,yellow): #public multiple Derivation\n", + " def __init__(self):\n", + " red.__init__(self)\n", + " yellow.__init__(self)\n", + " print \"=Orange\",\n", + " \n", + "class green(blue,yellow): #public multiple Derivation\n", + " def __init__(self):\n", + " blue.__init__(self)\n", + " yellow.__init__(self)\n", + " print \"=Green\",\n", + " \n", + "class violet(red,blue): #public multiple Derivation\n", + " def __init__(self):\n", + " red.__init__(self)\n", + " blue.__init__(self)\n", + " print \"=Violet\",\n", + " \n", + "class reddishbrown(orange,violet): #public multiple & multilevel Derivation\n", + " def __init__(self):\n", + " for b in self.__class__.__bases__:\n", + " b.__init__(self)\n", + " print \"=Reddishbrown\"\n", + " \n", + "class yellowishbrown(green,orange): #public multiple & multilevel Derivation\n", + " def __init__(self):\n", + " for b in self.__class__.__bases__:\n", + " b.__init__(self)\n", + " print \"=Yellowishbrown\"\n", + " \n", + "class bluishbrown(violet,green): #public multiple & multilevel Derivation\n", + " def __init__(self):\n", + " for b in self.__class__.__bases__:\n", + " b.__init__(self)\n", + " print \"=Bluishbrown\"\n", + " \n", + " \n", + " \n", + "r=reddishbrown() #create instances of the derived class\n", + "b=bluishbrown()\n", + "y=yellowishbrown()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Red Yellow =Orange Red Blue =Violet =Reddishbrown\n", + "Red Blue =Violet Blue Yellow =Green =Bluishbrown\n", + "Blue Yellow =Green Red Yellow =Orange =Yellowishbrown\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.10, Page Number:463" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# WAP to create a derived class from multiple base classes\n", + "\n", + "class PLAYER: #these three are the base classes\n", + " def __init__(self):\n", + " self._name=None\n", + " self._gender=None\n", + " self._age\n", + " \n", + "class PHYSIQUE(PLAYER):\n", + " def __init__(self):\n", + " self._height=None\n", + " self._weight=None\n", + " \n", + "class LOCATION:\n", + " def __init__(self):\n", + " self._city=None\n", + " self._pin=None\n", + " \n", + "class GAME(PHYSIQUE,LOCATION): #Multiple derivation\n", + " def __init__(self):\n", + " self._game=None\n", + " def getdata(self): #Method to take inputes\n", + " print\"Enter the following information\\n\\n\"\n", + " self._name=raw_input(\"Name:\")\n", + " self._gender=raw_input(\"Gender:\")\n", + " self._age=raw_input(\"Age:\")\n", + " self._height=raw_input(\"Height:\")\n", + " self._weight=raw_input(\"Weight:\")\n", + " self._city=raw_input(\"City:\")\n", + " self._pin=raw_input(\"Pin:\")\n", + " self._game=raw_input(\"game:\")\n", + " \n", + " \n", + " \n", + " def show(self): #Method for displaying inputes\n", + " print\"Entered Information!!\"\n", + " print\"Name:\",self._name\n", + " print \"Gender:\",self._gender\n", + " print \"Age:\",self._age\n", + " print \"Height:\",self._height\n", + " print \"Weight:\",self._weight\n", + " print \"City :\",self._city\n", + " print \"Pincode:\",self._pin\n", + " print \"Game :\",self._game\n", + " \n", + " \n", + "G=GAME() #create an instance of the derived class\n", + "G.getdata() #call the public methods by the created instances\n", + "G.show()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the following information\n", + "\n", + "\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Name:Mahesh\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Gender:M\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Age:25\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Height:4.9\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Weight:55\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "City:Nanded\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Pin:431603\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "game:Cricket\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Entered Information!!\n", + "Name: Mahesh\n", + "Gender: M\n", + "Age: 25\n", + "Height: 4.9\n", + "Weight: 55\n", + "City : Nanded\n", + "Pincode: 431603\n", + "Game : Cricket\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.11, Page Number:467" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Multipath Inheritance,concept of virtual classes\n", + "\n", + "class A1: #Super base class\n", + " def __init__(self):\n", + " self._a1=None\n", + " \n", + "class A2(A1): #base class 1,inherites Super Base class\n", + " def __init__(self):\n", + " self._a2=None\n", + " \n", + "class A3(A1): #base class 2,inherites Super Base class\n", + " def __init__(self):\n", + " self._a3=None\n", + " \n", + "class A4(A2,A3): #derived class ,public derivation of both the base classes\n", + " def __init__(self):\n", + " self.__a4=None\n", + " \n", + " def get(self):\n", + " print \"Enter the value of a1,a2,a3,and a4:\"\n", + " self._a1=input()\n", + " self._a2=input()\n", + " self._a3=input()\n", + " self.__a4=input()\n", + " \n", + " def put(self):\n", + " print \"a1=\",self._a1,\"a2=\",self._a2,\"a3=\",self._a3,\"a4=\",self.__a4\n", + " \n", + " \n", + " \n", + "a=A4() #create the instance of the derived class\n", + "a.get()\n", + "a.put()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the value of a1,a2,a3,and a4:\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "8\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "7\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "a1= 5 a2= 8 a3= 7 a4= 3\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.12, Page Number:469" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#To show order of execution of the constructors and destructors in multiple inheritance\n", + "\n", + "#**NOTE:Python destuctor is called when program goes exit. So output may be differ than c++\n", + "\n", + "class A:\n", + " def __init__(self):\n", + " print\"Zero argument Constructor of base class A\"\n", + " \n", + " def __del__(self):\n", + " print\"Destructor of class A\"\n", + " \n", + "class B:\n", + " def __init__(self):\n", + " print\"Zero argument Constructor of base class B\"\n", + " \n", + " def __del__(self):\n", + " print\"Destructor of class B\"\n", + "\n", + "class C(A,B):\n", + " def __init__(self):\n", + " for b in self.__class__.__bases__:\n", + " b.__init__(self)\n", + " print\"Zero argument Constructor of base class C\"\n", + " \n", + " def __del__(self):\n", + " print\"Destructor of class C\"\n", + " for b in self.__class__.__bases__:\n", + " b.__del__(self)\n", + " \n", + "c=C() #create instance of derived class" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Zero argument Constructor of base class A\n", + "Zero argument Constructor of base class B\n", + "Zero argument Constructor of base class C\n", + "Destructor of class C\n", + "Destructor of class A\n", + "Destructor of class B\n" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.13, Page Number:471" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#WAP to use constructor and destructor in all the classess\n", + "\n", + "class A1:\n", + " def __init__(self): #take name and age as input in super base class\n", + " self._name=raw_input(\"Name:\")\n", + " self._age=raw_input(\"Age:\")\n", + " \n", + " def __del__(self):\n", + " print\"Name:\",self._name\n", + " print\"Age\",self._age\n", + " \n", + " \n", + "class A2(A1): #take height and weight as input in base base class,public derivation \n", + " def __init__(self):\n", + " A1.__init__(self)\n", + " self._height=raw_input(\"Height:\")\n", + " self._weight=raw_input(\"Weight:\")\n", + " \n", + " def __del__(self):\n", + " print\"Height:\",self._height\n", + " print\"Weight:\",self._weight\n", + " A1.__del__(self)\n", + " \n", + " \n", + "class A3(A2): #take sex as input in derived class,derived from class A2\n", + " def __init__(self):\n", + " A2.__init__(self)\n", + " self.__sex=raw_input(\"Sex:\")\n", + " def __del__(self): #display all the input taken by all the base classes\n", + " print\"Sex:\",self.__sex\n", + " A2.__del__(self)\n", + " \n", + " \n", + "x=A3() #create instance x of the class A3\n", + "\n", + "del x #call the destructor" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Name:Ajay\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Age:20\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Height:4.5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Weight:40\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Sex:M\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Sex: M\n", + "Height: 4.5\n", + "Weight: 40\n", + "Name: Ajay\n", + "Age 20\n" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.14, Page Number:472" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#To create derived class from the base class,by constructor and destructor\n", + "class in_t:\n", + " def __init__(self):\n", + " self._i=1\n", + " print\"Constructor in_t()\"\n", + " \n", + " def __del__(self):\n", + " print\"Destructor in_t()\"\n", + " \n", + "class floa_t:\n", + " def __init__(self):\n", + " self._f=1.5\n", + " print\"Constructor floa_t()\"\n", + " \n", + " def __del__(self):\n", + " print\"Destructor floa_t()\"\n", + " \n", + " \n", + "class cha_r(in_t,floa_t): #multiple derivation\n", + " def __init__(self):\n", + " self._c='A'\n", + " print\"Constructor cha_r()\"\n", + " for b in self.__class__.__bases__:\n", + " b.__init__(self)\n", + " \n", + " def show(self):\n", + " print\"i=\",self._i\n", + " print \"f=\",self._f\n", + " print \"c=\",self._c\n", + " \n", + " def __del__(self):\n", + " print \"Destructing cha_r()\"\n", + " for b in self.__class__.__bases__:\n", + " b.__del__(self)\n", + " \n", + "a=cha_r() #create derived class instance and call the public method of the derived class\n", + "a.show() #**NOTE:Python destuctor is called when program goes exit. So output may be differ than c++" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Constructor cha_r()\n", + "Constructor in_t()\n", + "Constructor floa_t()\n", + "i= 1\n", + "f= 1.5\n", + "c= A\n" + ] + } + ], + "prompt_number": 8 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.15, Page Number:474" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "class I:\n", + " def __init__(self):\n", + " self.x=None\n", + " \n", + "class II(I):\n", + " def __init__(self):\n", + " self.__y=None\n", + " \n", + " def set(self,j,k):\n", + " self.x=j\n", + " self.__y=k\n", + " \n", + " def show(self):\n", + " print \"X=\",self.x, \"Y=\",self.__y\n", + " \n", + " \n", + "i=II()\n", + "i.set(4,5)\n", + "i.show()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "X= 4 Y= 5\n" + ] + } + ], + "prompt_number": 9 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.16, Page Number:475" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class I:\n", + " def __init__(self):\n", + " self.x=10\n", + " print \"In the Base class constuctor\"\n", + " \n", + "class II(I):\n", + " def __init__(self):\n", + " I.__init__(self)\n", + " self.__y=None\n", + " \n", + "i=II()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "In the Base class constuctor\n" + ] + } + ], + "prompt_number": 10 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.17, Page Number:475" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#base class without constructor and derived class with constructor\n", + "class I:\n", + " pass\n", + "class II(I):\n", + " def __init__(self):\n", + " print \"In derived class constructor\"\n", + " \n", + "i=II()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "In derived class constructor\n" + ] + } + ], + "prompt_number": 11 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.18, Page Number:476" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#both the class have constructor\n", + "class I:\n", + " def __init__(self):\n", + " print \"In base class Constructor\"\n", + " \n", + "class II(I):\n", + " def __init__(self):\n", + " I.__init__(self)\n", + " print \"In derived Class constructor\"\n", + " \n", + "i=II()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "In base class Constructor\n", + "In derived Class constructor\n" + ] + } + ], + "prompt_number": 12 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.19, Page Number:477" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#multiple constructor in base class and single constructor in the derived class\n", + "\n", + "class I:\n", + " def __init__(self):\n", + " print \"Zero argument base class construtor\"\n", + " \n", + " def __init__(self,k):\n", + " self.x=None\n", + " print \"One argument base class construtor\"\n", + " \n", + " \n", + "class II(I):\n", + " def __init__(self,j,k=None): #default constructor\n", + " I.__init__(self,k)\n", + " self.__y=j\n", + " print \"One argument derived class constructor\"\n", + " \n", + "i=II(2) #create the instance of the base class by passing initial value " + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "One argument base class construtor\n", + "One argument derived class constructor\n" + ] + } + ], + "prompt_number": 13 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.20, Page Number:478" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#base and derived class without default constructor\n", + "class I:\n", + " def __init__(self,k):\n", + " self.x=k\n", + " print \"One argument base class construtor\"\n", + " \n", + "class II(I):\n", + " def __init__(self,j):\n", + " I.__init__(self,j)\n", + " self.__y=j\n", + " print \"One argument derived class construtor\"\n", + " \n", + "i=II(2) #create the instance of the base class by passing initial value " + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "One argument base class construtor\n", + "One argument derived class construtor\n" + ] + } + ], + "prompt_number": 14 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.21, Page Number:479" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Constructors and multiple inheritance\n", + "class I:\n", + " def __init__(self):\n", + " print \"Zero argument constructor of base class I\"\n", + " \n", + "class II:\n", + " def __init__(self):\n", + " print \"Zero argument constructor of base class II\"\n", + " \n", + "class III(II,I): #class III inhrites class II and I\n", + " def __init__(self):\n", + " for b in self.__class__.__bases__:\n", + " b.__init__(self) \n", + " print \"Zero argument constructor of base class III\"\n", + " \n", + "i=III() #create an instance of the base class\n", + " " + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Zero argument constructor of base class II\n", + "Zero argument constructor of base class I\n", + "Zero argument constructor of base class III\n" + ] + } + ], + "prompt_number": 15 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.22, Page Number:480" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Constructors in multiple inhritance with invoking constructor of the base classes\n", + "class I:\n", + " def __init__(self):\n", + " print \"Zero argument constructor of base class I\"\n", + " \n", + "class II:\n", + " def __init__(self):\n", + " print \"Zero argument constructor of base class II\"\n", + " \n", + "class III(II,I):\n", + " def __init__(self):\n", + " II.__init__(self)\n", + " I.__init__(self)\n", + " print \"Zero argument constructor of base class III\"\n", + " \n", + "i=III()\n", + " " + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Zero argument constructor of base class II\n", + "Zero argument constructor of base class I\n", + "Zero argument constructor of base class III\n" + ] + } + ], + "prompt_number": 16 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.23, Page Number:481" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#multiple inheritance,invoking the base classes explicitly\n", + "\n", + "class I:\n", + " def __init__(self):\n", + " print \"Zero argument constructor of base class I\"\n", + " \n", + "class II:\n", + " def __init__(self):\n", + " print \"Zero argument constructor of base class II\"\n", + " \n", + "class III(II,I): #Class I is virtually inherited so its constructor called first\n", + " def __init__(self):\n", + " I.__init__(self)\n", + " II.__init__(self)\n", + " print \"Zero argument constructor of base class III\"\n", + " \n", + "i=III()\n", + " " + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Zero argument constructor of base class I\n", + "Zero argument constructor of base class II\n", + "Zero argument constructor of base class III\n" + ] + } + ], + "prompt_number": 17 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.24, Page Number:482" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#multilevel Inheritance,observation of the execution of the constructors\n", + "class I:\n", + " def __init__(self):\n", + " print \"Zero argument constructor of base class I\"\n", + " \n", + "class II(I):\n", + " def __init__(self):\n", + " I.__init__(self)\n", + " print \"Zero argument constructor of base class II\"\n", + " \n", + "class III(II): #Class I is virtually inherited so its constructor called first\n", + " def __init__(self):\n", + " II.__init__(self)\n", + " print \"Zero argument constructor of base class III\"\n", + " \n", + "i=III()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Zero argument constructor of base class I\n", + "Zero argument constructor of base class II\n", + "Zero argument constructor of base class III\n" + ] + } + ], + "prompt_number": 18 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.25, Page Number:484" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#use object of one class in another class as a member\n", + "class I:\n", + " def __init__(self):\n", + " self.x=20\n", + " print \"Constructor of class I\"\n", + " \n", + "class II:\n", + " \n", + " def __init__(self):\n", + " self.k=30\n", + " y=I()\n", + " print \"Constructor of class II\"\n", + " print \"x=\",y.x #print here because it become local variable in this scope only,it not visible to def show\n", + " \n", + " \n", + " def show(self):\n", + " print \"k=\",self.k\n", + " \n", + "ii=II()\n", + "ii.show() #**NOTE:Python destuctor is called when program goes exit. So output may be differ than c++" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Constructor of class I\n", + "Constructor of class II\n", + "x= 20\n", + "k= 30\n" + ] + } + ], + "prompt_number": 19 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.26, Page Number:484" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#access a member variable of base class using object,class name, and direct\n", + "\n", + "class A1:\n", + " def __init__(self):\n", + " self.name=None\n", + " self.age=None\n", + " \n", + "class A2(A1):\n", + " def __init__(self):\n", + " A1.__init__(self)\n", + " a=A1()\n", + " print \"Access using name of the class:\"\n", + " A1.name=raw_input(\"Name:\")\n", + " A1.age=raw_input(\"Age:\")\n", + " \n", + " print \"Access using object of the class\"\n", + " a.name=raw_input(\"Name:\")\n", + " a.age=raw_input(\"Age:\")\n", + " \n", + " print \"Access using direct member variables:\"\n", + " self.name=raw_input(\"Name:\")\n", + " self.age=raw_input(\"Age:\")\n", + " self.__height=raw_input(\"Height:\")\n", + " self.__weight=raw_input(\"Weight:\")\n", + " \n", + " print \"Display using object of the class\" #since object of class A1 has scope in constructor method so we can access it only \n", + " print \"Name:\",a.name # within this method.It is not visible in destructor function.\n", + " print \"Age:\",a.age\n", + " \n", + " \n", + " \n", + " def __del__(self):\n", + " print \"Destructor of Derived class\"\n", + " print \"Display using class name\"\n", + " print \"Name:\",A1.name\n", + " print \"Age:\",A1.age\n", + " \n", + " print \"Display using direct member variable\"\n", + " print \"Name:\",self.name\n", + " print \"Age\",self.age\n", + " print \"height:\",self.__height\n", + " print \"Weight:\",self.__weight\n", + " \n", + "x=A2()\n", + "\n", + "del x\n", + " " + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Access using name of the class:\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Name:Ajay\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Age:21\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Access using object of the class\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Name:Amit\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Age:20\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Access using direct member variables:\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Name:Arun\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Age:19\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Height:5.5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Weight:31\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Display using object of the class\n", + "Name: Amit\n", + "Age: 20\n", + "Destructor of Derived class\n", + "Display using class name\n", + "Name: Ajay\n", + "Age: 21\n", + "Display using direct member variable\n", + "Name: Arun\n", + "Age 19\n", + "height: 5.5\n", + "Weight: 31\n" + ] + } + ], + "prompt_number": 20 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.27, Page Number:488" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#derive a class from two base classes,object of these 2 classes is the member variable of the third class\n", + "\n", + "class A:\n", + " def __init__(self):\n", + " self.a1=None\n", + " \n", + "class B:\n", + " def __init__(self):\n", + " self.b1=None\n", + " \n", + "class AB:\n", + " def __init__(self):\n", + " a=A()\n", + " b=B()\n", + " a.a1=65 #initialize the two data members of the class A and B and Display them\n", + " b.b1=66\n", + " print \"a1=\",a.a1, \"b1=\",b.b1\n", + " \n", + " def __del__(self):\n", + " pass\n", + " \n", + " \n", + "ab=AB()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "a1= 65 b1= 66\n" + ] + } + ], + "prompt_number": 21 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.28, Page Number:489" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#create derived class from qualifier class\n", + "\n", + "class A:\n", + " def __init__(self):\n", + " self.x=None\n", + " \n", + " class B:\n", + " def __init__(self):\n", + " self.y=None\n", + " \n", + " \n", + "class C(A,A.B): #A.B is the inner class of the class A\n", + " def __init__(self,j,k,l):\n", + " self.x=j\n", + " self.y=k\n", + " self.z=l\n", + " \n", + " def show(self):\n", + " print \"x=\",self.x,\"y=\",self.y,\"z=\",self.z\n", + " \n", + " \n", + "c=C(4,7,1)\n", + "c.show()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "x= 4 y= 7 z= 1\n" + ] + } + ], + "prompt_number": 23 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.29, Page Number:490" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#initialize member variable of the base class and derived class using constructor of the derived class\n", + "\n", + "class A:\n", + " def __init__(self):\n", + " self._x=None #protected members\n", + " self._y=None\n", + " \n", + "class B(A):\n", + " def __init__(self):\n", + " self.z=3\n", + " self.__x=1 #private members\n", + " self.__y=2\n", + " \n", + " print \"x=\",self.__x,\"y=\",self.__y,\"z=\",self.z\n", + " \n", + "b=B()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "x= 1 y= 2 z= 3\n" + ] + } + ], + "prompt_number": 24 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.30, Page Number:491" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#access data members by object pointer\n", + "\n", + "from ctypes import *\n", + "import ctypes\n", + "class A:\n", + " def __init__(self):\n", + " self.x=1\n", + " self.y=2\n", + " \n", + "class B(A):\n", + " def __init__(self):\n", + " A.__init__(self)\n", + " self.z=3\n", + " \n", + "b=B()\n", + "\n", + "\n", + "i=c_int(b.z)\n", + "p=pointer(i)\n", + "print \"Address of z:\",addressof(p),\"Value of Z:\",p[0] #access the\n", + "\n", + "i = c_int(b.y)\n", + "p = pointer(i)\n", + "print \"Address of y:\",addressof(p),\"Value of y:\",p[0] \n", + "\n", + "i = c_int(b.x)\n", + "p = pointer(i)\n", + "print \"Address of x:\",addressof(p),\"Value of x:\",p[0] \n", + "\n", + "#**NOTE-In case of C++ the data members of the derived class and base class are stored in contigious memory locations so we can \n", + "#access the three variables by using a pointer of derived class and decrementing its value. But in case of Python they are NOT stored \n", + "#in contogious memory locations so for accessing each data member we have to create individual object pointer for each class." + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Address of z: 57077392 Value of Z: 3\n", + "Address of y: 57074448 Value of y: 2\n", + "Address of x: 57077648 Value of x: 1\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.31, Page Number:492" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#overload member function in base and derived class\n", + "\n", + "class B:\n", + " def show(self):\n", + " print \"In base class function\"\n", + " \n", + "class D(B):\n", + " def show(self):\n", + " \n", + " print \"In Derived class\"\n", + " \n", + " \n", + "b=B()\n", + "d=D()\n", + "\n", + "b.show()\n", + "d.show()\n", + "\n", + "bp=[B()] #create a base class pointer variable\n", + "bp[0]=d #assign address of the derived class object to the base class pointer\n", + "bp[0].show() #call the derived class method by base class pointer\n", + "b.show() #calling the base class method by base class object" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "In base class function\n", + "In Derived class\n", + "In Derived class\n", + "In base class function\n" + ] + } + ], + "prompt_number": 25 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.32, Page Number:495" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Constuctor and destructors in single inheritance\n", + "class Father:\n", + " def __init__(self):\n", + " print \"Base Class constructor.\"\n", + " self._name=raw_input(\"Enter Father Name:\")\n", + " \n", + " def __del__(self):\n", + " print \"Base class Destructor.\"\n", + " \n", + "class Child(Father):\n", + " def __init__(self):\n", + " Father.__init__(self)\n", + " print \"Derived class constructor.\"\n", + " self.__cname=raw_input(\"Enter child name:\")\n", + " \n", + " def __del__(self):\n", + " print \"Derived class destructor.\"\n", + " print \"\",self.__cname,\"\",self.__name\n", + " for b in self.__class__.__bases__:\n", + " b.__del__(self)\n", + " \n", + " \n", + " \n", + "C=Child()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Base Class constructor.\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter Father Name:Manoj\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Derived class constructor.\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter child name:Sanjay\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Derived class destructor.\n", + " Sanjay Manoj\n", + "Base class Destructor.\n" + ] + } + ], + "prompt_number": 37 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.33, Page Number:496" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Constuctor and destructors in multilevel inheritance\n", + "\n", + "class Grandfather:\n", + " def __init__(self):\n", + " print\"Constructor of class grandfather\"\n", + " self._gname=raw_input(\"Enter Grandfather Name:\")\n", + " \n", + " \n", + " def __del__(self):\n", + " print \"Destructor of class grandfather\"\n", + " \n", + " \n", + "class Father(Grandfather):\n", + " def __init__(self):\n", + " Grandfather.__init__(self)\n", + " print\"Constructor of class Father\"\n", + " self._name=raw_input(\"Enter Father Name:\")\n", + " \n", + " \n", + " def __del__(self):\n", + " print \"Destructor of class Father\"\n", + " Grandfather.__del__(self)\n", + " \n", + "class Child(Father):\n", + " def __init__(self):\n", + " Father.__init__(self)\n", + " print\"Constructor of class Child\"\n", + " self.__cname=raw_input(\"Enter Child Name:\")\n", + " \n", + " \n", + " def __del__(self):\n", + " print \"Destructor of class Child\"\n", + " print \"Grandfather:\",self._gname,\"Father:\",self._name,\"Child:\",self.__cname\n", + " Father.__del__(self) \n", + " \n", + " \n", + "C=Child()\n", + "\n", + "del C #call the destructor of the derived class\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Constructor of class grandfather\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter Grandfather Name:x\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Constructor of class Father\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter Father Name:y\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Constructor of class Child\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter Child Name:z\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Destructor of class Child\n", + "Grandfather: x Father: y Child: z\n", + "Destructor of class Father\n", + "Destructor of class grandfather\n" + ] + } + ], + "prompt_number": 39 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.34, Page Number:498" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#program to explain multilevel inheritance with member function\n", + "class Grandfather:\n", + " def __init__(self):\n", + " self.__gname=None\n", + " \n", + " def getg(self):\n", + " self.__gname=raw_input(\"Enter Grandfather Name:\")\n", + " \n", + " def showg(self):\n", + " print \"Grandfather Name:\",self.__gname\n", + " \n", + " \n", + "class Father(Grandfather):\n", + " def __init__(self):\n", + " self.__name=None\n", + " \n", + " def getf(self):\n", + " self.__name=raw_input(\"Enter Father Name:\")\n", + " \n", + " def showf(self):\n", + " print \"Father Name:\",self.__name\n", + " \n", + " \n", + "class Child(Father):\n", + " def __init__(self):\n", + " self.__cname=None\n", + " \n", + " def getc(self):\n", + " self.getg()\n", + " self.getf()\n", + " self.__cname=raw_input(\"Enter Child Name:\")\n", + " \n", + " def showc(self):\n", + " self.showg()\n", + " self.showf()\n", + " print \"child Name:\",self.__cname\n", + " \n", + "C=Child()\n", + "C.getc()\n", + "C.showc()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter Grandfather Name:XXX\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter Father Name:YYY\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter Child Name:ZZZ\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Grandfather Name: XXX\n", + "Father Name: YYY\n", + "child Name: ZZZ\n" + ] + } + ], + "prompt_number": 40 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.35, Page Number:499" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#execution of constructor and destructor in multilevel inheritance\n", + "\n", + "class A:\n", + " def __init__(self):\n", + " print \"Constructor of class A\"\n", + " \n", + " def __del__(self):\n", + " print \"Destructor of class A\"\n", + " \n", + " \n", + "class B:\n", + " def __init__(self):\n", + " print \"Constructor of class B\"\n", + " \n", + " def __del__(self):\n", + " print \"Destructor of class B\"\n", + " \n", + "class C:\n", + " def __init__(self):\n", + " print \"Constructor of class C\"\n", + " \n", + " def __del__(self):\n", + " print \"Destructor of class C\"\n", + " \n", + " \n", + "class D(A,B,C):\n", + " def __init__(self):\n", + " for b in self.__class__.__bases__:\n", + " b.__init__(self) \n", + " print \"Constructor of class D\"\n", + " \n", + " def __del__(self):\n", + " print \"Destructor of class D\"\n", + " for b in self.__class__.__bases__:\n", + " b.__del__(self)\n", + " \n", + "x=D() \n", + " #**NOTE:Python destuctor is called when program goes exit. So output may be differ than c++" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Constructor of class A\n", + "Constructor of class B\n", + "Constructor of class C\n", + "Constructor of class D\n", + "Destructor of class D\n", + "Destructor of class A\n", + "Destructor of class B\n", + "Destructor of class C\n" + ] + } + ], + "prompt_number": 42 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.37, Page Number:502" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#execution of constructor and destructor in multilevel inheritance\n", + "\n", + "class A:\n", + " def __init__(self):\n", + " print \"Constructor of class A\"\n", + " \n", + " def __del__(self):\n", + " print \"Destructor of class A\"\n", + " \n", + " \n", + "class B(A):\n", + " def __init__(self):\n", + " A.__init__(self)\n", + " print \"Constructor of class B\"\n", + " \n", + " def __del__(self):\n", + " print \"Destructor of class B\"\n", + " A.__del__(self)\n", + " \n", + "class C:\n", + " def __init__(self):\n", + " print \"Constructor of class C\"\n", + " \n", + " def __del__(self):\n", + " print \"Destructor of class C\"\n", + " \n", + " \n", + "class D(B,C):\n", + " def __init__(self):\n", + " B.__init__(self)\n", + " C.__init__(self)\n", + " print \"Constructor of class D\"\n", + " \n", + " def __del__(self):\n", + " print \"Destructor of class D\"\n", + " B.__del__(self)\n", + " C.__del__(self)\n", + " \n", + "x=D() \n", + "del x\n", + " #**NOTE:Python destuctor is called when program goes exit. So output may be differ than c++" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Constructor of class A\n", + "Constructor of class B\n", + "Constructor of class C\n", + "Constructor of class D\n", + "Destructor of class D\n", + "Destructor of class B\n", + "Destructor of class A\n", + "Destructor of class C\n" + ] + } + ], + "prompt_number": 54 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.37, Page Number:502" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#demonstrate single inheritance\n", + "\n", + "class A:\n", + " def __init__(self,j=0):\n", + " self._c=j\n", + " \n", + " def show(self):\n", + " print \"c=\",self._c\n", + " \n", + " def inc(self):\n", + " self._c=self._c+1\n", + " return self._c\n", + " \n", + "class B(A):\n", + " \n", + " def __init_(self):\n", + " for b in self.__class__.__bases__:\n", + " b.__init__(self)\n", + " \n", + " def dec(self):\n", + " self._c=self._c-1\n", + " return self._c\n", + " \n", + " \n", + "a=B()\n", + "a.inc()\n", + "a.show()\n", + "\n", + "\n", + "a.dec()\n", + "a.show()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "c= 1\n", + "c= 0\n" + ] + } + ], + "prompt_number": 44 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.38, Page Number:502" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#access method from private inheritance\n", + "class B:\n", + " def one(self):\n", + " print \"one\"\n", + " \n", + " def __two(self):\n", + " print \"two\"\n", + " \n", + "class D(B):\n", + " def __init__(self):\n", + " pass\n", + " \n", + "d=D()\n", + "d.one()\n", + "#d.two() #Not accesible" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "one\n" + ] + } + ], + "prompt_number": 45 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.39, Page Number:503" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#create a comman constructor in the lowermost class, in the multilevel inheritance\n", + "class A:\n", + " def __init__(self):\n", + " self.x=None\n", + " \n", + "class B(A):\n", + " def __init__(self):\n", + " self.y=None\n", + " \n", + "class C(B):\n", + " def __init__(self,j,k,l):\n", + " self.z=l\n", + " self.x=j\n", + " self.y=k\n", + " \n", + " def show(self):\n", + " print \"x=\",self.x,\"Y=\",self.y,\"z=\",self.z\n", + " \n", + "c=C(4,7,1)\n", + "c.show()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "x= 4 Y= 7 z= 1\n" + ] + } + ], + "prompt_number": 46 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.40, Page Number:504" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Explicitly call the base constructor in multiple inheritance\n", + "\n", + "class X:\n", + " def __init__(self,a):\n", + " print a,\n", + " \n", + "class Y:\n", + " def __init__(self,b):\n", + " print b,\n", + " \n", + "class Z(X,Y):\n", + " def __init__(self,p,q,r):\n", + " X.__init__(self,p)\n", + " Y.__init__(self,q)\n", + " print r\n", + " \n", + " \n", + "z=Z(1,2,3)\n", + " " + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "1 2 3\n" + ] + } + ], + "prompt_number": 47 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [], + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/sample_notebooks/AJEET KUMARSINGH/Chapter_11_Inheritance_2.ipynb b/sample_notebooks/AJEET KUMARSINGH/Chapter_11_Inheritance_2.ipynb new file mode 100755 index 00000000..d92c896a --- /dev/null +++ b/sample_notebooks/AJEET KUMARSINGH/Chapter_11_Inheritance_2.ipynb @@ -0,0 +1,2661 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:4d2b31f88da13c65a205238b2aa0d40205b037d2cf9c5ea74665d1d4bd106552" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.1, Page Number:444" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Public Derivation of a class\n", + "class A: #Base class\n", + " def __init__(self):\n", + " self.x=None\n", + "\n", + " \n", + "class B(A): #derived class\n", + " def __init__(self):\n", + " self.y=None\n", + " \n", + "b=B() #declaration of object\n", + "b.x=20\n", + "b.y=30\n", + "\n", + "print 'member of A:',b.x\n", + "print 'Member of B:',b.y" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "member of A: 20\n", + "Member of B: 30\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.2, Page Number:445" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Base class\n", + "class A: #Base class\n", + " def __init__(self): #class A having x as a private data member\n", + " self.__x=20\n", + " \n", + " def showx(self):\n", + " print \"x=\",self.__x\n", + " \n", + "#derived class \n", + "class B(A): #Derived class\n", + " def __init__(self):\n", + " self.y=30 #class B having y as a public data member\n", + " \n", + " def show(self):\n", + " a=A()\n", + " a.showx()\n", + " print \"y=\",self.y\n", + " \n", + " \n", + "b=B() #declaration of object\n", + " \n", + "b.show()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "x= 20\n", + "y= 30\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.3, Page Number:447" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#base class\n", + "class A:\n", + " def __init__(self):\n", + " self.x=None #x is a public member\n", + " \n", + " \n", + "#derived class\n", + "class B(A):\n", + " def __init__(self):\n", + " self.y=40 \n", + " A.__x=20 #since it is privately inherites base class ,x become private member of it\n", + " \n", + " def show(self):\n", + " print \"x=\",A.__x\n", + " print \"y=\",self.y\n", + " \n", + "b=B() #declaration of object\n", + "b.show()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "x= 20\n", + "y= 40\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.4, Page Number:448" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#derivation of a class privately\n", + "class A:\n", + " def __init__(self):\n", + " self.__x=20 \n", + " \n", + " def showx(self): \n", + " print \"x=\",self.__x\n", + " \n", + "#derived class \n", + "class B(A):\n", + " def __init__(self):\n", + " self.y=40 \n", + " \n", + " def show(self):\n", + " a=A()\n", + " a.showx() #call the base class method\n", + " print \"y=\",self.y\n", + " \n", + " \n", + "b=B()\n", + "b.show()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "x= 20\n", + "y= 40\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.5, Page Number:449" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class A:\n", + " def __init__(self):\n", + " self._x=None #x is a protected member of the base class\n", + " \n", + " \n", + "class B(A): #private inheritance,x become a private member of the derived class\n", + " def __init__(self):\n", + " self.y=40\n", + " self.__x=30\n", + " \n", + " \n", + " def show(self): #method to display all the values of all the data memeber\n", + " print \"x=\",self.__x\n", + " print \"y=\",self.y\n", + " \n", + " #declaration of object \n", + "b=B()\n", + "b.show()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "x= 30\n", + "y= 40\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.6, Page Number:456" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class ABC: #Base class\n", + " def __init__(self):\n", + " self._name=None #these 2 are protected data member\n", + " self._age=None\n", + " \n", + "class abc(ABC): #Derived class ,Public derivation\n", + " def __init__(self):\n", + " self.height=None\n", + " self.weight=None\n", + " \n", + " def getdata(self):\n", + " \n", + " self.name=raw_input(\"Enter a name: \") #take inputes to all the data members \n", + " self.age=raw_input(\"Enter a age: \") \n", + " self._height=raw_input(\"Enter a Height: \") \n", + " self._weight=raw_input(\"Enter a Weight: \") \n", + " \n", + " def show(self): #display the value of data members\n", + " print 'Name:',self.name \n", + " print 'Age:',self.age,\"years\"\n", + " print 'Height:',self._height,\"Feets\"\n", + " print 'Weight:',self._weight,\"kg.\"\n", + " \n", + " \n", + "x=abc()\n", + "x.getdata()\n", + "x.show()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter a name: Santosh\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter a age: 24\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter a Height: 4.5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter a Weight: 50\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Name: Santosh\n", + "Age: 24 years\n", + "Height: 4.5 Feets\n", + "Weight: 50 kg.\n" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.7, Page Number:458" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class A1: #super Base class,have 2 protected data members\n", + " def __init__(self):\n", + " self._name=None\n", + " self._age=None\n", + "\n", + " \n", + "class A2(A1): #Public derivation\n", + " def __init(self):\n", + " self._height=None\n", + " self._weight=None\n", + "\n", + "class A3(A2): #public Derivation\n", + " def __init__(self):\n", + " self._sex=None\n", + " \n", + " \n", + " def get(self): #get input \n", + " self._name=raw_input(\"Name: \")\n", + " self._age=raw_input(\"Age: \")\n", + " self._sex=raw_input(\"Sex: \")\n", + " \n", + " self._height=raw_input(\"Height: \")\n", + " self._weight=raw_input(\"Weight: \")\n", + " \n", + " def show(self): #Display values of all the data members\n", + " print \"Name:\",self._name\n", + " print \"Age:\",self._age ,\"years\"\n", + " print \"Sex:\",self._sex\n", + " print \"Height:\",self._height ,\"Feet\"\n", + " print \"Weight:\",self._weight ,\"Kg.\"\n", + " \n", + "\n", + "x=A3()\n", + "x.get()\n", + "x.show()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Name: Balaji\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Age: 26\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Sex: M\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Height: 4\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Weight: 49.5\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Name: Balaji\n", + "Age: 26 years\n", + "Sex: M\n", + "Height: 4 Feet\n", + "Weight: 49.5 Kg.\n" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.8, Page Number:459" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Example of multiple Inheritance\n", + "#Base1 class\n", + "class A:\n", + " def __init__(self):\n", + " self._a=None\n", + "#Base2 class\n", + "class B:\n", + " def __init__(self):\n", + " self._b=None\n", + "#Base3 class\n", + "class C:\n", + " def __init__(self):\n", + " self._c=None\n", + "#Base4 class \n", + "class D:\n", + " def __init__(self):\n", + " self._d=None\n", + "#derived class,multiple derivation\n", + "class E(A,B,C,D): #inherites all the base classes publically\n", + " def __init__(self):\n", + " self.e=None\n", + " \n", + " def getdata(self): #member method to take input for all the data members \n", + " print \"Enter the value of a,b,c &d &e:\"\n", + " self._a=input()\n", + " self._b=input()\n", + " self._c=input()\n", + " self._d=input()\n", + " self._e=input()\n", + " \n", + " def show(self): #member method to display for all the data members \n", + " print\"a=\",self._a,\"b=\",self._b,\"c=\",self._c,\"d=\",self._d,\"e=\",self._e\n", + " \n", + " \n", + "x=E() #x is the instance of the derived class\n", + "x.getdata() #call the methods of derived class through x \n", + "x.show()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the value of a,b,c &d &e:\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "4\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "8\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "16\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "a= 1 b= 2 c= 4 d= 8 e= 16\n" + ] + } + ], + "prompt_number": 8 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.9, Page Number:461" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class red: #these three base class\n", + " def __init__(self):\n", + " print \"Red\",\n", + " \n", + "class yellow:\n", + " def __init__(self):\n", + " print \"Yellow\",\n", + " \n", + "class blue:\n", + " def __init__(self):\n", + " print \"Blue\",\n", + " \n", + "class orange(red,yellow): #public multiple Derivation\n", + " def __init__(self):\n", + " red.__init__(self)\n", + " yellow.__init__(self)\n", + " print \"=Orange\",\n", + " \n", + "class green(blue,yellow): #public multiple Derivation\n", + " def __init__(self):\n", + " blue.__init__(self)\n", + " yellow.__init__(self)\n", + " print \"=Green\",\n", + " \n", + "class violet(red,blue): #public multiple Derivation\n", + " def __init__(self):\n", + " red.__init__(self)\n", + " blue.__init__(self)\n", + " print \"=Violet\",\n", + " \n", + "class reddishbrown(orange,violet): #public multiple & multilevel Derivation\n", + " def __init__(self):\n", + " for b in self.__class__.__bases__:\n", + " b.__init__(self)\n", + " print \"=Reddishbrown\"\n", + " \n", + "class yellowishbrown(green,orange): #public multiple & multilevel Derivation\n", + " def __init__(self):\n", + " for b in self.__class__.__bases__:\n", + " b.__init__(self)\n", + " print \"=Yellowishbrown\"\n", + " \n", + "class bluishbrown(violet,green): #public multiple & multilevel Derivation\n", + " def __init__(self):\n", + " for b in self.__class__.__bases__:\n", + " b.__init__(self)\n", + " print \"=Bluishbrown\"\n", + " \n", + " \n", + " \n", + "r=reddishbrown() #create instances of the derived class\n", + "b=bluishbrown()\n", + "y=yellowishbrown()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Red Yellow =Orange Red Blue =Violet =Reddishbrown\n", + "Red Blue =Violet Blue Yellow =Green =Bluishbrown\n", + "Blue Yellow =Green Red Yellow =Orange =Yellowishbrown\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.10, Page Number:463" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# WAP to create a derived class from multiple base classes\n", + "\n", + "class PLAYER: #these three are the base classes\n", + " def __init__(self):\n", + " self._name=None\n", + " self._gender=None\n", + " self._age\n", + " \n", + "class PHYSIQUE(PLAYER):\n", + " def __init__(self):\n", + " self._height=None\n", + " self._weight=None\n", + " \n", + "class LOCATION:\n", + " def __init__(self):\n", + " self._city=None\n", + " self._pin=None\n", + " \n", + "class GAME(PHYSIQUE,LOCATION): #Multiple derivation\n", + " def __init__(self):\n", + " self._game=None\n", + " def getdata(self): #Method to take inputes\n", + " print\"Enter the following information\\n\\n\"\n", + " self._name=raw_input(\"Name:\")\n", + " self._gender=raw_input(\"Gender:\")\n", + " self._age=raw_input(\"Age:\")\n", + " self._height=raw_input(\"Height:\")\n", + " self._weight=raw_input(\"Weight:\")\n", + " self._city=raw_input(\"City:\")\n", + " self._pin=raw_input(\"Pin:\")\n", + " self._game=raw_input(\"game:\")\n", + " \n", + " \n", + " \n", + " def show(self): #Method for displaying inputes\n", + " print\"Entered Information!!\"\n", + " print\"Name:\",self._name\n", + " print \"Gender:\",self._gender\n", + " print \"Age:\",self._age\n", + " print \"Height:\",self._height\n", + " print \"Weight:\",self._weight\n", + " print \"City :\",self._city\n", + " print \"Pincode:\",self._pin\n", + " print \"Game :\",self._game\n", + " \n", + " \n", + "G=GAME() #create an instance of the derived class\n", + "G.getdata() #call the public methods by the created instances\n", + "G.show()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the following information\n", + "\n", + "\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Name:Mahesh\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Gender:M\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Age:25\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Height:4.9\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Weight:55\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "City:Nanded\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Pin:431603\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "game:Cricket\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Entered Information!!\n", + "Name: Mahesh\n", + "Gender: M\n", + "Age: 25\n", + "Height: 4.9\n", + "Weight: 55\n", + "City : Nanded\n", + "Pincode: 431603\n", + "Game : Cricket\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.11, Page Number:467" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Multipath Inheritance,concept of virtual classes\n", + "\n", + "class A1: #Super base class\n", + " def __init__(self):\n", + " self._a1=None\n", + " \n", + "class A2(A1): #base class 1,inherites Super Base class\n", + " def __init__(self):\n", + " self._a2=None\n", + " \n", + "class A3(A1): #base class 2,inherites Super Base class\n", + " def __init__(self):\n", + " self._a3=None\n", + " \n", + "class A4(A2,A3): #derived class ,public derivation of both the base classes\n", + " def __init__(self):\n", + " self.__a4=None\n", + " \n", + " def get(self):\n", + " print \"Enter the value of a1,a2,a3,and a4:\"\n", + " self._a1=input()\n", + " self._a2=input()\n", + " self._a3=input()\n", + " self.__a4=input()\n", + " \n", + " def put(self):\n", + " print \"a1=\",self._a1,\"a2=\",self._a2,\"a3=\",self._a3,\"a4=\",self.__a4\n", + " \n", + " \n", + " \n", + "a=A4() #create the instance of the derived class\n", + "a.get()\n", + "a.put()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the value of a1,a2,a3,and a4:\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "8\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "7\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "a1= 5 a2= 8 a3= 7 a4= 3\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.12, Page Number:469" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#To show order of execution of the constructors and destructors in multiple inheritance\n", + "\n", + "#Base1 class\n", + "class A:\n", + " def __init__(self):\n", + " print\"Zero argument Constructor of base class A\"\n", + " \n", + " def __del__(self):\n", + " print\"Destructor of class A\"\n", + "#Base2 class \n", + "class B:\n", + " def __init__(self):\n", + " print\"Zero argument Constructor of base class B\"\n", + " \n", + " def __del__(self):\n", + " print\"Destructor of class B\"\n", + "#derived class,multiple derivation\n", + "class C(A,B):\n", + " def __init__(self):\n", + " for b in self.__class__.__bases__: #invocation of the constructor of all the base classes\n", + " b.__init__(self)\n", + " print\"Zero argument Constructor of base class C\"\n", + " \n", + " def __del__(self): \n", + " print\"Destructor of class C\"\n", + " for b in self.__class__.__bases__: #invocation of the destructor of all the base classes\n", + " b.__del__(self)\n", + " \n", + "c=C() #create instance of derived class" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Zero argument Constructor of base class A\n", + "Zero argument Constructor of base class B\n", + "Zero argument Constructor of base class C\n", + "Destructor of class C\n", + "Destructor of class A\n", + "Destructor of class B\n" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.13, Page Number:471" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#WAP to use constructor and destructor in all the classess\n", + "\n", + "class A1:\n", + " def __init__(self): #take name and age as input in super base class\n", + " self._name=raw_input(\"Name:\")\n", + " self._age=raw_input(\"Age:\")\n", + " \n", + " def __del__(self): #show name and age as input in super base class\n", + " print\"Name:\",self._name\n", + " print\"Age\",self._age\n", + " \n", + " \n", + "class A2(A1): #take height and weight as input in base base class,public derivation \n", + " def __init__(self):\n", + " A1.__init__(self)\n", + " self._height=raw_input(\"Height:\")\n", + " self._weight=raw_input(\"Weight:\")\n", + " \n", + " def __del__(self): #show height and weight as input in base base class,public derivation \n", + " print\"Height:\",self._height\n", + " print\"Weight:\",self._weight\n", + " A1.__del__(self)\n", + " \n", + " \n", + "class A3(A2): #take sex as input in derived class,derived from class A2\n", + " def __init__(self):\n", + " A2.__init__(self)\n", + " self.__sex=raw_input(\"Sex:\")\n", + " def __del__(self): #display all the input taken by all the base classes\n", + " print\"Sex:\",self.__sex\n", + " A2.__del__(self)\n", + " \n", + " \n", + "x=A3() #create instance x of the class A3\n", + "\n", + "del x #call the destructor" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Name:Ajay\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Age:20\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Height:4.5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Weight:40\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Sex:M\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Sex: M\n", + "Height: 4.5\n", + "Weight: 40\n", + "Name: Ajay\n", + "Age 20\n" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.14, Page Number:472" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#To create derived class from the base class,by constructor and destructor\n", + "#Base1 class\n", + "class in_t:\n", + " def __init__(self): #constructor of base1 class\n", + " self._i=1\n", + " print\"Constructor in_t()\"\n", + " \n", + " def __del__(self): #destructor of base1 class\n", + " print\"Destructor in_t()\"\n", + "\n", + "#Base2 class\n", + "class floa_t:\n", + " def __init__(self): #constructor of base2 class\n", + " self._f=1.5\n", + " print\"Constructor floa_t()\"\n", + " \n", + " def __del__(self): #destructor of base2 class\n", + " print\"Destructor floa_t()\"\n", + " \n", + "#Derived class \n", + "class cha_r(in_t,floa_t): #multiple derivation\n", + " def __init__(self):\n", + " self._c='A'\n", + " print\"Constructor cha_r()\"\n", + " for b in self.__class__.__bases__: #invocation of the base class constructors\n", + " b.__init__(self)\n", + " \n", + " def show(self): #member method to show all the data member \n", + " print\"i=\",self._i\n", + " print \"f=\",self._f\n", + " print \"c=\",self._c\n", + " \n", + " def __del__(self): #Destructor of the derived cladd\n", + " print \"Destructing cha_r()\"\n", + " for b in self.__class__.__bases__:\n", + " b.__del__(self)\n", + " \n", + "a=cha_r() #create derived class instance and call the public method of the derived class\n", + "a.show() #**NOTE:Python destuctor is called when program goes exit. So output may be differ than c++" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Constructor cha_r()\n", + "Constructor in_t()\n", + "Constructor floa_t()\n", + "i= 1\n", + "f= 1.5\n", + "c= A\n" + ] + } + ], + "prompt_number": 8 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.15, Page Number:474" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Base class\n", + "class I:\n", + " def __init__(self):\n", + " self.x=None\n", + "#derived class \n", + "class II(I):\n", + " def __init__(self):\n", + " self.__y=None #private member of the classII\n", + " \n", + " def set(self,j,k): #Parametrized constructor \n", + " self.x=j\n", + " self.__y=k\n", + " \n", + " def show(self):\n", + " print \"X=\",self.x, \"Y=\",self.__y\n", + " \n", + " \n", + "i=II() #creation of instance of the Derived class\n", + "i.set(4,5) #invocation of the derived class member method by instance of the derived class\n", + "i.show()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "X= 4 Y= 5\n" + ] + } + ], + "prompt_number": 9 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.16, Page Number:475" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Base class\n", + "class I:\n", + " def __init__(self):\n", + " self.x=10\n", + " print \"In the Base class constuctor\"\n", + "#Derived class\n", + "class II(I):\n", + " def __init__(self):\n", + " I.__init__(self) #invocation of the base class constructor\n", + " self.__y=None\n", + " \n", + "i=II() #instance of the derived class" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "In the Base class constuctor\n" + ] + } + ], + "prompt_number": 10 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.17, Page Number:475" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#base class without constructor and derived class with constructor\n", + "class I:\n", + " pass #Empty body of the base class\n", + "class II(I):\n", + " def __init__(self):\n", + " print \"In derived class constructor\"\n", + " \n", + "i=II()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "In derived class constructor\n" + ] + } + ], + "prompt_number": 11 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.18, Page Number:476" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#both the class have constructor\n", + "class I:\n", + " def __init__(self):\n", + " print \"In base class Constructor\"\n", + "#Derived class \n", + "class II(I):\n", + " def __init__(self):\n", + " I.__init__(self)#invocation of the base class constructor\n", + " print \"In derived Class constructor\"\n", + " \n", + "i=II()#instance of the derived class" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "In base class Constructor\n", + "In derived Class constructor\n" + ] + } + ], + "prompt_number": 12 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.19, Page Number:477" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#multiple constructor in base class and single constructor in the derived class\n", + "#Base class\n", + "class I:\n", + " def __init__(self):\n", + " print \"Zero argument base class construtor\"\n", + " \n", + " def __init__(self,k): #parametrized constructor\n", + " self.x=None\n", + " print \"One argument base class construtor\"\n", + " \n", + "#Derived class \n", + "class II(I):\n", + " def __init__(self,j,k=None): #default constructor\n", + " I.__init__(self,k)\n", + " self.__y=j\n", + " print \"One argument derived class constructor\"\n", + " \n", + "i=II(2) #create the instance of the base class by passing initial value " + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "One argument base class construtor\n", + "One argument derived class constructor\n" + ] + } + ], + "prompt_number": 13 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.20, Page Number:478" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#base and derived class without default constructor\n", + "class I:\n", + " def __init__(self,k):\n", + " self.x=k\n", + " print \"One argument base class construtor\"\n", + "#Derived class \n", + "class II(I):\n", + " def __init__(self,j):\n", + " I.__init__(self,j) #invlocation of baser class constructor\n", + " self.__y=j\n", + " print \"One argument derived class construtor\"\n", + " \n", + "i=II(2) #create the instance of the base class by passing initial value " + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "One argument base class construtor\n", + "One argument derived class construtor\n" + ] + } + ], + "prompt_number": 14 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.21, Page Number:479" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Constructors and multiple inheritance\n", + "class I:\n", + " def __init__(self):\n", + " print \"Zero argument constructor of base class I\"\n", + " \n", + "class II:\n", + " def __init__(self):\n", + " print \"Zero argument constructor of base class II\"\n", + " \n", + "class III(II,I): #class III inhrites class II and I\n", + " def __init__(self):\n", + " for b in self.__class__.__bases__: #invocation \n", + " b.__init__(self) \n", + " print \"Zero argument constructor of base class III\"\n", + " \n", + "i=III() #create an instance of the base class\n", + " " + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Zero argument constructor of base class II\n", + "Zero argument constructor of base class I\n", + "Zero argument constructor of base class III\n" + ] + } + ], + "prompt_number": 15 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.22, Page Number:480" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Constructors in multiple inhritance with invoking constructor of the base classes\n", + "class I:\n", + " def __init__(self):\n", + " print \"Zero argument constructor of base class I\"\n", + " \n", + "class II:\n", + " def __init__(self):\n", + " print \"Zero argument constructor of base class II\"\n", + " \n", + "class III(II,I):\n", + " def __init__(self):\n", + " II.__init__(self) #invocation of base class constructors\n", + " I.__init__(self)\n", + " print \"Zero argument constructor of base class III\"\n", + " \n", + "i=III() #instaace of thr derived class\n", + " " + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Zero argument constructor of base class II\n", + "Zero argument constructor of base class I\n", + "Zero argument constructor of base class III\n" + ] + } + ], + "prompt_number": 16 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.23, Page Number:481" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#multiple inheritance,invoking the base classes explicitly\n", + "#Base1 class\n", + "class I:\n", + " def __init__(self):\n", + " print \"Zero argument constructor of base class I\"\n", + "#Base2 class\n", + "class II:\n", + " def __init__(self):\n", + " print \"Zero argument constructor of base class II\"\n", + " \n", + "class III(II,I): #Class I is virtually inherited so its constructor called first\n", + " def __init__(self):\n", + " I.__init__(self) #invocation of the base classes\n", + " II.__init__(self)\n", + " print \"Zero argument constructor of base class III\"\n", + " \n", + "i=III() #instane of the Derived class\n", + " " + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Zero argument constructor of base class I\n", + "Zero argument constructor of base class II\n", + "Zero argument constructor of base class III\n" + ] + } + ], + "prompt_number": 17 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.24, Page Number:482" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#multilevel Inheritance,observation of the execution of the constructors\n", + "class I:\n", + " def __init__(self):\n", + " print \"Zero argument constructor of base class I\"\n", + "#Derived1 class \n", + "class II(I):\n", + " def __init__(self):\n", + " I.__init__(self)\n", + " print \"Zero argument constructor of base class II\"\n", + " \n", + "class III(II): #Class I is virtually inherited so its constructor called first\n", + " def __init__(self):\n", + " II.__init__(self)\n", + " print \"Zero argument constructor of base class III\"\n", + " \n", + "i=III() #instance of the class III" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Zero argument constructor of base class I\n", + "Zero argument constructor of base class II\n", + "Zero argument constructor of base class III\n" + ] + } + ], + "prompt_number": 18 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.25, Page Number:484" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#use object of one class in another class as a member\n", + "class I:\n", + " def __init__(self):\n", + " self.x=20\n", + " print \"Constructor of class I\"\n", + " \n", + "class II(I):\n", + " \n", + " def __init__(self):\n", + " self.k=30\n", + " y=I()\n", + " print \"Constructor of class II\"\n", + " print \"x=\",y.x #print here because it become local variable in this scope only,it not visible to def show\n", + " \n", + " \n", + " def show(self):\n", + " print \"k=\",self.k\n", + " \n", + "ii=II()\n", + "ii.show() #**NOTE:Python destuctor is called when program goes exit. So output may be differ than c++" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Constructor of class I\n", + "Constructor of class II\n", + "x= 20\n", + "k= 30\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.26, Page Number:484" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#access a member variable of base class using object,class name, and direct\n", + "#Base class\n", + "class A1:\n", + " def __init__(self): #constructor of the base class\n", + " self.name=None\n", + " self.age=None\n", + "\n", + "#Derived class\n", + "class A2(A1):\n", + " def __init__(self):\n", + " A1.__init__(self)\n", + " a=A1() #create the instances of the base classe,to visible in this block\n", + " print \"Access using name of the class:\"\n", + " A1.name=raw_input(\"Name:\")\n", + " A1.age=raw_input(\"Age:\")\n", + " \n", + " print \"Access using object of the class\"\n", + " a.name=raw_input(\"Name:\")\n", + " a.age=raw_input(\"Age:\")\n", + " \n", + " print \"Access using direct member variables:\"\n", + " self.name=raw_input(\"Name:\")\n", + " self.age=raw_input(\"Age:\")\n", + " self.__height=raw_input(\"Height:\")\n", + " self.__weight=raw_input(\"Weight:\")\n", + " \n", + " print \"Display using object of the class\" #since object of class A1 has scope in constructor method so we can access it only \n", + " print \"Name:\",a.name # within this method.It is not visible in destructor function.\n", + " print \"Age:\",a.age\n", + " \n", + " \n", + " \n", + " def __del__(self):\n", + " print \"Destructor of Derived class\"\n", + " print \"Display using class name\"\n", + " print \"Name:\",A1.name\n", + " print \"Age:\",A1.age\n", + " \n", + " print \"Display using direct member variable\"\n", + " print \"Name:\",self.name\n", + " print \"Age\",self.age\n", + " print \"height:\",self.__height\n", + " print \"Weight:\",self.__weight\n", + " \n", + "x=A2()\n", + "\n", + "del x #call the destructor of the derived class\n", + " " + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Access using name of the class:\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Name:Ajay\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Age:21\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Access using object of the class\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Name:Amit\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Age:20\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Access using direct member variables:\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Name:Arun\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Age:19\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Height:5.5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Weight:31\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Display using object of the class\n", + "Name: Amit\n", + "Age: 20\n", + "Destructor of Derived class\n", + "Display using class name\n", + "Name: Ajay\n", + "Age: 21\n", + "Display using direct member variable\n", + "Name: Arun\n", + "Age 19\n", + "height: 5.5\n", + "Weight: 31\n" + ] + } + ], + "prompt_number": 20 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.27, Page Number:488" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#derive a class from two base classes,object of these 2 classes is the member variable of the third class\n", + "#Base1 class\n", + "class A:\n", + " def __init__(self):\n", + " self.a1=None\n", + "#Base2 class \n", + "class B:\n", + " def __init__(self):\n", + " self.b1=None\n", + "#derived class\n", + "class AB:\n", + " def __init__(self):\n", + " a=A() #create the instances of the base classes,to visible in this block\n", + " b=B()\n", + " a.a1=65 #initialize the two data members of the class A and B and Display them\n", + " b.b1=66\n", + " print \"a1=\",a.a1, \"b1=\",b.b1\n", + " \n", + " def __del__(self):\n", + " pass\n", + " \n", + " \n", + "ab=AB() #ab is the instance of the derived class" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "a1= 65 b1= 66\n" + ] + } + ], + "prompt_number": 21 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.28, Page Number:489" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#create derived class from qualifier class\n", + "#Base class,container class for the class B\n", + "class A:\n", + " def __init__(self):\n", + " self.x=None\n", + " \n", + " class B: #nested class\n", + " def __init__(self):\n", + " self.y=None\n", + " \n", + " \n", + "class C(A,A.B): #A.B is the inner class of the class A\n", + " def __init__(self,j,k,l):\n", + " self.x=j #set the container base class data member\n", + " self.y=k #set the data member of the nested class\n", + " self.z=l\n", + " \n", + " def show(self): #show method to show all the data members\n", + " print \"x=\",self.x,\"y=\",self.y,\"z=\",self.z\n", + " \n", + " \n", + "c=C(4,7,1) #assign all the data members by invocation of the derived class constructor\n", + "c.show()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "x= 4 y= 7 z= 1\n" + ] + } + ], + "prompt_number": 23 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.29, Page Number:490" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#initialize member variable of the base class and derived class using constructor of the derived class\n", + "#Base class\n", + "class A:\n", + " def __init__(self): #constructor\n", + " self._x=None #protected members\n", + " self._y=None\n", + " \n", + "class B(A):\n", + " def __init__(self): #derived class constructor\n", + " self.z=3 \n", + " self.__x=1 #private members\n", + " self.__y=2\n", + " \n", + " print \"x=\",self.__x,\"y=\",self.__y,\"z=\",self.z\n", + " \n", + "b=B() #instance of the derived class" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "x= 1 y= 2 z= 3\n" + ] + } + ], + "prompt_number": 24 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.30, Page Number:491" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#access data members by object pointer\n", + "\n", + "from ctypes import *\n", + "import ctypes\n", + "class A:\n", + " def __init__(self):\n", + " self.x=1\n", + " self.y=2\n", + " \n", + "class B(A):\n", + " def __init__(self):\n", + " A.__init__(self)\n", + " self.z=3\n", + " \n", + "b=B()\n", + "\n", + "\n", + "i=c_int(b.z)\n", + "p=pointer(i)\n", + "print \"Address of z:\",addressof(p),\"Value of Z:\",p[0] #access the\n", + "\n", + "i = c_int(b.y)\n", + "p = pointer(i)\n", + "print \"Address of y:\",addressof(p),\"Value of y:\",p[0] \n", + "\n", + "i = c_int(b.x)\n", + "p = pointer(i)\n", + "print \"Address of x:\",addressof(p),\"Value of x:\",p[0] \n", + "\n", + "#**NOTE-In case of C++ the data members of the derived class and base class are stored in contigious memory locations so we can \n", + "#access the three variables by using a pointer of derived class and decrementing its value. But in case of Python they are NOT stored \n", + "#in contogious memory locations so for accessing each data member we have to create individual object pointer for each class." + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Address of z: 57077392 Value of Z: 3\n", + "Address of y: 57074448 Value of y: 2\n", + "Address of x: 57077648 Value of x: 1\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.31, Page Number:492" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#overload member function in base and derived class\n", + "#Base class\n", + "class B:\n", + " def show(self): #method to be ovveriden\n", + " print \"In base class function\"\n", + " \n", + "class D(B):\n", + " def show(self): #Derived class method,ovveride to the base class\n", + " \n", + " print \"In Derived class\"\n", + " \n", + " \n", + "b=B() #b is the base class instance\n", + "d=D() #d is the derived class instance\n", + "\n", + "b.show()\n", + "d.show()\n", + "\n", + "bp=[B()] #create a base class pointer variable\n", + "bp[0]=d #assign address of the derived class object to the base class pointer\n", + "bp[0].show() #call the derived class method by base class pointer\n", + "b.show() #calling the base class method by base class object" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "In base class function\n", + "In Derived class\n", + "In Derived class\n", + "In base class function\n" + ] + } + ], + "prompt_number": 25 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.32, Page Number:495" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Constuctor and destructors in single inheritance\n", + "\n", + "#Base class\n", + "class Father:\n", + " def __init__(self): #Constructor to set the data member name\n", + " print \"Base Class constructor.\"\n", + " self._name=raw_input(\"Enter Father Name:\")\n", + " \n", + " def __del__(self):\n", + " print \"Base class Destructor.\"\n", + "\n", + "#Derived class\n", + "class Child(Father):\n", + " def __init__(self):#Constructor to set the data member cname\n", + " Father.__init__(self) #invocation of base class constructor\n", + " print \"Derived class constructor.\"\n", + " self.__cname=raw_input(\"Enter child name:\")\n", + " \n", + " def __del__(self): #destructor to set the data member cname\n", + " print \"Derived class destructor.\"\n", + " print \"\",self.__cname,\"\",self.__name \n", + " for b in self.__class__.__bases__: #invocation of base class destructor\n", + " b.__del__(self)\n", + " \n", + " \n", + " \n", + "C=Child()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Base Class constructor.\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter Father Name:Manoj\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Derived class constructor.\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter child name:Sanjay\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Derived class destructor.\n", + " Sanjay Manoj\n", + "Base class Destructor.\n" + ] + } + ], + "prompt_number": 37 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.33, Page Number:496" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Constuctor and destructors in multilevel inheritance\n", + "#Base class\n", + "class Grandfather:\n", + " def __init__(self): #Constructor to set the data member gname\n", + " print\"Constructor of class grandfather\"\n", + " self._gname=raw_input(\"Enter Grandfather Name:\")\n", + " \n", + " \n", + " def __del__(self): #Destructor to show the data member gname\n", + " print \"Destructor of class grandfather\"\n", + " \n", + " \n", + "#Derived1 class\n", + "class Father(Grandfather):\n", + " def __init__(self): #Constructor to set the data member name\n", + " Grandfather.__init__(self) #invocation of base class constructor\n", + " print\"Constructor of class Father\"\n", + " self._name=raw_input(\"Enter Father Name:\")\n", + " \n", + " \n", + " def __del__(self): #Destructor to show the data member name\n", + " print \"Destructor of class Father\"\n", + " Grandfather.__del__(self) #invocation of base class destructor\n", + " \n", + "#Derived2 class\n", + "class Child(Father):\n", + " def __init__(self): #Constructor to set the data member cname\n", + " Father.__init__(self) #invocation of base class constructor\n", + " print\"Constructor of class Child\"\n", + " self.__cname=raw_input(\"Enter Child Name:\")\n", + " \n", + " \n", + " def __del__(self): #Destructor to show the data member name\n", + " print \"Destructor of class Child\"\n", + " print \"Grandfather:\",self._gname,\"Father:\",self._name,\"Child:\",self.__cname\n", + " Father.__del__(self) #invocation of base class destructor\n", + " \n", + "#instance of the Derived2 class \n", + "C=Child()\n", + "\n", + "del C #call the destructor of the derived class\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Constructor of class grandfather\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter Grandfather Name:x\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Constructor of class Father\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter Father Name:y\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Constructor of class Child\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter Child Name:z\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Destructor of class Child\n", + "Grandfather: x Father: y Child: z\n", + "Destructor of class Father\n", + "Destructor of class grandfather\n" + ] + } + ], + "prompt_number": 39 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.34, Page Number:498" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#program to explain multilevel inheritance with member function\n", + "\n", + "#Base class\n", + "class Grandfather:\n", + " def __init__(self):\n", + " self.__gname=None\n", + " \n", + " def getg(self): #method to set the data member gname\n", + " self.__gname=raw_input(\"Enter Grandfather Name:\")\n", + " \n", + " def showg(self):#method to show the data member gname\n", + " print \"Grandfather Name:\",self.__gname\n", + " \n", + " \n", + "#Derived1 class\n", + "class Father(Grandfather):\n", + " def __init__(self):\n", + " self.__name=None\n", + " \n", + " def getf(self): #method to set the data member name\n", + " self.__name=raw_input(\"Enter Father Name:\")\n", + " \n", + " def showf(self): #method to show the data member name\n", + " print \"Father Name:\",self.__name\n", + " \n", + "#Derived2 class \n", + "class Child(Father):\n", + " def __init__(self):\n", + " self.__cname=None\n", + " \n", + " def getc(self): #method for invocation of base class methods and set the data member cname\n", + " self.getg()\n", + " self.getf()\n", + " self.__cname=raw_input(\"Enter Child Name:\")\n", + " \n", + " def showc(self): #method for invocation of base class methods and set the data member cname\n", + " self.showg()\n", + " self.showf()\n", + " print \"child Name:\",self.__cname\n", + " \n", + "C=Child() #cretaion of a instance of Derived2 class\n", + "C.getc() #invocation of the method of Derived2 class for setting all the data member value\n", + "C.showc()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter Grandfather Name:XXX\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter Father Name:YYY\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter Child Name:ZZZ\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Grandfather Name: XXX\n", + "Father Name: YYY\n", + "child Name: ZZZ\n" + ] + } + ], + "prompt_number": 40 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.35, Page Number:499" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#execution of constructor and destructor in multilevel inheritance\n", + "\n", + "#Base1 class\n", + "class A:\n", + " def __init__(self):\n", + " print \"Constructor of class A\"\n", + " \n", + " def __del__(self):\n", + " print \"Destructor of class A\"\n", + " \n", + "#Base2 class \n", + "class B:\n", + " def __init__(self):\n", + " print \"Constructor of class B\"\n", + " \n", + " def __del__(self):\n", + " print \"Destructor of class B\"\n", + "#Base3 class \n", + "class C:\n", + " def __init__(self):\n", + " print \"Constructor of class C\"\n", + " \n", + " def __del__(self):\n", + " print \"Destructor of class C\"\n", + " \n", + "#Derived class,Multiple Derivation \n", + "class D(A,B,C):\n", + " def __init__(self):\n", + " for b in self.__class__.__bases__: #invoke all the base class constructors\n", + " b.__init__(self) \n", + " print \"Constructor of class D\"\n", + " \n", + " def __del__(self):\n", + " print \"Destructor of class D\" #invoke all the base class destructors\n", + " for b in self.__class__.__bases__:\n", + " b.__del__(self)\n", + " \n", + "x=D() \n", + " #**NOTE:Python destuctor is called when program goes exit. So output may be differ than c++" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Constructor of class A\n", + "Constructor of class B\n", + "Constructor of class C\n", + "Constructor of class D\n", + "Destructor of class D\n", + "Destructor of class A\n", + "Destructor of class B\n", + "Destructor of class C\n" + ] + } + ], + "prompt_number": 42 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.37, Page Number:502" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#execution of constructor and destructor in multilevel inheritance\n", + "\n", + "#Base class\n", + "class A:\n", + " def __init__(self):\n", + " print \"Constructor of class A\"\n", + " \n", + " def __del__(self):\n", + " print \"Destructor of class A\"\n", + " \n", + "#Derived1 class\n", + "class B(A):\n", + " def __init__(self):\n", + " A.__init__(self)\n", + " print \"Constructor of class B\"\n", + " \n", + " def __del__(self):\n", + " print \"Destructor of class B\"\n", + " A.__del__(self)\n", + "\n", + "#Derived2 class\n", + "class C:\n", + " def __init__(self):\n", + " print \"Constructor of class C\"\n", + " \n", + " def __del__(self):\n", + " print \"Destructor of class C\"\n", + " \n", + "#Derived3 class,multiple derivation\n", + "class D(B,C):\n", + " def __init__(self):\n", + " B.__init__(self)\n", + " C.__init__(self)\n", + " print \"Constructor of class D\"\n", + " \n", + " def __del__(self):\n", + " print \"Destructor of class D\"\n", + " B.__del__(self)\n", + " C.__del__(self)\n", + " \n", + "x=D() #creation of Derived3 class instance\n", + "del x\n", + " #**NOTE:Python destuctor is called when program goes exit. So output may be differ than c++" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Constructor of class A\n", + "Constructor of class B\n", + "Constructor of class C\n", + "Constructor of class D\n", + "Destructor of class D\n", + "Destructor of class B\n", + "Destructor of class A\n", + "Destructor of class C\n" + ] + } + ], + "prompt_number": 54 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.37, Page Number:502" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#demonstrate single inheritance\n", + "\n", + "#Base class\n", + "class A:\n", + " def __init__(self,j=0):\n", + " self._c=j #protected Member\n", + " \n", + " def show(self): #Public member \n", + " print \"c=\",self._c\n", + " \n", + " def inc(self): #public increment method of base class\n", + " self._c=self._c+1\n", + " return self._c\n", + " \n", + "class B(A):\n", + " \n", + " def __init_(self):\n", + " for b in self.__class__.__bases__:\n", + " b.__init__(self)\n", + " \n", + " def dec(self): #public increment method of derived class\n", + " self._c=self._c-1\n", + " return self._c\n", + " \n", + " \n", + "a=B() #create a instance of a derived class\n", + "\n", + "a.inc() #call the base class public member function by derived class instance for increment\n", + "a.show()\n", + "\n", + "\n", + "a.dec() #call the derived class public member function by derived class instance for decrement\n", + "a.show()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "c= 1\n", + "c= 0\n" + ] + } + ], + "prompt_number": 44 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.38, Page Number:502" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#access method from private inheritance\n", + "\n", + "#base class\n", + "class B:\n", + " def one(self): #Public Member\n", + " print \"one\"\n", + " \n", + " def __two(self): #Private Member\n", + " print \"two\"\n", + " \n", + "class D(B):\n", + " def __init__(self):\n", + " pass\n", + " \n", + "d=D()\n", + "d.one()\n", + "#d.two() #Not accesible" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "one\n" + ] + } + ], + "prompt_number": 45 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.39, Page Number:503" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#create a comman constructor in the lowermost class, in the multilevel inheritance\n", + "\n", + "#Base class\n", + "class A:\n", + " def __init__(self):\n", + " self.x=None\n", + " \n", + "#Derived1 class\n", + "class B(A):\n", + " def __init__(self):\n", + " self.y=None\n", + "\n", + "#Derived2 class\n", + "class C(B):\n", + " def __init__(self,j,k,l):\n", + " self.z=l #Initializing the base class data members by calling base class constructor\n", + " self.x=j\n", + " self.y=k\n", + " \n", + " def show(self):\n", + " print \"x=\",self.x,\"Y=\",self.y,\"z=\",self.z\n", + "\n", + "#Creation of instance of derived2 class,with constructor invocation \n", + "c=C(4,7,1)\n", + "c.show()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "x= 4 Y= 7 z= 1\n" + ] + } + ], + "prompt_number": 46 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.40, Page Number:504" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Explicitly call the base constructor in multiple inheritance\n", + "\n", + "#Base class\n", + "class X:\n", + " def __init__(self,a):\n", + " print a,\n", + "#Base class \n", + "class Y:\n", + " def __init__(self,b):\n", + " print b,\n", + "#multiple Derivation \n", + "class Z(X,Y):\n", + " def __init__(self,p,q,r):\n", + " X.__init__(self,p)\n", + " Y.__init__(self,q)\n", + " print r\n", + " \n", + "#Creation of instance of derived class,with constructor invocation \n", + "z=Z(1,2,3)\n", + " " + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "1 2 3\n" + ] + } + ], + "prompt_number": 47 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [], + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/sample_notebooks/AJEET KUMARSINGH/Chapter_11_Inheritance_3.ipynb b/sample_notebooks/AJEET KUMARSINGH/Chapter_11_Inheritance_3.ipynb new file mode 100755 index 00000000..41ddb0e8 --- /dev/null +++ b/sample_notebooks/AJEET KUMARSINGH/Chapter_11_Inheritance_3.ipynb @@ -0,0 +1,2669 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:e47f32c0e16bc3f4815d950d4b9461754960bd5d341df7cb91b2b6bbb77174c7" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 11: Inheritance" + ] + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.1, Page Number:444" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Public Derivation of a class\n", + "class A: #Base class\n", + " def __init__(self):\n", + " self.x=None\n", + "\n", + " \n", + "class B(A): #derived class\n", + " def __init__(self):\n", + " self.y=None\n", + " \n", + "b=B() #declaration of object\n", + "b.x=20\n", + "b.y=30\n", + "\n", + "print 'member of A:',b.x\n", + "print 'Member of B:',b.y" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "member of A: 20\n", + "Member of B: 30\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.2, Page Number:445" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Base class\n", + "class A: #Base class\n", + " def __init__(self): #class A having x as a private data member\n", + " self.__x=20\n", + " \n", + " def showx(self):\n", + " print \"x=\",self.__x\n", + " \n", + "#derived class \n", + "class B(A): #Derived class\n", + " def __init__(self):\n", + " self.y=30 #class B having y as a public data member\n", + " \n", + " def show(self):\n", + " a=A()\n", + " a.showx()\n", + " print \"y=\",self.y\n", + " \n", + " \n", + "b=B() #declaration of object\n", + " \n", + "b.show()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "x= 20\n", + "y= 30\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.3, Page Number:447" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#base class\n", + "class A:\n", + " def __init__(self):\n", + " self.x=None #x is a public member\n", + " \n", + " \n", + "#derived class\n", + "class B(A):\n", + " def __init__(self):\n", + " self.y=40 \n", + " A.__x=20 #since it is privately inherites base class ,x become private member of it\n", + " \n", + " def show(self):\n", + " print \"x=\",A.__x\n", + " print \"y=\",self.y\n", + " \n", + "b=B() #declaration of object\n", + "b.show()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "x= 20\n", + "y= 40\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.4, Page Number:448" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#derivation of a class privately\n", + "class A:\n", + " def __init__(self):\n", + " self.__x=20 \n", + " \n", + " def showx(self): \n", + " print \"x=\",self.__x\n", + " \n", + "#derived class \n", + "class B(A):\n", + " def __init__(self):\n", + " self.y=40 \n", + " \n", + " def show(self):\n", + " a=A()\n", + " a.showx() #call the base class method\n", + " print \"y=\",self.y\n", + " \n", + " \n", + "b=B()\n", + "b.show()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "x= 20\n", + "y= 40\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.5, Page Number:449" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class A:\n", + " def __init__(self):\n", + " self._x=None #x is a protected member of the base class\n", + " \n", + " \n", + "class B(A): #private inheritance,x become a private member of the derived class\n", + " def __init__(self):\n", + " self.y=40\n", + " self.__x=30\n", + " \n", + " \n", + " def show(self): #method to display all the values of all the data memeber\n", + " print \"x=\",self.__x\n", + " print \"y=\",self.y\n", + " \n", + " #declaration of object \n", + "b=B()\n", + "b.show()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "x= 30\n", + "y= 40\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.6, Page Number:456" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class ABC: #Base class\n", + " def __init__(self):\n", + " self._name=None #these 2 are protected data member\n", + " self._age=None\n", + " \n", + "class abc(ABC): #Derived class ,Public derivation\n", + " def __init__(self):\n", + " self.height=None\n", + " self.weight=None\n", + " \n", + " def getdata(self):\n", + " \n", + " self.name=raw_input(\"Enter a name: \") #take inputes to all the data members \n", + " self.age=raw_input(\"Enter a age: \") \n", + " self._height=raw_input(\"Enter a Height: \") \n", + " self._weight=raw_input(\"Enter a Weight: \") \n", + " \n", + " def show(self): #display the value of data members\n", + " print 'Name:',self.name \n", + " print 'Age:',self.age,\"years\"\n", + " print 'Height:',self._height,\"Feets\"\n", + " print 'Weight:',self._weight,\"kg.\"\n", + " \n", + " \n", + "x=abc()\n", + "x.getdata()\n", + "x.show()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter a name: Santosh\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter a age: 24\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter a Height: 4.5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter a Weight: 50\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Name: Santosh\n", + "Age: 24 years\n", + "Height: 4.5 Feets\n", + "Weight: 50 kg.\n" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.7, Page Number:458" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class A1: #super Base class,have 2 protected data members\n", + " def __init__(self):\n", + " self._name=None\n", + " self._age=None\n", + "\n", + " \n", + "class A2(A1): #Public derivation\n", + " def __init(self):\n", + " self._height=None\n", + " self._weight=None\n", + "\n", + "class A3(A2): #public Derivation\n", + " def __init__(self):\n", + " self._sex=None\n", + " \n", + " \n", + " def get(self): #get input \n", + " self._name=raw_input(\"Name: \")\n", + " self._age=raw_input(\"Age: \")\n", + " self._sex=raw_input(\"Sex: \")\n", + " \n", + " self._height=raw_input(\"Height: \")\n", + " self._weight=raw_input(\"Weight: \")\n", + " \n", + " def show(self): #Display values of all the data members\n", + " print \"Name:\",self._name\n", + " print \"Age:\",self._age ,\"years\"\n", + " print \"Sex:\",self._sex\n", + " print \"Height:\",self._height ,\"Feet\"\n", + " print \"Weight:\",self._weight ,\"Kg.\"\n", + " \n", + "\n", + "x=A3()\n", + "x.get()\n", + "x.show()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Name: Balaji\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Age: 26\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Sex: M\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Height: 4\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Weight: 49.5\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Name: Balaji\n", + "Age: 26 years\n", + "Sex: M\n", + "Height: 4 Feet\n", + "Weight: 49.5 Kg.\n" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.8, Page Number:459" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Example of multiple Inheritance\n", + "#Base1 class\n", + "class A:\n", + " def __init__(self):\n", + " self._a=None\n", + "#Base2 class\n", + "class B:\n", + " def __init__(self):\n", + " self._b=None\n", + "#Base3 class\n", + "class C:\n", + " def __init__(self):\n", + " self._c=None\n", + "#Base4 class \n", + "class D:\n", + " def __init__(self):\n", + " self._d=None\n", + "#derived class,multiple derivation\n", + "class E(A,B,C,D): #inherites all the base classes publically\n", + " def __init__(self):\n", + " self.e=None\n", + " \n", + " def getdata(self): #member method to take input for all the data members \n", + " print \"Enter the value of a,b,c &d &e:\"\n", + " self._a=input()\n", + " self._b=input()\n", + " self._c=input()\n", + " self._d=input()\n", + " self._e=input()\n", + " \n", + " def show(self): #member method to display for all the data members \n", + " print\"a=\",self._a,\"b=\",self._b,\"c=\",self._c,\"d=\",self._d,\"e=\",self._e\n", + " \n", + " \n", + "x=E() #x is the instance of the derived class\n", + "x.getdata() #call the methods of derived class through x \n", + "x.show()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the value of a,b,c &d &e:\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "4\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "8\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "16\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "a= 1 b= 2 c= 4 d= 8 e= 16\n" + ] + } + ], + "prompt_number": 8 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.9, Page Number:461" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class red: #these three base class\n", + " def __init__(self):\n", + " print \"Red\",\n", + " \n", + "class yellow:\n", + " def __init__(self):\n", + " print \"Yellow\",\n", + " \n", + "class blue:\n", + " def __init__(self):\n", + " print \"Blue\",\n", + " \n", + "class orange(red,yellow): #public multiple Derivation\n", + " def __init__(self):\n", + " red.__init__(self)\n", + " yellow.__init__(self)\n", + " print \"=Orange\",\n", + " \n", + "class green(blue,yellow): #public multiple Derivation\n", + " def __init__(self):\n", + " blue.__init__(self)\n", + " yellow.__init__(self)\n", + " print \"=Green\",\n", + " \n", + "class violet(red,blue): #public multiple Derivation\n", + " def __init__(self):\n", + " red.__init__(self)\n", + " blue.__init__(self)\n", + " print \"=Violet\",\n", + " \n", + "class reddishbrown(orange,violet): #public multiple & multilevel Derivation\n", + " def __init__(self):\n", + " for b in self.__class__.__bases__:\n", + " b.__init__(self)\n", + " print \"=Reddishbrown\"\n", + " \n", + "class yellowishbrown(green,orange): #public multiple & multilevel Derivation\n", + " def __init__(self):\n", + " for b in self.__class__.__bases__:\n", + " b.__init__(self)\n", + " print \"=Yellowishbrown\"\n", + " \n", + "class bluishbrown(violet,green): #public multiple & multilevel Derivation\n", + " def __init__(self):\n", + " for b in self.__class__.__bases__:\n", + " b.__init__(self)\n", + " print \"=Bluishbrown\"\n", + " \n", + " \n", + " \n", + "r=reddishbrown() #create instances of the derived class\n", + "b=bluishbrown()\n", + "y=yellowishbrown()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Red Yellow =Orange Red Blue =Violet =Reddishbrown\n", + "Red Blue =Violet Blue Yellow =Green =Bluishbrown\n", + "Blue Yellow =Green Red Yellow =Orange =Yellowishbrown\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.10, Page Number:463" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# WAP to create a derived class from multiple base classes\n", + "\n", + "class PLAYER: #these three are the base classes\n", + " def __init__(self):\n", + " self._name=None\n", + " self._gender=None\n", + " self._age\n", + " \n", + "class PHYSIQUE(PLAYER):\n", + " def __init__(self):\n", + " self._height=None\n", + " self._weight=None\n", + " \n", + "class LOCATION:\n", + " def __init__(self):\n", + " self._city=None\n", + " self._pin=None\n", + " \n", + "class GAME(PHYSIQUE,LOCATION): #Multiple derivation\n", + " def __init__(self):\n", + " self._game=None\n", + " def getdata(self): #Method to take inputes\n", + " print\"Enter the following information\\n\\n\"\n", + " self._name=raw_input(\"Name:\")\n", + " self._gender=raw_input(\"Gender:\")\n", + " self._age=raw_input(\"Age:\")\n", + " self._height=raw_input(\"Height:\")\n", + " self._weight=raw_input(\"Weight:\")\n", + " self._city=raw_input(\"City:\")\n", + " self._pin=raw_input(\"Pin:\")\n", + " self._game=raw_input(\"game:\")\n", + " \n", + " \n", + " \n", + " def show(self): #Method for displaying inputes\n", + " print\"Entered Information!!\"\n", + " print\"Name:\",self._name\n", + " print \"Gender:\",self._gender\n", + " print \"Age:\",self._age\n", + " print \"Height:\",self._height\n", + " print \"Weight:\",self._weight\n", + " print \"City :\",self._city\n", + " print \"Pincode:\",self._pin\n", + " print \"Game :\",self._game\n", + " \n", + " \n", + "G=GAME() #create an instance of the derived class\n", + "G.getdata() #call the public methods by the created instances\n", + "G.show()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the following information\n", + "\n", + "\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Name:Mahesh\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Gender:M\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Age:25\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Height:4.9\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Weight:55\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "City:Nanded\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Pin:431603\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "game:Cricket\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Entered Information!!\n", + "Name: Mahesh\n", + "Gender: M\n", + "Age: 25\n", + "Height: 4.9\n", + "Weight: 55\n", + "City : Nanded\n", + "Pincode: 431603\n", + "Game : Cricket\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.11, Page Number:467" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Multipath Inheritance,concept of virtual classes\n", + "\n", + "class A1: #Super base class\n", + " def __init__(self):\n", + " self._a1=None\n", + " \n", + "class A2(A1): #base class 1,inherites Super Base class\n", + " def __init__(self):\n", + " self._a2=None\n", + " \n", + "class A3(A1): #base class 2,inherites Super Base class\n", + " def __init__(self):\n", + " self._a3=None\n", + " \n", + "class A4(A2,A3): #derived class ,public derivation of both the base classes\n", + " def __init__(self):\n", + " self.__a4=None\n", + " \n", + " def get(self):\n", + " print \"Enter the value of a1,a2,a3,and a4:\"\n", + " self._a1=input()\n", + " self._a2=input()\n", + " self._a3=input()\n", + " self.__a4=input()\n", + " \n", + " def put(self):\n", + " print \"a1=\",self._a1,\"a2=\",self._a2,\"a3=\",self._a3,\"a4=\",self.__a4\n", + " \n", + " \n", + " \n", + "a=A4() #create the instance of the derived class\n", + "a.get()\n", + "a.put()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the value of a1,a2,a3,and a4:\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "8\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "7\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "a1= 5 a2= 8 a3= 7 a4= 3\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.12, Page Number:469" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#To show order of execution of the constructors and destructors in multiple inheritance\n", + "\n", + "#Base1 class\n", + "class A:\n", + " def __init__(self):\n", + " print\"Zero argument Constructor of base class A\"\n", + " \n", + " def __del__(self):\n", + " print\"Destructor of class A\"\n", + "#Base2 class \n", + "class B:\n", + " def __init__(self):\n", + " print\"Zero argument Constructor of base class B\"\n", + " \n", + " def __del__(self):\n", + " print\"Destructor of class B\"\n", + "#derived class,multiple derivation\n", + "class C(A,B):\n", + " def __init__(self):\n", + " for b in self.__class__.__bases__: #invocation of the constructor of all the base classes\n", + " b.__init__(self)\n", + " print\"Zero argument Constructor of base class C\"\n", + " \n", + " def __del__(self): \n", + " print\"Destructor of class C\"\n", + " for b in self.__class__.__bases__: #invocation of the destructor of all the base classes\n", + " b.__del__(self)\n", + " \n", + "c=C() #create instance of derived class" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Zero argument Constructor of base class A\n", + "Zero argument Constructor of base class B\n", + "Zero argument Constructor of base class C\n", + "Destructor of class C\n", + "Destructor of class A\n", + "Destructor of class B\n" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.13, Page Number:471" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#WAP to use constructor and destructor in all the classess\n", + "\n", + "class A1:\n", + " def __init__(self): #take name and age as input in super base class\n", + " self._name=raw_input(\"Name:\")\n", + " self._age=raw_input(\"Age:\")\n", + " \n", + " def __del__(self): #show name and age as input in super base class\n", + " print\"Name:\",self._name\n", + " print\"Age\",self._age\n", + " \n", + " \n", + "class A2(A1): #take height and weight as input in base base class,public derivation \n", + " def __init__(self):\n", + " A1.__init__(self)\n", + " self._height=raw_input(\"Height:\")\n", + " self._weight=raw_input(\"Weight:\")\n", + " \n", + " def __del__(self): #show height and weight as input in base base class,public derivation \n", + " print\"Height:\",self._height\n", + " print\"Weight:\",self._weight\n", + " A1.__del__(self)\n", + " \n", + " \n", + "class A3(A2): #take sex as input in derived class,derived from class A2\n", + " def __init__(self):\n", + " A2.__init__(self)\n", + " self.__sex=raw_input(\"Sex:\")\n", + " def __del__(self): #display all the input taken by all the base classes\n", + " print\"Sex:\",self.__sex\n", + " A2.__del__(self)\n", + " \n", + " \n", + "x=A3() #create instance x of the class A3\n", + "\n", + "del x #call the destructor" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Name:Ajay\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Age:20\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Height:4.5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Weight:40\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Sex:M\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Sex: M\n", + "Height: 4.5\n", + "Weight: 40\n", + "Name: Ajay\n", + "Age 20\n" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.14, Page Number:472" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#To create derived class from the base class,by constructor and destructor\n", + "#Base1 class\n", + "class in_t:\n", + " def __init__(self): #constructor of base1 class\n", + " self._i=1\n", + " print\"Constructor in_t()\"\n", + " \n", + " def __del__(self): #destructor of base1 class\n", + " print\"Destructor in_t()\"\n", + "\n", + "#Base2 class\n", + "class floa_t:\n", + " def __init__(self): #constructor of base2 class\n", + " self._f=1.5\n", + " print\"Constructor floa_t()\"\n", + " \n", + " def __del__(self): #destructor of base2 class\n", + " print\"Destructor floa_t()\"\n", + " \n", + "#Derived class \n", + "class cha_r(in_t,floa_t): #multiple derivation\n", + " def __init__(self):\n", + " self._c='A'\n", + " print\"Constructor cha_r()\"\n", + " for b in self.__class__.__bases__: #invocation of the base class constructors\n", + " b.__init__(self)\n", + " \n", + " def show(self): #member method to show all the data member \n", + " print\"i=\",self._i\n", + " print \"f=\",self._f\n", + " print \"c=\",self._c\n", + " \n", + " def __del__(self): #Destructor of the derived cladd\n", + " print \"Destructing cha_r()\"\n", + " for b in self.__class__.__bases__:\n", + " b.__del__(self)\n", + " \n", + "a=cha_r() #create derived class instance and call the public method of the derived class\n", + "a.show() #**NOTE:Python destuctor is called when program goes exit. So output may be differ than c++" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Constructor cha_r()\n", + "Constructor in_t()\n", + "Constructor floa_t()\n", + "i= 1\n", + "f= 1.5\n", + "c= A\n" + ] + } + ], + "prompt_number": 8 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.15, Page Number:474" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Base class\n", + "class I:\n", + " def __init__(self):\n", + " self.x=None\n", + "#derived class \n", + "class II(I):\n", + " def __init__(self):\n", + " self.__y=None #private member of the classII\n", + " \n", + " def set(self,j,k): #Parametrized constructor \n", + " self.x=j\n", + " self.__y=k\n", + " \n", + " def show(self):\n", + " print \"X=\",self.x, \"Y=\",self.__y\n", + " \n", + " \n", + "i=II() #creation of instance of the Derived class\n", + "i.set(4,5) #invocation of the derived class member method by instance of the derived class\n", + "i.show()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "X= 4 Y= 5\n" + ] + } + ], + "prompt_number": 9 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.16, Page Number:475" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Base class\n", + "class I:\n", + " def __init__(self):\n", + " self.x=10\n", + " print \"In the Base class constuctor\"\n", + "#Derived class\n", + "class II(I):\n", + " def __init__(self):\n", + " I.__init__(self) #invocation of the base class constructor\n", + " self.__y=None\n", + " \n", + "i=II() #instance of the derived class" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "In the Base class constuctor\n" + ] + } + ], + "prompt_number": 10 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.17, Page Number:475" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#base class without constructor and derived class with constructor\n", + "class I:\n", + " pass #Empty body of the base class\n", + "class II(I):\n", + " def __init__(self):\n", + " print \"In derived class constructor\"\n", + " \n", + "i=II()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "In derived class constructor\n" + ] + } + ], + "prompt_number": 11 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.18, Page Number:476" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#both the class have constructor\n", + "class I:\n", + " def __init__(self):\n", + " print \"In base class Constructor\"\n", + "#Derived class \n", + "class II(I):\n", + " def __init__(self):\n", + " I.__init__(self)#invocation of the base class constructor\n", + " print \"In derived Class constructor\"\n", + " \n", + "i=II()#instance of the derived class" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "In base class Constructor\n", + "In derived Class constructor\n" + ] + } + ], + "prompt_number": 12 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.19, Page Number:477" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#multiple constructor in base class and single constructor in the derived class\n", + "#Base class\n", + "class I:\n", + " def __init__(self):\n", + " print \"Zero argument base class construtor\"\n", + " \n", + " def __init__(self,k): #parametrized constructor\n", + " self.x=None\n", + " print \"One argument base class construtor\"\n", + " \n", + "#Derived class \n", + "class II(I):\n", + " def __init__(self,j,k=None): #default constructor\n", + " I.__init__(self,k)\n", + " self.__y=j\n", + " print \"One argument derived class constructor\"\n", + " \n", + "i=II(2) #create the instance of the base class by passing initial value " + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "One argument base class construtor\n", + "One argument derived class constructor\n" + ] + } + ], + "prompt_number": 13 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.20, Page Number:478" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#base and derived class without default constructor\n", + "class I:\n", + " def __init__(self,k):\n", + " self.x=k\n", + " print \"One argument base class construtor\"\n", + "#Derived class \n", + "class II(I):\n", + " def __init__(self,j):\n", + " I.__init__(self,j) #invlocation of baser class constructor\n", + " self.__y=j\n", + " print \"One argument derived class construtor\"\n", + " \n", + "i=II(2) #create the instance of the base class by passing initial value " + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "One argument base class construtor\n", + "One argument derived class construtor\n" + ] + } + ], + "prompt_number": 14 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.21, Page Number:479" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Constructors and multiple inheritance\n", + "class I:\n", + " def __init__(self):\n", + " print \"Zero argument constructor of base class I\"\n", + " \n", + "class II:\n", + " def __init__(self):\n", + " print \"Zero argument constructor of base class II\"\n", + " \n", + "class III(II,I): #class III inhrites class II and I\n", + " def __init__(self):\n", + " for b in self.__class__.__bases__: #invocation \n", + " b.__init__(self) \n", + " print \"Zero argument constructor of base class III\"\n", + " \n", + "i=III() #create an instance of the base class\n", + " " + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Zero argument constructor of base class II\n", + "Zero argument constructor of base class I\n", + "Zero argument constructor of base class III\n" + ] + } + ], + "prompt_number": 15 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.22, Page Number:480" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Constructors in multiple inhritance with invoking constructor of the base classes\n", + "class I:\n", + " def __init__(self):\n", + " print \"Zero argument constructor of base class I\"\n", + " \n", + "class II:\n", + " def __init__(self):\n", + " print \"Zero argument constructor of base class II\"\n", + " \n", + "class III(II,I):\n", + " def __init__(self):\n", + " II.__init__(self) #invocation of base class constructors\n", + " I.__init__(self)\n", + " print \"Zero argument constructor of base class III\"\n", + " \n", + "i=III() #instaace of thr derived class\n", + " " + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Zero argument constructor of base class II\n", + "Zero argument constructor of base class I\n", + "Zero argument constructor of base class III\n" + ] + } + ], + "prompt_number": 16 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.23, Page Number:481" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#multiple inheritance,invoking the base classes explicitly\n", + "#Base1 class\n", + "class I:\n", + " def __init__(self):\n", + " print \"Zero argument constructor of base class I\"\n", + "#Base2 class\n", + "class II:\n", + " def __init__(self):\n", + " print \"Zero argument constructor of base class II\"\n", + " \n", + "class III(II,I): #Class I is virtually inherited so its constructor called first\n", + " def __init__(self):\n", + " I.__init__(self) #invocation of the base classes\n", + " II.__init__(self)\n", + " print \"Zero argument constructor of base class III\"\n", + " \n", + "i=III() #instane of the Derived class\n", + " " + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Zero argument constructor of base class I\n", + "Zero argument constructor of base class II\n", + "Zero argument constructor of base class III\n" + ] + } + ], + "prompt_number": 17 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.24, Page Number:482" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#multilevel Inheritance,observation of the execution of the constructors\n", + "class I:\n", + " def __init__(self):\n", + " print \"Zero argument constructor of base class I\"\n", + "#Derived1 class \n", + "class II(I):\n", + " def __init__(self):\n", + " I.__init__(self)\n", + " print \"Zero argument constructor of base class II\"\n", + " \n", + "class III(II): #Class I is virtually inherited so its constructor called first\n", + " def __init__(self):\n", + " II.__init__(self)\n", + " print \"Zero argument constructor of base class III\"\n", + " \n", + "i=III() #instance of the class III" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Zero argument constructor of base class I\n", + "Zero argument constructor of base class II\n", + "Zero argument constructor of base class III\n" + ] + } + ], + "prompt_number": 18 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.25, Page Number:484" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#use object of one class in another class as a member\n", + "class I:\n", + " def __init__(self):\n", + " self.x=20\n", + " print \"Constructor of class I\"\n", + " \n", + "class II(I):\n", + " \n", + " def __init__(self):\n", + " self.k=30\n", + " y=I()\n", + " print \"Constructor of class II\"\n", + " print \"x=\",y.x #print here because it become local variable in this scope only,it not visible to def show\n", + " \n", + " \n", + " def show(self):\n", + " print \"k=\",self.k\n", + " \n", + "ii=II()\n", + "ii.show() #**NOTE:Python destuctor is called when program goes exit. So output may be differ than c++" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Constructor of class I\n", + "Constructor of class II\n", + "x= 20\n", + "k= 30\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.26, Page Number:484" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#access a member variable of base class using object,class name, and direct\n", + "#Base class\n", + "class A1:\n", + " def __init__(self): #constructor of the base class\n", + " self.name=None\n", + " self.age=None\n", + "\n", + "#Derived class\n", + "class A2(A1):\n", + " def __init__(self):\n", + " A1.__init__(self)\n", + " a=A1() #create the instances of the base classe,to visible in this block\n", + " print \"Access using name of the class:\"\n", + " A1.name=raw_input(\"Name:\")\n", + " A1.age=raw_input(\"Age:\")\n", + " \n", + " print \"Access using object of the class\"\n", + " a.name=raw_input(\"Name:\")\n", + " a.age=raw_input(\"Age:\")\n", + " \n", + " print \"Access using direct member variables:\"\n", + " self.name=raw_input(\"Name:\")\n", + " self.age=raw_input(\"Age:\")\n", + " self.__height=raw_input(\"Height:\")\n", + " self.__weight=raw_input(\"Weight:\")\n", + " \n", + " print \"Display using object of the class\" #since object of class A1 has scope in constructor method so we can access it only \n", + " print \"Name:\",a.name # within this method.It is not visible in destructor function.\n", + " print \"Age:\",a.age\n", + " \n", + " \n", + " \n", + " def __del__(self):\n", + " print \"Destructor of Derived class\"\n", + " print \"Display using class name\"\n", + " print \"Name:\",A1.name\n", + " print \"Age:\",A1.age\n", + " \n", + " print \"Display using direct member variable\"\n", + " print \"Name:\",self.name\n", + " print \"Age\",self.age\n", + " print \"height:\",self.__height\n", + " print \"Weight:\",self.__weight\n", + " \n", + "x=A2()\n", + "\n", + "del x #call the destructor of the derived class\n", + " " + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Access using name of the class:\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Name:Ajay\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Age:21\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Access using object of the class\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Name:Amit\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Age:20\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Access using direct member variables:\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Name:Arun\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Age:19\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Height:5.5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Weight:31\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Display using object of the class\n", + "Name: Amit\n", + "Age: 20\n", + "Destructor of Derived class\n", + "Display using class name\n", + "Name: Ajay\n", + "Age: 21\n", + "Display using direct member variable\n", + "Name: Arun\n", + "Age 19\n", + "height: 5.5\n", + "Weight: 31\n" + ] + } + ], + "prompt_number": 20 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.27, Page Number:488" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#derive a class from two base classes,object of these 2 classes is the member variable of the third class\n", + "#Base1 class\n", + "class A:\n", + " def __init__(self):\n", + " self.a1=None\n", + "#Base2 class \n", + "class B:\n", + " def __init__(self):\n", + " self.b1=None\n", + "#derived class\n", + "class AB:\n", + " def __init__(self):\n", + " a=A() #create the instances of the base classes,to visible in this block\n", + " b=B()\n", + " a.a1=65 #initialize the two data members of the class A and B and Display them\n", + " b.b1=66\n", + " print \"a1=\",a.a1, \"b1=\",b.b1\n", + " \n", + " def __del__(self):\n", + " pass\n", + " \n", + " \n", + "ab=AB() #ab is the instance of the derived class" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "a1= 65 b1= 66\n" + ] + } + ], + "prompt_number": 21 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.28, Page Number:489" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#create derived class from qualifier class\n", + "#Base class,container class for the class B\n", + "class A:\n", + " def __init__(self):\n", + " self.x=None\n", + " \n", + " class B: #nested class\n", + " def __init__(self):\n", + " self.y=None\n", + " \n", + " \n", + "class C(A,A.B): #A.B is the inner class of the class A\n", + " def __init__(self,j,k,l):\n", + " self.x=j #set the container base class data member\n", + " self.y=k #set the data member of the nested class\n", + " self.z=l\n", + " \n", + " def show(self): #show method to show all the data members\n", + " print \"x=\",self.x,\"y=\",self.y,\"z=\",self.z\n", + " \n", + " \n", + "c=C(4,7,1) #assign all the data members by invocation of the derived class constructor\n", + "c.show()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "x= 4 y= 7 z= 1\n" + ] + } + ], + "prompt_number": 23 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.29, Page Number:490" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#initialize member variable of the base class and derived class using constructor of the derived class\n", + "#Base class\n", + "class A:\n", + " def __init__(self): #constructor\n", + " self._x=None #protected members\n", + " self._y=None\n", + " \n", + "class B(A):\n", + " def __init__(self): #derived class constructor\n", + " self.z=3 \n", + " self.__x=1 #private members\n", + " self.__y=2\n", + " \n", + " print \"x=\",self.__x,\"y=\",self.__y,\"z=\",self.z\n", + " \n", + "b=B() #instance of the derived class" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "x= 1 y= 2 z= 3\n" + ] + } + ], + "prompt_number": 24 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.30, Page Number:491" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#access data members by object pointer\n", + "\n", + "from ctypes import *\n", + "import ctypes\n", + "class A:\n", + " def __init__(self):\n", + " self.x=1\n", + " self.y=2\n", + " \n", + "class B(A):\n", + " def __init__(self):\n", + " A.__init__(self)\n", + " self.z=3\n", + " \n", + "b=B()\n", + "\n", + "\n", + "i=c_int(b.z)\n", + "p=pointer(i)\n", + "print \"Address of z:\",addressof(p),\"Value of Z:\",p[0] #access the\n", + "\n", + "i = c_int(b.y)\n", + "p = pointer(i)\n", + "print \"Address of y:\",addressof(p),\"Value of y:\",p[0] \n", + "\n", + "i = c_int(b.x)\n", + "p = pointer(i)\n", + "print \"Address of x:\",addressof(p),\"Value of x:\",p[0] \n", + "\n", + "#**NOTE-In case of C++ the data members of the derived class and base class are stored in contigious memory locations so we can \n", + "#access the three variables by using a pointer of derived class and decrementing its value. But in case of Python they are NOT stored \n", + "#in contogious memory locations so for accessing each data member we have to create individual object pointer for each class." + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Address of z: 57077392 Value of Z: 3\n", + "Address of y: 57074448 Value of y: 2\n", + "Address of x: 57077648 Value of x: 1\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.31, Page Number:492" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#overload member function in base and derived class\n", + "#Base class\n", + "class B:\n", + " def show(self): #method to be ovveriden\n", + " print \"In base class function\"\n", + " \n", + "class D(B):\n", + " def show(self): #Derived class method,ovveride to the base class\n", + " \n", + " print \"In Derived class\"\n", + " \n", + " \n", + "b=B() #b is the base class instance\n", + "d=D() #d is the derived class instance\n", + "\n", + "b.show()\n", + "d.show()\n", + "\n", + "bp=[B()] #create a base class pointer variable\n", + "bp[0]=d #assign address of the derived class object to the base class pointer\n", + "bp[0].show() #call the derived class method by base class pointer\n", + "b.show() #calling the base class method by base class object" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "In base class function\n", + "In Derived class\n", + "In Derived class\n", + "In base class function\n" + ] + } + ], + "prompt_number": 25 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.32, Page Number:495" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Constuctor and destructors in single inheritance\n", + "\n", + "#Base class\n", + "class Father:\n", + " def __init__(self): #Constructor to set the data member name\n", + " print \"Base Class constructor.\"\n", + " self._name=raw_input(\"Enter Father Name:\")\n", + " \n", + " def __del__(self):\n", + " print \"Base class Destructor.\"\n", + "\n", + "#Derived class\n", + "class Child(Father):\n", + " def __init__(self):#Constructor to set the data member cname\n", + " Father.__init__(self) #invocation of base class constructor\n", + " print \"Derived class constructor.\"\n", + " self.__cname=raw_input(\"Enter child name:\")\n", + " \n", + " def __del__(self): #destructor to set the data member cname\n", + " print \"Derived class destructor.\"\n", + " print \"\",self.__cname,\"\",self.__name \n", + " for b in self.__class__.__bases__: #invocation of base class destructor\n", + " b.__del__(self)\n", + " \n", + " \n", + " \n", + "C=Child()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Base Class constructor.\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter Father Name:Manoj\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Derived class constructor.\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter child name:Sanjay\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Derived class destructor.\n", + " Sanjay Manoj\n", + "Base class Destructor.\n" + ] + } + ], + "prompt_number": 37 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.33, Page Number:496" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Constuctor and destructors in multilevel inheritance\n", + "#Base class\n", + "class Grandfather:\n", + " def __init__(self): #Constructor to set the data member gname\n", + " print\"Constructor of class grandfather\"\n", + " self._gname=raw_input(\"Enter Grandfather Name:\")\n", + " \n", + " \n", + " def __del__(self): #Destructor to show the data member gname\n", + " print \"Destructor of class grandfather\"\n", + " \n", + " \n", + "#Derived1 class\n", + "class Father(Grandfather):\n", + " def __init__(self): #Constructor to set the data member name\n", + " Grandfather.__init__(self) #invocation of base class constructor\n", + " print\"Constructor of class Father\"\n", + " self._name=raw_input(\"Enter Father Name:\")\n", + " \n", + " \n", + " def __del__(self): #Destructor to show the data member name\n", + " print \"Destructor of class Father\"\n", + " Grandfather.__del__(self) #invocation of base class destructor\n", + " \n", + "#Derived2 class\n", + "class Child(Father):\n", + " def __init__(self): #Constructor to set the data member cname\n", + " Father.__init__(self) #invocation of base class constructor\n", + " print\"Constructor of class Child\"\n", + " self.__cname=raw_input(\"Enter Child Name:\")\n", + " \n", + " \n", + " def __del__(self): #Destructor to show the data member name\n", + " print \"Destructor of class Child\"\n", + " print \"Grandfather:\",self._gname,\"Father:\",self._name,\"Child:\",self.__cname\n", + " Father.__del__(self) #invocation of base class destructor\n", + " \n", + "#instance of the Derived2 class \n", + "C=Child()\n", + "\n", + "del C #call the destructor of the derived class\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Constructor of class grandfather\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter Grandfather Name:x\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Constructor of class Father\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter Father Name:y\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Constructor of class Child\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter Child Name:z\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Destructor of class Child\n", + "Grandfather: x Father: y Child: z\n", + "Destructor of class Father\n", + "Destructor of class grandfather\n" + ] + } + ], + "prompt_number": 39 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.34, Page Number:498" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#program to explain multilevel inheritance with member function\n", + "\n", + "#Base class\n", + "class Grandfather:\n", + " def __init__(self):\n", + " self.__gname=None\n", + " \n", + " def getg(self): #method to set the data member gname\n", + " self.__gname=raw_input(\"Enter Grandfather Name:\")\n", + " \n", + " def showg(self):#method to show the data member gname\n", + " print \"Grandfather Name:\",self.__gname\n", + " \n", + " \n", + "#Derived1 class\n", + "class Father(Grandfather):\n", + " def __init__(self):\n", + " self.__name=None\n", + " \n", + " def getf(self): #method to set the data member name\n", + " self.__name=raw_input(\"Enter Father Name:\")\n", + " \n", + " def showf(self): #method to show the data member name\n", + " print \"Father Name:\",self.__name\n", + " \n", + "#Derived2 class \n", + "class Child(Father):\n", + " def __init__(self):\n", + " self.__cname=None\n", + " \n", + " def getc(self): #method for invocation of base class methods and set the data member cname\n", + " self.getg()\n", + " self.getf()\n", + " self.__cname=raw_input(\"Enter Child Name:\")\n", + " \n", + " def showc(self): #method for invocation of base class methods and set the data member cname\n", + " self.showg()\n", + " self.showf()\n", + " print \"child Name:\",self.__cname\n", + " \n", + "C=Child() #cretaion of a instance of Derived2 class\n", + "C.getc() #invocation of the method of Derived2 class for setting all the data member value\n", + "C.showc()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter Grandfather Name:XXX\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter Father Name:YYY\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter Child Name:ZZZ\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Grandfather Name: XXX\n", + "Father Name: YYY\n", + "child Name: ZZZ\n" + ] + } + ], + "prompt_number": 40 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.35, Page Number:499" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#execution of constructor and destructor in multilevel inheritance\n", + "\n", + "#Base1 class\n", + "class A:\n", + " def __init__(self):\n", + " print \"Constructor of class A\"\n", + " \n", + " def __del__(self):\n", + " print \"Destructor of class A\"\n", + " \n", + "#Base2 class \n", + "class B:\n", + " def __init__(self):\n", + " print \"Constructor of class B\"\n", + " \n", + " def __del__(self):\n", + " print \"Destructor of class B\"\n", + "#Base3 class \n", + "class C:\n", + " def __init__(self):\n", + " print \"Constructor of class C\"\n", + " \n", + " def __del__(self):\n", + " print \"Destructor of class C\"\n", + " \n", + "#Derived class,Multiple Derivation \n", + "class D(A,B,C):\n", + " def __init__(self):\n", + " for b in self.__class__.__bases__: #invoke all the base class constructors\n", + " b.__init__(self) \n", + " print \"Constructor of class D\"\n", + " \n", + " def __del__(self):\n", + " print \"Destructor of class D\" #invoke all the base class destructors\n", + " for b in self.__class__.__bases__:\n", + " b.__del__(self)\n", + " \n", + "x=D() \n", + " #**NOTE:Python destuctor is called when program goes exit. So output may be differ than c++" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Constructor of class A\n", + "Constructor of class B\n", + "Constructor of class C\n", + "Constructor of class D\n", + "Destructor of class D\n", + "Destructor of class A\n", + "Destructor of class B\n", + "Destructor of class C\n" + ] + } + ], + "prompt_number": 42 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.37, Page Number:502" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#execution of constructor and destructor in multilevel inheritance\n", + "\n", + "#Base class\n", + "class A:\n", + " def __init__(self):\n", + " print \"Constructor of class A\"\n", + " \n", + " def __del__(self):\n", + " print \"Destructor of class A\"\n", + " \n", + "#Derived1 class\n", + "class B(A):\n", + " def __init__(self):\n", + " A.__init__(self)\n", + " print \"Constructor of class B\"\n", + " \n", + " def __del__(self):\n", + " print \"Destructor of class B\"\n", + " A.__del__(self)\n", + "\n", + "#Derived2 class\n", + "class C:\n", + " def __init__(self):\n", + " print \"Constructor of class C\"\n", + " \n", + " def __del__(self):\n", + " print \"Destructor of class C\"\n", + " \n", + "#Derived3 class,multiple derivation\n", + "class D(B,C):\n", + " def __init__(self):\n", + " B.__init__(self)\n", + " C.__init__(self)\n", + " print \"Constructor of class D\"\n", + " \n", + " def __del__(self):\n", + " print \"Destructor of class D\"\n", + " B.__del__(self)\n", + " C.__del__(self)\n", + " \n", + "x=D() #creation of Derived3 class instance\n", + "del x\n", + " #**NOTE:Python destuctor is called when program goes exit. So output may be differ than c++" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Constructor of class A\n", + "Constructor of class B\n", + "Constructor of class C\n", + "Constructor of class D\n", + "Destructor of class D\n", + "Destructor of class B\n", + "Destructor of class A\n", + "Destructor of class C\n" + ] + } + ], + "prompt_number": 54 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.37, Page Number:502" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#demonstrate single inheritance\n", + "\n", + "#Base class\n", + "class A:\n", + " def __init__(self,j=0):\n", + " self._c=j #protected Member\n", + " \n", + " def show(self): #Public member \n", + " print \"c=\",self._c\n", + " \n", + " def inc(self): #public increment method of base class\n", + " self._c=self._c+1\n", + " return self._c\n", + " \n", + "class B(A):\n", + " \n", + " def __init_(self):\n", + " for b in self.__class__.__bases__:\n", + " b.__init__(self)\n", + " \n", + " def dec(self): #public increment method of derived class\n", + " self._c=self._c-1\n", + " return self._c\n", + " \n", + " \n", + "a=B() #create a instance of a derived class\n", + "\n", + "a.inc() #call the base class public member function by derived class instance for increment\n", + "a.show()\n", + "\n", + "\n", + "a.dec() #call the derived class public member function by derived class instance for decrement\n", + "a.show()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "c= 1\n", + "c= 0\n" + ] + } + ], + "prompt_number": 44 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.38, Page Number:502" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#access method from private inheritance\n", + "\n", + "#base class\n", + "class B:\n", + " def one(self): #Public Member\n", + " print \"one\"\n", + " \n", + " def __two(self): #Private Member\n", + " print \"two\"\n", + " \n", + "class D(B):\n", + " def __init__(self):\n", + " pass\n", + " \n", + "d=D()\n", + "d.one()\n", + "#d.two() #Not accesible" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "one\n" + ] + } + ], + "prompt_number": 45 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.39, Page Number:503" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#create a comman constructor in the lowermost class, in the multilevel inheritance\n", + "\n", + "#Base class\n", + "class A:\n", + " def __init__(self):\n", + " self.x=None\n", + " \n", + "#Derived1 class\n", + "class B(A):\n", + " def __init__(self):\n", + " self.y=None\n", + "\n", + "#Derived2 class\n", + "class C(B):\n", + " def __init__(self,j,k,l):\n", + " self.z=l #Initializing the base class data members by calling base class constructor\n", + " self.x=j\n", + " self.y=k\n", + " \n", + " def show(self):\n", + " print \"x=\",self.x,\"Y=\",self.y,\"z=\",self.z\n", + "\n", + "#Creation of instance of derived2 class,with constructor invocation \n", + "c=C(4,7,1)\n", + "c.show()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "x= 4 Y= 7 z= 1\n" + ] + } + ], + "prompt_number": 46 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 11.40, Page Number:504" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Explicitly call the base constructor in multiple inheritance\n", + "\n", + "#Base class\n", + "class X:\n", + " def __init__(self,a):\n", + " print a,\n", + "#Base class \n", + "class Y:\n", + " def __init__(self,b):\n", + " print b,\n", + "#multiple Derivation \n", + "class Z(X,Y):\n", + " def __init__(self,p,q,r):\n", + " X.__init__(self,p)\n", + " Y.__init__(self,q)\n", + " print r\n", + " \n", + "#Creation of instance of derived class,with constructor invocation \n", + "z=Z(1,2,3)\n", + " " + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "1 2 3\n" + ] + } + ], + "prompt_number": 47 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [], + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/sample_notebooks/Abu BakkerSiddik/Functions_.ipynb b/sample_notebooks/Abu BakkerSiddik/Functions_.ipynb new file mode 100755 index 00000000..4a23de26 --- /dev/null +++ b/sample_notebooks/Abu BakkerSiddik/Functions_.ipynb @@ -0,0 +1,1441 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:63dae7729725476213c038c75f708a0afd5386cd85264fbfba4fc7a16437b94f" + }, + "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 1 page 217" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def power(x,n): #Function header\n", + " # Function body starts here....\n", + " result=1.0 #declaration of variable result \n", + " for i in range(1,n+1,1):\n", + " result*=x #computing X to power n\n", + " return result #returning result\n", + "#Function body ends here....\n", + "\n", + "print \"result=%f\" % (power(2,2)) #function call\n", + "print \"result=%f\" % (power(3,3)) #function call" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "result=4.000000\n", + "result=27.000000\n" + ] + } + ], + "prompt_number": 23 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2 page 218" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def FtoC(faren): #Function header\n", + " # Function body starts here....\n", + " factor =5.0/9.0\n", + " freezing=32.0 \n", + " celcius=factor*(faren-freezing) #computing temp in celcius\n", + " return celcius #returning result\n", + "#Function body ends here....\n", + "\n", + "print \"result=%f\" % (FtoC(32.0)) #function call" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "result=0.000000\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 3 page 218" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#prototype not present in Python def sum_(a,b);\n", + "def main():\n", + " a=5\n", + " b=10\n", + " print \"sum=%d\" % (sum_(a,b))\n", + " return\n", + "#Main ends here....\n", + "\n", + "def sum_(x,y):\n", + "# Function body starts here....\n", + " return x+y #returning result\n", + "#Function body ends here....\n", + "main() #Calling main function" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "sum=15\n" + ] + } + ], + "prompt_number": 12 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 4 page 219" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def leap_yr(yr): #Function Defination\n", + "# Function body starts here....\n", + " if((yr%4==0) and (yr%100!=0) or yr%400==0):\n", + " return 1 #returning result\n", + " else:\n", + " return 0 #returning result\n", + "#Function body ends here.... \n", + "print leap_yr(2016) #Calling function" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 5 page 220" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def main(): #main starts here\n", + " print \"Temperature in Fahrenheit scale: \"\n", + " tempInF=eval(raw_input())\n", + " tempInC=FtoC(tempInF) #function FtoC() called from In[2]\n", + " print \"%f Fahrenheit equals %f Celcius \\n \" % (tempInF,tempInC)\n", + " return\n", + " #main ends here\n", + "main()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Temperature in Fahrenheit scale: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "32\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "32.000000 Fahrenheit equals 0.000000 Celcius \n", + " \n" + ] + } + ], + "prompt_number": 18 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 6 page 221" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#main starts here\n", + "def main(): \n", + " num=3\n", + " print \"\\n num = %d before function call.\" % (num) #taking input from user\n", + " result=mul_by_10(num) #function call\n", + " print \"\\n result = %d, after return from function.\" % (result) #Displaying result\n", + " print \"\\n num = %d\" % (num)\n", + " return\n", + "#main ends here\n", + "def mul_by_10(num): #Function header\n", + " num*=10; #Function body starts here\n", + " return num #returning result\n", + " #Function body ends here\n", + "main() #calling main function" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + " num = 3 before function call.\n", + "\n", + " result = 30, after return from function.\n", + "\n", + " num = 3\n" + ] + } + ], + "prompt_number": 22 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 7 page 222" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def main(): #main starts here\n", + " print \"Enter two numbers: \"\n", + " nOne=eval(raw_input()) #taking input from user\n", + " nTwo=eval(raw_input()) #taking input from user\n", + " n=GCD(nOne,nTwo)\n", + " print \"\\nGCD of %d and %d is %d \\n\" %(nOne,nTwo,n) #main ends here\n", + " return\n", + "def GCD(x,y): #function starts here\n", + " result=1\n", + " k=2\n", + " while((k<=x) and (k<=y)): #checking if x and y are greater than k\n", + " if((x%k==0) and (y%k==0)):\n", + " result=k\n", + " k+=1\n", + " return result #function ends here\n", + "main() #function call" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter two numbers: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "4\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "8\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "GCD of 4 and 8 is 4 \n", + "\n" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 8 page 222" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def main(): #main starts here\n", + " print \"\\nEnter the Number: \"\n", + " n=eval(raw_input()) #taking input from user\n", + " print \"\\nPrime factors on %d is...\\n \" % (n) \n", + " for d in range(2,(n/2)+1,1):\n", + " if((n%d==0) and isPrime(d)): #calculating prime factors, isPrime() function\n", + " print \"%d\" % (d)\n", + " return #main ends here \n", + "def isPrime(x): #isPrime() function starts here\n", + " for d in range(2,(x/2)+1,1):\n", + " if(x%d==0):\n", + " return 0\n", + " return 1 #isPrime() function ends here\n", + "main() #calling main" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "Enter the Number: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "Prime factors on 2 is...\n", + " \n" + ] + } + ], + "prompt_number": 12 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 9 page 223" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def main(): #main starts here\n", + " arr=[1,2,3] #Array declaration and initialisation\n", + " change(arr) #calling function\n", + " print \"Elements are %d, %d and %d\" % (arr[0],arr[1],arr[2])\n", + " return #main starts here\n", + "def change(my_array):\n", + " my_array[0]=10 #Changing values\n", + " my_array[1]=20\n", + " return\n", + "main()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Elements are 10, 20 and 3\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 10 page 223 and 224" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#main starts here\n", + "def main():\n", + " arr=[3,2,7,0,6,4,9,8,1,5] #array declaration and initializtion\n", + " print \"The array before sort is: \\n\" \n", + " for i in range(0,10,1): #displaying array values before changing\n", + " print arr[i],\n", + " sort(arr,10)\n", + " print \"\\nThe array after sort is: \\n\"\n", + " for i in range(0,10,1): #displaying array values after changing\n", + " print arr[i],\n", + " return\n", + "#main ends here\n", + "#functions starts here\n", + "def sort(a,n):\n", + " for i in range(0,n-1,1): #sorting array values\n", + " for j in range(0,n-i-1,1):\n", + " if(a[j]>a[j+1]):\n", + " temp=a[j]\n", + " a[j]=a[j+1]\n", + " a[j+1]=temp\n", + " return\n", + "#functions ends here\n", + "main()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The array before sort is: \n", + "\n", + "3 2 7 0 6 4 9 8 1 5 \n", + "The array after sort is: \n", + "\n", + "0 1 2 3 4 5 6 7 8 9\n" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 11 page 224" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#main starts here\n", + "def main():\n", + " myInts=[1,2,3,4,5,6,7,8,9,10] #Array initialisation and declaration\n", + " size=10\n", + " print \"\\n\\nThe given numbers are:\"\n", + " for i in range(0,size,1): #displaying array values before changing\n", + " print myInts[i],\n", + " doubleThem(myInts,size) #calling function\n", + " print \"\\nThe double numbers are: \"\n", + " for i in range(0,size,1): #displaying array values after changing\n", + " print myInts[i],\n", + " return\n", + "#main ends here\n", + "#*********function defination***********#\n", + "def doubleThem(a,size):\n", + " for i in range(0,size,1): #multplying array values by 2\n", + " a[i]=2*a[i]\n", + " return\n", + "main()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "\n", + "The given numbers are:\n", + "1 2 3 4 5 6 7 8 9 10 \n", + "The double numbers are: \n", + "2 4 6 8 10 12 14 16 18 20\n" + ] + } + ], + "prompt_number": 11 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 12 page 224 and 225" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#main starts here\n", + "def main():\n", + " b=list() #array declaration\n", + " n=input(\"How many students?\")\n", + " print \"Enter the age of students\\n\"\n", + " for i in range(0,n,1): #taking input from user\n", + " val=input()\n", + " b.append(val)\n", + " average=avg_age(b,n) #calling function\n", + " print \"\\nThe avg age of students is = %f\" % average\n", + " return\n", + "#main ends here\n", + "def avg_age(a,n):\n", + " sum=0.0\n", + " for j in range(0,n,1): #calculating avg age\n", + " sum=sum+a[j]\n", + " return sum/n\n", + "main()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "How many students?5\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the age of students\n", + "\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "10\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "10\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "10\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "10\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "10\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "The avg age of students is = 10.000000\n" + ] + } + ], + "prompt_number": 16 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 13 page 225" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#main starts here\n", + "def main():\n", + " values=list() #array declaration\n", + " print \"Enter 5 numbers\"\n", + " for i in range(0,5,1): #taking input from user\n", + " val=input()\n", + " values.append(val)\n", + " max1=maximum(values,5) #calling function\n", + " print \"Maximum value is %d\\n\" % max1\n", + " return\n", + "#main ends here\n", + "#function begins here\n", + "def maximum(values,n):\n", + " max_value=values[0]\n", + " for i in range(0,n,1): #finding maximum value\n", + " if(values[i]>max_value):\n", + " max_value=values[i]\n", + " return max_value\n", + "#function ends here\n", + "main()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter 5 numbers\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "125\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "365\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "552\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "001\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "-568\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Maximum value is 552\n", + "\n" + ] + } + ], + "prompt_number": 20 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 14 page 225 and 226" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def main():\n", + " a=list() #source string\n", + " b=list() #destination string\n", + " print \"Input source string: \"\n", + " a=raw_input() #read input string\n", + " b=string_copy(b,a) #function call\n", + " print \"\\nDestination string: %s\\n\" % b\n", + " return\n", + "#function call\n", + "def string_copy(d,s):\n", + " print \"\\nSource string: %s\" % s \n", + " #copying the string\n", + " d=s\n", + " return d\n", + "main()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Input source string: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "i love python\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "Source string: i love python\n", + "\n", + "Destination string: i love python\n", + "\n" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 15 page 226" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "ROW=2 #define row 3\n", + "COLUMN=3 #define col 3\n", + "def main(): \n", + " global ROW,COLUMN\n", + " a=[[0 for row in range(0,COLUMN)] for col in range(0,ROW)]\n", + " b=[[0 for row in range(0,COLUMN)] for col in range(0,ROW)]\n", + " print \"\\nEnter elements of the first matrix.\\n\"\n", + " #Read first matrix elements\n", + " for i in range(ROW):\n", + " for j in range(COLUMN):\n", + " a[i][j]=input()\n", + " print \"\\nEnter elements of the second matrix.\\n\"\n", + " #Read second matrix elements\n", + " for i in range(ROW):\n", + " for j in range(COLUMN):\n", + " b[i][j]=input()\n", + " mat_arith(a,b) #function call\n", + "def mat_arith(a,b):\n", + " global ROW,COLUMN\n", + " c=[[0 for row in range(0,COLUMN)] for col in range(0,ROW)]\n", + " print \"\\nFor addition enter: 1\\n\"\n", + " print \"\\nFor subtraction enter: 2\\n\"\n", + " print \"\\nEnter your choice\\n\"\n", + " choice=eval(raw_input())\n", + " for i in range(ROW):\n", + " for j in range(COLUMN):\n", + " if choice == 1: #checking for addation and subtraction and performing it\n", + " c[i][j]=a[i][j]+b[i][j]\n", + " elif choice == 2:\n", + " c[i][j]=a[i][j]-b[i][j]\n", + " else:\n", + " print \"Invalid choice. Task not done.\"\n", + " return\n", + " print \"\\nThe resulting matrix is:\\n\"\n", + " for i in range(ROW):\n", + " for j in range(COLUMN):\n", + " print c[i][j],\n", + " print(\"\\n\")\n", + "main()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "Enter elements of the first matrix.\n", + "\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "4\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "6\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "7\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "8\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "10\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "Enter elements of the second matrix.\n", + "\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "7\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "9\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "11\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "For addition enter: 1\n", + "\n", + "\n", + "For subtraction enter: 2\n", + "\n", + "\n", + "Enter your choice\n", + "\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "The resulting matrix is:\n", + "\n", + "3 7 11 \n", + "\n", + "14 17 21 \n", + "\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 16 page 227 and 228" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#main strats here\n", + "def main():\n", + " a=5\n", + " b=7\n", + " print \"In main: a= %d, b= %d\\n\" % (a,b)\n", + " exchange(a,b) #calling function\n", + " print\"\\nBack in main:\"\n", + " print\"a= %d, b= %d\" % (a,b)\n", + " return\n", + "#main ends here\n", + "#function starts here\n", + "def exchange(a,b):\n", + " print\"In function exchange() before change: just received from main... a=%d and b=%d \" % (a,b)\n", + " #exchanging values\n", + " temp=a\n", + " a=b\n", + " b=temp\n", + " print\"In function exchange() after change:\"\n", + " print\"a=%d and b=%d \" % (a,b)\n", + " return\n", + "main()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "In main: a= 5, b= 7\n", + "\n", + "In function exchange() before change: just received from main... a=5 and b=7 \n", + "In function exchange() after change:\n", + "a=7 and b=5 \n", + "\n", + "Back in main:\n", + "a= 5, b= 7\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 17 page 228" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "a=5 #declaration of global variables\n", + "b=7\n", + "#main starts here\n", + "def main():\n", + " global a\n", + " global b\n", + " print \"In main: a= %d, b= %d\\n\" % (a,b)\n", + " exchange() #calling function\n", + " print\"\\nBack in main:\",\n", + " print\"a= %d, b= %d\" % (a,b)\n", + "#main ends here\n", + "#function starts here\n", + "def exchange():\n", + " global a\n", + " global b\n", + " print\"In function exchange() before change: just received from main... a=%d and b=%d \" % (a,b)\n", + " temp=a\n", + " a=b\n", + " b=temp\n", + " print\"In function exchange() after change:\",\n", + " print\"a=%d and b=%d \" % (a,b)\n", + " return\n", + "#function ends here\n", + "main()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "In main: a= 5, b= 7\n", + "\n", + "In function exchange() before change: just received from main... a=5 and b=7 \n", + "In function exchange() after change: a=7 and b=5 \n", + "\n", + "Back in main: a= 7, b= 5\n" + ] + } + ], + "prompt_number": 9 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 18 page 229" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def fn1():\n", + " #variable declaration in inner block\n", + " x=45\n", + " print\"\\nin inner block x= %d\" % x\n", + "#variable declaration in outer block\n", + "x=3\n", + "print \"\\nin outer block x = %d before executing inner block\" % x\n", + "fn1()\n", + "print \"\\nin outer block x = %d after executing inner block\" % x" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "in outer block x = 3 before executing inner block\n", + "\n", + "in inner block x= 45\n", + "\n", + "in outer block x = 3 after executing inner block\n" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 19 page 231" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#function starts here\n", + "def fn1():\n", + " a=10\n", + " i=4199232\n", + " print \"a=%d\" % a\n", + " print \"i=%d\" % i\n", + "#function ends here\n", + "a=5\n", + "print \"a=%d\" % a\n", + "fn1() #function call\n", + "print \"a=%d\" % a" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "a=5\n", + "a=10\n", + "i=4199232\n", + "a=5\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 20 page 232" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "i = 0\n", + "#function starts here\n", + "def show():\n", + " global i\n", + " print 'i =',i\n", + " i+=1\n", + "#function ends here\n", + "print\"\\nFirst call of show()\"\n", + "show() #function call\n", + "print\"\\nSecond call of show()\"\n", + "show() #function call\n", + "print\"\\nThird call of show()\"\n", + "show() #function call" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "First call of show()\n", + "i = 0\n", + "\n", + "Second call of show()\n", + "i = 1\n", + "\n", + "Third call of show()\n", + "i = 2\n" + ] + } + ], + "prompt_number": 18 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 21 page 231" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "i = None\n", + "#function starts here\n", + "def show():\n", + " print 'Value of i in pgm2.c =',i\n", + "#function ends here\n", + "i = 10\n", + "#function call\n", + "show()\n", + "print 'Value of i in pgm1.c =',i" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Value of i in pgm2.c = 10\n", + "Value of i in pgm1.c = 10\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 22 page 233" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "i = None\n", + "#function starts here\n", + "def show():\n", + " global i\n", + " i = 20\n", + " print \"value of i in pgm2.c = \", i\n", + "#function ends here\n", + "show() #function call\n", + "print\"Value of i in pgm1.c =\",i" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "value of i in pgm2.c = 20\n", + "Value of i in pgm1.c = 20\n" + ] + } + ], + "prompt_number": 15 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 23 page 236" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import sys, traceback\n", + "#main starts here\n", + "def main():\n", + " print \"\\nEnter the number of terms: \"\n", + " i=eval(raw_input()) #taking input\n", + " if (i<0): \n", + " print\"\\nError - Number of terms cannot be negative\\n\"\n", + " sys.exit(0)\n", + " print\"Fibonacci sequence for %d terms is: \" % i\n", + " for j in range(1,i+1,1):\n", + " print\"%d\" % fib(j) #function call\n", + "#function ends here\n", + "#function starts here\n", + "def fib(val):\n", + " if(val<=2):\n", + " return 1\n", + " else:\n", + " return(fib(val-1)+fib(val-2))\n", + "#function ends here\n", + "main()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "Enter the number of terms: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Fibonacci sequence for 5 terms is: \n", + "1\n", + "1\n", + "2\n", + "3\n", + "5\n" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 24 page 237" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#main starts here\n", + "def main():\n", + " print\"Enter the numbers: \"\n", + " i=eval(raw_input())\n", + " j=eval(raw_input())\n", + " print\"The GCD of %d and %d is %d\" % (i,j,gcd(i,j)) #calling gcd function\n", + " return\n", + "#function ends here\n", + "#function starts here\n", + "def gcd(a,b):\n", + " remainder=a%b\n", + " if(remainder==0):\n", + " return b\n", + " else:\n", + " return gcd(b,remainder) #recursive call to itself, gcd function\n", + "#function ends here\n", + "main()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the numbers: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "4\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "8\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The GCD of 4 and 8 is 4\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [], + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/sample_notebooks/AkshayShende/chapter2.ipynb b/sample_notebooks/AkshayShende/chapter2.ipynb new file mode 100755 index 00000000..d82b3395 --- /dev/null +++ b/sample_notebooks/AkshayShende/chapter2.ipynb @@ -0,0 +1,65 @@ +{ + "metadata": { + "name": "chapter2.ipynb" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 2:Concurrent Forces in A Plane" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2.2-1, Page No:10" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import math\n", + "\n", + "#Initilization of variables\n", + "\n", + "P=50 #N\n", + "Q=100 #N\n", + "beta=150 #degree # angle between P & the horizontal\n", + "\n", + "#Calculations\n", + "\n", + "R=(P**2+Q**2-(2*P*Q*cos(beta*(pi/180))))**0.5 # using the Trignometric solution\n", + "Alpha=(arcsin(((sin(beta*(pi/180))*Q)/R)))*(180/pi)+15 #Angle in degrees\n", + "\n", + "#Result\n", + "print \"The magnitude of resultant is\",round(R,2),\"N\"\n", + "print \"The direction of resultant is\",round(Alpha,2),\"degrees\"\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The magnitude of resultant is 145.47 N\n", + "The direction of resultant is 35.1 degrees\n" + ] + } + ], + "prompt_number": 19 + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/sample_notebooks/Aman KumarJain/Chapter_6_Objects_and_Classes_1.ipynb b/sample_notebooks/Aman KumarJain/Chapter_6_Objects_and_Classes_1.ipynb new file mode 100755 index 00000000..89a18f99 --- /dev/null +++ b/sample_notebooks/Aman KumarJain/Chapter_6_Objects_and_Classes_1.ipynb @@ -0,0 +1,934 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:bd4e7931ddf89d8dc8befb681e1e57c7a9c742cd8abe8e18a494a55156d56cee" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 6: Objects and Classes" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 6.1, Page Number: 216" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class smallobj: #define a class\n", + " \n", + " def setdata(self,d): #member function to set class variable somdata\n", + " self.__somedata = d\n", + " \n", + " def showdata(self): #member function to display somedata \n", + " print 'Data is ' , self.__somedata\n", + "\n", + "\n", + "#define two objects of class smallobj\n", + "s1=smallobj()\n", + "s2=smallobj()\n", + "\n", + "#call member function to set data \n", + "s1.setdata(1066)\n", + "s2.setdata(1776)\n", + "\n", + "#call member function to display data \n", + "s1.showdata()\n", + "s2.showdata()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Data is 1066\n", + "Data is 1776\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 6.2, Page Number: 223" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class part: #define class \n", + " \n", + " def setpart(self,mn,pn,c): #set data\n", + " self.__modelnumber = mn\n", + " self.__partnumber = pn\n", + " self.__cost = c\n", + " \n", + " def showpart(self): #display data \n", + " print 'Model' , self.__modelnumber ,\n", + " print ', part' , self.__partnumber , \n", + " print ', costs $',self.__cost\n", + " \n", + "#define object of class part \n", + "part1 = part()\n", + "\n", + "#call member function setpart\n", + "part1.setpart(6244,373,217.55)\n", + "\n", + "#call member function showpart\n", + "part1.showpart()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Model 6244 , part 373 , costs $ 217.55\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 6.3, Page Number: 225" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from turtle import * #importing turtles library\n", + "\n", + "class circle: #defining circle class\n", + " \n", + " def set(self,x,y,r,fc): #sets circle attribute\n", + " self._xCo = x\n", + " self._yCo = y\n", + " self._radius = r\n", + " self._fillcolor = fc\n", + " \n", + " def draw(self): #draws the circle \n", + " setup() #set screen\n", + " turtle = Turtle() #object of Turtle class\n", + " turtle.begin_fill() #start filling color in circle\n", + " turtle.color(self._fillcolor) #color\n", + " turtle.up()\n", + " turtle.goto(self._xCo,self._yCo) #set center of circle\n", + " turtle.circle(self._radius) #draw circle of radius self.__radius\n", + " turtle.end_fill() #stop filling\n", + " turtle.hideturtle()\n", + " done()\n", + "\n", + "#creating objects of class circle \n", + "c1 = circle()\n", + "c2 = circle()\n", + "c3 = circle()\n", + "\n", + "#sending the value to set fnction\n", + "c1.set(15,7,5,\"blue\")\n", + "c2.set(41,12,7,\"red\")\n", + "c3.set(65,18,4,\"green\")\n", + "\n", + "#draw circle\n", + "c1.draw()\n", + "c2.draw()\n", + "c3.draw()\n", + "\n", + "#In the above example the cirlcle's in the book are constructed using 'X' and 'O' but such feature is not available in Python.\n", + "#So i have created a simple circle filled with color" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 6.4, Page Number: 226" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Distance: #Distance class\n", + " \n", + " def setdist(self,ft,inc): #set distance to class variables\n", + " self.__feet = ft\n", + " self.__inches = inc\n", + " \n", + " def getdist(self): #get distance from user\n", + " self.__feet = input('Enter feet:')\n", + " self.__inches = input('Enter inches:')\n", + " \n", + " def showdist(self): #display distance\n", + " print self.__feet , '\\' -' , self.__inches , '\\\"'\n", + "\n", + "#define two distance\n", + "dist1 = Distance()\n", + "dist2 = Distance()\n", + "\n", + "dist1.setdist(11,6.25) #set dist1\n", + "dist2.getdist() #set dist2 from user\n", + "\n", + "#show distances\n", + "print \"dist1 = \",\n", + "dist1.showdist()\n", + "print 'dist2 = ',\n", + "dist2.showdist()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter feet:17\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter inches:5.75\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "dist1 = 11 ' - 6.25 \"\n", + "dist2 = 17 ' - 5.75 \"\n" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 6.5, Page Number: 228" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Counter:\n", + " \n", + " def __init__(self): #constructor\n", + " self.__count = 0\n", + " \n", + " def inc_count(self): #increment count\n", + " self.__count = self.__count + 1\n", + " \n", + " def get_count(self): #return count\n", + " return self.__count\n", + "\n", + "#define and initialize class objects\n", + "c1=Counter()\n", + "c2=Counter()\n", + "\n", + "#display count for each object\n", + "print 'c1 =',c1.get_count()\n", + "print 'c2 =',c2.get_count()\n", + "\n", + "\n", + "c1.inc_count() #increment c1\n", + "c2.inc_count() #increment c2\n", + "c2.inc_count() #increment c2\n", + "\n", + "#display count again for each object\n", + "print 'c1 =',c1.get_count()\n", + "print 'c2 =',c2.get_count()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "c1 = 0\n", + "c2 = 0\n", + "c1 = 1\n", + "c2 = 2\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 6.6, Page Number: 231" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from turtle import * #importing turtles library\n", + "\n", + "class circle: #defining circle class\n", + " \n", + " def __init__(self,x,y,r,fc): #constructor for set circle attribute\n", + " self._xCo = x\n", + " self._yCo = y\n", + " self._radius = r\n", + " self._fillcolor = fc\n", + " \n", + " def draw(self): #draws the circle\n", + " setup()\n", + " turtle = Turtle()\n", + " turtle.begin_fill()\n", + " turtle.color(self._fillcolor)\n", + " turtle.up()\n", + " turtle.goto(self._xCo,self._yCo)\n", + " turtle.down()\n", + " turtle.circle(self._radius)\n", + " turtle.end_fill()\n", + " turtle.hideturtle()\n", + " done()\n", + "\n", + "#creating objects of class circle \n", + "c1 = circle(15,7,5,\"blue\") \n", + "c2 = circle(41,12,7,\"red\")\n", + "c3 = circle(65,18,4,\"green\")\n", + "\n", + "#draw circle\n", + "c1.draw()\n", + "c2.draw()\n", + "c3.draw()" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 6.7, Page Number: 233" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Distance: #Distance class\n", + " \n", + " def __init__(self,ft=0,inc=0): #constructor \n", + " self.__feet = ft\n", + " self.__inches = inc\n", + " \n", + " def getdist(self): #get length from user\n", + " self.__feet = input('Enter feet:')\n", + " self.__inches = input('Enter inches:')\n", + " \n", + " def showdist(self): #display distance\n", + " print self.__feet , '\\' -' , self.__inches , '\\\"'\n", + " \n", + " def add_dist(self,d2,d3): #add length d2 and d3\n", + " self.__inches = d2.__inches + d3.__inches #add inches\n", + " self.__feet = 0\n", + " if self.__inches >= 12.0: #if total exceeds 12.0\n", + " self.__inches = self.__inches - 12.0 #then decrease inches by 12.0\n", + " self.__feet = self.__feet + 1 #and increase feet by 1\n", + " self.__feet = self.__feet + d2.__feet + d3.__feet #add the feet\n", + "\n", + "#define two length\n", + "dist1 = Distance()\n", + "dist3 = Distance()\n", + "\n", + "#define and initialize dist2\n", + "dist2 = Distance(11,6.25)\n", + "\n", + "#get dist1 from user\n", + "dist1.getdist()\n", + "\n", + "#dist3 = dist1 + dist2\n", + "dist3.add_dist(dist1,dist2)\n", + "\n", + "#display all lengths\n", + "print 'dist1 = ',\n", + "dist1.showdist()\n", + "print 'dist2 = ',\n", + "dist2.showdist()\n", + "print 'dist3 = ',\n", + "dist3.showdist()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter feet:17\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter inches:5.75\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "dist1 = 17 ' - 5.75 \"\n", + "dist2 = 11 ' - 6.25 \"\n", + "dist3 = 29 ' - 0.0 \"\n" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 6.8, Page Number: 238" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Distance: #Distance class\n", + " \n", + " def __init__(self,ft=0,inc=0): #overloaded constructor that takes no arguments or two args or one object(copy constructor)\n", + " if isinstance(ft,int):\n", + " self.__feet = ft\n", + " self.__inches = inc\n", + " else:\n", + " self.__feet = ft.__feet\n", + " self.__inches = ft.__inches\n", + " \n", + " def getdist(self): #get length from user\n", + " self.__feet = input('Enter feet:')\n", + " self.__inches = input('Enter inches:')\n", + " \n", + " def showdist(self): #display distance\n", + " print self.__feet , '\\' -' , self.__inches , '\\\"'\n", + "\n", + "#two argument constructor\n", + "dist1 = Distance(11,6.25)\n", + "\n", + "#one argument(object) constructor explicitly pass\n", + "dist2 = Distance(dist1)\n", + "\n", + "#also one argument(object) constructor implicitly pass\n", + "dist3 = dist1\n", + "\n", + "#display all lengths\n", + "print 'dist1 = ',\n", + "dist1.showdist()\n", + "print 'dist2 = ',\n", + "dist2.showdist()\n", + "print 'dist3 = ',\n", + "dist3.showdist()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "dist1 = 11 ' - 6.25 \"\n", + "dist2 = 11 ' - 6.25 \"\n", + "dist3 = 11 ' - 6.25 \"\n" + ] + } + ], + "prompt_number": 8 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 6.9, Page Number: 240" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Distance: #Distance class\n", + " \n", + " def __init__(self,ft=0,inc=0): #constructor \n", + " self.__feet = ft\n", + " self.__inches = inc\n", + " \n", + " def getdist(self): #get length from user\n", + " self.__feet = input('Enter feet:')\n", + " self.__inches = input('Enter inches:')\n", + " \n", + " def showdist(self): #display distance\n", + " print self.__feet , '\\' -' , self.__inches , '\\\"'\n", + " \n", + " def add_dist(self,d2): #add this length to d2 and return object\n", + " temp = Distance() #temporary object\n", + " temp.__inches = self.__inches + d2.__inches\n", + " if temp.__inches >= 12.0:\n", + " temp.__inches = temp.__inches - 12.0\n", + " temp.__feet = 1\n", + " temp.__feet = temp.__feet + self.__feet + d2.__feet\n", + " return temp #return sum as object\n", + "\n", + "#define two length\n", + "dist1 = Distance()\n", + "dist3 = Distance()\n", + "\n", + "#define and initialize dist2\n", + "dist2 = Distance(11,6.25)\n", + "\n", + "#get dist1 from user\n", + "dist1.getdist()\n", + "\n", + "#dist3 = dist1 + dist2\n", + "dist3 = dist1.add_dist(dist2)\n", + "\n", + "#display all lengths\n", + "print 'dist1 = ',\n", + "dist1.showdist()\n", + "print 'dist2 = ',\n", + "dist2.showdist()\n", + "print 'dist3 = ',\n", + "dist3.showdist()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter feet:17\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter inches:5.75\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "dist1 = 17 ' - 5.75 \"\n", + "dist2 = 11 ' - 6.25 \"\n", + "dist3 = 29 ' - 0.0 \"\n" + ] + } + ], + "prompt_number": 9 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 6.10, Page Number: 243" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "Suit = [\"clubs\",\"diamonds\",\"hearts\",\"spades\"] \n", + "\n", + "(clubs,diamonds,hearts,spades) = (0,1,2,3) #Atteching the names with number \n", + "\n", + "\n", + "#from 2 to 10 are integers without names\n", + "jack = 11 \n", + "queen = 12 \n", + "king = 13\n", + "ace = 14\n", + "\n", + "\n", + "class card: \n", + " \n", + " def __init__(self,n=None,s=None): #constructor\n", + " self.__number = n #2 to 10, jack, queen, king, ace\n", + " self.__suit = s #clubs, diamonds, hearts, spades\n", + " \n", + " def display(self): #display the cards\n", + " \n", + " if self.__number >= 2 and self.__number<=10:\n", + " print self.__number , 'of',\n", + " \n", + " else:\n", + " if self.__number == jack:\n", + " print 'jack of',\n", + " elif self.__number == queen:\n", + " print 'queen of',\n", + " elif self.__number == king:\n", + " print 'king of',\n", + " else:\n", + " print 'ace of',\n", + " \n", + " if self.__suit == clubs:\n", + " print 'clubs'\n", + " elif self.__suit == diamonds:\n", + " print 'diamonds'\n", + " elif self.__suit == hearts:\n", + " print 'hearts'\n", + " else:\n", + " print 'spades'\n", + " \n", + " def isEqual(self,c2): #return 1 if cards equal\n", + " \n", + " if self.__number == c2.__number and self.__suit == c2.__suit:\n", + " return 1\n", + " else:\n", + " return 0\n", + "\n", + "\n", + "#define various cards\n", + "temp = card()\n", + "chosen = card()\n", + "prize = card()\n", + "\n", + "\n", + "#define and initialize card1\n", + "card1 = card(7,clubs)\n", + "print 'card 1 is the',\n", + "card1.display() #display card1\n", + "\n", + "#define and initialize card2\n", + "card2 = card(jack,hearts)\n", + "print 'card 2 is the',\n", + "card2.display() #display card2\n", + "\n", + "#define and initialize card3\n", + "card3 = card(ace,spades)\n", + "print 'card 3 is the',\n", + "card3.display() #display card3\n", + "\n", + "\n", + "#prize is the card to guess\n", + "prize = card3\n", + "\n", + "\n", + "#swapping cards\n", + "print 'I\\'m swapping card 1 and card 3'\n", + "temp = card3\n", + "card3 = card1\n", + "card1 = temp\n", + "\n", + "print 'I\\'m swapping card 2 and card 3'\n", + "temp = card2\n", + "card3 = card2\n", + "card2 = temp\n", + "\n", + "print 'I\\'m swapping card 1 and card 2'\n", + "temp = card2\n", + "card2 = card1\n", + "card1 = temp\n", + "\n", + "print 'Now, where (1,2, or 3) is the',\n", + "prize.display() #display prize\n", + "print '?'\n", + "\n", + "position = input() #get user's guess of position\n", + "\n", + "\n", + "#set chosen to user's choice \n", + "if position == 1:\n", + " chosen = card1\n", + "elif position == 2:\n", + " chosen = card2\n", + "else:\n", + " chosen = card3\n", + "\n", + "#is chosen card the prize?\n", + "\n", + "x=chosen.isEqual(prize)\n", + "\n", + "if x==1:\n", + " print 'That\\'s right! You win!'\n", + "else:\n", + " print 'Sorry. You lose.'\n", + "\n", + "print 'You choose the',\n", + "\n", + "#display chosen card\n", + "chosen.display()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "card 1 is the 7 of clubs\n", + "card 2 is the jack of hearts\n", + "card 3 is the ace of spades\n", + "I'm swapping card 1 and card 3\n", + "I'm swapping card 2 and card 3\n", + "I'm swapping card 1 and card 2\n", + "Now, where (1,2, or 3) is the ace of spades\n", + "?\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "That's right! You win!\n", + "You choose the ace of spades\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 6.11, Page Number: 249" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class foo: \n", + " \n", + " __count = 0 #only one data item for all objects\n", + " \n", + " def __init__(self):\n", + " foo.__count = foo.__count + 1 #increment count when object created\n", + " \n", + " def getcount(self): #returns count\n", + " return foo.__count\n", + "\n", + "#create three objecs\n", + "f1 = foo()\n", + "f2 = foo()\n", + "f3 = foo()\n", + "\n", + "#Each object displays the same count value\n", + "print 'count is', f1.getcount()\n", + "print 'count is', f2.getcount()\n", + "print 'count is', f3.getcount()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "count is 3\n", + "count is 3\n", + "count is 3\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 6.12, Page Number: 253" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Distance: #Distance class\n", + " \n", + " def __init__(self,ft=0,inc=0.0): #constructor \n", + " self.__feet = ft\n", + " self.__inches = inc\n", + " \n", + " def getdist(self): #get length from user\n", + " self.__feet = input('Enter feet:')\n", + " self.__inches = input('Enter inches:')\n", + " \n", + " def showdist(self): #display distance\n", + " print self.__feet , '\\' -' , self.__inches , '\\\"'\n", + "\n", + "#There's no const keyword \n", + " \n", + " def add_dist(self,d2): #add this length to d2 and return object\n", + " \n", + " temp = Distance()\n", + " temp.__inches = self.__inches + d2.__inches\n", + " \n", + " if temp.__inches >= 12.0:\n", + " temp.__inches = temp.__inches - 12.0\n", + " temp.__feet = 1\n", + " \n", + " temp.__feet = temp.__feet + self.__feet + d2.__feet\n", + " \n", + " return temp #return sum as object\n", + " \n", + "dist1 = Distance()\n", + "dist3 = Distance()\n", + "dist2 = Distance(11,6.25)\n", + "\n", + "dist1.getdist()\n", + "\n", + "dist3 = dist1.add_dist(dist2)\n", + "\n", + "print 'dist1 = ',\n", + "dist1.showdist()\n", + "print 'dist2 = ',\n", + "dist2.showdist()\n", + "print 'dist3 = ',\n", + "dist3.showdist()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter feet:17\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter inches:5.75\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "dist1 = 17 ' - 5.75 \"\n", + "dist2 = 11 ' - 6.25 \"\n", + "dist3 = 29 ' - 0.0 \"\n" + ] + } + ], + "prompt_number": 10 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 6.13, Page Number: 255" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Distance:\n", + " \n", + " def __init__(self,ft,inc):\n", + " self.__feet = ft\n", + " self.__inches = inc\n", + " \n", + " def getdist(self):\n", + " self.__feet = input('Enter feet:')\n", + " self.__inches = input('Enter inches:')\n", + " \n", + " def showdist(self):\n", + " print self.__feet , '\\' -' , self.__inches , '\\\"'\n", + "\n", + "football = Distance(300,0)\n", + "\n", + "print 'football = ',\n", + "football.showdist()\n", + "\n", + "#There's no const keyword in python" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "football = 300 ' - 0 \"\n" + ] + } + ], + "prompt_number": 11 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [], + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/sample_notebooks/ApurvaBhushan/Chapter_3_1.ipynb b/sample_notebooks/ApurvaBhushan/Chapter_3_1.ipynb new file mode 100755 index 00000000..3168a0f9 --- /dev/null +++ b/sample_notebooks/ApurvaBhushan/Chapter_3_1.ipynb @@ -0,0 +1,212 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Chapter 3: Elements of the Theory of Plasticity" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Example 3.1, True Stress and True Strain, Page No. 76" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Engineering Stress at maximum load = 99852.1 psi\n", + "True Fracture Stress = 112785 psi\n", + "True Strain at fracture = 0.344939\n", + "Engineering strain at fracture = 0.411903\n" + ] + } + ], + "source": [ + "from math import pi\n", + "from math import log\n", + "from math import exp\n", + "\n", + "#variable declaration\n", + "D_i=0.505;\n", + "L=2;\n", + "P_max=20000;\n", + "P_f=16000;\n", + "D_f=0.425;\n", + "\n", + "#calculation\n", + "E_St= P_max*4/(pi*D_i**2);\n", + "T_fr_St= P_f*4/(pi*D_f**2);\n", + "e_f=log(D_i**2/D_f**2);\n", + "e=exp(e_f)-1;\n", + "\n", + "#result\n", + "print('\\nEngineering Stress at maximum load = %g psi\\nTrue Fracture Stress = %g psi\\nTrue Strain at fracture = %g\\nEngineering strain at fracture = %g')%(E_St,T_fr_St,e_f,e);\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Example 3.2, Yielding Criteria for Ductile Metals, Page No. 78" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Since the calculated value of sigma0 = 224.054 MPa, which is less than the yield strength of the aluminium alloy\n", + "Thus safety factor is = 2.23161\n" + ] + } + ], + "source": [ + "\n", + "from math import sqrt\n", + "\n", + "#variable declaration\n", + "sigma00=500;\n", + "sigma_z=-50;\n", + "sigma_y=100;\n", + "sigma_x=200;\n", + "T_xy=30;\n", + "T_yz=0;\n", + "T_xz=0;\n", + "\n", + "#calculation\n", + "sigma0=sqrt((sigma_x-sigma_y)**2+(sigma_y-sigma_z)**2+(sigma_z-sigma_x)**2+6*(T_xy**2+T_yz**2+T_xz**2))/sqrt(2);\n", + "s=sigma00/sigma0;\n", + "\n", + "#result\n", + "print('\\nSince the calculated value of sigma0 = %g MPa, which is less than the yield strength of the aluminium alloy\\nThus safety factor is = %g')%(sigma0,s);\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Example 3.3, Tresca Criterion, Page No. 81" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Since the calculated value of sigma0 = 250 MPa, which is less than the yield strength of the aluminium alloy\n", + "Thus safety factor is = 2\n" + ] + } + ], + "source": [ + "\n", + "\n", + "#variable declaration\n", + "sigma00=500;\n", + "sigma_z=-50;\n", + "sigma_y=100;\n", + "sigma_x=200;\n", + "T_xy=30;\n", + "T_yz=0;\n", + "T_xz=0;\n", + "\n", + "#calculation\n", + "sigma0=sigma_x-sigma_z;\n", + "s=sigma00/sigma0;\n", + "\n", + "#result\n", + "print('\\nSince the calculated value of sigma0 = %g MPa, which is less than the yield strength of the aluminium alloy\\nThus safety factor is = %g')%(sigma0,s);\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "### Example 3.4, Levy-Mises Equation, Page No. 91" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Plastic Strain = 0.199532\n" + ] + } + ], + "source": [ + "from math import sqrt\n", + "\n", + "#variable declaration\n", + "r_t=20;\n", + "p=1000;\n", + "\n", + "#calculation\n", + "sigma1=p*r_t;\n", + "sigma1=sigma1/1000; #conversion to ksi\n", + "sigma=sqrt(3)*sigma1/2;\n", + "e=(sigma/25)**(1/0.25);\n", + "e1=sqrt(3)*e/2;\n", + "\n", + "#result\n", + "print('\\nPlastic Strain = %g')%(e1);\n" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 2", + "language": "python", + "name": "python2" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 2 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython2", + "version": "2.7.9" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} diff --git a/sample_notebooks/ChaitanyaPotti/Chapter2.ipynb b/sample_notebooks/ChaitanyaPotti/Chapter2.ipynb new file mode 100755 index 00000000..2cf4427b --- /dev/null +++ b/sample_notebooks/ChaitanyaPotti/Chapter2.ipynb @@ -0,0 +1,286 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:b659456964884efc24c6f36b543fc4c77e2e36dfdbc033ad2f4c3e2f2bce479c" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 2 - Amplitude Modulation" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1 - pg 51" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#calcualate the carrier frequency\n", + "#given\n", + "import math\n", + "L = 50*10**-6#in henry\n", + "C = 1*10**-9#in farads\n", + "#calculation\n", + "F_c = 1/(2.*math.pi*math.sqrt(L*C))/1000.;\n", + "#results\n", + "print '%s %d %s' %(\"Carrier frequency F_c =\",math.ceil(F_c),\" kHz\")\n", + "print(\"Now , it is given that the highest modulation frequency is 8KHz \");\n", + "print(\"Therefore, the frequency range occupied by the sidebands will range from 8KHz \\nabove to 8KHz below the carrier frequency, extending fom 712KHz to 720KHz.\");\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Carrier frequency F_c = 712 kHz\n", + "Now , it is given that the highest modulation frequency is 8KHz \n", + "Therefore, the frequency range occupied by the sidebands will range from 8KHz \n", + "above to 8KHz below the carrier frequency, extending fom 712KHz to 720KHz.\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2 - pg 51" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#calculate the modulation index, upper and lower sideband frequency, bandwidth of modulation signal\n", + "\n", + "\n", + "#given\n", + "#v_m = 10*sin(2*%pi*10^3*t)\n", + "#by comparing with v_m = V_m*sin(2*%pi*f_c*t) we get\n", + "V_m = 10.#in volts\n", + "f_m = 1*10**3#in hertz\n", + "V_c = 20.#in volts\n", + "f_c = 1*10**4#in hertz\n", + "\n", + "#calculations\n", + "m_a = V_m/V_c;#modulation index formula\n", + "m_a1 = m_a*100;#percentage modulation index\n", + "f_usb = f_c + f_m;#Upper sideband\n", + "f_lsb = f_c - f_m;#lower sideband\n", + "A = (m_a*V_c)/2#amplitude of upper as well as lower sideband\n", + "B = 2*f_m;#bandwidth of the modulation signal\n", + "\n", + "#results\n", + "print '%s %.2f' %(\"i.a.Modulation index= \",m_a);\n", + "print '%s %d %s' %(\" b.Percentage modulation index=\",m_a1,\" percent\");\n", + "print '%s %.f %s' %(\"ii.a.Upper sidebandfrequency=\",f_usb,\"Hz\");\n", + "print '%s %.f %s' %(\" b.Lower sideband frequency=\",f_lsb,\"Hz \"); \n", + "print '%s %.f %s' %(\"iii.Amplitude of Upper sideband and Lower sideband =\",A,\"V\");\n", + "print '%s %.f %s' %(\"\\iv.Bandwidth of the modulation signal=\",B,\"Hz\");\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "i.a.Modulation index= 0.50\n", + " b.Percentage modulation index= 50 percent\n", + "ii.a.Upper sidebandfrequency= 11000 Hz\n", + " b.Lower sideband frequency= 9000 Hz \n", + "iii.Amplitude of Upper sideband and Lower sideband = 5 V\n", + "\\iv.Bandwidth of the modulation signal= 2000 Hz\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 3 - pg 54" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#calculate the total power in the amplitude modulated wave\n", + "\n", + "\n", + "#given\n", + "m_a = .75;#modulation index\n", + "P_c = 400.;#carrier power in watts\n", + "\n", + "#calculation\n", + "P_t = P_c*(1+(m_a**2/2));#total power \n", + "\n", + "#results\n", + "print \"Total power in the amplitude modulated wave (in W) = \",P_t;\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Total power in the amplitude modulated wave (in W) = 512.5\n" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 4 - pg 54" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#calculate the carrier power\n", + "#given\n", + "P_t = 10*10**3;#total power in watts\n", + "m_a = .6;#modulation index\n", + "#calculation\n", + "P_c = (P_t/(1+(m_a**2/2)));# carrier power\n", + "#results\n", + "print \"Carrier power (in kW) = \",round(P_c/1000.,2)\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Carrier power (in kW) = 8.47\n" + ] + } + ], + "prompt_number": 8 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 5 - pg 55" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#calculate the modulation index and antenna current\n", + "\n", + "import math\n", + "#given\n", + "I_t = 8.93;#total modulated current in ampers\n", + "I_c= 8;#carrier or unmodulated current in ampers\n", + "#calculation\n", + "m_a = math.sqrt(2*((I_t/I_c)**2 -1));#formula for modulation index\n", + "M_a=m_a*100;#percentage modulation\n", + "#for \n", + "m_a1 = .8;#given modulation index\n", + "\n", + "#calculation\n", + "I_t1 = I_c*math.sqrt(1+(m_a1**2/2));#new antenna current \n", + "\n", + "#results\n", + "print \"i.a. Modulation index = \",round(m_a,3)\n", + "print \"b.Percentage modulation index (percent) = \",round(M_a,1)\n", + "print \"ii. Antenna current (in A) = \",round(I_t1,2)\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "i.a. Modulation index = 0.701\n", + "b.Percentage modulation index (percent) = 70.1\n", + "ii. Antenna current (in A) = 9.19\n" + ] + } + ], + "prompt_number": 10 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 6 - pg 56" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#calculate the carrier signal current, modulation indexes\n", + "import math\n", + "#given\n", + "I_t1 = 10#antenna current in amps\n", + "m1 = .3#modulation index\n", + "I_t2 = 11#increased antenna current\n", + "\n", + "#calculation\n", + "I_c = (I_t1/(1+(m1**2/2))**.5);#formula for carrier signal current\n", + "m_t = math.sqrt(2*((I_t2/I_c)**2 -1));#formula for modulation index\n", + "m2 = math.sqrt(m_t**2 - m1**2);\n", + "m3 = m2*100;#percentage modulation index\n", + "\n", + "#results\n", + "print \"i.Carrier signal current (in A) = \",round(I_c,2)\n", + "print \"ii.Modulation index of signal = \",round(m_t,2)\n", + "print \"iii.a.Modulation index of second signal = \",round(m2,2)\n", + "print \"b.Percentage modulation index of second signal (percent) = \",round(m3,0)\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "i.Carrier signal current (in A) = 9.78\n", + "ii.Modulation index of signal = 0.73\n", + "iii.a.Modulation index of second signal = 0.66\n", + "b.Percentage modulation index of second signal (percent) = 66.0\n" + ] + } + ], + "prompt_number": 11 + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/sample_notebooks/Dileep KumarShakya/chapter1.ipynb b/sample_notebooks/Dileep KumarShakya/chapter1.ipynb new file mode 100755 index 00000000..74cd1fa5 --- /dev/null +++ b/sample_notebooks/Dileep KumarShakya/chapter1.ipynb @@ -0,0 +1 @@ +{"nbformat_minor": 0, "cells": [{"source": "#Chapter1 : Electromagnetic Field Radiation", "cell_type": "markdown", "metadata": {}}, {"source": "##Example 1.1, Page number 23", "cell_type": "markdown", "metadata": {}}, {"execution_count": 13, "cell_type": "code", "source": "from __future__ import division\nimport math\n\n#variable declaration\nE= 2 #electric field strength of wave in V/m\nn=120*math.pi #where n is mu [free space impedence (120xpi)]\n\n#calculations\nH=E/n # As n = E/H\nH=H*10**3\n\n#results\nprint \"strength of magnetic field H in free space is %r mA/metre\" % round(H,3)\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "strength of magnetic field H in free space is 5.305 mA/metre\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "##Example 1.2, Page number 23", "cell_type": "markdown", "metadata": {}}, {"execution_count": 14, "cell_type": "code", "source": "from __future__ import division\nimport math\n\n#variable Declaration\nP= 625*10**3 #power of transmitting antenna in Watt\nr=30*10**3 #distance in meter\n\n#calculations\nErms=math.sqrt(90*P)/r\nErms=Erms*10**3\n\n#Results\nprint\"Field strength is %r mV/metre.\" %round(Erms,3)\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Field strength is 250.0 mV/metre.\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "##Example 1.3, Page number 24", "cell_type": "markdown", "metadata": {}}, {"execution_count": 15, "cell_type": "code", "source": "from __future__ import division\nimport math\n\n#variable Declaration\nf=10 #frequency in Mega Hertz\nle=60 #Height of antenna in metres\nlemda=300/f\n\n#calculations\nRr= 160*(math.pi)**2*le**2/lemda**2\nRr=Rr/10**3\n\n#Results\nprint \"Radiation resistance of antenna is %r Kilo ohms.\" %round(Rr,3)\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Radiation resistance of antenna is 6.317 Kilo ohms.\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "##Example 1.4, Page number 24", "cell_type": "markdown", "metadata": {}}, {"execution_count": 16, "cell_type": "code", "source": "from __future__ import division\nimport math\n\n#variable Declaration\nle=100 # height of antenna in Metres\nIrms=450 # current at the base in Amperes\nf= 40000.0 # frequency in Hertz\nf=f/10**6 # frequency in Mega Hertz\n\nlemda= 300/f # as per the formula where frequncy in MHz\n\n#calculations\nRr=160*(math.pi)**2*le**2/lemda**2 #Rr is radiated resistance in ohms\n\n#Results\nprint \"Radiation resistance is %r ohms.\"%round(Rr,2)\n\n#calculations\nPr= Irms**2*Rr # Power radiated in Watts\nPr= Pr/10**3 # Power radiated in Kilo Watts\n\n#Results\nprint \"Power radiated is %r kW.\"%round(Pr,2)\n\n#variable Declaration\nRl=1.12 # otal reistance of antenna circuit in ohms\nn= Rr/Rl # Efficiancy of the antenna n= Radiation Resistance/Total antenna Resistance\n\n#calculations\nnper= n*100 # Efficiancy of the antenna (in percentage)\n\n#Results\nprint \"Efficiancy of antenna is %r percent. \"%round(nper,2)\n\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Radiation resistance is 0.28 ohms.\nPower radiated is 56.85 kW.\nEfficiancy of antenna is 25.07 percent. \n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "##Example 1.5, Page number 25", "cell_type": "markdown", "metadata": {}}, {"execution_count": 17, "cell_type": "code", "source": "\n#variable Declaration\nI=20 # current in Amperers\nRrad= 50 # Radiated resistance in Ohms\n\n#calculations\nPr= I**2*Rrad # Power radiated in watts\n\n#Results\nprint \"Antenna will radiate %r Watts of power.\"%Pr\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Antenna will radiate 20000 Watts of power.\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "##Example 1.6, Page number 25", "cell_type": "markdown", "metadata": {}}, {"execution_count": 18, "cell_type": "code", "source": "\n#variable Declaration\nP=5*10**3 # Power radiated in watts\nI= 15.0 # current in Ampers\n#calculations\nRrad=P/I**2 # Radiated resistance in Ohms\n\n#Results\nprint \"Radiated power is %r ohms.\"%round(Rrad,2)\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Radiated power is 22.22 ohms.\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "##Example 1.7, Page number 26", "cell_type": "markdown", "metadata": {}}, {"execution_count": 19, "cell_type": "code", "source": "from __future__ import division\nimport math\n\n#variable Declaration\nRrad= 75 # Radiation resistance in ohms\nPr= 10 # Power radiated in kW\nPr=Pr*10**3 # Power radiated in W\n\n#calculations\nI=math.sqrt(Pr/Rrad)\n\n#Results\nprint \"%r Amperes current flows in the antenna\"%round(I,2)\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "11.55 Amperes current flows in the antenna\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "##Example 1.8, Page number 26", "cell_type": "markdown", "metadata": {}}, {"execution_count": 20, "cell_type": "code", "source": "from __future__ import division\nimport math\n\n#variable Declaration\nP=W= 100*10**3 # power radiated in watt\nr= 100 # distance in kilo metres\nr=r*10**3 # distance in metres\n\n#calculations\nErms= math.sqrt(90*W)/r\n\n#Results\nprint \"Strength of electric field Erms is %r V/m.\"%Erms\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Strength of electric field Erms is 0.03 V/m.\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "##Example 1.9, Page number 26", "cell_type": "markdown", "metadata": {}}, {"execution_count": 21, "cell_type": "code", "source": "from __future__ import division\nimport math\n\n#variable Declaration\nIrms= 25 # transmitting antenna rms current in A\nf=0.150 # frequency in Mega Hertz(MHz)\nErms= 1.5 # Field strength in mV/m\nErms=Erms/10**3 # Field strength in V/m\nr=25 # distance in kilo metre\nr=r*10**3 # distance in metre\n\n#calculations\nlemda= 300/f\nle= Erms*lemda*r/(60*math.pi*Irms) # le is effective height of the antenna in metres\n\n#Results\nprint \"Effective heigth of the antenna le = %r metres.\"%round(le,2)\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Effective heigth of the antenna le = 15.92 metres.\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "##Example 1.10, Page number 27", "cell_type": "markdown", "metadata": {}}, {"execution_count": 22, "cell_type": "code", "source": "from __future__ import division\nimport math\n\nle= 100 # Heigth of the antenna in metre\nIrms= 100 # rms current in amperes\nr=10 # distance in kilo metre\nr=r*10**3 # distance in metre\nf=300.0 # frequency in KHz\nf=f/10**3 # frequency in MHz\n\nlemda=300/f\n\n#Calculations\nErms= (120*math.pi*Irms*le)/(lemda*r)\n\nRr= (160*(math.pi)**2*le**2)/(lemda**2)\n\nP= Irms**2*Rr # Power radiated in watts\nP= P/10**3 # POwer radiated in kilo watts(kW)\n\n#Results\nprint \"(i)Field strength at Erms is %r mV/m\"%round(Erms*10**3,2)\nprint \"(ii)The power radiated is %r kW.\" %round(P,2)\n\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "(i)Field strength at Erms is 376.99 mV/m\n(ii)The power radiated is 157.91 kW.\n"}], "metadata": {"collapsed": false, "trusted": true}}], "nbformat": 4, "metadata": {"kernelspec": {"display_name": "Python 2", "name": "python2", "language": "python"}, "language_info": {"mimetype": "text/x-python", "nbconvert_exporter": "python", "version": "2.7.8", "name": "python", "file_extension": ".py", "pygments_lexer": "ipython2", "codemirror_mode": {"version": 2, "name": "ipython"}}}} \ No newline at end of file diff --git a/sample_notebooks/Dileep KumarShakya/chapter1_1.ipynb b/sample_notebooks/Dileep KumarShakya/chapter1_1.ipynb new file mode 100755 index 00000000..74cd1fa5 --- /dev/null +++ b/sample_notebooks/Dileep KumarShakya/chapter1_1.ipynb @@ -0,0 +1 @@ +{"nbformat_minor": 0, "cells": [{"source": "#Chapter1 : Electromagnetic Field Radiation", "cell_type": "markdown", "metadata": {}}, {"source": "##Example 1.1, Page number 23", "cell_type": "markdown", "metadata": {}}, {"execution_count": 13, "cell_type": "code", "source": "from __future__ import division\nimport math\n\n#variable declaration\nE= 2 #electric field strength of wave in V/m\nn=120*math.pi #where n is mu [free space impedence (120xpi)]\n\n#calculations\nH=E/n # As n = E/H\nH=H*10**3\n\n#results\nprint \"strength of magnetic field H in free space is %r mA/metre\" % round(H,3)\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "strength of magnetic field H in free space is 5.305 mA/metre\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "##Example 1.2, Page number 23", "cell_type": "markdown", "metadata": {}}, {"execution_count": 14, "cell_type": "code", "source": "from __future__ import division\nimport math\n\n#variable Declaration\nP= 625*10**3 #power of transmitting antenna in Watt\nr=30*10**3 #distance in meter\n\n#calculations\nErms=math.sqrt(90*P)/r\nErms=Erms*10**3\n\n#Results\nprint\"Field strength is %r mV/metre.\" %round(Erms,3)\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Field strength is 250.0 mV/metre.\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "##Example 1.3, Page number 24", "cell_type": "markdown", "metadata": {}}, {"execution_count": 15, "cell_type": "code", "source": "from __future__ import division\nimport math\n\n#variable Declaration\nf=10 #frequency in Mega Hertz\nle=60 #Height of antenna in metres\nlemda=300/f\n\n#calculations\nRr= 160*(math.pi)**2*le**2/lemda**2\nRr=Rr/10**3\n\n#Results\nprint \"Radiation resistance of antenna is %r Kilo ohms.\" %round(Rr,3)\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Radiation resistance of antenna is 6.317 Kilo ohms.\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "##Example 1.4, Page number 24", "cell_type": "markdown", "metadata": {}}, {"execution_count": 16, "cell_type": "code", "source": "from __future__ import division\nimport math\n\n#variable Declaration\nle=100 # height of antenna in Metres\nIrms=450 # current at the base in Amperes\nf= 40000.0 # frequency in Hertz\nf=f/10**6 # frequency in Mega Hertz\n\nlemda= 300/f # as per the formula where frequncy in MHz\n\n#calculations\nRr=160*(math.pi)**2*le**2/lemda**2 #Rr is radiated resistance in ohms\n\n#Results\nprint \"Radiation resistance is %r ohms.\"%round(Rr,2)\n\n#calculations\nPr= Irms**2*Rr # Power radiated in Watts\nPr= Pr/10**3 # Power radiated in Kilo Watts\n\n#Results\nprint \"Power radiated is %r kW.\"%round(Pr,2)\n\n#variable Declaration\nRl=1.12 # otal reistance of antenna circuit in ohms\nn= Rr/Rl # Efficiancy of the antenna n= Radiation Resistance/Total antenna Resistance\n\n#calculations\nnper= n*100 # Efficiancy of the antenna (in percentage)\n\n#Results\nprint \"Efficiancy of antenna is %r percent. \"%round(nper,2)\n\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Radiation resistance is 0.28 ohms.\nPower radiated is 56.85 kW.\nEfficiancy of antenna is 25.07 percent. \n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "##Example 1.5, Page number 25", "cell_type": "markdown", "metadata": {}}, {"execution_count": 17, "cell_type": "code", "source": "\n#variable Declaration\nI=20 # current in Amperers\nRrad= 50 # Radiated resistance in Ohms\n\n#calculations\nPr= I**2*Rrad # Power radiated in watts\n\n#Results\nprint \"Antenna will radiate %r Watts of power.\"%Pr\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Antenna will radiate 20000 Watts of power.\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "##Example 1.6, Page number 25", "cell_type": "markdown", "metadata": {}}, {"execution_count": 18, "cell_type": "code", "source": "\n#variable Declaration\nP=5*10**3 # Power radiated in watts\nI= 15.0 # current in Ampers\n#calculations\nRrad=P/I**2 # Radiated resistance in Ohms\n\n#Results\nprint \"Radiated power is %r ohms.\"%round(Rrad,2)\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Radiated power is 22.22 ohms.\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "##Example 1.7, Page number 26", "cell_type": "markdown", "metadata": {}}, {"execution_count": 19, "cell_type": "code", "source": "from __future__ import division\nimport math\n\n#variable Declaration\nRrad= 75 # Radiation resistance in ohms\nPr= 10 # Power radiated in kW\nPr=Pr*10**3 # Power radiated in W\n\n#calculations\nI=math.sqrt(Pr/Rrad)\n\n#Results\nprint \"%r Amperes current flows in the antenna\"%round(I,2)\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "11.55 Amperes current flows in the antenna\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "##Example 1.8, Page number 26", "cell_type": "markdown", "metadata": {}}, {"execution_count": 20, "cell_type": "code", "source": "from __future__ import division\nimport math\n\n#variable Declaration\nP=W= 100*10**3 # power radiated in watt\nr= 100 # distance in kilo metres\nr=r*10**3 # distance in metres\n\n#calculations\nErms= math.sqrt(90*W)/r\n\n#Results\nprint \"Strength of electric field Erms is %r V/m.\"%Erms\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Strength of electric field Erms is 0.03 V/m.\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "##Example 1.9, Page number 26", "cell_type": "markdown", "metadata": {}}, {"execution_count": 21, "cell_type": "code", "source": "from __future__ import division\nimport math\n\n#variable Declaration\nIrms= 25 # transmitting antenna rms current in A\nf=0.150 # frequency in Mega Hertz(MHz)\nErms= 1.5 # Field strength in mV/m\nErms=Erms/10**3 # Field strength in V/m\nr=25 # distance in kilo metre\nr=r*10**3 # distance in metre\n\n#calculations\nlemda= 300/f\nle= Erms*lemda*r/(60*math.pi*Irms) # le is effective height of the antenna in metres\n\n#Results\nprint \"Effective heigth of the antenna le = %r metres.\"%round(le,2)\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Effective heigth of the antenna le = 15.92 metres.\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "##Example 1.10, Page number 27", "cell_type": "markdown", "metadata": {}}, {"execution_count": 22, "cell_type": "code", "source": "from __future__ import division\nimport math\n\nle= 100 # Heigth of the antenna in metre\nIrms= 100 # rms current in amperes\nr=10 # distance in kilo metre\nr=r*10**3 # distance in metre\nf=300.0 # frequency in KHz\nf=f/10**3 # frequency in MHz\n\nlemda=300/f\n\n#Calculations\nErms= (120*math.pi*Irms*le)/(lemda*r)\n\nRr= (160*(math.pi)**2*le**2)/(lemda**2)\n\nP= Irms**2*Rr # Power radiated in watts\nP= P/10**3 # POwer radiated in kilo watts(kW)\n\n#Results\nprint \"(i)Field strength at Erms is %r mV/m\"%round(Erms*10**3,2)\nprint \"(ii)The power radiated is %r kW.\" %round(P,2)\n\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "(i)Field strength at Erms is 376.99 mV/m\n(ii)The power radiated is 157.91 kW.\n"}], "metadata": {"collapsed": false, "trusted": true}}], "nbformat": 4, "metadata": {"kernelspec": {"display_name": "Python 2", "name": "python2", "language": "python"}, "language_info": {"mimetype": "text/x-python", "nbconvert_exporter": "python", "version": "2.7.8", "name": "python", "file_extension": ".py", "pygments_lexer": "ipython2", "codemirror_mode": {"version": 2, "name": "ipython"}}}} \ No newline at end of file diff --git a/sample_notebooks/HeminChheda/chapter1.ipynb b/sample_notebooks/HeminChheda/chapter1.ipynb new file mode 100755 index 00000000..af04eee8 --- /dev/null +++ b/sample_notebooks/HeminChheda/chapter1.ipynb @@ -0,0 +1,281 @@ +{ + "metadata": { + "name": "" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 1:Water" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example no:1,Page no:1.7" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Variable declaration\n", + "W1=219.0 #amount of Mg(HCO3)2 in water in ppm#\n", + "W2=36.0 #amount of Mg2+ in water in ppm#\n", + "W3=18.3 #amount of (HCO3)- in water in ppm#\n", + "W4=1.5 #amount of H+_in water in ppm#\n", + "M1=100/146.0 #multiplication factor of Mg(HCO3)2#\n", + "M2=100/24.0 #multiplication factor of Mg(HCO3)2#\n", + "M3=100/122.0 #multiplication factor of Mg(HCO3)2#\n", + "M4=100/2.0 #multiplication factor of Mg(HCO3)2#\n", + "\n", + "#Calculation\n", + "P1=W1*M1 #in terms of CaCO3#\n", + "P2=W2*M2 #in terms of CaCO3#\n", + "P3=W3*M3 #in terms of CaCO3#\n", + "P4=W4*M4 #in terms of CaCO3#\n", + "L=0.74*((2*P1)+P2+P3+P4) \n", + "\n", + "R=1.0 #water supply rate in m**3/s#\n", + "D=R*60.0*60.0*24.0*L \n", + "K=D*1000.0 #in lit/day#\n", + "T=K/10.0**9 #in tonnes#\n", + "S=1.06*(P2+P4-P3) \n", + "D2=R*60*60*24*S \n", + "A=D2*1000 #in lit/day#\n", + "B=A/10.0**9 #in tonnes#\n", + "J1=90/100.0 #purity of lime#\n", + "J2=95/100.0 #purity of soda#\n", + "C1=500.0 #cost of one tonne lime#\n", + "C2=7000.0 #cost of one tonne soda#\n", + "CL=round(T,1)*C1/J1 \n", + "print\"\\ncost of lime is\",CL,\"Rs\"\n", + "CS=round(B,1)*C2/J2 \n", + "print\"\\ncost of soda is \",CS,\"Rs\"\n", + "C=CL+CS \n", + "\n", + "#Result\n", + "print\"\\ntotal cost is \",round(C) ,\"Rs\"\n", + "\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "cost of lime is 19166.6666667 Rs\n", + "\n", + "cost of soda is 141473.684211 Rs\n", + "\n", + "total cost is 160640.0 Rs\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example no:15,Page no:1.25" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Variable declaration\n", + "W1=40.0 #amount of Ca2+ in water in mg/l#\n", + "W2=24.0 #amount of Mg2+ in water in mg/l#\n", + "W3=8.05 #amount of Na+ in water in mg/l#\n", + "W4=183.0 #amount of (HCO3)- in water in mg/l#\n", + "W5=55.68 #amount of (SO4)2- in water in mg/l#\n", + "W6=6.74 #amount of Cl- in water in mg/l#\n", + "M1=100/40.0 #multiplication factor of Ca2+#\n", + "M2=100/24.0 #multiplication factor of Mg2+#\n", + "M3=100/(23.0*2) #multiplication factor of Na+#\n", + "M4=100/(61.0*2) #multiplication factor of (HCO3)-#\n", + "M5=100/96.0 #multiplication factor of (SO4)2-#\n", + "M6=100/(35.5*2) #multiplication factor of Cl-#\n", + "\n", + "#Calculation\n", + "P1=W1*M1 #in terms of CaCO3#\n", + "P2=W2*M2 #in terms of CaCO3#\n", + "P3=W3*M3 #in terms of CaCO3#\n", + "P4=W4*M4 #in terms of CaCO3#\n", + "P5=W5*M5 #in terms of CaCO3#\n", + "P6=W6*M6 #in terms of CaCO3#\n", + "\n", + "\n", + "#Result\n", + "print\"\\nCalcium alkalinity =\",P1,\"ppm\" \n", + "print\"\\nMagnesium alkalinity =\",P4-P1,\"ppm\"\n", + "print\"\\n total alkalinity = \",P1+P4-P1,\"ppm\"\n", + "print\"\\n total hardness = \",P1+P2,\"ppm\"\n", + "print\"\\nCa temporary hardness = \",P1,\"ppm\"\n", + "print\"\\nMg temporary hardness = \",P4-P1,\"ppm\"\n", + "print\"\\nMg permanant hardness = \",P2-(P4-P1),\"ppm\"\n", + "print\"\\nSalts are:\"\n", + "print\"\\nCa(HCO3)2 salt = \",P1,\"ppm\"\n", + "print\"\\nMg(HCO3)2 salt = \",P4-P1,\"ppm\"\n", + "print\"\\nMgSO4 salt = \",P2-(P4-P1),\"ppm\"\n", + "print\"\\nNaCl salt = \",P6,\"ppm\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "Calcium alkalinity = 100.0 ppm\n", + "\n", + "Magnesium alkalinity = 50.0 ppm\n", + "\n", + " total alkalinity = 150.0 ppm\n", + "\n", + " total hardness = 200.0 ppm\n", + "\n", + "Ca temporary hardness = 100.0 ppm\n", + "\n", + "Mg temporary hardness = 50.0 ppm\n", + "\n", + "Mg permanant hardness = 50.0 ppm\n", + "\n", + "Salts are:\n", + "\n", + "Ca(HCO3)2 salt = 100.0 ppm\n", + "\n", + "Mg(HCO3)2 salt = 50.0 ppm\n", + "\n", + "MgSO4 salt = 50.0 ppm\n", + "\n", + "NaCl salt = 9.49295774648 ppm\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example no:19,Page no:1.39" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Variable declaration\n", + "P=4.7 #required HCl in ml using HpH indicator #\n", + "H=10.5 #required HCl im ml using MeOH indicator#\n", + "M=P+H \n", + "N=0.02 #normality of HCl#\n", + "\n", + "print\"\\nSince P<0.5*M sample contain (CO3)2- and (HCO3)- alkalinity\"\n", + "C=50 #equivalent of CaCO3 in mg for 1ml 1N HCl#\n", + "\n", + "#Calculation\n", + "A=C*(2*P)*N #amount of (CO3)2- alkalinity in mg in 100 ml of water#\n", + "B=A*1000/100 \n", + "D=C*(M-2*P)*N #the amount of (HCO3)- alkalinity in mg in 100 ml of water#\n", + "E=D*1000/100 \n", + "T=B+E \n", + "\n", + "#Result\n", + "print\"\\nTotal alkalinity is \",T,\"mg/l or ppm\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "Since P<0.5*M sample contain (CO3)2- and (HCO3)- alkalinity\n", + "\n", + "Total alkalinity is 152.0 mg/l or ppm\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example no:25,Page no:1.56" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Variable declaration\n", + "W1=160.0 #amount of Ca2+ in ppm#\n", + "W2=88.0 #amount of Mg2+ in ppm#\n", + "W3=72.0 #amount of CO2 in ppm#\n", + "W4=488.0 #amount of (HCO3)- in ppm#\n", + "W5=139.0 #amount of (FeSO4).7H2O in ppm#\n", + "M1=100/40.0 #multiplication factor of Ca2+#\n", + "M2=100/24.0 #multiplication factor of Mg2+#\n", + "M3=100/44.0 #multiplication factor of CO2#\n", + "M4=100/(61.0*2.0) #multiplication factor of (HCO3)-#\n", + "M5=100/278.0 #multiplication factor of (FeSO4).7H2O#\n", + "\n", + "P1=400 #in terms of CaCO3#\n", + "P2=300 #in terms of CaCO3#\n", + "P3=200 #in terms of CaCO3#\n", + "P4=400 #in terms of CaCO3#\n", + "P5=50 #in terms of CaCO3#\n", + "V=100000.0 #volume of water in litres#\n", + "\n", + "\n", + "#Calculation\n", + "L=0.74*(P2+P3+P4+P5)*V #lime required in mg#\n", + "L=L/10.0**6 \n", + "S=1.06*(P1+P2+P5-P4)*V #soda required in mg#\n", + "S=S/10.0**6 \n", + "\n", + "#Result\n", + "print\"Lime required is \",L,\"kg\"\n", + "print\"\\nSoda required is \",S,\"kg\"\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Lime required is 70.3 kg\n", + "\n", + "Soda required is 37.1 kg\n" + ] + } + ], + "prompt_number": 4 + }, + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/sample_notebooks/LaxmanSole/Pinciples_of_electronic_Instrumentation_Ch1.ipynb b/sample_notebooks/LaxmanSole/Pinciples_of_electronic_Instrumentation_Ch1.ipynb new file mode 100755 index 00000000..229d9b52 --- /dev/null +++ b/sample_notebooks/LaxmanSole/Pinciples_of_electronic_Instrumentation_Ch1.ipynb @@ -0,0 +1,406 @@ +{ + "metadata": { + "name": "" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 1 : Basic Concepts" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example1_1,pg 481" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# unknown resistance\n", + "\n", + "import math\n", + "#VAriable declaration\n", + "Ir=10*10**-3 #current drawn by resistor\n", + "Vr=100.0 #voltage across resistor\n", + "Rv=40*10**3 #voltmeter resistance\n", + "\n", + "#Calcualtions\n", + "Ru=(Vr/Ir)*(1/(1-(Vr/(Ir*Rv)))) \n", + "\n", + "#Result\n", + "print(\"output resistance:\")\n", + "print(\"Ru=%.2f ohm\"%Ru)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "output resistance:\n", + "Ru=13333.33 ohm\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example1_2,pg 481" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# unknown resistance\n", + "\n", + "import math\n", + "#Variable declaration\n", + "Ir=10*10**-3 #current drawn by resistor\n", + "Vr=100.0 #voltage across resistor\n", + "Rv=40*10**3 #voltmeter resistance\n", + "Ra=1.0 #ammeter resistance\n", + "\n", + "#Calculations\n", + "Ru=(Rv/Ir)-Ra\n", + "\n", + "#Result\n", + "print(\"output resistance:\")\n", + "print(\"Ru=%.2f ohm\"%Ru)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "output resistance:\n", + "Ru=3999999.00 ohm\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example1_3,pg 481" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# find ammeter reading\n", + "\n", + "import math\n", + "#Variable declaration\n", + "Rv=40*10**3 #voltmeter resistance\n", + "Ra=1.0 #ammeter resistance\n", + "Vr=40.0 #voltmeter reading\n", + "Ru=10*10**3 #unknown resistance\n", + "\n", + "#Calculations\n", + "Ir=(Vr*(Rv+Ru))/(Ru*Rv)\n", + "Ir1=(Vr/(Ru+Ra))\n", + "\n", + "#Result\n", + "print(\"ammeter reading case1:\")\n", + "print(\"Ir=%.4f A\"%Ir)\n", + "print(\"\\nammeter reading case2:\")\n", + "print(\"Ir1=%.4f A\"%Ir1)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "ammeter reading case1:\n", + "Ir=0.0050 A\n", + "\n", + "ammeter reading case2:\n", + "Ir1=0.0040 A\n" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example1_4,pg 482" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# unknown resistance\n", + "\n", + "import math\n", + "#Variable declaration\n", + "Vs=3.0 #supply voltage\n", + "Vu=2.75 #voltmeter reading\n", + "Rp=10*10**3 #parallel resistance\n", + "\n", + "#Calculations\n", + "Ru=Rp*((Vs/Vu)-1)\n", + "\n", + "#Result\n", + "print(\"unknown resistance:\")\n", + "print(\"Ru=%.2f ohm\"%Ru)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "unknown resistance:\n", + "Ru=909.09 ohm\n" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example1_5,pg 482" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Find input vlotage\n", + "\n", + "#with input voltage exceding 2Vd,diodes conduct and the voltage divider circuit with diodes can allow only a Vi given by Vi=2Vd\n", + "\n", + "#Result\n", + "print(\"input voltage to amplifier:\")\n", + "print(\"Vi=2Vd\")" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "input voltage to amplifier:\n", + "Vi=2Vd\n" + ] + } + ], + "prompt_number": 8 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "Example1_6,pg 482" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# find shunt resistance\n", + "\n", + "import math\n", + "#Variable declaration\n", + "Rm=1000.0 #meter resistance\n", + "Is=900*10**-6 #shunt current\n", + "Vm=100*10**-3 #drop across meter\n", + "\n", + "#Result\n", + "Rs=Vm/Is\n", + "It=1*10**-3\n", + "#Is=It*(Rm/(Rs+Rm))\n", + "Rs=(Rm*(It-Is))/Is\n", + "\n", + "#Result\n", + "print(\"shunt resistance:\")\n", + "print(\"Rs=%.2f ohm\"%Rs)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "shunt resistance:\n", + "Rs=111.11 ohm\n" + ] + } + ], + "prompt_number": 10 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example1_7,pg 483" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# find series resistor\n", + "\n", + "import math\n", + "#Variable declaration\n", + "If=100*10**-6 #full scale current\n", + "Rm=1000.0 #meter resistance\n", + "Vf=10.0 #full scale voltage\n", + "\n", + "#Calculations\n", + "Rs=(Vf/If)-Rm\n", + "\n", + "#Result\n", + "print(\"series resistance:\")\n", + "print(\"Rs=%.2f ohm\"%Rs)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "series resistance:\n", + "Rs=99000.00 ohm\n" + ] + } + ], + "prompt_number": 11 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "Example1_8,pg 483" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# sensitivity\n", + "\n", + "import math\n", + "#Variable declaration\n", + "If=100*10**-6 # Current\n", + "\n", + "#Calculations\n", + "S=1/If\n", + "\n", + "#Result\n", + "print(\"sensitivity:\")\n", + "print(\"S=%.2f ohm/volt\"%S)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "sensitivity:\n", + "S=10000.00 ohm/volt\n" + ] + } + ], + "prompt_number": 12 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example1_9,pg 483" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# error in measurment\n", + "\n", + "import math\n", + "# Variable declaration\n", + "\n", + "#assume that the voltmeter full scale reading is 12V which gives its resistance as 1.2*10^6 ohm \n", + "#which is in parallel with 10*10^6 ohm making as equivalent of Rq given as\n", + "R=1.2*10**6 #voltmeter resistance\n", + "R1=10*10**6 #voltage divider resistance\n", + "Vin=12.0 #input voltage to divider network\n", + "Rs=4*10**6 # series resistance\n", + "\n", + "\n", + "#Calculations\n", + "Rq=(R*R1)/(R+R1)\n", + "Vq=(Rq*Vin)/(Rq+Rs) #voltage across equivalent combination\n", + "Va=(R1*Vin)/(R1+Rs) #actual volatge\n", + "er=(Vq-Va)/Va #error\n", + "\n", + "#Result\n", + "print(\"error in measurement:\")\n", + "print(\"\\ner=%.3f \"%er)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "error in measurement:\n", + "\n", + "er=-0.704 \n" + ] + } + ], + "prompt_number": 14 + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/sample_notebooks/MohdAsif/Chapter2_-_Measurement_Errors.ipynb b/sample_notebooks/MohdAsif/Chapter2_-_Measurement_Errors.ipynb new file mode 100755 index 00000000..3442a20e --- /dev/null +++ b/sample_notebooks/MohdAsif/Chapter2_-_Measurement_Errors.ipynb @@ -0,0 +1,1549 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:95b9e0f83468dda84f2de4d99c5a704a6fadf8064c232b063678fd245192ca75" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter2 - Measurement Errors" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2.3.1 - page : 2-8" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#precision of the 5th measurement\n", + "#given data :\n", + "X1=98.0 \n", + "X2=101.0\n", + "X3=102.0 \n", + "X4=97.0 \n", + "X5=101.0 \n", + "X6=100.0 \n", + "X7=103.0 \n", + "X8=98.0 \n", + "X9=106.0 \n", + "X10=99.0 \n", + "Xn_bar=(X1+X2+X3+X4+X5+X6+X7+X8+X9+X10)/10 \n", + "Xn=101 # value of 5th measurement\n", + "P=(1-abs((Xn-Xn_bar)/Xn_bar))*100 \n", + "print \"Precision of the 5th measurement, P = \", round(P,2), \" %\"\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Precision of the 5th measurement, P = 99.5 %\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2.3.2.i - page : 2-10" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Absolute error\n", + "#given data :\n", + "Ae=80.0 # in V\n", + "Am=79.0 # in V\n", + "e=Ae-Am \n", + "print \"Absolute error, e = \", e, \" V\"\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Absolute error, e = 1.0 V\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2.3.2.ii - page : 2-10" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Error\n", + "#given data :\n", + "Ae=80.0 # in V\n", + "Am=79.0 # in V\n", + "e=Ae-Am \n", + "error1=(e/Ae)*100 \n", + "print \"Error = \", error1, \" %\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Error = 1.25 %\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2.3.2.iii - page : 2-10" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Relative accuracy\n", + "#given data :\n", + "Ae=80.0 # in V\n", + "Am=79.0 # in V\n", + "e=Ae-Am \n", + "error1=(e/Ae)*100 \n", + "A=(1-abs(e/Ae)) \n", + "print \"Relative Accuracy, A = \", A" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Relative Accuracy, A = 0.9875\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2.3.2.iv - page : 2-10" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# % accuracy\n", + "#given data :\n", + "Ae=80.0 # in V\n", + "Am=79.0 # in V\n", + "e=Ae-Am \n", + "error1=(e/Ae)*100 \n", + "A=(1-abs(e/Ae)) \n", + "accuracy=A*100 \n", + "print \"Accuracy = \", accuracy, \" %\"\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Accuracy = 98.75 %\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2.3.2.v - page : 2-10" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# % error\n", + "#given data :\n", + "Ae=80.0 # in V\n", + "Am=79.0 # in V\n", + "e=Ae-Am \n", + "f=100.0 #full scale deflection\n", + "error1=(e/Ae)*100 \n", + "A=(1-abs(e/Ae)) \n", + "accuracy=A*100 \n", + "P_error=(e/f)*100 \n", + "print \"% error = \", P_error, \" %\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "% error = 1.0 %\n" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2.3.3 - page : 2-11" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Maximum error\n", + "#given data :\n", + "V1=100.0 # in V\n", + "V2=200.0 #in V\n", + "V=V2-V1 \n", + "A=0.25 #may be \u00b1 in %\n", + "max_error=(A/100)*V \n", + "print \"Maximum error = \u00b1 \", max_error, \" V\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Maximum error = \u00b1 0.25 V\n" + ] + } + ], + "prompt_number": 8 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2.3.4 - page : 2-12" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# sensitivity and deflection error\n", + "#given data :\n", + "C=4.0 # change in output in mm\n", + "M=8.0 # magnitude of input in ohm\n", + "S=C/M \n", + "print \"sensitivity, S = \", S, \" mm/ohm\"\n", + "D=M/C \n", + "print \"Deflection factor, D = \", D, \" ohm/mm\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "sensitivity, S = 0.5 mm/ohm\n", + "Deflection factor, D = 2.0 ohm/mm\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2.3.5 - page : 2-14" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Resolution\n", + "#given data :\n", + "V=200.0 # full scale reading in V\n", + "N=100.0 # number of divisions \n", + "Scale_div=V/N \n", + "R=(1.0/10)*Scale_div \n", + "print \"Resolution, R = \", R, \" V\"\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Resolution, R = 0.2 V\n" + ] + } + ], + "prompt_number": 11 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2.3.6 - page : 2-14" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Resolution\n", + "#given data :\n", + "V=9.999 # full scale read out in volt\n", + "c=9999.0 # range from 0 to 9999\n", + "R=(1/c)*V*10**3 \n", + "print \"Resolution, R = \", R, \" mV\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Resolution, R = 1.0 mV\n" + ] + } + ], + "prompt_number": 12 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2.6.1 - page : 2-23" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Magnitude and relative error\n", + "#given data :\n", + "R1=15.0 #ohm\n", + "E1=R1*5.0/100 # \u00b1 limiting error for R1\n", + "R2=33.0 #ohm\n", + "E2=R2*2.0/100 # \u00b1 limiting error for R2\n", + "R3=75.0 #ohm\n", + "E3=R3*5.0/100 # \u00b1 limiting error for R3\n", + "RT=R1+R2+R3 # ohm(in series)\n", + "ET=E1+E2+E3 #\u00b1limiting error for RT\n", + "print \"For series connection, magnitude is \", RT, \" ohm & limiting error is \u00b1 \", ET, \" ohm.\" \n", + "Epr=ET/RT*100 #%\n", + "print \"Percent relative error : \u00b1\", round(Epr,1),\" %\" \n", + "\n", + "# Answer is not accurate in the textbook." + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "For series connection, magnitude is 123.0 ohm & limiting error is \u00b1 5.16 ohm.\n", + "Percent relative error : \u00b1 4.2 %\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2.6.2 - page : 2-23" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Magnitude and relative error\n", + "#given data :\n", + "R1=36.0 #ohm\n", + "E1=5.0 # \u00b1 limiting error for R1\n", + "R2=75.0 #ohm\n", + "E2=5.0 # \u00b1 limiting error for R2\n", + "RT=(R1*R2)/(R1+R2) #ohm(in parallel)\n", + "EP1=E1+E2 # \u00b1 limiting error\n", + "EP2=((R1*E1)/(R1+R2))+((R2*E2)/(R1+R2)) \n", + "ET=EP1+EP2 \n", + "etm=(ET/100)*RT \n", + "print \"Magnitude of limiting error is \u00b1\", round(etm,2), \" ohm\"\n", + "print \"Percentage relative error is \u00b1\", ET, \" %\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Magnitude of limiting error is \u00b1 3.65 ohm\n", + "Percentage relative error is \u00b1 15.0 %\n" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2.6.3 page : 2-24" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Limiting error\n", + "vr=40.0 #reading of voltmeter in volts\n", + "v=50.0 #rane in volts\n", + "va=50.0 #ammeeter reading in mA\n", + "i=125.0 #range in mA\n", + "fsd=2.0 #accurace in percentage in \u00b1\n", + "dv=(2.0/100)*v #limiting error of voltmeter\n", + "da=(2./100)*i #liming error of the ammeter in mA\n", + "erv=dv/vr #relative limiting error in voltmeter reading\n", + "eri=da/i #relative limiting error in ammeter reading\n", + "et=erv+eri \n", + "pet=et*100 #percentage limiting error of the power calcultaed\n", + "print \"Percentage limiting error of the power calcultaed is \u00b1 \",pet,\" %\"\n", + "\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Percentage limiting error of the power calcultaed is \u00b1 4.5 %\n" + ] + } + ], + "prompt_number": 8 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2.6.4 - page : 2-25" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# limiting error\n", + "r1=120.0 # ohm\n", + "er1=0.5 #limiting error in resistance 1 in ohm \u00b1\n", + "r2=2 #in A\n", + "er2=0.02 #limiting error in amperes \u00b1\n", + "e1=er2/r2 #limiting error in current\n", + "e2=er1/r1 #limiting eror in resistance\n", + "et=(2*e1+e2) #total error\n", + "etp=et*100 #percentage limtimg error\n", + "print \"Percentage limiting error in the value of power dissipation is \u00b1\",round(etp,3)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Percentage limiting error in the value of power dissipation is \u00b1 2.417\n" + ] + } + ], + "prompt_number": 11 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2.6.5 - page : 2-25" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#magnitude and limiting error\n", + "r1=120 #in ohm\n", + "er1=0.1 #limiting error in resistance 1 in ohm \u00b1\n", + "r2=2700 #in ohm\n", + "er2=0.5 #limiting error in resistance 2 in ohm \u00b1\n", + "r3=470 #in ohm\n", + "er3=0.5 #limiting error in resistance 3 in ohm \u00b1\n", + "rxm=(r2*r3)/r1 #magnitude of unknown resistance in ohm\n", + "rxe=(er1+er2+er3) #error\n", + "er=(rxe*rxm)/100 #relative error \u00b1\n", + "print \"Magnitude of unknown resistance is \",rxm,\" kohm\"\n", + "print \"Relative limiting error is \u00b1\",er,\" ohm\"\n", + "print \"Guranteed value of resistance is between \",rxm-er, \" ohm to \" ,rxm+er,\" ohm\"\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Magnitude of unknown resistance is 10575 kohm\n", + "Relative limiting error is \u00b1 116.325 ohm\n", + "Guranteed value of resistance is between 10458.675 ohm to 10691.325 ohm\n" + ] + } + ], + "prompt_number": 14 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2.6.6 - page : 2-26" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# absolute error, % error, relative error, % accuracy and % error of full scale reading\n", + "#given data :\n", + "Ae=80.0 # in volt\n", + "Am=79 # in volt\n", + "fsd=100 #full scale reading in volt\n", + "e=Ae-Am \n", + "print \"Absolute error, e = \",e,\" V\"\n", + "error1=(e/Ae)*100 \n", + "print \"Error = \",error1,\" %\"\n", + "A=1-abs(e/Ae) \n", + "print \"Relative accuracy, A = \",A,\" %\"\n", + "p_accuracy=A*100 \n", + "print \"% accuracy = \",p_accuracy,\" %\"\n", + "error2=(e/fsd)*100 \n", + "print \"% error expressed as percentage of full scale reading = \",error2,\" %\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Absolute error, e = 1.0 V\n", + "Error = 1.25 %\n", + "Relative accuracy, A = 0.9875 %\n", + "% accuracy = 98.75 %\n", + "% error expressed as percentage of full scale reading = 1.0 %\n" + ] + } + ], + "prompt_number": 17 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2.6.7 - page : 2-27" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# limiting error\n", + "#given data :\n", + "fsd=100.0 # in V\n", + "A=1.0 # (+ve or -ve) in %\n", + "del_A=(A/100)*fsd \n", + "As=15.0 #in V\n", + "e1=del_A/As \n", + "e=e1*100 \n", + "print \"Limiting error, e = \",round(e,4),\" %\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Limiting error, e = 6.6667 %\n" + ] + } + ], + "prompt_number": 22 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2.6.8 - page : 2-27 " + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# limiting value of current and % limiting error\n", + "#given data :\n", + "As=2.5 # in A\n", + "fsd=10 #full scale reading in A\n", + "A=1.5/100 \n", + "del_A=A*fsd \n", + "At1=As+del_A \n", + "At2=As-del_A \n", + "print \"Limiting value of current, At1 = \",At1,\" A\"\n", + "print \"Limiting value of current, At2 = \",At2,\" A\"\n", + "e=(del_A/As)*100 \n", + "print \"Percentage limiting error, e = \",e,\" %\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Limiting value of current, At1 = 2.65 A\n", + "Limiting value of current, At2 = 2.35 A\n", + "Percentage limiting error, e = 6.0 %\n" + ] + } + ], + "prompt_number": 23 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2.7.1.i - page : 2-30" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#ARITHEMATIC MEAN\n", + "import numpy\n", + "q=[49.7,50.1,50.2,49.6,49.7] #\n", + "AM= numpy.mean(q) #arithematic mean in mm\n", + "print \"Arithematic mean is \",AM\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Arithematic mean is 49.86\n" + ] + } + ], + "prompt_number": 57 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2.7.1.ii - page : 2-30" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#deviation\n", + "import numpy\n", + "q=[49.7,50.1,50.2,49.6,49.7] #\n", + "AM= numpy.mean(q) #arithematic mean in mm\n", + "d=q-AM\n", + "print \"Deviations of each value are : \"\n", + "for dev in d:\n", + " print dev\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Deviations of each value are : \n", + "-0.16\n", + "0.24\n", + "0.34\n", + "-0.26\n", + "-0.16\n" + ] + } + ], + "prompt_number": 58 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2.7.1.iii - page : 2-30" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#algebric sum of deviation\n", + "import numpy\n", + "q=[49.7,50.1,50.2,49.6,49.7] #\n", + "AM= numpy.mean(q) #arithematic mean in mm\n", + "d=q-AM\n", + "dtotal=sum(d)\n", + "print \"Algebric sum of deviation is\", round(dtotal,4)\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Algebric sum of deviation is 0.0\n" + ] + } + ], + "prompt_number": 59 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2.7.1.iv - page : 2-30" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#standard deviation\n", + "import numpy\n", + "q=[49.7,50.1,50.2,49.6,49.7] #\n", + "AM= numpy.mean(q) #arithematic mean in mm\n", + "d=q-AM\n", + "sigma=0\n", + "n=5 # no. of measurements\n", + "for dev in d:\n", + " sigma+=dev**2\n", + "sigma/=(n-1)\n", + "sigma**=(1.0/2)\n", + "print \"Standard Deviation is \",round(sigma,2)\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Standard Deviation is 0.27\n" + ] + } + ], + "prompt_number": 60 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2.7.2.i - page : 2-31" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#ARITHEMATIC MEAN\n", + "import numpy\n", + "q=[101.2,101.4,101.7,101.3,101.3,101.2,101.0,101.3,101.5,101.1] #\n", + "AM= numpy.mean(q) #arithematic mean in mm\n", + "print \"Arithematic mean is \",AM,\" V\"\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Arithematic mean is 101.3 V\n" + ] + } + ], + "prompt_number": 61 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2.7.2.ii - page : 2-31" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Deviation from mean\n", + "import numpy\n", + "q=[101.2,101.4,101.7,101.3,101.3,101.2,101.0,101.3,101.5,101.1] #\n", + "AM= numpy.mean(q) #arithematic mean in mm\n", + "d=q-AM\n", + "print \"Deviations of each value are : \"\n", + "for dev in d:\n", + " print dev\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Deviations of each value are : \n", + "-0.1\n", + "0.1\n", + "0.4\n", + "0.0\n", + "0.0\n", + "-0.1\n", + "-0.3\n", + "0.0\n", + "0.2\n", + "-0.2\n" + ] + } + ], + "prompt_number": 62 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2.7.2.iii - page : 2-31" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#standard deviation\n", + "import numpy\n", + "q=[101.2,101.4,101.7,101.3,101.3,101.2,101.0,101.3,101.5,101.1] \n", + "AM= numpy.mean(q) #arithematic mean in mm\n", + "d=q-AM\n", + "sigma=0\n", + "n=10 # no. of measurements\n", + "for dev in d:\n", + " sigma+=dev**2\n", + "sigma/=(n-1)\n", + "sigma**=(1.0/2)\n", + "print \"Standard Deviation is \",round(sigma,2)\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Standard Deviation is 0.2\n" + ] + } + ], + "prompt_number": 63 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2.7.2.iv - page : 2-31" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#probable error\n", + "import numpy\n", + "q=[101.2,101.4,101.7,101.3,101.3,101.2,101.0,101.3,101.5,101.1] \n", + "AM= numpy.mean(q) #arithematic mean in mm\n", + "d=q-AM\n", + "sigma=0\n", + "n=10 # no. of measurements\n", + "for dev in d:\n", + " sigma+=dev**2\n", + "sigma/=(n-1)\n", + "sigma**=(1.0/2)\n", + "pe1=0.6745*sigma # Probable error of one reading\n", + "print \"Probable error of one reading is \",pe1,\" V\"\n", + "pm=pe1/(n-1)**(1.0/2)\n", + "print \"Probable error of mean is \",round(pm,5)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Probable error of one reading is 0.1349 V\n", + "Probable error of mean is 0.04497\n" + ] + } + ], + "prompt_number": 64 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2.7.3.i - page : 2-32" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Arithmetic mean\n", + "#given data :\n", + "X1=147.2 # in nF\n", + "X2=147.4 # in nF\n", + "X3=147.9 # in nF\n", + "X4=148.1 # in nF\n", + "X5=148.1 # in nF\n", + "X6=147.5 # in nF\n", + "X7=147.6 # in nF\n", + "X8=147.4 # in nF\n", + "X9=147.6 # in nF\n", + "X10=147.5 # in nF\n", + "AM=(X1+X2+X3+X4+X5+X6+X7+X8+X9+X10)/10 \n", + "print \"Arithmetic mean, AM = \",AM,\" nF\" " + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Arithmetic mean, AM = 147.63 nF\n" + ] + } + ], + "prompt_number": 77 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2.7.3.ii - page : 2-32" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Average deviation\n", + "#given data :\n", + "n=10 \n", + "X1=147.2 # in nF\n", + "X2=147.4 # in nF\n", + "X3=147.9 # in nF\n", + "X4=148.1 # in nF\n", + "X5=148.1 # in nF\n", + "X6=147.5 # in nF\n", + "X7=147.6 # in nF\n", + "X8=147.4 # in nF\n", + "X9=147.6 # in nF\n", + "X10=147.5 # in nF\n", + "AM=(X1+X2+X3+X4+X5+X6+X7+X8+X9+X10)/n \n", + "d1=X1-AM \n", + "d2=X2-AM \n", + "d3=X3-AM \n", + "d4=X4-AM \n", + "d5=X5-AM \n", + "d6=X6-AM \n", + "d7=X7-AM \n", + "d8=X8-AM \n", + "d9=X9-AM \n", + "d10=X10-AM \n", + "Average_deviation=(abs(d1)+abs(d2)+abs(d3)+abs(d4)+abs(d5)+abs(d5)+abs(d6)+abs(d7)+abs(d8)+abs(d9)+abs(d10))/n \n", + "print \"Average deviation = \",Average_deviation,\" nF\"\n", + "# answer is wrong in book" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Average deviation = 0.289 nF\n" + ] + } + ], + "prompt_number": 79 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2.7.3.iii - page : 2-32" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Standard deviation\n", + "#given data :\n", + "n=10 \n", + "X1=147.2 # in nF\n", + "X2=147.4 # in nF\n", + "X3=147.9 # in nF\n", + "X4=148.1 # in nF\n", + "X5=148.1 # in nF\n", + "X6=147.5 # in nF\n", + "X7=147.6 # in nF\n", + "X8=147.4 # in nF\n", + "X9=147.6 # in nF\n", + "X10=147.5 # in nF\n", + "AM=(X1+X2+X3+X4+X5+X6+X7+X8+X9+X10)/n \n", + "d1=X1-AM \n", + "d2=X2-AM \n", + "d3=X3-AM \n", + "d4=X4-AM \n", + "d5=X5-AM \n", + "d6=X6-AM \n", + "d7=X7-AM \n", + "d8=X8-AM \n", + "d9=X9-AM \n", + "d10=X10-AM \n", + "sigma=((d1**2+d2**2+d3**2+d4**2+d5**2+d6**2+d7**2+d8**2+d9**2+d10**2)/(n-1))**(1.0/2) \n", + "print \"Standard deviation = \",round(sigma,4),\" nF\"\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Standard deviation = 0.3057 nF\n" + ] + } + ], + "prompt_number": 82 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2.7.3.iv - page : 2-32" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#: Probable error\n", + "#given data :\n", + "n=10 \n", + "X1=147.2 # in nF\n", + "X2=147.4 # in nF\n", + "X3=147.9 # in nF\n", + "X4=148.1 # in nF\n", + "X5=148.1 # in nF\n", + "X6=147.5 # in nF\n", + "X7=147.6 # in nF\n", + "X8=147.4 # in nF\n", + "X9=147.6 # in nF\n", + "X10=147.5 # in nF\n", + "AM=(X1+X2+X3+X4+X5+X6+X7+X8+X9+X10)/n \n", + "d1=X1-AM \n", + "d2=X2-AM \n", + "d3=X3-AM \n", + "d4=X4-AM \n", + "d5=X5-AM \n", + "d6=X6-AM \n", + "d7=X7-AM \n", + "d8=X8-AM \n", + "d9=X9-AM \n", + "d10=X10-AM \n", + "sigma=((d1**2+d2**2+d3**2+d4**2+d5**2+d6**2+d7**2+d8**2+d9**2+d10**2)/(n-1))**(1.0/2)\n", + "Pe1=0.6745*sigma # probable error of one reading\n", + "probable_error=Pe1/(n-1)**(1.0/2)\n", + "print \"Probable error of one reading = \",round(Pe1,4),\" nF\"\n", + "print \"Probable error of mean = \",round(probable_error,4),\" nF\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Probable error of one reading = 0.2062 nF\n", + "Probable error of mean = 0.0687 nF\n" + ] + } + ], + "prompt_number": 86 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2.7.4.i - page : 2-34" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#ARITHEMATIC MEAN\n", + "import numpy\n", + "q=[10.3,10.7,10.9,9.7,9.5,9.2,10.3,11.7] #\n", + "AM= numpy.mean(q) #arithematic mean in mm\n", + "print \"Arithematic mean is \",AM,\" kg/cm2\"\n", + "#answer is wrong in textbook\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Arithematic mean is 10.2875 kg/cm2\n" + ] + } + ], + "prompt_number": 65 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2.7.4.ii - page : 2-34" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#average deviation\n", + "import numpy\n", + "n=8 # NO. OF MEASUREMENTS\n", + "q=[10.3,10.7,10.9,9.7,9.5,9.2,10.3,11.7] #\n", + "AM= numpy.mean(q) #arithematic mean in mm\n", + "d=q-AM # deviation\n", + "davg=sum(abs(d))/n # average deviation\n", + "print \"Average deviation = \",round(davg,4),\" kg/cm2\"\n", + "#answer is wrong in textbook" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Average deviation = 0.6156 kg/cm2\n" + ] + } + ], + "prompt_number": 66 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2.7.4.iii - page : 2-34" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#standard deviation\n", + "import numpy\n", + "q=[10.3,10.7,10.9,9.7,9.5,9.2,10.3,11.7] #\n", + "AM= numpy.mean(q) #arithematic mean in mm\n", + "d=q-AM\n", + "sigma=0\n", + "n=8 # no. of measurements\n", + "for dev in d:\n", + " sigma+=dev**2\n", + "sigma/=(n-1)\n", + "sigma**=(1.0/2)\n", + "print \"Standard Deviation is \",round(sigma,4),\" kg/cm2\"\n", + "#answer is wrong in textbook\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Standard Deviation is 0.8184 kg/cm2\n" + ] + } + ], + "prompt_number": 95 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2.7.4.iv - page : 2-34" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#probable error\n", + "n=8 # no. of measurements\n", + "q=[10.3,10.7,10.9,9.7,9.5,9.2,10.3,11.7] #\n", + "AM= numpy.mean(q) #arithematic mean in mm\n", + "d=q-AM\n", + "sigma=0\n", + "n=10 # no. of measurements\n", + "for dev in d:\n", + " sigma+=dev**2\n", + "sigma/=(n-1)\n", + "sigma**=(1.0/2)\n", + "pe1=0.6745*sigma # Probable error of one reading\n", + "print \"Probable error of one reading is \",round(pe1,4),\" kg/cm2\"\n", + "pm=pe1/(n-1)**(1.0/2)\n", + "print \"Probable error of mean is \",round(pm,4),\" kg/cm2\"\n", + "#answer is wrong in textbook\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Probable error of one reading is 0.4868 kg/cm2\n", + "Probable error of mean is 0.1623 kg/cm2\n" + ] + } + ], + "prompt_number": 67 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2.8.1 - page : 2-34" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#ARITHEMATIC MEAN ,median value ,standard deviation and variance\n", + "q=[25.5,30.3,31.1,29.6,32.4,39.4,28.9,30.0,33.3,31.4,29.5,30.5,31.7,33.0,29.2] #\n", + "AM= numpy.mean(q) #arithematic mean in mm\n", + "n=len(q) # no. of measurements\n", + "Q=q-AM\n", + "mv=sorted(q)[n/2] # get the median value from sorted q\n", + "d=q-AM\n", + "sigma=0\n", + "for dev in d:\n", + " sigma+=dev**2\n", + "sigma/=(n-1)\n", + "sigma**=(1.0/2) #standard deviation\n", + "V=sigma**2 #variance\n", + "print \"Arithematic mean is \",round(AM,4),\" V\"\n", + "print \"Median value is\",round(mv,1)\n", + "\n", + "print \"Standard Deviation is \",round(sigma,2)\n", + "\n", + "print \"Variance is \",round(V,0)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Arithematic mean is 31.0533 V\n", + "Median value is 30.5\n", + "Standard Deviation is 3.0\n", + "Variance is 9.0\n" + ] + } + ], + "prompt_number": 116 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2.8.2 - page : 2-37" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#ARITHEMATIC MEAN\n", + "#from __future__ import division\n", + "v=[10,11,12,13,14] #\n", + "f=[03,12,18,12,03] #\n", + "xn=[a*b for a,b in zip(v,f)]\n", + "am=sum(xn)/sum(f) # arithmetic mean\n", + "print \"Arithematic mean is \",am,\" V\"\n", + "dn=[x-am for x in v] # deviation\n", + "n_dn=[a*b for a,b in zip(f,dn)]\n", + "dn2=[a*b for a,b in zip(dn,dn)]\n", + "n_dn2=[a*b for a,b in zip(f,dn2)]\n", + "absn_dn=[abs(a) for a in n_dn]\n", + "mean_dev=sum(absn_dn)/sum(f)\n", + "print \"Mean deviation = \",mean_dev\n", + "sigma=(sum(n_dn2)/sum(f))**(1.0/2)\n", + "print \"Standard deviation is \", sigma\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Arithematic mean is 12.0 V\n", + "Mean deviation = 0.75\n", + "Standard deviation is 1.0\n" + ] + } + ], + "prompt_number": 46 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2.8.3 - page : 2-37" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#ARITHEMATIC MEAN ,median value ,standard deviation \n", + "import numpy\n", + "q=[29.2,29.5,29.6,30.0,30.5,31.4,31.7,32.4,33.0,33.3,39.4,28.9] #\n", + "AM= numpy.mean(q)#arithematic mean in mm\n", + "print \"Arithematic mean is \",round(AM,2)\n", + "mv=sorted(q)[int(len(q)/2-1)]\n", + "print \"Median value = \",mv\n", + "d=[x-AM for x in q]\n", + "d2=[x**2 for x in d]\n", + "sigma=(sum(d2)/(len(q)-1))**(1.0/2)\n", + "print \"Standard Deviation = \",round(sigma,3)\n", + "\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Arithematic mean is 31.57\n", + "Median value = 30.5\n", + "Standard Deviation = 2.886\n" + ] + } + ], + "prompt_number": 97 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2.8.4 - page:2-39" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Unknown resistor \n", + "#given data :\n", + "S=1000.0 # ohm/V\n", + "V=100.0 #in V\n", + "I=5*10**-3 # in A\n", + "# part (i)\n", + "R_app=(V/I)*10**-3 \n", + "print \"(i) Apparent Resistor, R_app = \",R_app, \" kohm\"\n", + "# part (ii)\n", + "V1=150 #in V\n", + "Rv=S*V1*10**-3 \n", + "Rx=Rv/6.5 #actual resistance in kohm\n", + "print \"(ii) Actual resistance is \",round(Rx,2),\" kohm.\"\n", + "# part(iii)\n", + "per=(Rx-R_app)/Rx*100 # in %\n", + "print \"(iii) Percentage error due to loading effect of voltmeter is \",round(per,1), \" %\" \n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "(i) Apparent Resistor, R_app = 20.0 kohm\n", + "(ii) Actual resistance is 23.08 kohm.\n", + "(iii) Percentage error due to loading effect of voltmeter is 13.3 %\n" + ] + } + ], + "prompt_number": 103 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2.8.5 - page : 2-40" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# limiting error\n", + "#given data :\n", + "del_A=2.5 # may be +ve or-ve in %\n", + "As=400.0 \n", + "FSD=600.0 # in V\n", + "del_A1=(del_A/100)*FSD \n", + "e=(del_A1/As)*100 # in %\n", + "print \"Limiting error, e = \",e, \" %\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Limiting error, e = 3.75 %\n" + ] + } + ], + "prompt_number": 104 + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/sample_notebooks/NIKHILESH DAMLE/ANTENNAS_AND_WAVE_PROPAGATION_BY_U.A_BAKSHI,_A.V_BAKSHI,_K.A_BAKSHI_10.ipynb b/sample_notebooks/NIKHILESH DAMLE/ANTENNAS_AND_WAVE_PROPAGATION_BY_U.A_BAKSHI,_A.V_BAKSHI,_K.A_BAKSHI_10.ipynb new file mode 100755 index 00000000..e025dd86 --- /dev/null +++ b/sample_notebooks/NIKHILESH DAMLE/ANTENNAS_AND_WAVE_PROPAGATION_BY_U.A_BAKSHI,_A.V_BAKSHI,_K.A_BAKSHI_10.ipynb @@ -0,0 +1,462 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:02197244985b7dfa2823dacf9fac72c0a7536b50c383ffa1801ccf3066222ec3" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Radiation" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.7.1, PAGE NO.-30" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import math\n", + "\n", + "# Variable declaration\n", + "\n", + "I_m = 15 #Current in Ampere\n", + "P_rad = 6 #Power radiated in kW\n", + "\n", + "#Calculation\n", + "\n", + "# By formula\n", + "I_rms = I_m/math.sqrt(2) #I_rms is r.m.s. current\n", + "\n", + "R_rad = P_rad/(I_rms**2) #R_rad is radiation resistance\n", + "\n", + "#Result\n", + "\n", + "print \"The radiation resistance of Antenna is\",round(R_rad*1000,2),\"kW\"\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The radiation resistance of Antenna is 53.33 kW\n" + ] + } + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.9.1,PAGE NO.-42" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from __future__ import division\n", + "from math import sin,cos,pi,sqrt \n", + "from sympy import Symbol\n", + "\n", + "# Variable Declaration\n", + "\n", + "Lm = Symbol('Lm') # Taking lamda as Lm\n", + "dL = Lm/25 # Length of dipole for Hertian dipole\n", + "H_phi = 5 # Magnetic field strength in uA/m\n", + "theta = pi/2\n", + "r = 2 # Distance in Km\n", + "dL_1 = Lm/2 # Length of dipole for Half wave dipole \n", + "R_rad = 73 # Radiation resistance for half wave dipole in ohm\n", + "R_rad1 = 36.5 # Radiation resistance for quarter wave monopole in ohm\n", + "dL_2 = Lm/4 # Length of dipole for quarter wave monopole \n", + "\n", + "# Calculation\n", + "\n", + "# By formula : H_phi = I_m*dL*sin(theta)/2*Lm*r\n", + "I_m = (H_phi*2*Lm*r)/(dL*sin(theta))\n", + "I_rms = I_m/sqrt(2)\n", + "P_rad = 80*(pi**2)*((dL/Lm)**2)*(I_rms**2)\n", + "I_m1 = (H_phi*2*pi*r*sin(theta))/(cos(pi/2*cos(theta)))\n", + "I_rms1 = I_m1/sqrt(2)\n", + "P_rad1 = (I_rms1**2)*R_rad\n", + "P_rad2 = (I_rms1**2)*R_rad1\n", + "\n", + "# Result\n", + "\n", + "print \" The power radiated by hertzian dipole is \",round(P_rad*10**-3,2),\"mW\"\n", + "print \" The power radiated by half wave dipole is \",round(P_rad1*10**-3,2),\"mW\"\n", + "print \" The power radiated by Quarter wave monopole is \",round(P_rad2*10**-3,2),\"mW\"\n" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.9.2,PAGE NO.-43" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from __future__ import division\n", + "from math import sin,cos,pi,sqrt\n", + "\n", + "\n", + "\n", + "#variable declaration \n", + "\n", + "\n", + "E_theta = 10*(10**(-6)) #Electric field strength in V/m\n", + "theta = pi/2 #observation angle\n", + "r = 500*(10**3) #distance in metrs\n", + "f = 50*(10**6) #frequency in Hertz\n", + "c = 3*(10**8) #speed of light in m/sec\n", + "R_rad = 73 #for half wave dipole Radiation resistance in ohms\n", + "\n", + "\n", + "# calculation\n", + "lamda = c/f\n", + "L = lamda/2 #L is the length of half wave dipole\n", + "# formula : E=((60*I_m)/r)*((cos(pi/2*cos(theta)))*sin(theta))\n", + "I_m = (E_theta*r*sin(theta))/(60*cos(pi/2*cos(theta)))\n", + "I_rms = I_m/sqrt(2)\n", + "P_avg = (R_rad*(I_m**2))/2\n", + "\n", + "#Result\n", + "\n", + "print \"Length of Dipole is\" ,round(L,2),\"metres\"\n", + "print \"Current fed to Antenna\",round(I_rms*1000,2),\"mA\"\n", + "print \"Average Power\",round(P_avg*1000,2),\"mW\"\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.9.3,PAGE NO.-44" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from __future__ import division\n", + "from math import pi,sqrt\n", + "\n", + "# variable declaration\n", + "\n", + "l_eff = 100 #effective hieght in m\n", + "f = 60*(10**3) #frequency in Hertz\n", + "r = 100*(10**3) #Distance in m\n", + "c = 3*(10**8) #Speed of light in m/sec\n", + "P_rad = 100*(10**3) #radiated power\n", + "\n", + "# calculation\n", + "\n", + "lamda = c/f\n", + "R_rad = 160*(pi**2)*((l_eff/lamda)**2)\n", + "I_rms = sqrt(P_rad/R_rad)\n", + "E_rms = (120*pi*l_eff*(I_rms**2))/(lamda*r)\n", + "\n", + "# Results\n", + "\n", + "print \"Strength of Electric field\",round(E_rms,2),\"V/m\"\n" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.9.4,PAGE NO.-45" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from __future__ import division\n", + "from math import pi\n", + "\n", + "# variable declaration\n", + "\n", + "l_eff = 113.3 #Effective length in metres\n", + "lamda = 18.8 #Wavelength in metres\n", + "I_rms = 725 #Base current in Ampere\n", + "r = 175 #Distance in metre\n", + "Eta_o = 120*pi\n", + "\n", + "#Calculation\n", + "\n", + "E = (120*pi*l_eff*I_rms)/(lamda*r)\n", + "H = E/Eta_o\n", + "R_rad = 160*(pi**2)*((l_eff/lamda)**2)\n", + "P_rad = (I_rms**2)*(R_rad)\n", + "\n", + "#Result\n", + "\n", + "print \"The electric field at a distance r is \",round(E*0.001,2),\"mV/m\"\n", + "print \"The H field is\",round(H,2),\"uA/m\"\n", + "print \"The power radiated by Antenna is \",round(P_rad*(10**(-9)),2),\"kW\"\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.9.5,PAGE NO.-46" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from __future__ import division\n", + "from math import pi,sin\n", + "\n", + "# variable declaration\n", + "\n", + "lamda = 10*(10**(-2)) # In cm\n", + "r = 200*(10**(-2)) # In cm\n", + "theta = 90 # In Degrees\n", + "phi = 0 # In Degrees\n", + "IdL = 3*(10**(-4)) # current distribution in Am\n", + "\n", + "#Calculation\n", + "\n", + "E_theta = (60*pi*IdL*sin(theta))/(lamda*r)\n", + "\n", + "#Result\n", + "\n", + "print \"The magnitude of component E_theta is \",round(E_theta,2),\"V/m\"\n", + "\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.9.6,PAGE NO.-46" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from __future__ import division\n", + "from math import pi\n", + "\n", + "# variable declaration\n", + "\n", + "dL = lamda/12\n", + "\n", + "\n", + "#Calculation\n", + "\n", + "R_rad = 80*(pi**2)*((dL/lamda)**2)\n", + "\n", + "#Result\n", + "\n", + "print \"The Radiation resistance of dipole antenna is \",round(R_rad,2),\"ohm\"\n", + "\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.9.7,PAGE NO.-46" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from __future__ import division\n", + "from math import pi,sqrt\n", + "from sympy import Symbol\n", + "# variable declaration\n", + "\n", + "I_m = 100 # uniform current in ampere\n", + "Lm = Symbol('Lm') #Taking Lm as lamda\n", + "dL = Lm/16\n", + "\n", + "\n", + "#Calculation\n", + "\n", + "P_rad = 80*(pi**2)*((dL/Lm)**2)*((I_m/sqrt(2))**2)\n", + "R_rad = 80*(pi**2)*((dL/Lm)**2)\n", + "\n", + "#Result\n", + "\n", + "print \"The Radiation resistance of dipole is \",round(R_rad,2),\"ohm\"\n", + "\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.9.8,PAGE NO.-47" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# variable declaration\n", + "\n", + "f = 30*(10**6) #Frequency in Hz\n", + "c = 3*(10**8) #speed of light in m/s\n", + "\n", + "#Calculation\n", + "\n", + "lamda = c/f\n", + "\n", + "#Result\n", + "\n", + "print \"The Length of Half wave dipole is \",round((lamda/2),2),\"m\"\n", + "\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.9.10,PAGE NO.-48" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from __future__ import division\n", + "from math import pi,sin,sqrt \n", + "from sympy import Symbol\n", + "# variable declaration\n", + "\n", + "Lm = Symbol('Lm') # Taking lamda as Lm\n", + "dL = 0.01*Lm # Length of dipole \n", + "theta = 45\n", + "P_rad = 1 # Power radiated in kW\n", + "phi = 90\n", + "r = 1 # Distance in Km\n", + "Eta_o=120*pi\n", + "\n", + "\n", + "\n", + "#Calculation\n", + "\n", + "R_rad = 20*(pi**2)*((dL/Lm)**2)\n", + "I_m = sqrt(2*P_rad*R_rad)\n", + "\n", + "# formula : P = (Eta_o/2)*(((Omega*I_m*dL*sin(theta))/(4*pi*r*v))**2)\n", + "P = (Eta_o/2)*(((I_m*dL*sin(theta))/(4*pi*(r**2)))**2)\n", + "\n", + "#Result\n", + "\n", + "print \"The Power density is \",P,\"Watt/m^2\"\n", + "\n", + "# Note : The Solving in the book is wrong they put 0.1 instead of 0.1*lamda \n" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.9.11,PAGE NO.-49" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from __future__ import division\n", + "from math import pi \n", + "\n", + "# variable declaration\n", + "\n", + "dL = 75 #Length of dipole in m\n", + "f = 800 # Frequency in kHz\n", + "I_rms = 10 #rms Current in Amp\n", + "c = 3*(10**8) #Speed of light in m/s \n", + "\n", + "#Calculation\n", + "\n", + "lamda = c/f\n", + "P_rad = 80*(pi**2)*((dL/lamda)**2)*(I_rms**2)\n", + "\n", + "#Result\n", + "\n", + "print \"The Total Power radiated by Antenna is \",round(P_rad*1000,2),\"kW\"\n", + "\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/sample_notebooks/NIKHILESH DAMLE/ANTENNAS_AND_WAVE_PROPAGATION_BY_U.A_BAKSHI,_A.V_BAKSHI,_K.A_BAKSHI_11.ipynb b/sample_notebooks/NIKHILESH DAMLE/ANTENNAS_AND_WAVE_PROPAGATION_BY_U.A_BAKSHI,_A.V_BAKSHI,_K.A_BAKSHI_11.ipynb new file mode 100755 index 00000000..e025dd86 --- /dev/null +++ b/sample_notebooks/NIKHILESH DAMLE/ANTENNAS_AND_WAVE_PROPAGATION_BY_U.A_BAKSHI,_A.V_BAKSHI,_K.A_BAKSHI_11.ipynb @@ -0,0 +1,462 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:02197244985b7dfa2823dacf9fac72c0a7536b50c383ffa1801ccf3066222ec3" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Radiation" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.7.1, PAGE NO.-30" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import math\n", + "\n", + "# Variable declaration\n", + "\n", + "I_m = 15 #Current in Ampere\n", + "P_rad = 6 #Power radiated in kW\n", + "\n", + "#Calculation\n", + "\n", + "# By formula\n", + "I_rms = I_m/math.sqrt(2) #I_rms is r.m.s. current\n", + "\n", + "R_rad = P_rad/(I_rms**2) #R_rad is radiation resistance\n", + "\n", + "#Result\n", + "\n", + "print \"The radiation resistance of Antenna is\",round(R_rad*1000,2),\"kW\"\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The radiation resistance of Antenna is 53.33 kW\n" + ] + } + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.9.1,PAGE NO.-42" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from __future__ import division\n", + "from math import sin,cos,pi,sqrt \n", + "from sympy import Symbol\n", + "\n", + "# Variable Declaration\n", + "\n", + "Lm = Symbol('Lm') # Taking lamda as Lm\n", + "dL = Lm/25 # Length of dipole for Hertian dipole\n", + "H_phi = 5 # Magnetic field strength in uA/m\n", + "theta = pi/2\n", + "r = 2 # Distance in Km\n", + "dL_1 = Lm/2 # Length of dipole for Half wave dipole \n", + "R_rad = 73 # Radiation resistance for half wave dipole in ohm\n", + "R_rad1 = 36.5 # Radiation resistance for quarter wave monopole in ohm\n", + "dL_2 = Lm/4 # Length of dipole for quarter wave monopole \n", + "\n", + "# Calculation\n", + "\n", + "# By formula : H_phi = I_m*dL*sin(theta)/2*Lm*r\n", + "I_m = (H_phi*2*Lm*r)/(dL*sin(theta))\n", + "I_rms = I_m/sqrt(2)\n", + "P_rad = 80*(pi**2)*((dL/Lm)**2)*(I_rms**2)\n", + "I_m1 = (H_phi*2*pi*r*sin(theta))/(cos(pi/2*cos(theta)))\n", + "I_rms1 = I_m1/sqrt(2)\n", + "P_rad1 = (I_rms1**2)*R_rad\n", + "P_rad2 = (I_rms1**2)*R_rad1\n", + "\n", + "# Result\n", + "\n", + "print \" The power radiated by hertzian dipole is \",round(P_rad*10**-3,2),\"mW\"\n", + "print \" The power radiated by half wave dipole is \",round(P_rad1*10**-3,2),\"mW\"\n", + "print \" The power radiated by Quarter wave monopole is \",round(P_rad2*10**-3,2),\"mW\"\n" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.9.2,PAGE NO.-43" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from __future__ import division\n", + "from math import sin,cos,pi,sqrt\n", + "\n", + "\n", + "\n", + "#variable declaration \n", + "\n", + "\n", + "E_theta = 10*(10**(-6)) #Electric field strength in V/m\n", + "theta = pi/2 #observation angle\n", + "r = 500*(10**3) #distance in metrs\n", + "f = 50*(10**6) #frequency in Hertz\n", + "c = 3*(10**8) #speed of light in m/sec\n", + "R_rad = 73 #for half wave dipole Radiation resistance in ohms\n", + "\n", + "\n", + "# calculation\n", + "lamda = c/f\n", + "L = lamda/2 #L is the length of half wave dipole\n", + "# formula : E=((60*I_m)/r)*((cos(pi/2*cos(theta)))*sin(theta))\n", + "I_m = (E_theta*r*sin(theta))/(60*cos(pi/2*cos(theta)))\n", + "I_rms = I_m/sqrt(2)\n", + "P_avg = (R_rad*(I_m**2))/2\n", + "\n", + "#Result\n", + "\n", + "print \"Length of Dipole is\" ,round(L,2),\"metres\"\n", + "print \"Current fed to Antenna\",round(I_rms*1000,2),\"mA\"\n", + "print \"Average Power\",round(P_avg*1000,2),\"mW\"\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.9.3,PAGE NO.-44" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from __future__ import division\n", + "from math import pi,sqrt\n", + "\n", + "# variable declaration\n", + "\n", + "l_eff = 100 #effective hieght in m\n", + "f = 60*(10**3) #frequency in Hertz\n", + "r = 100*(10**3) #Distance in m\n", + "c = 3*(10**8) #Speed of light in m/sec\n", + "P_rad = 100*(10**3) #radiated power\n", + "\n", + "# calculation\n", + "\n", + "lamda = c/f\n", + "R_rad = 160*(pi**2)*((l_eff/lamda)**2)\n", + "I_rms = sqrt(P_rad/R_rad)\n", + "E_rms = (120*pi*l_eff*(I_rms**2))/(lamda*r)\n", + "\n", + "# Results\n", + "\n", + "print \"Strength of Electric field\",round(E_rms,2),\"V/m\"\n" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.9.4,PAGE NO.-45" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from __future__ import division\n", + "from math import pi\n", + "\n", + "# variable declaration\n", + "\n", + "l_eff = 113.3 #Effective length in metres\n", + "lamda = 18.8 #Wavelength in metres\n", + "I_rms = 725 #Base current in Ampere\n", + "r = 175 #Distance in metre\n", + "Eta_o = 120*pi\n", + "\n", + "#Calculation\n", + "\n", + "E = (120*pi*l_eff*I_rms)/(lamda*r)\n", + "H = E/Eta_o\n", + "R_rad = 160*(pi**2)*((l_eff/lamda)**2)\n", + "P_rad = (I_rms**2)*(R_rad)\n", + "\n", + "#Result\n", + "\n", + "print \"The electric field at a distance r is \",round(E*0.001,2),\"mV/m\"\n", + "print \"The H field is\",round(H,2),\"uA/m\"\n", + "print \"The power radiated by Antenna is \",round(P_rad*(10**(-9)),2),\"kW\"\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.9.5,PAGE NO.-46" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from __future__ import division\n", + "from math import pi,sin\n", + "\n", + "# variable declaration\n", + "\n", + "lamda = 10*(10**(-2)) # In cm\n", + "r = 200*(10**(-2)) # In cm\n", + "theta = 90 # In Degrees\n", + "phi = 0 # In Degrees\n", + "IdL = 3*(10**(-4)) # current distribution in Am\n", + "\n", + "#Calculation\n", + "\n", + "E_theta = (60*pi*IdL*sin(theta))/(lamda*r)\n", + "\n", + "#Result\n", + "\n", + "print \"The magnitude of component E_theta is \",round(E_theta,2),\"V/m\"\n", + "\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.9.6,PAGE NO.-46" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from __future__ import division\n", + "from math import pi\n", + "\n", + "# variable declaration\n", + "\n", + "dL = lamda/12\n", + "\n", + "\n", + "#Calculation\n", + "\n", + "R_rad = 80*(pi**2)*((dL/lamda)**2)\n", + "\n", + "#Result\n", + "\n", + "print \"The Radiation resistance of dipole antenna is \",round(R_rad,2),\"ohm\"\n", + "\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.9.7,PAGE NO.-46" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from __future__ import division\n", + "from math import pi,sqrt\n", + "from sympy import Symbol\n", + "# variable declaration\n", + "\n", + "I_m = 100 # uniform current in ampere\n", + "Lm = Symbol('Lm') #Taking Lm as lamda\n", + "dL = Lm/16\n", + "\n", + "\n", + "#Calculation\n", + "\n", + "P_rad = 80*(pi**2)*((dL/Lm)**2)*((I_m/sqrt(2))**2)\n", + "R_rad = 80*(pi**2)*((dL/Lm)**2)\n", + "\n", + "#Result\n", + "\n", + "print \"The Radiation resistance of dipole is \",round(R_rad,2),\"ohm\"\n", + "\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.9.8,PAGE NO.-47" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# variable declaration\n", + "\n", + "f = 30*(10**6) #Frequency in Hz\n", + "c = 3*(10**8) #speed of light in m/s\n", + "\n", + "#Calculation\n", + "\n", + "lamda = c/f\n", + "\n", + "#Result\n", + "\n", + "print \"The Length of Half wave dipole is \",round((lamda/2),2),\"m\"\n", + "\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.9.10,PAGE NO.-48" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from __future__ import division\n", + "from math import pi,sin,sqrt \n", + "from sympy import Symbol\n", + "# variable declaration\n", + "\n", + "Lm = Symbol('Lm') # Taking lamda as Lm\n", + "dL = 0.01*Lm # Length of dipole \n", + "theta = 45\n", + "P_rad = 1 # Power radiated in kW\n", + "phi = 90\n", + "r = 1 # Distance in Km\n", + "Eta_o=120*pi\n", + "\n", + "\n", + "\n", + "#Calculation\n", + "\n", + "R_rad = 20*(pi**2)*((dL/Lm)**2)\n", + "I_m = sqrt(2*P_rad*R_rad)\n", + "\n", + "# formula : P = (Eta_o/2)*(((Omega*I_m*dL*sin(theta))/(4*pi*r*v))**2)\n", + "P = (Eta_o/2)*(((I_m*dL*sin(theta))/(4*pi*(r**2)))**2)\n", + "\n", + "#Result\n", + "\n", + "print \"The Power density is \",P,\"Watt/m^2\"\n", + "\n", + "# Note : The Solving in the book is wrong they put 0.1 instead of 0.1*lamda \n" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.9.11,PAGE NO.-49" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from __future__ import division\n", + "from math import pi \n", + "\n", + "# variable declaration\n", + "\n", + "dL = 75 #Length of dipole in m\n", + "f = 800 # Frequency in kHz\n", + "I_rms = 10 #rms Current in Amp\n", + "c = 3*(10**8) #Speed of light in m/s \n", + "\n", + "#Calculation\n", + "\n", + "lamda = c/f\n", + "P_rad = 80*(pi**2)*((dL/lamda)**2)*(I_rms**2)\n", + "\n", + "#Result\n", + "\n", + "print \"The Total Power radiated by Antenna is \",round(P_rad*1000,2),\"kW\"\n", + "\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/sample_notebooks/NIKHILESH DAMLE/ANTENNAS_AND_WAVE_PROPAGATION_BY_U.A_BAKSHI,_A.V_BAKSHI,_K.A_BAKSHI_12.ipynb b/sample_notebooks/NIKHILESH DAMLE/ANTENNAS_AND_WAVE_PROPAGATION_BY_U.A_BAKSHI,_A.V_BAKSHI,_K.A_BAKSHI_12.ipynb new file mode 100755 index 00000000..e025dd86 --- /dev/null +++ b/sample_notebooks/NIKHILESH DAMLE/ANTENNAS_AND_WAVE_PROPAGATION_BY_U.A_BAKSHI,_A.V_BAKSHI,_K.A_BAKSHI_12.ipynb @@ -0,0 +1,462 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:02197244985b7dfa2823dacf9fac72c0a7536b50c383ffa1801ccf3066222ec3" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Radiation" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.7.1, PAGE NO.-30" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import math\n", + "\n", + "# Variable declaration\n", + "\n", + "I_m = 15 #Current in Ampere\n", + "P_rad = 6 #Power radiated in kW\n", + "\n", + "#Calculation\n", + "\n", + "# By formula\n", + "I_rms = I_m/math.sqrt(2) #I_rms is r.m.s. current\n", + "\n", + "R_rad = P_rad/(I_rms**2) #R_rad is radiation resistance\n", + "\n", + "#Result\n", + "\n", + "print \"The radiation resistance of Antenna is\",round(R_rad*1000,2),\"kW\"\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The radiation resistance of Antenna is 53.33 kW\n" + ] + } + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.9.1,PAGE NO.-42" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from __future__ import division\n", + "from math import sin,cos,pi,sqrt \n", + "from sympy import Symbol\n", + "\n", + "# Variable Declaration\n", + "\n", + "Lm = Symbol('Lm') # Taking lamda as Lm\n", + "dL = Lm/25 # Length of dipole for Hertian dipole\n", + "H_phi = 5 # Magnetic field strength in uA/m\n", + "theta = pi/2\n", + "r = 2 # Distance in Km\n", + "dL_1 = Lm/2 # Length of dipole for Half wave dipole \n", + "R_rad = 73 # Radiation resistance for half wave dipole in ohm\n", + "R_rad1 = 36.5 # Radiation resistance for quarter wave monopole in ohm\n", + "dL_2 = Lm/4 # Length of dipole for quarter wave monopole \n", + "\n", + "# Calculation\n", + "\n", + "# By formula : H_phi = I_m*dL*sin(theta)/2*Lm*r\n", + "I_m = (H_phi*2*Lm*r)/(dL*sin(theta))\n", + "I_rms = I_m/sqrt(2)\n", + "P_rad = 80*(pi**2)*((dL/Lm)**2)*(I_rms**2)\n", + "I_m1 = (H_phi*2*pi*r*sin(theta))/(cos(pi/2*cos(theta)))\n", + "I_rms1 = I_m1/sqrt(2)\n", + "P_rad1 = (I_rms1**2)*R_rad\n", + "P_rad2 = (I_rms1**2)*R_rad1\n", + "\n", + "# Result\n", + "\n", + "print \" The power radiated by hertzian dipole is \",round(P_rad*10**-3,2),\"mW\"\n", + "print \" The power radiated by half wave dipole is \",round(P_rad1*10**-3,2),\"mW\"\n", + "print \" The power radiated by Quarter wave monopole is \",round(P_rad2*10**-3,2),\"mW\"\n" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.9.2,PAGE NO.-43" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from __future__ import division\n", + "from math import sin,cos,pi,sqrt\n", + "\n", + "\n", + "\n", + "#variable declaration \n", + "\n", + "\n", + "E_theta = 10*(10**(-6)) #Electric field strength in V/m\n", + "theta = pi/2 #observation angle\n", + "r = 500*(10**3) #distance in metrs\n", + "f = 50*(10**6) #frequency in Hertz\n", + "c = 3*(10**8) #speed of light in m/sec\n", + "R_rad = 73 #for half wave dipole Radiation resistance in ohms\n", + "\n", + "\n", + "# calculation\n", + "lamda = c/f\n", + "L = lamda/2 #L is the length of half wave dipole\n", + "# formula : E=((60*I_m)/r)*((cos(pi/2*cos(theta)))*sin(theta))\n", + "I_m = (E_theta*r*sin(theta))/(60*cos(pi/2*cos(theta)))\n", + "I_rms = I_m/sqrt(2)\n", + "P_avg = (R_rad*(I_m**2))/2\n", + "\n", + "#Result\n", + "\n", + "print \"Length of Dipole is\" ,round(L,2),\"metres\"\n", + "print \"Current fed to Antenna\",round(I_rms*1000,2),\"mA\"\n", + "print \"Average Power\",round(P_avg*1000,2),\"mW\"\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.9.3,PAGE NO.-44" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from __future__ import division\n", + "from math import pi,sqrt\n", + "\n", + "# variable declaration\n", + "\n", + "l_eff = 100 #effective hieght in m\n", + "f = 60*(10**3) #frequency in Hertz\n", + "r = 100*(10**3) #Distance in m\n", + "c = 3*(10**8) #Speed of light in m/sec\n", + "P_rad = 100*(10**3) #radiated power\n", + "\n", + "# calculation\n", + "\n", + "lamda = c/f\n", + "R_rad = 160*(pi**2)*((l_eff/lamda)**2)\n", + "I_rms = sqrt(P_rad/R_rad)\n", + "E_rms = (120*pi*l_eff*(I_rms**2))/(lamda*r)\n", + "\n", + "# Results\n", + "\n", + "print \"Strength of Electric field\",round(E_rms,2),\"V/m\"\n" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.9.4,PAGE NO.-45" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from __future__ import division\n", + "from math import pi\n", + "\n", + "# variable declaration\n", + "\n", + "l_eff = 113.3 #Effective length in metres\n", + "lamda = 18.8 #Wavelength in metres\n", + "I_rms = 725 #Base current in Ampere\n", + "r = 175 #Distance in metre\n", + "Eta_o = 120*pi\n", + "\n", + "#Calculation\n", + "\n", + "E = (120*pi*l_eff*I_rms)/(lamda*r)\n", + "H = E/Eta_o\n", + "R_rad = 160*(pi**2)*((l_eff/lamda)**2)\n", + "P_rad = (I_rms**2)*(R_rad)\n", + "\n", + "#Result\n", + "\n", + "print \"The electric field at a distance r is \",round(E*0.001,2),\"mV/m\"\n", + "print \"The H field is\",round(H,2),\"uA/m\"\n", + "print \"The power radiated by Antenna is \",round(P_rad*(10**(-9)),2),\"kW\"\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.9.5,PAGE NO.-46" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from __future__ import division\n", + "from math import pi,sin\n", + "\n", + "# variable declaration\n", + "\n", + "lamda = 10*(10**(-2)) # In cm\n", + "r = 200*(10**(-2)) # In cm\n", + "theta = 90 # In Degrees\n", + "phi = 0 # In Degrees\n", + "IdL = 3*(10**(-4)) # current distribution in Am\n", + "\n", + "#Calculation\n", + "\n", + "E_theta = (60*pi*IdL*sin(theta))/(lamda*r)\n", + "\n", + "#Result\n", + "\n", + "print \"The magnitude of component E_theta is \",round(E_theta,2),\"V/m\"\n", + "\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.9.6,PAGE NO.-46" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from __future__ import division\n", + "from math import pi\n", + "\n", + "# variable declaration\n", + "\n", + "dL = lamda/12\n", + "\n", + "\n", + "#Calculation\n", + "\n", + "R_rad = 80*(pi**2)*((dL/lamda)**2)\n", + "\n", + "#Result\n", + "\n", + "print \"The Radiation resistance of dipole antenna is \",round(R_rad,2),\"ohm\"\n", + "\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.9.7,PAGE NO.-46" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from __future__ import division\n", + "from math import pi,sqrt\n", + "from sympy import Symbol\n", + "# variable declaration\n", + "\n", + "I_m = 100 # uniform current in ampere\n", + "Lm = Symbol('Lm') #Taking Lm as lamda\n", + "dL = Lm/16\n", + "\n", + "\n", + "#Calculation\n", + "\n", + "P_rad = 80*(pi**2)*((dL/Lm)**2)*((I_m/sqrt(2))**2)\n", + "R_rad = 80*(pi**2)*((dL/Lm)**2)\n", + "\n", + "#Result\n", + "\n", + "print \"The Radiation resistance of dipole is \",round(R_rad,2),\"ohm\"\n", + "\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.9.8,PAGE NO.-47" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# variable declaration\n", + "\n", + "f = 30*(10**6) #Frequency in Hz\n", + "c = 3*(10**8) #speed of light in m/s\n", + "\n", + "#Calculation\n", + "\n", + "lamda = c/f\n", + "\n", + "#Result\n", + "\n", + "print \"The Length of Half wave dipole is \",round((lamda/2),2),\"m\"\n", + "\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.9.10,PAGE NO.-48" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from __future__ import division\n", + "from math import pi,sin,sqrt \n", + "from sympy import Symbol\n", + "# variable declaration\n", + "\n", + "Lm = Symbol('Lm') # Taking lamda as Lm\n", + "dL = 0.01*Lm # Length of dipole \n", + "theta = 45\n", + "P_rad = 1 # Power radiated in kW\n", + "phi = 90\n", + "r = 1 # Distance in Km\n", + "Eta_o=120*pi\n", + "\n", + "\n", + "\n", + "#Calculation\n", + "\n", + "R_rad = 20*(pi**2)*((dL/Lm)**2)\n", + "I_m = sqrt(2*P_rad*R_rad)\n", + "\n", + "# formula : P = (Eta_o/2)*(((Omega*I_m*dL*sin(theta))/(4*pi*r*v))**2)\n", + "P = (Eta_o/2)*(((I_m*dL*sin(theta))/(4*pi*(r**2)))**2)\n", + "\n", + "#Result\n", + "\n", + "print \"The Power density is \",P,\"Watt/m^2\"\n", + "\n", + "# Note : The Solving in the book is wrong they put 0.1 instead of 0.1*lamda \n" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.9.11,PAGE NO.-49" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from __future__ import division\n", + "from math import pi \n", + "\n", + "# variable declaration\n", + "\n", + "dL = 75 #Length of dipole in m\n", + "f = 800 # Frequency in kHz\n", + "I_rms = 10 #rms Current in Amp\n", + "c = 3*(10**8) #Speed of light in m/s \n", + "\n", + "#Calculation\n", + "\n", + "lamda = c/f\n", + "P_rad = 80*(pi**2)*((dL/lamda)**2)*(I_rms**2)\n", + "\n", + "#Result\n", + "\n", + "print \"The Total Power radiated by Antenna is \",round(P_rad*1000,2),\"kW\"\n", + "\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/sample_notebooks/NIKHILESH DAMLE/ANTENNAS_AND_WAVE_PROPAGATION_BY_U.A_BAKSHI,_A.V_BAKSHI,_K.A_BAKSHI_5.ipynb b/sample_notebooks/NIKHILESH DAMLE/ANTENNAS_AND_WAVE_PROPAGATION_BY_U.A_BAKSHI,_A.V_BAKSHI,_K.A_BAKSHI_5.ipynb new file mode 100755 index 00000000..e025dd86 --- /dev/null +++ b/sample_notebooks/NIKHILESH DAMLE/ANTENNAS_AND_WAVE_PROPAGATION_BY_U.A_BAKSHI,_A.V_BAKSHI,_K.A_BAKSHI_5.ipynb @@ -0,0 +1,462 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:02197244985b7dfa2823dacf9fac72c0a7536b50c383ffa1801ccf3066222ec3" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Radiation" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.7.1, PAGE NO.-30" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import math\n", + "\n", + "# Variable declaration\n", + "\n", + "I_m = 15 #Current in Ampere\n", + "P_rad = 6 #Power radiated in kW\n", + "\n", + "#Calculation\n", + "\n", + "# By formula\n", + "I_rms = I_m/math.sqrt(2) #I_rms is r.m.s. current\n", + "\n", + "R_rad = P_rad/(I_rms**2) #R_rad is radiation resistance\n", + "\n", + "#Result\n", + "\n", + "print \"The radiation resistance of Antenna is\",round(R_rad*1000,2),\"kW\"\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The radiation resistance of Antenna is 53.33 kW\n" + ] + } + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.9.1,PAGE NO.-42" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from __future__ import division\n", + "from math import sin,cos,pi,sqrt \n", + "from sympy import Symbol\n", + "\n", + "# Variable Declaration\n", + "\n", + "Lm = Symbol('Lm') # Taking lamda as Lm\n", + "dL = Lm/25 # Length of dipole for Hertian dipole\n", + "H_phi = 5 # Magnetic field strength in uA/m\n", + "theta = pi/2\n", + "r = 2 # Distance in Km\n", + "dL_1 = Lm/2 # Length of dipole for Half wave dipole \n", + "R_rad = 73 # Radiation resistance for half wave dipole in ohm\n", + "R_rad1 = 36.5 # Radiation resistance for quarter wave monopole in ohm\n", + "dL_2 = Lm/4 # Length of dipole for quarter wave monopole \n", + "\n", + "# Calculation\n", + "\n", + "# By formula : H_phi = I_m*dL*sin(theta)/2*Lm*r\n", + "I_m = (H_phi*2*Lm*r)/(dL*sin(theta))\n", + "I_rms = I_m/sqrt(2)\n", + "P_rad = 80*(pi**2)*((dL/Lm)**2)*(I_rms**2)\n", + "I_m1 = (H_phi*2*pi*r*sin(theta))/(cos(pi/2*cos(theta)))\n", + "I_rms1 = I_m1/sqrt(2)\n", + "P_rad1 = (I_rms1**2)*R_rad\n", + "P_rad2 = (I_rms1**2)*R_rad1\n", + "\n", + "# Result\n", + "\n", + "print \" The power radiated by hertzian dipole is \",round(P_rad*10**-3,2),\"mW\"\n", + "print \" The power radiated by half wave dipole is \",round(P_rad1*10**-3,2),\"mW\"\n", + "print \" The power radiated by Quarter wave monopole is \",round(P_rad2*10**-3,2),\"mW\"\n" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.9.2,PAGE NO.-43" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from __future__ import division\n", + "from math import sin,cos,pi,sqrt\n", + "\n", + "\n", + "\n", + "#variable declaration \n", + "\n", + "\n", + "E_theta = 10*(10**(-6)) #Electric field strength in V/m\n", + "theta = pi/2 #observation angle\n", + "r = 500*(10**3) #distance in metrs\n", + "f = 50*(10**6) #frequency in Hertz\n", + "c = 3*(10**8) #speed of light in m/sec\n", + "R_rad = 73 #for half wave dipole Radiation resistance in ohms\n", + "\n", + "\n", + "# calculation\n", + "lamda = c/f\n", + "L = lamda/2 #L is the length of half wave dipole\n", + "# formula : E=((60*I_m)/r)*((cos(pi/2*cos(theta)))*sin(theta))\n", + "I_m = (E_theta*r*sin(theta))/(60*cos(pi/2*cos(theta)))\n", + "I_rms = I_m/sqrt(2)\n", + "P_avg = (R_rad*(I_m**2))/2\n", + "\n", + "#Result\n", + "\n", + "print \"Length of Dipole is\" ,round(L,2),\"metres\"\n", + "print \"Current fed to Antenna\",round(I_rms*1000,2),\"mA\"\n", + "print \"Average Power\",round(P_avg*1000,2),\"mW\"\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.9.3,PAGE NO.-44" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from __future__ import division\n", + "from math import pi,sqrt\n", + "\n", + "# variable declaration\n", + "\n", + "l_eff = 100 #effective hieght in m\n", + "f = 60*(10**3) #frequency in Hertz\n", + "r = 100*(10**3) #Distance in m\n", + "c = 3*(10**8) #Speed of light in m/sec\n", + "P_rad = 100*(10**3) #radiated power\n", + "\n", + "# calculation\n", + "\n", + "lamda = c/f\n", + "R_rad = 160*(pi**2)*((l_eff/lamda)**2)\n", + "I_rms = sqrt(P_rad/R_rad)\n", + "E_rms = (120*pi*l_eff*(I_rms**2))/(lamda*r)\n", + "\n", + "# Results\n", + "\n", + "print \"Strength of Electric field\",round(E_rms,2),\"V/m\"\n" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.9.4,PAGE NO.-45" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from __future__ import division\n", + "from math import pi\n", + "\n", + "# variable declaration\n", + "\n", + "l_eff = 113.3 #Effective length in metres\n", + "lamda = 18.8 #Wavelength in metres\n", + "I_rms = 725 #Base current in Ampere\n", + "r = 175 #Distance in metre\n", + "Eta_o = 120*pi\n", + "\n", + "#Calculation\n", + "\n", + "E = (120*pi*l_eff*I_rms)/(lamda*r)\n", + "H = E/Eta_o\n", + "R_rad = 160*(pi**2)*((l_eff/lamda)**2)\n", + "P_rad = (I_rms**2)*(R_rad)\n", + "\n", + "#Result\n", + "\n", + "print \"The electric field at a distance r is \",round(E*0.001,2),\"mV/m\"\n", + "print \"The H field is\",round(H,2),\"uA/m\"\n", + "print \"The power radiated by Antenna is \",round(P_rad*(10**(-9)),2),\"kW\"\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.9.5,PAGE NO.-46" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from __future__ import division\n", + "from math import pi,sin\n", + "\n", + "# variable declaration\n", + "\n", + "lamda = 10*(10**(-2)) # In cm\n", + "r = 200*(10**(-2)) # In cm\n", + "theta = 90 # In Degrees\n", + "phi = 0 # In Degrees\n", + "IdL = 3*(10**(-4)) # current distribution in Am\n", + "\n", + "#Calculation\n", + "\n", + "E_theta = (60*pi*IdL*sin(theta))/(lamda*r)\n", + "\n", + "#Result\n", + "\n", + "print \"The magnitude of component E_theta is \",round(E_theta,2),\"V/m\"\n", + "\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.9.6,PAGE NO.-46" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from __future__ import division\n", + "from math import pi\n", + "\n", + "# variable declaration\n", + "\n", + "dL = lamda/12\n", + "\n", + "\n", + "#Calculation\n", + "\n", + "R_rad = 80*(pi**2)*((dL/lamda)**2)\n", + "\n", + "#Result\n", + "\n", + "print \"The Radiation resistance of dipole antenna is \",round(R_rad,2),\"ohm\"\n", + "\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.9.7,PAGE NO.-46" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from __future__ import division\n", + "from math import pi,sqrt\n", + "from sympy import Symbol\n", + "# variable declaration\n", + "\n", + "I_m = 100 # uniform current in ampere\n", + "Lm = Symbol('Lm') #Taking Lm as lamda\n", + "dL = Lm/16\n", + "\n", + "\n", + "#Calculation\n", + "\n", + "P_rad = 80*(pi**2)*((dL/Lm)**2)*((I_m/sqrt(2))**2)\n", + "R_rad = 80*(pi**2)*((dL/Lm)**2)\n", + "\n", + "#Result\n", + "\n", + "print \"The Radiation resistance of dipole is \",round(R_rad,2),\"ohm\"\n", + "\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.9.8,PAGE NO.-47" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# variable declaration\n", + "\n", + "f = 30*(10**6) #Frequency in Hz\n", + "c = 3*(10**8) #speed of light in m/s\n", + "\n", + "#Calculation\n", + "\n", + "lamda = c/f\n", + "\n", + "#Result\n", + "\n", + "print \"The Length of Half wave dipole is \",round((lamda/2),2),\"m\"\n", + "\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.9.10,PAGE NO.-48" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from __future__ import division\n", + "from math import pi,sin,sqrt \n", + "from sympy import Symbol\n", + "# variable declaration\n", + "\n", + "Lm = Symbol('Lm') # Taking lamda as Lm\n", + "dL = 0.01*Lm # Length of dipole \n", + "theta = 45\n", + "P_rad = 1 # Power radiated in kW\n", + "phi = 90\n", + "r = 1 # Distance in Km\n", + "Eta_o=120*pi\n", + "\n", + "\n", + "\n", + "#Calculation\n", + "\n", + "R_rad = 20*(pi**2)*((dL/Lm)**2)\n", + "I_m = sqrt(2*P_rad*R_rad)\n", + "\n", + "# formula : P = (Eta_o/2)*(((Omega*I_m*dL*sin(theta))/(4*pi*r*v))**2)\n", + "P = (Eta_o/2)*(((I_m*dL*sin(theta))/(4*pi*(r**2)))**2)\n", + "\n", + "#Result\n", + "\n", + "print \"The Power density is \",P,\"Watt/m^2\"\n", + "\n", + "# Note : The Solving in the book is wrong they put 0.1 instead of 0.1*lamda \n" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.9.11,PAGE NO.-49" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from __future__ import division\n", + "from math import pi \n", + "\n", + "# variable declaration\n", + "\n", + "dL = 75 #Length of dipole in m\n", + "f = 800 # Frequency in kHz\n", + "I_rms = 10 #rms Current in Amp\n", + "c = 3*(10**8) #Speed of light in m/s \n", + "\n", + "#Calculation\n", + "\n", + "lamda = c/f\n", + "P_rad = 80*(pi**2)*((dL/lamda)**2)*(I_rms**2)\n", + "\n", + "#Result\n", + "\n", + "print \"The Total Power radiated by Antenna is \",round(P_rad*1000,2),\"kW\"\n", + "\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/sample_notebooks/NIKHILESH DAMLE/ANTENNAS_AND_WAVE_PROPAGATION_BY_U.A_BAKSHI,_A.V_BAKSHI,_K.A_BAKSHI_6.ipynb b/sample_notebooks/NIKHILESH DAMLE/ANTENNAS_AND_WAVE_PROPAGATION_BY_U.A_BAKSHI,_A.V_BAKSHI,_K.A_BAKSHI_6.ipynb new file mode 100755 index 00000000..e025dd86 --- /dev/null +++ b/sample_notebooks/NIKHILESH DAMLE/ANTENNAS_AND_WAVE_PROPAGATION_BY_U.A_BAKSHI,_A.V_BAKSHI,_K.A_BAKSHI_6.ipynb @@ -0,0 +1,462 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:02197244985b7dfa2823dacf9fac72c0a7536b50c383ffa1801ccf3066222ec3" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Radiation" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.7.1, PAGE NO.-30" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import math\n", + "\n", + "# Variable declaration\n", + "\n", + "I_m = 15 #Current in Ampere\n", + "P_rad = 6 #Power radiated in kW\n", + "\n", + "#Calculation\n", + "\n", + "# By formula\n", + "I_rms = I_m/math.sqrt(2) #I_rms is r.m.s. current\n", + "\n", + "R_rad = P_rad/(I_rms**2) #R_rad is radiation resistance\n", + "\n", + "#Result\n", + "\n", + "print \"The radiation resistance of Antenna is\",round(R_rad*1000,2),\"kW\"\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The radiation resistance of Antenna is 53.33 kW\n" + ] + } + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.9.1,PAGE NO.-42" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from __future__ import division\n", + "from math import sin,cos,pi,sqrt \n", + "from sympy import Symbol\n", + "\n", + "# Variable Declaration\n", + "\n", + "Lm = Symbol('Lm') # Taking lamda as Lm\n", + "dL = Lm/25 # Length of dipole for Hertian dipole\n", + "H_phi = 5 # Magnetic field strength in uA/m\n", + "theta = pi/2\n", + "r = 2 # Distance in Km\n", + "dL_1 = Lm/2 # Length of dipole for Half wave dipole \n", + "R_rad = 73 # Radiation resistance for half wave dipole in ohm\n", + "R_rad1 = 36.5 # Radiation resistance for quarter wave monopole in ohm\n", + "dL_2 = Lm/4 # Length of dipole for quarter wave monopole \n", + "\n", + "# Calculation\n", + "\n", + "# By formula : H_phi = I_m*dL*sin(theta)/2*Lm*r\n", + "I_m = (H_phi*2*Lm*r)/(dL*sin(theta))\n", + "I_rms = I_m/sqrt(2)\n", + "P_rad = 80*(pi**2)*((dL/Lm)**2)*(I_rms**2)\n", + "I_m1 = (H_phi*2*pi*r*sin(theta))/(cos(pi/2*cos(theta)))\n", + "I_rms1 = I_m1/sqrt(2)\n", + "P_rad1 = (I_rms1**2)*R_rad\n", + "P_rad2 = (I_rms1**2)*R_rad1\n", + "\n", + "# Result\n", + "\n", + "print \" The power radiated by hertzian dipole is \",round(P_rad*10**-3,2),\"mW\"\n", + "print \" The power radiated by half wave dipole is \",round(P_rad1*10**-3,2),\"mW\"\n", + "print \" The power radiated by Quarter wave monopole is \",round(P_rad2*10**-3,2),\"mW\"\n" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.9.2,PAGE NO.-43" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from __future__ import division\n", + "from math import sin,cos,pi,sqrt\n", + "\n", + "\n", + "\n", + "#variable declaration \n", + "\n", + "\n", + "E_theta = 10*(10**(-6)) #Electric field strength in V/m\n", + "theta = pi/2 #observation angle\n", + "r = 500*(10**3) #distance in metrs\n", + "f = 50*(10**6) #frequency in Hertz\n", + "c = 3*(10**8) #speed of light in m/sec\n", + "R_rad = 73 #for half wave dipole Radiation resistance in ohms\n", + "\n", + "\n", + "# calculation\n", + "lamda = c/f\n", + "L = lamda/2 #L is the length of half wave dipole\n", + "# formula : E=((60*I_m)/r)*((cos(pi/2*cos(theta)))*sin(theta))\n", + "I_m = (E_theta*r*sin(theta))/(60*cos(pi/2*cos(theta)))\n", + "I_rms = I_m/sqrt(2)\n", + "P_avg = (R_rad*(I_m**2))/2\n", + "\n", + "#Result\n", + "\n", + "print \"Length of Dipole is\" ,round(L,2),\"metres\"\n", + "print \"Current fed to Antenna\",round(I_rms*1000,2),\"mA\"\n", + "print \"Average Power\",round(P_avg*1000,2),\"mW\"\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.9.3,PAGE NO.-44" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from __future__ import division\n", + "from math import pi,sqrt\n", + "\n", + "# variable declaration\n", + "\n", + "l_eff = 100 #effective hieght in m\n", + "f = 60*(10**3) #frequency in Hertz\n", + "r = 100*(10**3) #Distance in m\n", + "c = 3*(10**8) #Speed of light in m/sec\n", + "P_rad = 100*(10**3) #radiated power\n", + "\n", + "# calculation\n", + "\n", + "lamda = c/f\n", + "R_rad = 160*(pi**2)*((l_eff/lamda)**2)\n", + "I_rms = sqrt(P_rad/R_rad)\n", + "E_rms = (120*pi*l_eff*(I_rms**2))/(lamda*r)\n", + "\n", + "# Results\n", + "\n", + "print \"Strength of Electric field\",round(E_rms,2),\"V/m\"\n" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.9.4,PAGE NO.-45" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from __future__ import division\n", + "from math import pi\n", + "\n", + "# variable declaration\n", + "\n", + "l_eff = 113.3 #Effective length in metres\n", + "lamda = 18.8 #Wavelength in metres\n", + "I_rms = 725 #Base current in Ampere\n", + "r = 175 #Distance in metre\n", + "Eta_o = 120*pi\n", + "\n", + "#Calculation\n", + "\n", + "E = (120*pi*l_eff*I_rms)/(lamda*r)\n", + "H = E/Eta_o\n", + "R_rad = 160*(pi**2)*((l_eff/lamda)**2)\n", + "P_rad = (I_rms**2)*(R_rad)\n", + "\n", + "#Result\n", + "\n", + "print \"The electric field at a distance r is \",round(E*0.001,2),\"mV/m\"\n", + "print \"The H field is\",round(H,2),\"uA/m\"\n", + "print \"The power radiated by Antenna is \",round(P_rad*(10**(-9)),2),\"kW\"\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.9.5,PAGE NO.-46" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from __future__ import division\n", + "from math import pi,sin\n", + "\n", + "# variable declaration\n", + "\n", + "lamda = 10*(10**(-2)) # In cm\n", + "r = 200*(10**(-2)) # In cm\n", + "theta = 90 # In Degrees\n", + "phi = 0 # In Degrees\n", + "IdL = 3*(10**(-4)) # current distribution in Am\n", + "\n", + "#Calculation\n", + "\n", + "E_theta = (60*pi*IdL*sin(theta))/(lamda*r)\n", + "\n", + "#Result\n", + "\n", + "print \"The magnitude of component E_theta is \",round(E_theta,2),\"V/m\"\n", + "\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.9.6,PAGE NO.-46" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from __future__ import division\n", + "from math import pi\n", + "\n", + "# variable declaration\n", + "\n", + "dL = lamda/12\n", + "\n", + "\n", + "#Calculation\n", + "\n", + "R_rad = 80*(pi**2)*((dL/lamda)**2)\n", + "\n", + "#Result\n", + "\n", + "print \"The Radiation resistance of dipole antenna is \",round(R_rad,2),\"ohm\"\n", + "\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.9.7,PAGE NO.-46" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from __future__ import division\n", + "from math import pi,sqrt\n", + "from sympy import Symbol\n", + "# variable declaration\n", + "\n", + "I_m = 100 # uniform current in ampere\n", + "Lm = Symbol('Lm') #Taking Lm as lamda\n", + "dL = Lm/16\n", + "\n", + "\n", + "#Calculation\n", + "\n", + "P_rad = 80*(pi**2)*((dL/Lm)**2)*((I_m/sqrt(2))**2)\n", + "R_rad = 80*(pi**2)*((dL/Lm)**2)\n", + "\n", + "#Result\n", + "\n", + "print \"The Radiation resistance of dipole is \",round(R_rad,2),\"ohm\"\n", + "\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.9.8,PAGE NO.-47" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# variable declaration\n", + "\n", + "f = 30*(10**6) #Frequency in Hz\n", + "c = 3*(10**8) #speed of light in m/s\n", + "\n", + "#Calculation\n", + "\n", + "lamda = c/f\n", + "\n", + "#Result\n", + "\n", + "print \"The Length of Half wave dipole is \",round((lamda/2),2),\"m\"\n", + "\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.9.10,PAGE NO.-48" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from __future__ import division\n", + "from math import pi,sin,sqrt \n", + "from sympy import Symbol\n", + "# variable declaration\n", + "\n", + "Lm = Symbol('Lm') # Taking lamda as Lm\n", + "dL = 0.01*Lm # Length of dipole \n", + "theta = 45\n", + "P_rad = 1 # Power radiated in kW\n", + "phi = 90\n", + "r = 1 # Distance in Km\n", + "Eta_o=120*pi\n", + "\n", + "\n", + "\n", + "#Calculation\n", + "\n", + "R_rad = 20*(pi**2)*((dL/Lm)**2)\n", + "I_m = sqrt(2*P_rad*R_rad)\n", + "\n", + "# formula : P = (Eta_o/2)*(((Omega*I_m*dL*sin(theta))/(4*pi*r*v))**2)\n", + "P = (Eta_o/2)*(((I_m*dL*sin(theta))/(4*pi*(r**2)))**2)\n", + "\n", + "#Result\n", + "\n", + "print \"The Power density is \",P,\"Watt/m^2\"\n", + "\n", + "# Note : The Solving in the book is wrong they put 0.1 instead of 0.1*lamda \n" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.9.11,PAGE NO.-49" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from __future__ import division\n", + "from math import pi \n", + "\n", + "# variable declaration\n", + "\n", + "dL = 75 #Length of dipole in m\n", + "f = 800 # Frequency in kHz\n", + "I_rms = 10 #rms Current in Amp\n", + "c = 3*(10**8) #Speed of light in m/s \n", + "\n", + "#Calculation\n", + "\n", + "lamda = c/f\n", + "P_rad = 80*(pi**2)*((dL/lamda)**2)*(I_rms**2)\n", + "\n", + "#Result\n", + "\n", + "print \"The Total Power radiated by Antenna is \",round(P_rad*1000,2),\"kW\"\n", + "\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/sample_notebooks/NIKHILESH DAMLE/ANTENNAS_AND_WAVE_PROPAGATION_BY_U.A_BAKSHI,_A.V_BAKSHI,_K.A_BAKSHI_7.ipynb b/sample_notebooks/NIKHILESH DAMLE/ANTENNAS_AND_WAVE_PROPAGATION_BY_U.A_BAKSHI,_A.V_BAKSHI,_K.A_BAKSHI_7.ipynb new file mode 100755 index 00000000..e025dd86 --- /dev/null +++ b/sample_notebooks/NIKHILESH DAMLE/ANTENNAS_AND_WAVE_PROPAGATION_BY_U.A_BAKSHI,_A.V_BAKSHI,_K.A_BAKSHI_7.ipynb @@ -0,0 +1,462 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:02197244985b7dfa2823dacf9fac72c0a7536b50c383ffa1801ccf3066222ec3" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Radiation" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.7.1, PAGE NO.-30" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import math\n", + "\n", + "# Variable declaration\n", + "\n", + "I_m = 15 #Current in Ampere\n", + "P_rad = 6 #Power radiated in kW\n", + "\n", + "#Calculation\n", + "\n", + "# By formula\n", + "I_rms = I_m/math.sqrt(2) #I_rms is r.m.s. current\n", + "\n", + "R_rad = P_rad/(I_rms**2) #R_rad is radiation resistance\n", + "\n", + "#Result\n", + "\n", + "print \"The radiation resistance of Antenna is\",round(R_rad*1000,2),\"kW\"\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The radiation resistance of Antenna is 53.33 kW\n" + ] + } + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.9.1,PAGE NO.-42" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from __future__ import division\n", + "from math import sin,cos,pi,sqrt \n", + "from sympy import Symbol\n", + "\n", + "# Variable Declaration\n", + "\n", + "Lm = Symbol('Lm') # Taking lamda as Lm\n", + "dL = Lm/25 # Length of dipole for Hertian dipole\n", + "H_phi = 5 # Magnetic field strength in uA/m\n", + "theta = pi/2\n", + "r = 2 # Distance in Km\n", + "dL_1 = Lm/2 # Length of dipole for Half wave dipole \n", + "R_rad = 73 # Radiation resistance for half wave dipole in ohm\n", + "R_rad1 = 36.5 # Radiation resistance for quarter wave monopole in ohm\n", + "dL_2 = Lm/4 # Length of dipole for quarter wave monopole \n", + "\n", + "# Calculation\n", + "\n", + "# By formula : H_phi = I_m*dL*sin(theta)/2*Lm*r\n", + "I_m = (H_phi*2*Lm*r)/(dL*sin(theta))\n", + "I_rms = I_m/sqrt(2)\n", + "P_rad = 80*(pi**2)*((dL/Lm)**2)*(I_rms**2)\n", + "I_m1 = (H_phi*2*pi*r*sin(theta))/(cos(pi/2*cos(theta)))\n", + "I_rms1 = I_m1/sqrt(2)\n", + "P_rad1 = (I_rms1**2)*R_rad\n", + "P_rad2 = (I_rms1**2)*R_rad1\n", + "\n", + "# Result\n", + "\n", + "print \" The power radiated by hertzian dipole is \",round(P_rad*10**-3,2),\"mW\"\n", + "print \" The power radiated by half wave dipole is \",round(P_rad1*10**-3,2),\"mW\"\n", + "print \" The power radiated by Quarter wave monopole is \",round(P_rad2*10**-3,2),\"mW\"\n" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.9.2,PAGE NO.-43" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from __future__ import division\n", + "from math import sin,cos,pi,sqrt\n", + "\n", + "\n", + "\n", + "#variable declaration \n", + "\n", + "\n", + "E_theta = 10*(10**(-6)) #Electric field strength in V/m\n", + "theta = pi/2 #observation angle\n", + "r = 500*(10**3) #distance in metrs\n", + "f = 50*(10**6) #frequency in Hertz\n", + "c = 3*(10**8) #speed of light in m/sec\n", + "R_rad = 73 #for half wave dipole Radiation resistance in ohms\n", + "\n", + "\n", + "# calculation\n", + "lamda = c/f\n", + "L = lamda/2 #L is the length of half wave dipole\n", + "# formula : E=((60*I_m)/r)*((cos(pi/2*cos(theta)))*sin(theta))\n", + "I_m = (E_theta*r*sin(theta))/(60*cos(pi/2*cos(theta)))\n", + "I_rms = I_m/sqrt(2)\n", + "P_avg = (R_rad*(I_m**2))/2\n", + "\n", + "#Result\n", + "\n", + "print \"Length of Dipole is\" ,round(L,2),\"metres\"\n", + "print \"Current fed to Antenna\",round(I_rms*1000,2),\"mA\"\n", + "print \"Average Power\",round(P_avg*1000,2),\"mW\"\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.9.3,PAGE NO.-44" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from __future__ import division\n", + "from math import pi,sqrt\n", + "\n", + "# variable declaration\n", + "\n", + "l_eff = 100 #effective hieght in m\n", + "f = 60*(10**3) #frequency in Hertz\n", + "r = 100*(10**3) #Distance in m\n", + "c = 3*(10**8) #Speed of light in m/sec\n", + "P_rad = 100*(10**3) #radiated power\n", + "\n", + "# calculation\n", + "\n", + "lamda = c/f\n", + "R_rad = 160*(pi**2)*((l_eff/lamda)**2)\n", + "I_rms = sqrt(P_rad/R_rad)\n", + "E_rms = (120*pi*l_eff*(I_rms**2))/(lamda*r)\n", + "\n", + "# Results\n", + "\n", + "print \"Strength of Electric field\",round(E_rms,2),\"V/m\"\n" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.9.4,PAGE NO.-45" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from __future__ import division\n", + "from math import pi\n", + "\n", + "# variable declaration\n", + "\n", + "l_eff = 113.3 #Effective length in metres\n", + "lamda = 18.8 #Wavelength in metres\n", + "I_rms = 725 #Base current in Ampere\n", + "r = 175 #Distance in metre\n", + "Eta_o = 120*pi\n", + "\n", + "#Calculation\n", + "\n", + "E = (120*pi*l_eff*I_rms)/(lamda*r)\n", + "H = E/Eta_o\n", + "R_rad = 160*(pi**2)*((l_eff/lamda)**2)\n", + "P_rad = (I_rms**2)*(R_rad)\n", + "\n", + "#Result\n", + "\n", + "print \"The electric field at a distance r is \",round(E*0.001,2),\"mV/m\"\n", + "print \"The H field is\",round(H,2),\"uA/m\"\n", + "print \"The power radiated by Antenna is \",round(P_rad*(10**(-9)),2),\"kW\"\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.9.5,PAGE NO.-46" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from __future__ import division\n", + "from math import pi,sin\n", + "\n", + "# variable declaration\n", + "\n", + "lamda = 10*(10**(-2)) # In cm\n", + "r = 200*(10**(-2)) # In cm\n", + "theta = 90 # In Degrees\n", + "phi = 0 # In Degrees\n", + "IdL = 3*(10**(-4)) # current distribution in Am\n", + "\n", + "#Calculation\n", + "\n", + "E_theta = (60*pi*IdL*sin(theta))/(lamda*r)\n", + "\n", + "#Result\n", + "\n", + "print \"The magnitude of component E_theta is \",round(E_theta,2),\"V/m\"\n", + "\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.9.6,PAGE NO.-46" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from __future__ import division\n", + "from math import pi\n", + "\n", + "# variable declaration\n", + "\n", + "dL = lamda/12\n", + "\n", + "\n", + "#Calculation\n", + "\n", + "R_rad = 80*(pi**2)*((dL/lamda)**2)\n", + "\n", + "#Result\n", + "\n", + "print \"The Radiation resistance of dipole antenna is \",round(R_rad,2),\"ohm\"\n", + "\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.9.7,PAGE NO.-46" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from __future__ import division\n", + "from math import pi,sqrt\n", + "from sympy import Symbol\n", + "# variable declaration\n", + "\n", + "I_m = 100 # uniform current in ampere\n", + "Lm = Symbol('Lm') #Taking Lm as lamda\n", + "dL = Lm/16\n", + "\n", + "\n", + "#Calculation\n", + "\n", + "P_rad = 80*(pi**2)*((dL/Lm)**2)*((I_m/sqrt(2))**2)\n", + "R_rad = 80*(pi**2)*((dL/Lm)**2)\n", + "\n", + "#Result\n", + "\n", + "print \"The Radiation resistance of dipole is \",round(R_rad,2),\"ohm\"\n", + "\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.9.8,PAGE NO.-47" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# variable declaration\n", + "\n", + "f = 30*(10**6) #Frequency in Hz\n", + "c = 3*(10**8) #speed of light in m/s\n", + "\n", + "#Calculation\n", + "\n", + "lamda = c/f\n", + "\n", + "#Result\n", + "\n", + "print \"The Length of Half wave dipole is \",round((lamda/2),2),\"m\"\n", + "\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.9.10,PAGE NO.-48" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from __future__ import division\n", + "from math import pi,sin,sqrt \n", + "from sympy import Symbol\n", + "# variable declaration\n", + "\n", + "Lm = Symbol('Lm') # Taking lamda as Lm\n", + "dL = 0.01*Lm # Length of dipole \n", + "theta = 45\n", + "P_rad = 1 # Power radiated in kW\n", + "phi = 90\n", + "r = 1 # Distance in Km\n", + "Eta_o=120*pi\n", + "\n", + "\n", + "\n", + "#Calculation\n", + "\n", + "R_rad = 20*(pi**2)*((dL/Lm)**2)\n", + "I_m = sqrt(2*P_rad*R_rad)\n", + "\n", + "# formula : P = (Eta_o/2)*(((Omega*I_m*dL*sin(theta))/(4*pi*r*v))**2)\n", + "P = (Eta_o/2)*(((I_m*dL*sin(theta))/(4*pi*(r**2)))**2)\n", + "\n", + "#Result\n", + "\n", + "print \"The Power density is \",P,\"Watt/m^2\"\n", + "\n", + "# Note : The Solving in the book is wrong they put 0.1 instead of 0.1*lamda \n" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.9.11,PAGE NO.-49" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from __future__ import division\n", + "from math import pi \n", + "\n", + "# variable declaration\n", + "\n", + "dL = 75 #Length of dipole in m\n", + "f = 800 # Frequency in kHz\n", + "I_rms = 10 #rms Current in Amp\n", + "c = 3*(10**8) #Speed of light in m/s \n", + "\n", + "#Calculation\n", + "\n", + "lamda = c/f\n", + "P_rad = 80*(pi**2)*((dL/lamda)**2)*(I_rms**2)\n", + "\n", + "#Result\n", + "\n", + "print \"The Total Power radiated by Antenna is \",round(P_rad*1000,2),\"kW\"\n", + "\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/sample_notebooks/NIKHILESH DAMLE/ANTENNAS_AND_WAVE_PROPAGATION_BY_U.A_BAKSHI,_A.V_BAKSHI,_K.A_BAKSHI_8.ipynb b/sample_notebooks/NIKHILESH DAMLE/ANTENNAS_AND_WAVE_PROPAGATION_BY_U.A_BAKSHI,_A.V_BAKSHI,_K.A_BAKSHI_8.ipynb new file mode 100755 index 00000000..e025dd86 --- /dev/null +++ b/sample_notebooks/NIKHILESH DAMLE/ANTENNAS_AND_WAVE_PROPAGATION_BY_U.A_BAKSHI,_A.V_BAKSHI,_K.A_BAKSHI_8.ipynb @@ -0,0 +1,462 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:02197244985b7dfa2823dacf9fac72c0a7536b50c383ffa1801ccf3066222ec3" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Radiation" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.7.1, PAGE NO.-30" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import math\n", + "\n", + "# Variable declaration\n", + "\n", + "I_m = 15 #Current in Ampere\n", + "P_rad = 6 #Power radiated in kW\n", + "\n", + "#Calculation\n", + "\n", + "# By formula\n", + "I_rms = I_m/math.sqrt(2) #I_rms is r.m.s. current\n", + "\n", + "R_rad = P_rad/(I_rms**2) #R_rad is radiation resistance\n", + "\n", + "#Result\n", + "\n", + "print \"The radiation resistance of Antenna is\",round(R_rad*1000,2),\"kW\"\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The radiation resistance of Antenna is 53.33 kW\n" + ] + } + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.9.1,PAGE NO.-42" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from __future__ import division\n", + "from math import sin,cos,pi,sqrt \n", + "from sympy import Symbol\n", + "\n", + "# Variable Declaration\n", + "\n", + "Lm = Symbol('Lm') # Taking lamda as Lm\n", + "dL = Lm/25 # Length of dipole for Hertian dipole\n", + "H_phi = 5 # Magnetic field strength in uA/m\n", + "theta = pi/2\n", + "r = 2 # Distance in Km\n", + "dL_1 = Lm/2 # Length of dipole for Half wave dipole \n", + "R_rad = 73 # Radiation resistance for half wave dipole in ohm\n", + "R_rad1 = 36.5 # Radiation resistance for quarter wave monopole in ohm\n", + "dL_2 = Lm/4 # Length of dipole for quarter wave monopole \n", + "\n", + "# Calculation\n", + "\n", + "# By formula : H_phi = I_m*dL*sin(theta)/2*Lm*r\n", + "I_m = (H_phi*2*Lm*r)/(dL*sin(theta))\n", + "I_rms = I_m/sqrt(2)\n", + "P_rad = 80*(pi**2)*((dL/Lm)**2)*(I_rms**2)\n", + "I_m1 = (H_phi*2*pi*r*sin(theta))/(cos(pi/2*cos(theta)))\n", + "I_rms1 = I_m1/sqrt(2)\n", + "P_rad1 = (I_rms1**2)*R_rad\n", + "P_rad2 = (I_rms1**2)*R_rad1\n", + "\n", + "# Result\n", + "\n", + "print \" The power radiated by hertzian dipole is \",round(P_rad*10**-3,2),\"mW\"\n", + "print \" The power radiated by half wave dipole is \",round(P_rad1*10**-3,2),\"mW\"\n", + "print \" The power radiated by Quarter wave monopole is \",round(P_rad2*10**-3,2),\"mW\"\n" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.9.2,PAGE NO.-43" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from __future__ import division\n", + "from math import sin,cos,pi,sqrt\n", + "\n", + "\n", + "\n", + "#variable declaration \n", + "\n", + "\n", + "E_theta = 10*(10**(-6)) #Electric field strength in V/m\n", + "theta = pi/2 #observation angle\n", + "r = 500*(10**3) #distance in metrs\n", + "f = 50*(10**6) #frequency in Hertz\n", + "c = 3*(10**8) #speed of light in m/sec\n", + "R_rad = 73 #for half wave dipole Radiation resistance in ohms\n", + "\n", + "\n", + "# calculation\n", + "lamda = c/f\n", + "L = lamda/2 #L is the length of half wave dipole\n", + "# formula : E=((60*I_m)/r)*((cos(pi/2*cos(theta)))*sin(theta))\n", + "I_m = (E_theta*r*sin(theta))/(60*cos(pi/2*cos(theta)))\n", + "I_rms = I_m/sqrt(2)\n", + "P_avg = (R_rad*(I_m**2))/2\n", + "\n", + "#Result\n", + "\n", + "print \"Length of Dipole is\" ,round(L,2),\"metres\"\n", + "print \"Current fed to Antenna\",round(I_rms*1000,2),\"mA\"\n", + "print \"Average Power\",round(P_avg*1000,2),\"mW\"\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.9.3,PAGE NO.-44" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from __future__ import division\n", + "from math import pi,sqrt\n", + "\n", + "# variable declaration\n", + "\n", + "l_eff = 100 #effective hieght in m\n", + "f = 60*(10**3) #frequency in Hertz\n", + "r = 100*(10**3) #Distance in m\n", + "c = 3*(10**8) #Speed of light in m/sec\n", + "P_rad = 100*(10**3) #radiated power\n", + "\n", + "# calculation\n", + "\n", + "lamda = c/f\n", + "R_rad = 160*(pi**2)*((l_eff/lamda)**2)\n", + "I_rms = sqrt(P_rad/R_rad)\n", + "E_rms = (120*pi*l_eff*(I_rms**2))/(lamda*r)\n", + "\n", + "# Results\n", + "\n", + "print \"Strength of Electric field\",round(E_rms,2),\"V/m\"\n" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.9.4,PAGE NO.-45" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from __future__ import division\n", + "from math import pi\n", + "\n", + "# variable declaration\n", + "\n", + "l_eff = 113.3 #Effective length in metres\n", + "lamda = 18.8 #Wavelength in metres\n", + "I_rms = 725 #Base current in Ampere\n", + "r = 175 #Distance in metre\n", + "Eta_o = 120*pi\n", + "\n", + "#Calculation\n", + "\n", + "E = (120*pi*l_eff*I_rms)/(lamda*r)\n", + "H = E/Eta_o\n", + "R_rad = 160*(pi**2)*((l_eff/lamda)**2)\n", + "P_rad = (I_rms**2)*(R_rad)\n", + "\n", + "#Result\n", + "\n", + "print \"The electric field at a distance r is \",round(E*0.001,2),\"mV/m\"\n", + "print \"The H field is\",round(H,2),\"uA/m\"\n", + "print \"The power radiated by Antenna is \",round(P_rad*(10**(-9)),2),\"kW\"\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.9.5,PAGE NO.-46" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from __future__ import division\n", + "from math import pi,sin\n", + "\n", + "# variable declaration\n", + "\n", + "lamda = 10*(10**(-2)) # In cm\n", + "r = 200*(10**(-2)) # In cm\n", + "theta = 90 # In Degrees\n", + "phi = 0 # In Degrees\n", + "IdL = 3*(10**(-4)) # current distribution in Am\n", + "\n", + "#Calculation\n", + "\n", + "E_theta = (60*pi*IdL*sin(theta))/(lamda*r)\n", + "\n", + "#Result\n", + "\n", + "print \"The magnitude of component E_theta is \",round(E_theta,2),\"V/m\"\n", + "\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.9.6,PAGE NO.-46" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from __future__ import division\n", + "from math import pi\n", + "\n", + "# variable declaration\n", + "\n", + "dL = lamda/12\n", + "\n", + "\n", + "#Calculation\n", + "\n", + "R_rad = 80*(pi**2)*((dL/lamda)**2)\n", + "\n", + "#Result\n", + "\n", + "print \"The Radiation resistance of dipole antenna is \",round(R_rad,2),\"ohm\"\n", + "\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.9.7,PAGE NO.-46" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from __future__ import division\n", + "from math import pi,sqrt\n", + "from sympy import Symbol\n", + "# variable declaration\n", + "\n", + "I_m = 100 # uniform current in ampere\n", + "Lm = Symbol('Lm') #Taking Lm as lamda\n", + "dL = Lm/16\n", + "\n", + "\n", + "#Calculation\n", + "\n", + "P_rad = 80*(pi**2)*((dL/Lm)**2)*((I_m/sqrt(2))**2)\n", + "R_rad = 80*(pi**2)*((dL/Lm)**2)\n", + "\n", + "#Result\n", + "\n", + "print \"The Radiation resistance of dipole is \",round(R_rad,2),\"ohm\"\n", + "\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.9.8,PAGE NO.-47" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# variable declaration\n", + "\n", + "f = 30*(10**6) #Frequency in Hz\n", + "c = 3*(10**8) #speed of light in m/s\n", + "\n", + "#Calculation\n", + "\n", + "lamda = c/f\n", + "\n", + "#Result\n", + "\n", + "print \"The Length of Half wave dipole is \",round((lamda/2),2),\"m\"\n", + "\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.9.10,PAGE NO.-48" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from __future__ import division\n", + "from math import pi,sin,sqrt \n", + "from sympy import Symbol\n", + "# variable declaration\n", + "\n", + "Lm = Symbol('Lm') # Taking lamda as Lm\n", + "dL = 0.01*Lm # Length of dipole \n", + "theta = 45\n", + "P_rad = 1 # Power radiated in kW\n", + "phi = 90\n", + "r = 1 # Distance in Km\n", + "Eta_o=120*pi\n", + "\n", + "\n", + "\n", + "#Calculation\n", + "\n", + "R_rad = 20*(pi**2)*((dL/Lm)**2)\n", + "I_m = sqrt(2*P_rad*R_rad)\n", + "\n", + "# formula : P = (Eta_o/2)*(((Omega*I_m*dL*sin(theta))/(4*pi*r*v))**2)\n", + "P = (Eta_o/2)*(((I_m*dL*sin(theta))/(4*pi*(r**2)))**2)\n", + "\n", + "#Result\n", + "\n", + "print \"The Power density is \",P,\"Watt/m^2\"\n", + "\n", + "# Note : The Solving in the book is wrong they put 0.1 instead of 0.1*lamda \n" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.9.11,PAGE NO.-49" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from __future__ import division\n", + "from math import pi \n", + "\n", + "# variable declaration\n", + "\n", + "dL = 75 #Length of dipole in m\n", + "f = 800 # Frequency in kHz\n", + "I_rms = 10 #rms Current in Amp\n", + "c = 3*(10**8) #Speed of light in m/s \n", + "\n", + "#Calculation\n", + "\n", + "lamda = c/f\n", + "P_rad = 80*(pi**2)*((dL/lamda)**2)*(I_rms**2)\n", + "\n", + "#Result\n", + "\n", + "print \"The Total Power radiated by Antenna is \",round(P_rad*1000,2),\"kW\"\n", + "\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/sample_notebooks/NIKHILESH DAMLE/ANTENNAS_AND_WAVE_PROPAGATION_BY_U.A_BAKSHI,_A.V_BAKSHI,_K.A_BAKSHI_9.ipynb b/sample_notebooks/NIKHILESH DAMLE/ANTENNAS_AND_WAVE_PROPAGATION_BY_U.A_BAKSHI,_A.V_BAKSHI,_K.A_BAKSHI_9.ipynb new file mode 100755 index 00000000..e025dd86 --- /dev/null +++ b/sample_notebooks/NIKHILESH DAMLE/ANTENNAS_AND_WAVE_PROPAGATION_BY_U.A_BAKSHI,_A.V_BAKSHI,_K.A_BAKSHI_9.ipynb @@ -0,0 +1,462 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:02197244985b7dfa2823dacf9fac72c0a7536b50c383ffa1801ccf3066222ec3" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Radiation" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.7.1, PAGE NO.-30" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import math\n", + "\n", + "# Variable declaration\n", + "\n", + "I_m = 15 #Current in Ampere\n", + "P_rad = 6 #Power radiated in kW\n", + "\n", + "#Calculation\n", + "\n", + "# By formula\n", + "I_rms = I_m/math.sqrt(2) #I_rms is r.m.s. current\n", + "\n", + "R_rad = P_rad/(I_rms**2) #R_rad is radiation resistance\n", + "\n", + "#Result\n", + "\n", + "print \"The radiation resistance of Antenna is\",round(R_rad*1000,2),\"kW\"\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The radiation resistance of Antenna is 53.33 kW\n" + ] + } + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.9.1,PAGE NO.-42" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from __future__ import division\n", + "from math import sin,cos,pi,sqrt \n", + "from sympy import Symbol\n", + "\n", + "# Variable Declaration\n", + "\n", + "Lm = Symbol('Lm') # Taking lamda as Lm\n", + "dL = Lm/25 # Length of dipole for Hertian dipole\n", + "H_phi = 5 # Magnetic field strength in uA/m\n", + "theta = pi/2\n", + "r = 2 # Distance in Km\n", + "dL_1 = Lm/2 # Length of dipole for Half wave dipole \n", + "R_rad = 73 # Radiation resistance for half wave dipole in ohm\n", + "R_rad1 = 36.5 # Radiation resistance for quarter wave monopole in ohm\n", + "dL_2 = Lm/4 # Length of dipole for quarter wave monopole \n", + "\n", + "# Calculation\n", + "\n", + "# By formula : H_phi = I_m*dL*sin(theta)/2*Lm*r\n", + "I_m = (H_phi*2*Lm*r)/(dL*sin(theta))\n", + "I_rms = I_m/sqrt(2)\n", + "P_rad = 80*(pi**2)*((dL/Lm)**2)*(I_rms**2)\n", + "I_m1 = (H_phi*2*pi*r*sin(theta))/(cos(pi/2*cos(theta)))\n", + "I_rms1 = I_m1/sqrt(2)\n", + "P_rad1 = (I_rms1**2)*R_rad\n", + "P_rad2 = (I_rms1**2)*R_rad1\n", + "\n", + "# Result\n", + "\n", + "print \" The power radiated by hertzian dipole is \",round(P_rad*10**-3,2),\"mW\"\n", + "print \" The power radiated by half wave dipole is \",round(P_rad1*10**-3,2),\"mW\"\n", + "print \" The power radiated by Quarter wave monopole is \",round(P_rad2*10**-3,2),\"mW\"\n" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.9.2,PAGE NO.-43" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from __future__ import division\n", + "from math import sin,cos,pi,sqrt\n", + "\n", + "\n", + "\n", + "#variable declaration \n", + "\n", + "\n", + "E_theta = 10*(10**(-6)) #Electric field strength in V/m\n", + "theta = pi/2 #observation angle\n", + "r = 500*(10**3) #distance in metrs\n", + "f = 50*(10**6) #frequency in Hertz\n", + "c = 3*(10**8) #speed of light in m/sec\n", + "R_rad = 73 #for half wave dipole Radiation resistance in ohms\n", + "\n", + "\n", + "# calculation\n", + "lamda = c/f\n", + "L = lamda/2 #L is the length of half wave dipole\n", + "# formula : E=((60*I_m)/r)*((cos(pi/2*cos(theta)))*sin(theta))\n", + "I_m = (E_theta*r*sin(theta))/(60*cos(pi/2*cos(theta)))\n", + "I_rms = I_m/sqrt(2)\n", + "P_avg = (R_rad*(I_m**2))/2\n", + "\n", + "#Result\n", + "\n", + "print \"Length of Dipole is\" ,round(L,2),\"metres\"\n", + "print \"Current fed to Antenna\",round(I_rms*1000,2),\"mA\"\n", + "print \"Average Power\",round(P_avg*1000,2),\"mW\"\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.9.3,PAGE NO.-44" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from __future__ import division\n", + "from math import pi,sqrt\n", + "\n", + "# variable declaration\n", + "\n", + "l_eff = 100 #effective hieght in m\n", + "f = 60*(10**3) #frequency in Hertz\n", + "r = 100*(10**3) #Distance in m\n", + "c = 3*(10**8) #Speed of light in m/sec\n", + "P_rad = 100*(10**3) #radiated power\n", + "\n", + "# calculation\n", + "\n", + "lamda = c/f\n", + "R_rad = 160*(pi**2)*((l_eff/lamda)**2)\n", + "I_rms = sqrt(P_rad/R_rad)\n", + "E_rms = (120*pi*l_eff*(I_rms**2))/(lamda*r)\n", + "\n", + "# Results\n", + "\n", + "print \"Strength of Electric field\",round(E_rms,2),\"V/m\"\n" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.9.4,PAGE NO.-45" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from __future__ import division\n", + "from math import pi\n", + "\n", + "# variable declaration\n", + "\n", + "l_eff = 113.3 #Effective length in metres\n", + "lamda = 18.8 #Wavelength in metres\n", + "I_rms = 725 #Base current in Ampere\n", + "r = 175 #Distance in metre\n", + "Eta_o = 120*pi\n", + "\n", + "#Calculation\n", + "\n", + "E = (120*pi*l_eff*I_rms)/(lamda*r)\n", + "H = E/Eta_o\n", + "R_rad = 160*(pi**2)*((l_eff/lamda)**2)\n", + "P_rad = (I_rms**2)*(R_rad)\n", + "\n", + "#Result\n", + "\n", + "print \"The electric field at a distance r is \",round(E*0.001,2),\"mV/m\"\n", + "print \"The H field is\",round(H,2),\"uA/m\"\n", + "print \"The power radiated by Antenna is \",round(P_rad*(10**(-9)),2),\"kW\"\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.9.5,PAGE NO.-46" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from __future__ import division\n", + "from math import pi,sin\n", + "\n", + "# variable declaration\n", + "\n", + "lamda = 10*(10**(-2)) # In cm\n", + "r = 200*(10**(-2)) # In cm\n", + "theta = 90 # In Degrees\n", + "phi = 0 # In Degrees\n", + "IdL = 3*(10**(-4)) # current distribution in Am\n", + "\n", + "#Calculation\n", + "\n", + "E_theta = (60*pi*IdL*sin(theta))/(lamda*r)\n", + "\n", + "#Result\n", + "\n", + "print \"The magnitude of component E_theta is \",round(E_theta,2),\"V/m\"\n", + "\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.9.6,PAGE NO.-46" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from __future__ import division\n", + "from math import pi\n", + "\n", + "# variable declaration\n", + "\n", + "dL = lamda/12\n", + "\n", + "\n", + "#Calculation\n", + "\n", + "R_rad = 80*(pi**2)*((dL/lamda)**2)\n", + "\n", + "#Result\n", + "\n", + "print \"The Radiation resistance of dipole antenna is \",round(R_rad,2),\"ohm\"\n", + "\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.9.7,PAGE NO.-46" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from __future__ import division\n", + "from math import pi,sqrt\n", + "from sympy import Symbol\n", + "# variable declaration\n", + "\n", + "I_m = 100 # uniform current in ampere\n", + "Lm = Symbol('Lm') #Taking Lm as lamda\n", + "dL = Lm/16\n", + "\n", + "\n", + "#Calculation\n", + "\n", + "P_rad = 80*(pi**2)*((dL/Lm)**2)*((I_m/sqrt(2))**2)\n", + "R_rad = 80*(pi**2)*((dL/Lm)**2)\n", + "\n", + "#Result\n", + "\n", + "print \"The Radiation resistance of dipole is \",round(R_rad,2),\"ohm\"\n", + "\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.9.8,PAGE NO.-47" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# variable declaration\n", + "\n", + "f = 30*(10**6) #Frequency in Hz\n", + "c = 3*(10**8) #speed of light in m/s\n", + "\n", + "#Calculation\n", + "\n", + "lamda = c/f\n", + "\n", + "#Result\n", + "\n", + "print \"The Length of Half wave dipole is \",round((lamda/2),2),\"m\"\n", + "\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.9.10,PAGE NO.-48" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from __future__ import division\n", + "from math import pi,sin,sqrt \n", + "from sympy import Symbol\n", + "# variable declaration\n", + "\n", + "Lm = Symbol('Lm') # Taking lamda as Lm\n", + "dL = 0.01*Lm # Length of dipole \n", + "theta = 45\n", + "P_rad = 1 # Power radiated in kW\n", + "phi = 90\n", + "r = 1 # Distance in Km\n", + "Eta_o=120*pi\n", + "\n", + "\n", + "\n", + "#Calculation\n", + "\n", + "R_rad = 20*(pi**2)*((dL/Lm)**2)\n", + "I_m = sqrt(2*P_rad*R_rad)\n", + "\n", + "# formula : P = (Eta_o/2)*(((Omega*I_m*dL*sin(theta))/(4*pi*r*v))**2)\n", + "P = (Eta_o/2)*(((I_m*dL*sin(theta))/(4*pi*(r**2)))**2)\n", + "\n", + "#Result\n", + "\n", + "print \"The Power density is \",P,\"Watt/m^2\"\n", + "\n", + "# Note : The Solving in the book is wrong they put 0.1 instead of 0.1*lamda \n" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1.9.11,PAGE NO.-49" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from __future__ import division\n", + "from math import pi \n", + "\n", + "# variable declaration\n", + "\n", + "dL = 75 #Length of dipole in m\n", + "f = 800 # Frequency in kHz\n", + "I_rms = 10 #rms Current in Amp\n", + "c = 3*(10**8) #Speed of light in m/s \n", + "\n", + "#Calculation\n", + "\n", + "lamda = c/f\n", + "P_rad = 80*(pi**2)*((dL/lamda)**2)*(I_rms**2)\n", + "\n", + "#Result\n", + "\n", + "print \"The Total Power radiated by Antenna is \",round(P_rad*1000,2),\"kW\"\n", + "\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/sample_notebooks/NirenNegandhi/ch2_1.ipynb b/sample_notebooks/NirenNegandhi/ch2_1.ipynb new file mode 100755 index 00000000..e79c5ef9 --- /dev/null +++ b/sample_notebooks/NirenNegandhi/ch2_1.ipynb @@ -0,0 +1,505 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:d53ace7eee908f1b365cd69f5f5bf3b12191ff7d43291af068a8947558149234" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 2: Special Theory of Relativity" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2.2, Page 34" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import math\n", + "\n", + "#Variable declaration\n", + "ly = 9.46e+015; # Distance travelled by light in an year, m\n", + "c = 3e+008; # Speed of light, m/s\n", + "L = 4.30*ly; # Distance of Alpha Centauri from earth, m\n", + "T0 = 16*365.25*24*60*60; # Proper time in system K_prime, s\n", + "\n", + "#Calculations\n", + "# As time measured on earth, T = 2*L/v = T0_prime/sqrt(1-(v/c)^2), solving for v\n", + "v = math.sqrt(4*L**2/(T0**2+4*L**2/c**2)); # Speed of the aircraft, m/s\n", + "gama = 1/math.sqrt(1-(v/c)**2); # Relativistic factor\n", + "T = gama*T0/(365.25*24*60*60); # Time interval as measured on Earth, y\n", + "\n", + "#Results\n", + "print \"The speed of the aircraft = %4.2e m/s\" %v\n", + "print \"The time interval as measured on earth = %4.1f y\"%T\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The speed of the aircraft = 1.42e+08 m/s\n", + "The time interval as measured on earth = 18.2 y\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2.3, Page 38" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import math\n", + "\n", + "#Variable declaration\n", + "L0 = 4.30; # Distance of Alpha Centauri from earth, ly\n", + "c = 3e+008; # Speed of light, m/s\n", + "T = 8; # Proper time in system K_prime, y\n", + "\n", + "#Calculations\n", + "# As v/c = L0*sqrt(1-(v/c)^2)/(c*T) or bita = L0*sqrt(1-bita^2)/(c*T), solving for bita\n", + "bita = math.sqrt(L0**2/(T**2 + L0**2)); # Boost parameter\n", + "v = L0*math.sqrt(1-bita**2)/T; # Speed of the aircraft, c units\n", + "\n", + "#Results\n", + "print \"The boost parameter = %5.3f\"%bita\n", + "print \"The speed of the aircraft = %5.3fc units\"%v" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The boost parameter = 0.473\n", + "The speed of the aircraft = 0.473c units\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2.4, Page 40" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import math\n", + "\n", + "#Variable declaration\n", + "c = 1; # For simplicity assume speed of light to be unity, m/s\n", + "bita = 0.600; # Boost parameter\n", + "gama = 1/math.sqrt(1-bita**2); # Relativistic factor\n", + "u_x_prime = 0; # Speed of the protons in spaceship frame along x-axis, m/s\n", + "u_y_prime = 0.99*c; # Speed of the protons in spaceship frame along y-axis, m/s\n", + "u_z_prime = 0; # Speed of the protons in spaceship frame along z-axis, m/s\n", + "v = 0.60*c; # Speed of the spaceship w.r.t. space station, m/s\n", + "\n", + "#Calculations\n", + "u_x = (u_x_prime + v)/(1 + v/c**2*u_x_prime); # Speed of protons in space station frame along x-axis, m/s\n", + "u_y = u_y_prime/(gama*(1 + v/c**2*u_x_prime)); # Speed of protons in space station frame along y-axis, m/s\n", + "u_z = u_z_prime/(gama*(1 + v/c**2*u_x_prime)); # Speed of protons in space station frame along y-axis, m/s\n", + "u = math.sqrt(u_x**2 + u_y**2 + u_z**2); # The speed of the protons measured by an observer in the space station, m/s\n", + "\n", + "#Result\n", + "print \"The speed of the protons measured by an observer in the space station = %5.3fc units\"%u" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The speed of the protons measured by an observer in the space station = 0.994c units\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2.5, Page 45" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Variable declaration\n", + "c = 2.998e+008; # Speed of light in free space, m/s\n", + "v = 7712; # Speed of the space shuttle, m/s\n", + "bita = v/c; # Boost parameter\n", + "T_loss = 295.02; # Total measured loss in time, ps/sec\n", + "Add_T_loss = 35.0; # Additional time loss for moving clock from general relativity prediction, ps/s\n", + "\n", + "#Calculations\n", + "# From time dilation relation, T0_prime = T*sqrt(1 - bita^2), solving for (T - T0_prime)/T\n", + "Calc_T_loss = bita**2/2*1e+012; # Expected time lost per sec by the moving clock, ps/sec\n", + "Measured_T_loss = T_loss + Add_T_loss; # Total measured loss in time per sec as per special relativity, ps/s\n", + "percent_T_loss = (Calc_T_loss - Measured_T_loss)/Calc_T_loss*100; # Percentage deviation of measured and the calculated time loss per sec\n", + "T = 6.05e+05; # Total time of the seven-day mission, s\n", + "delta_T = Calc_T_loss*1e-012*T; # The total time difference between the moving and stationary clocks during the entire shuttle flight, s\n", + "\n", + "#Results\n", + "print \"The expected time lost per second for the moving clock = %6.2f ps\"%Calc_T_loss\n", + "print \"The percentage deviation of measured and the calculated time loss per sec for moving clock = %3.1f percent\"%percent_T_loss #answer differs due to rounding errors\n", + "print \"The total time difference between the moving and stationary clocks during the entire shuttle flight = %3.1f ms\"%(delta_T/1e-003)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The expected time lost per second for the moving clock = 330.86 ps\n", + "The percentage deviation of measured and the calculated time loss per sec for moving clock = 0.3 percent\n", + "The total time difference between the moving and stationary clocks during the entire shuttle flight = 0.2 ms\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2.8, Page 57" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import math\n", + "\n", + "#Variable declaration\n", + "f0 = 1; # For simplicity assume frequency of the signals sent by Frank, Hz\n", + "# Outbound trip\n", + "bita = -0.8; # Boost parameter for receding frames\n", + "\n", + "#Calculations&Results\n", + "f = math.sqrt(1+bita)/math.sqrt(1-bita)*f0; # The frequency of the signals received by Mary in outbound trip, Hz\n", + "print \"The frequency of the signals received by Mary in outbound trip = f0/%d\", math.ceil(f*9)\n", + "# Return trip\n", + "bita = +0.8; # Boost parameter for approaching frames\n", + "f = math.sqrt(1+bita)/math.sqrt(1-bita)*f0; # The frequency of the signals received by Mary in return trip, Hz\n", + "print \"The frequency of the signals received by Mary in return trip = %df0\"%f" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " The frequency of the signals received by Mary in outbound trip = f0/%d 3.0\n", + "The frequency of the signals received by Mary in return trip = 3f0\n" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2.11, Page 64" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import math\n", + "\n", + "#Variable declaration\n", + "q = 1.6e-019; # Charge on an electron, C\n", + "V = 25e+003; # Accelerating potential, volt\n", + "K = q*V; # Kinetic energy of electrons, J\n", + "m = 9.11e-031; # Rest mass of an electron, kg\n", + "c = 3.00e+08; # Speed of light, m/s\n", + "\n", + "#Calculations\n", + "# From relativistic kinetic energy formula, K = (gama - 1)*m*C^2, solving for gama\n", + "gama = 1 + K/(m*c**2); # Relativistic factor\n", + "bita = math.sqrt((gama**2-1)/gama**2); # Boost parameter\n", + "u = bita*c; # Speed of the electrons, m/s\n", + "# From non-relativistic expression, K = 1/2*m*u^2, solving for u\n", + "u_classical = math.sqrt(2*K/m); # Non-relativistic speed of the electrons, m/s\n", + "u_error = (u_classical - u)/u_classical*100; # Percentage error in speed of electrons, m/s\n", + "\n", + "#Results\n", + "print \"The relativistic speed of the accelerated electrons = %4.2e m/s\"%u\n", + "print \"The classical speed is about %d percent greater than the relativistic speed\"%math.ceil(u_error)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The relativistic speed of the accelerated electrons = 9.04e+07 m/s\n", + "The classical speed is about 4 percent greater than the relativistic speed\n" + ] + } + ], + "prompt_number": 8 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2.13, Page 69" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import math\n", + "\n", + "#Variable declaration\n", + "c = 1; # For simplicity assume peed of light to be unity, m/s\n", + "K = 2.00; # Kinetic energy of protons, GeV\n", + "E0 = 0.938; # Rest mass of a proton, GeV\n", + "E = K + E0; # Total energy of the proton, GeV\n", + "\n", + "#Calculations\n", + "# From relativistic mass energy relation, E^2 = (p*c)^2+E0^2, solving for p\n", + "p = math.sqrt(E**2 - E0**2)/c; # Momentum of the protons, GeV/c\n", + "# As E = gama*E0, solving for gama\n", + "gama = E/E0; # Relativistic factor\n", + "bita = math.sqrt((gama**2-1)/gama**2); # Boost parameter\n", + "v = bita*3.00e+08; # Speed of 2 GeV proton, m/s\n", + "\n", + "#Results\n", + "print \"The energy of each initial proton = %5.3f GeV\"%E\n", + "print \"The momentum of each initial proton = %4.2f GeV/c\"%p\n", + "print \"The speed of each initial proton = %3.1e m/s\"%v\n", + "print \"The relativistic factor, gama = %4.2f\"%gama\n", + "print \"The boost parameter, beta = %5.3f\"%bita" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The energy of each initial proton = 2.938 GeV\n", + "The momentum of each initial proton = 2.78 GeV/c\n", + "The speed of each initial proton = 2.8e+08 m/s\n", + "The relativistic factor, gama = 3.13\n", + "The boost parameter, beta = 0.948\n" + ] + } + ], + "prompt_number": 9 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2.15, Page 71" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Variable declaration\n", + "E_d = 1875.6; # Rest mass energy of the deuterium, MeV\n", + "E_pi = 139.6; # Rest mass energy of the pion, MeV\n", + "E_p = 938.3; # Rest mass energy of the proton, MeV\n", + "\n", + "#Calculation\n", + "K = 1./2*(E_d + E_pi - 2*E_p); # Minimum kinetic energy of the protons, MeV \n", + "\n", + "#Result\n", + "print \"The minimum kinetic energy of the protons = %2d MeV\"%K" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The minimum kinetic energy of the protons = 69 MeV\n" + ] + } + ], + "prompt_number": 9 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2.16, Page 72" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Variable declaration\n", + "u = 931.5; # Energy equivalent of 1 amu, MeV\n", + "c = 1; # Speed of light in vacuum, m/s\n", + "\n", + "#Calculations\n", + "m_e = 0.000549*u; # Rest mass of an electron, MeV/c^2\n", + "m_p = 1.007276*u; # Rest mass of a proton, MeV/c^2\n", + "m_n = 1.008665*u; # Rest mass of a neutron, MeV/c^2\n", + "m_He = 4.002603*u; # Rest mass of a helium, MeV/c^2\n", + "M_He = m_He - 2*m_e; # Mass of He nucleus, MeV/c^2\n", + "E_B_He = (2*m_p + 2*m_n - M_He)*c**2; # Binding energy of the He nucleus, MeV\n", + "\n", + "#Result\n", + "print \"The binding energy of the He nucleus = %4.1f MeV\"%E_B_He" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The binding energy of the He nucleus = 28.3 MeV\n" + ] + } + ], + "prompt_number": 10 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2.17, Page 72" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Variable declaration\n", + "u = 931.5; # Energy equivalent of 1 amu, MeV/u\n", + "c = 1; # For simplicity assume speed of light in vacuum to be unity, m/s\n", + "E_B = 4.24; # The dissociationenergy of the NaCl molecule, MeV\n", + "\n", + "#Calculations\n", + "M = 58.44*u; # Energy corresponding to molecular mass of NaCl, MeV\n", + "f_r = E_B/M; # The fractional mass increase of the Na and Cl atoms\n", + "\n", + "#Result\n", + "print \"The fractional mass increase of the Na and Cl atoms when they are not bound together in NaCl = %4.1e\"%(f_r/1e+006)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The fractional mass increase of the Na and Cl atoms when they are not bound together in NaCl = 7.8e-11\n" + ] + } + ], + "prompt_number": 11 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2.18, Page 72" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import math\n", + "\n", + "#Variable declaration\n", + "c = 1; # For simplicity assume speed of light in vacuum to be unity, m/s\n", + "E0_n = 940; # Rest energy of a neutron, MeV\n", + "E0_pi = 140; # Rest energy of a pion, MeV\n", + "p_n = 4702; # Momentum of the neutron, MeV/c\n", + "p_pi = 169; # Momentum of the pion, MeV/c\n", + "\n", + "#Calculations\n", + "E_n = math.sqrt((p_n*c)**2+E0_n**2); # Total energy of the neutron, MeV\n", + "E_pi = math.sqrt((p_pi*c)**2+E0_pi**2); # Total energy of the pion, MeV\n", + "E = E_n + E_pi; # Total energy of the reaction, MeV\n", + "p_sigma = p_n + p_pi; # Momentum of the sigma particle, MeV/c\n", + "E0_sigma = math.sqrt(E**2 - (p_sigma*c)**2); # Rest mass energy of the sigma particle, MeV\n", + "m_sigma = E0_sigma/c**2; # Rest mass of sigma particle, MeV/c^2;\n", + "K = E - E0_sigma; # Kinetic energy of sigma particle, MeV\n", + "\n", + "#Results\n", + "print \"The rest mass of sigma particle = %4d MeV/c^2\"%math.ceil(m_sigma)\n", + "print \"The kinetic energy of sigma particle = %4d MeV\"%math.ceil(K)\n", + "\n", + "#Answers differ due to rounding errors" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The rest mass of sigma particle = 1192 MeV/c^2\n", + "The kinetic energy of sigma particle = 3824 MeV\n" + ] + } + ], + "prompt_number": 10 + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/sample_notebooks/NitamoniDas/Chapter_8_Data_Abstraction.ipynb b/sample_notebooks/NitamoniDas/Chapter_8_Data_Abstraction.ipynb new file mode 100755 index 00000000..7eb86cba --- /dev/null +++ b/sample_notebooks/NitamoniDas/Chapter_8_Data_Abstraction.ipynb @@ -0,0 +1,809 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:cceaf517d4ab644356e85522addd8c655c7a27bbc143387c58a9d5656be364df" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.1, page no: 208" + ] + }, + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 8: Data Abstraction through Classes and User-Defined Data Types " + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Super:\n", + " def __init__(self):\n", + " self.__IntegerData=None #private member\n", + " #public functions\n", + " def SetData(self,i):\n", + " self.__IntegerData=i #refer to IntegerData\n", + " def ShowData(self):\n", + " print \"Data is \",self.__IntegerData,' '\n", + "\n", + "ob1=Super()\n", + "ob2=Super()\n", + "ob1.SetData(1000)\n", + "ob2.SetData(2000)\n", + "ob1.ShowData()\n", + "ob2.ShowData()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Data is 1000 \n", + "Data is 2000 \n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.2, page no:211" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class X:\n", + " def __init__(self):\n", + " self.a=None #private members\n", + " self.b=None #private members\n", + " \n", + "#no structure type present in python \n", + "x = X()\n", + "\n", + "x.a=0\n", + "x.b=1\n", + "print \"x.a=\",x.a,\",x.b=\",x.b" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "x.a= 0 ,x.b= 1\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.3, page no:214" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction:\n", + " \n", + " def SetValue(self,a,b): #public functions\n", + " self.__num=a \n", + " self.__denom=b \n", + " def GetValue(self,a,b):\n", + " a=self.__num\n", + " b=self.__denom\n", + " \n", + "f=Fraction()\n", + "n=input(\"Enter n: \") #user input\n", + "d=input(\"Enter d: \") #user input\n", + "print \"enter the numerator and denominator: \", ' ',n,d\n", + "\n", + "f.SetValue(n,d) #call function SetValue\n", + "print \"Numerator value set: \", ' ',n\n", + "print \"Denominator value set: \", ' ',d\n", + "f.GetValue(n,d) #call function GetData\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter n: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter d: 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "enter the numerator and denominator: 3 4\n", + "Numerator value set: 3\n", + "Denominator value set: 4\n", + "Numerator value retrieved: 3\n", + "Denominator value retrieved: 4\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.4, page no:216" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class SimpleClass:\n", + " def __init__(self):\n", + " self.__i=None #private member\n", + " \n", + " def __init__(self):\n", + " self.__i=500 #constructor\n", + " \n", + " def SetData(self,d):\n", + " self.__i=d\n", + " \n", + " def GetData(self):\n", + " return self.__i\n", + " \n", + "#Initializing\n", + "s1=SimpleClass()\n", + "s2=SimpleClass()\n", + "print \"s1 has data: \",s1.GetData()\n", + "print \"s2 has data: \",s2.GetData()\n", + "s1.SetData(1000) #function call\n", + "s2.SetData(2000)\n", + "print \"s1 has data: \",s1.GetData(),' '\n", + "print \"s2 has data: \", s2.GetData(),' '\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "s1 has data: 500\n", + "s2 has data: 500\n", + "s1 has data: 1000 \n", + "s2 has data: 2000 \n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.5, page no:217" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class SimpleClass:\n", + " __IntegerData=None\n", + " def __init__(self,data=None):\n", + " if data==None:\n", + " self.__IntegerData=500 #default constructor\n", + " else:\n", + " self.__IntegerData=data #parameterised constructor\n", + " \n", + " def SetData(self,d):\n", + " self.__IntegerData=d\n", + " \n", + " def GetData(self):\n", + " return self.__IntegerData\n", + " \n", + "#Initializing\n", + "s1=SimpleClass()\n", + "s2=SimpleClass()\n", + "s3=SimpleClass(400)\n", + "s4=SimpleClass(600)\n", + "print \"s1 has data: \",s1.GetData(),' '\n", + "print \"s2 has data: \",s2.GetData(),' '\n", + "print \"s3 has data: \",s3.GetData(),' '\n", + "print \"s4 has data: \",s4.GetData(),' '\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "s1 has data: 500 \n", + "s2 has data: 500 \n", + "s3 has data: 400 \n", + "s4 has data: 600 \n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.6, page no:218" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction: \n", + " def_init_num=None #private members\n", + " def_init_denom=None #private members\n", + " def __init__(self,a=0,b=1):\n", + " self.__num=a\n", + " self.__denom=b\n", + " print \"Numerator set inside constructor\",' ',n\n", + " print \"Denominator set inside constructor\",' ',d\n", + " \n", + " def SetValue(self,a,b):\n", + " self.__num=a\n", + " self.__denom=b\n", + " def GetValue(self,a,b):\n", + " a= self.__num\n", + " b= self.__denom\n", + " return a,b\n", + " \n", + " def __del__(self): #destructor\n", + " pass\n", + " \n", + "n=input(\"Enter n: \") #user input\n", + "d=input(\"Enter d: \") #user input\n", + "print \"Please enter value of numerator and denominator: \",' ',n,d\n", + "f=Fraction(n,d)\n", + "f.GetValue(n,d)\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter n: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter d: 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter value of numerator and denominator: 3 4\n", + "Numerator set inside constructor 3\n", + "Denominator set inside constructor 4\n", + "Numerator value retrieved: 3\n", + "Denominator value retrieved: 4\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.7, page no:221" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction:\n", + " \n", + " def __init__(self,a=0,b=1): #constructor\n", + " self.__num=a\n", + " self.__denom=b\n", + " print \"Numerator set inside constructor: \",self.__num\n", + " print \"Denominator set inside constructor: \",self.__denom \n", + " \n", + " def GetValue(self,a,b):\n", + " return self.__num,self.__denom\n", + "n=input(\"Enter n: \") #user input\n", + "d=input(\"Enter d: \") #user input\n", + "print \"Please enter the value of the numerator and denominator: \",n,d\n", + "f=Fraction(n,d)\n", + "n,d=f.GetValue(n,d)\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d\n", + "n=input(\"Please enter the value of numerator only: \")\n", + "f1=Fraction(n)\n", + "n,d=f1.GetValue(n,d)\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d\n", + "print 'ok..now I will create a fraction-no input please'\n", + "f2=Fraction()\n", + "n,d=f2.GetValue(n,d)\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter n: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter d: 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter the value of the numerator and denominator: 3 4\n", + "Numerator set inside constructor: 3\n", + "Denominator set inside constructor: 4\n", + "Numerator value retrieved: 3\n", + "Denominator value retrieved: 4\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter the value of numerator only: 3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Numerator set inside constructor: 3\n", + "Denominator set inside constructor: 1\n", + "Numerator value retrieved: 3\n", + "Denominator value retrieved: 1\n", + "ok..now I will create a fraction-no input please\n", + "Numerator set inside constructor: 0\n", + "Denominator set inside constructor: 1\n", + "Numerator value retrieved: 0\n", + "Denominator value retrieved: 1\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.8, page no:223" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction:\n", + " def_init_num=None #private members\n", + " def_init_denom=None #private members\n", + " \n", + " def __init__(self,anotherFraction=None): \n", + " if anotherFraction==None: #normal constructor\n", + " self.__num=anotherFraction\n", + " self.__denom=anotherFraction\n", + " else: #copy constructor\n", + " self.__num=anotherFraction.self.__num\n", + " self.__denom=anotherFraction.self.__denom\n", + " \n", + " \n", + " #public functions\n", + " def SetValue(self,a,b):\n", + " self.__num=a\n", + " self.__denom=b#refer to IntegerData\n", + " def GetValue(self,a,b):\n", + " a=self.__num\n", + " b=self.__denom\n", + " return a,b\n", + " \n", + "f=Fraction()\n", + "n=input(\"Enter n: \") #user input\n", + "d=input(\"Enter d: \") #user input\n", + "print \"enter the numerator and denominator: \", ' ',n,d\n", + "\n", + "f.SetValue(n,d) #call function SetValue\n", + "print \"Numerator value set: \", ' ',n\n", + "print \"Denominator value set: \", ' ',d\n", + "f.GetValue(n,d) #call function GetData\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d\n", + "print \"Now a second clone copy is being created: \",''\n", + "f1=f\n", + "f1.GetValue(n,d)\n", + "print \"Clone's numerator value retrieved: \", ' ',n\n", + "print \"Clone's denominator value retrieved: \", ' ',d" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter n: 5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter d: 6\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "enter the numerator and denominator: 5 6\n", + "Numerator value set: 5\n", + "Denominator value set: 6\n", + "Numerator value retrieved: 5\n", + "Denominator value retrieved: 6\n", + "Now a second clone copy is being created: \n", + "Clone's numerator value retrieved: 5\n", + "Clone's denominator value retrieved: 6\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.9, page no:229" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def MyNewHandler():\n", + " print \"Sorry operator new failed to allocate memory\"\n", + " exit(0)\n", + " \n", + "def _set_new_handler(s):\n", + " s()\n", + "#In python there is no in-built _set_new_handler function, so i made this function and passed MyNewHandler function as a parameters\n", + "_set_new_handler(MyNewHandler)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Sorry operator new failed to allocate memory\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.10, page no:230" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from ctypes import * \n", + "class Fraction: \n", + " def __init__(self,a=0,b=1): \n", + " if isinstance(a,int): \n", + " c = c_int(a) \n", + " d = c_int(b) \n", + " self.__num = pointer(c) \n", + " self.__denom = pointer(d) \n", + " print 'constructor sets numerator = ', self.__num[0] , ', denominator = ', self.__denom[0] \n", + " else:\n", + " c=c_int(a.__num[0])\n", + " d = c_int(a.__denom[0])\n", + " self.__num = pointer(c) \n", + " self.__denom = pointer(d)\n", + " print 'copy constructor sets numerator = ', self.__num[0] , ', denominator = ', self.__denom[0] \n", + " \n", + " def __del__(self): \n", + " print 'destructor deallocates numerator = ', self.__num[0] , ', denominator = ', self.__denom[0] \n", + " \n", + "n = input(\"Please enter values of numerator: \") \n", + "d = input(\"Please enter values of denominator: \") \n", + "f = Fraction(n,d) \n", + "print 'Please enter another set of ' \n", + "n = input(\"numerator: \") \n", + "d = input(\"denominator: \") \n", + "print 'Creating fraction *pf : ' \n", + "pf = Fraction(n,d) \n", + "print 'Now a clone copy (f2) created from *pf: ' \n", + "f2 = Fraction(pf)\n", + "print 'Now another clone copy (*pf2) created from f:' \n", + "pf2 = Fraction(f) \n", + "print '*pf2 is being destroyed:' \n", + "del pf2\n", + "print '*pf is being destroyed:' \n", + "del pf \n", + "print 'now objects f2 and f automatically destroyed : '" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter values of numerator: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter values of denominator: 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "constructor sets numerator = 3 , denominator = 4\n", + "Please enter another set of \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "numerator: 5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "denominator: 6\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Creating fraction *pf : \n", + "constructor sets numerator = 5 , denominator = 6\n", + "Now a clone copy (f2) created from *pf: \n", + "copy constructor sets numerator = 5 , denominator = 6\n", + "Now another clone copy (*pf2) created from f:\n", + "copy constructor sets numerator = 3 , denominator = 4\n", + "*pf2 is being destroyed:\n", + "*pf is being destroyed:\n", + "now objects f2 and f automatically destroyed : \n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.11, page no:234" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def Memfail(self,s):\n", + " print \"Sorry Unable to allocate memory\"\n", + " sys.exit(0)\n", + "\n", + "MAX_SIZE = 60 + 1\n", + "\n", + "MAX_SIZE=[[0 for col in range(MAX_SIZE)]for row in range(MAX_SIZE)]\n", + "nChar=0\n", + "chArr=\"Hello\"\n", + "\n", + "print \"Please input a string( 60 characters max.): \",chArr\n", + "\n", + "nChar=len(chArr)+1\n", + "szStr=chArr\n", + "print \"required memory space for\",nChar,\n", + "print \"characters\"\n", + "chArr=szStr #string copy\n", + "szStr=chArr\n", + "print \"String copied in allocated space: \",szStr\n", + "print \"Memory space dellocated\"\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please input a string( 60 characters max.): Hello\n", + "required memory space for 6 characters\n", + "String copied in allocated space: Hello\n", + "Memory space dellocated\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.12, page no:236" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction:\n", + " \n", + " def __init__(self,a=0,b=1): #constructor\n", + " self.__num=a\n", + " self.__denom=b\n", + " \n", + " def __del__(self): #destructor\n", + " pass\n", + " \n", + " def GetValue(self,a,b):\n", + " a=self.__num\n", + " b=self.__denom\n", + " return self.__num,self.__denom\n", + "n=4\n", + "d=5\n", + "f=Fraction(4,5)\n", + "n,d=f.GetValue(n,d)" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.13, page no:239" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class X:\n", + " __sa=20 #initialising static member\n", + " a = None\n", + " def __init__(self):\n", + " self.a=None #public member\n", + " def f(self,b):\n", + " a=30\n", + " print \"Global a= \",b\n", + " print \"Local a= \",a\n", + " print \"Nonstatic member a= \",self.a\n", + " print \"static member sa= \",self.__sa\n", + "\n", + "a=10\n", + "\n", + "aXobj=X()\n", + "aXobj.a=40\n", + "aXobj.f(a)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Global a= 10\n", + "Local a= 30\n", + "Nonstatic member a= 40\n", + "static member sa= 20\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [], + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/sample_notebooks/NitamoniDas/Chapter_8_Data_Abstraction_through_Classes_and_User-Defined_Data_Types.ipynb b/sample_notebooks/NitamoniDas/Chapter_8_Data_Abstraction_through_Classes_and_User-Defined_Data_Types.ipynb new file mode 100755 index 00000000..a7c3a90a --- /dev/null +++ b/sample_notebooks/NitamoniDas/Chapter_8_Data_Abstraction_through_Classes_and_User-Defined_Data_Types.ipynb @@ -0,0 +1,809 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:da354bb89e19562e65167a0447837d31b8126b13ef9a65d132ea4ce7ac74a2e3" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 8: Data Abstraction through Classes and User-Defined Data Types" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.1, page no:208" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Super:\n", + " def __init__(self):\n", + " self.__IntegerData=None #private member\n", + " #public functions\n", + " def SetData(self,i):\n", + " self.__IntegerData=i #refer to IntegerData\n", + " def ShowData(self):\n", + " print \"Data is \",self.__IntegerData,' '\n", + "\n", + "ob1=Super()\n", + "ob2=Super()\n", + "ob1.SetData(1000)\n", + "ob2.SetData(2000)\n", + "ob1.ShowData()\n", + "ob2.ShowData()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Data is 1000 \n", + "Data is 2000 \n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.2, page no:211" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class X:\n", + " def __init__(self):\n", + " self.a=None #private members\n", + " self.b=None #private members\n", + " \n", + "#no structure type present in python \n", + "x = X()\n", + "\n", + "x.a=0\n", + "x.b=1\n", + "print \"x.a=\",x.a,\",x.b=\",x.b" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "x.a= 0 ,x.b= 1\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.3, page no:214" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction:\n", + " \n", + " def SetValue(self,a,b): #public functions\n", + " self.__num=a \n", + " self.__denom=b \n", + " def GetValue(self,a,b):\n", + " a=self.__num\n", + " b=self.__denom\n", + " \n", + "f=Fraction()\n", + "n=input(\"Enter n: \") #user input\n", + "d=input(\"Enter d: \") #user input\n", + "print \"enter the numerator and denominator: \", ' ',n,d\n", + "\n", + "f.SetValue(n,d) #call function SetValue\n", + "print \"Numerator value set: \", ' ',n\n", + "print \"Denominator value set: \", ' ',d\n", + "f.GetValue(n,d) #call function GetData\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter n: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter d: 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "enter the numerator and denominator: 3 4\n", + "Numerator value set: 3\n", + "Denominator value set: 4\n", + "Numerator value retrieved: 3\n", + "Denominator value retrieved: 4\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.4, page no:216" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class SimpleClass:\n", + " def __init__(self):\n", + " self.__i=None #private member\n", + " \n", + " def __init__(self):\n", + " self.__i=500 #constructor\n", + " \n", + " def SetData(self,d):\n", + " self.__i=d\n", + " \n", + " def GetData(self):\n", + " return self.__i\n", + " \n", + "#Initializing\n", + "s1=SimpleClass()\n", + "s2=SimpleClass()\n", + "print \"s1 has data: \",s1.GetData()\n", + "print \"s2 has data: \",s2.GetData()\n", + "s1.SetData(1000) #function call\n", + "s2.SetData(2000)\n", + "print \"s1 has data: \",s1.GetData(),' '\n", + "print \"s2 has data: \", s2.GetData(),' '\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "s1 has data: 500\n", + "s2 has data: 500\n", + "s1 has data: 1000 \n", + "s2 has data: 2000 \n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.5, page no:217" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class SimpleClass:\n", + " __IntegerData=None\n", + " def __init__(self,data=None):\n", + " if data==None:\n", + " self.__IntegerData=500 #default constructor\n", + " else:\n", + " self.__IntegerData=data #parameterised constructor\n", + " \n", + " def SetData(self,d):\n", + " self.__IntegerData=d\n", + " \n", + " def GetData(self):\n", + " return self.__IntegerData\n", + " \n", + "#Initializing\n", + "s1=SimpleClass()\n", + "s2=SimpleClass()\n", + "s3=SimpleClass(400)\n", + "s4=SimpleClass(600)\n", + "print \"s1 has data: \",s1.GetData(),' '\n", + "print \"s2 has data: \",s2.GetData(),' '\n", + "print \"s3 has data: \",s3.GetData(),' '\n", + "print \"s4 has data: \",s4.GetData(),' '\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "s1 has data: 500 \n", + "s2 has data: 500 \n", + "s3 has data: 400 \n", + "s4 has data: 600 \n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.6, page no:218" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction: \n", + " def_init_num=None #private members\n", + " def_init_denom=None #private members\n", + " def __init__(self,a=0,b=1):\n", + " self.__num=a\n", + " self.__denom=b\n", + " print \"Numerator set inside constructor\",' ',n\n", + " print \"Denominator set inside constructor\",' ',d\n", + " \n", + " def SetValue(self,a,b):\n", + " self.__num=a\n", + " self.__denom=b\n", + " def GetValue(self,a,b):\n", + " a= self.__num\n", + " b= self.__denom\n", + " return a,b\n", + " \n", + " def __del__(self): #destructor\n", + " pass\n", + " \n", + "n=input(\"Enter n: \") #user input\n", + "d=input(\"Enter d: \") #user input\n", + "print \"Please enter value of numerator and denominator: \",' ',n,d\n", + "f=Fraction(n,d)\n", + "f.GetValue(n,d)\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter n: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter d: 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter value of numerator and denominator: 3 4\n", + "Numerator set inside constructor 3\n", + "Denominator set inside constructor 4\n", + "Numerator value retrieved: 3\n", + "Denominator value retrieved: 4\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.7, page no:221" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction:\n", + " \n", + " def __init__(self,a=0,b=1): #constructor\n", + " self.__num=a\n", + " self.__denom=b\n", + " print \"Numerator set inside constructor: \",self.__num\n", + " print \"Denominator set inside constructor: \",self.__denom \n", + " \n", + " def GetValue(self,a,b):\n", + " return self.__num,self.__denom\n", + "n=input(\"Enter n: \") #user input\n", + "d=input(\"Enter d: \") #user input\n", + "print \"Please enter the value of the numerator and denominator: \",n,d\n", + "f=Fraction(n,d)\n", + "n,d=f.GetValue(n,d)\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d\n", + "n=input(\"Please enter the value of numerator only: \")\n", + "f1=Fraction(n)\n", + "n,d=f1.GetValue(n,d)\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d\n", + "print 'ok..now I will create a fraction-no input please'\n", + "f2=Fraction()\n", + "n,d=f2.GetValue(n,d)\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter n: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter d: 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter the value of the numerator and denominator: 3 4\n", + "Numerator set inside constructor: 3\n", + "Denominator set inside constructor: 4\n", + "Numerator value retrieved: 3\n", + "Denominator value retrieved: 4\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter the value of numerator only: 3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Numerator set inside constructor: 3\n", + "Denominator set inside constructor: 1\n", + "Numerator value retrieved: 3\n", + "Denominator value retrieved: 1\n", + "ok..now I will create a fraction-no input please\n", + "Numerator set inside constructor: 0\n", + "Denominator set inside constructor: 1\n", + "Numerator value retrieved: 0\n", + "Denominator value retrieved: 1\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.8, page no:223" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction:\n", + " def_init_num=None #private members\n", + " def_init_denom=None #private members\n", + " \n", + " def __init__(self,anotherFraction=None): \n", + " if anotherFraction==None: #normal constructor\n", + " self.__num=anotherFraction\n", + " self.__denom=anotherFraction\n", + " else: #copy constructor\n", + " self.__num=anotherFraction.self.__num\n", + " self.__denom=anotherFraction.self.__denom\n", + " \n", + " \n", + " #public functions\n", + " def SetValue(self,a,b):\n", + " self.__num=a\n", + " self.__denom=b#refer to IntegerData\n", + " def GetValue(self,a,b):\n", + " a=self.__num\n", + " b=self.__denom\n", + " return a,b\n", + " \n", + "f=Fraction()\n", + "n=input(\"Enter n: \") #user input\n", + "d=input(\"Enter d: \") #user input\n", + "print \"enter the numerator and denominator: \", ' ',n,d\n", + "\n", + "f.SetValue(n,d) #call function SetValue\n", + "print \"Numerator value set: \", ' ',n\n", + "print \"Denominator value set: \", ' ',d\n", + "f.GetValue(n,d) #call function GetData\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d\n", + "print \"Now a second clone copy is being created: \",''\n", + "f1=f\n", + "f1.GetValue(n,d)\n", + "print \"Clone's numerator value retrieved: \", ' ',n\n", + "print \"Clone's denominator value retrieved: \", ' ',d" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter n: 5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter d: 6\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "enter the numerator and denominator: 5 6\n", + "Numerator value set: 5\n", + "Denominator value set: 6\n", + "Numerator value retrieved: 5\n", + "Denominator value retrieved: 6\n", + "Now a second clone copy is being created: \n", + "Clone's numerator value retrieved: 5\n", + "Clone's denominator value retrieved: 6\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.9, page no:229" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def MyNewHandler():\n", + " print \"Sorry operator new failed to allocate memory\"\n", + " exit(0)\n", + " \n", + "def _set_new_handler(s):\n", + " s()\n", + "#In python there is no in-built _set_new_handler function, so i made this function and passed MyNewHandler function as a parameters\n", + "_set_new_handler(MyNewHandler)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Sorry operator new failed to allocate memory\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.10, page no:230" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from ctypes import * \n", + "class Fraction: \n", + " def __init__(self,a=0,b=1): \n", + " if isinstance(a,int): \n", + " c = c_int(a) \n", + " d = c_int(b) \n", + " self.__num = pointer(c) \n", + " self.__denom = pointer(d) \n", + " print 'constructor sets numerator = ', self.__num[0] , ', denominator = ', self.__denom[0] \n", + " else:\n", + " c=c_int(a.__num[0])\n", + " d = c_int(a.__denom[0])\n", + " self.__num = pointer(c) \n", + " self.__denom = pointer(d)\n", + " print 'copy constructor sets numerator = ', self.__num[0] , ', denominator = ', self.__denom[0] \n", + " \n", + " def __del__(self): \n", + " print 'destructor deallocates numerator = ', self.__num[0] , ', denominator = ', self.__denom[0] \n", + " \n", + "n = input(\"Please enter values of numerator: \") \n", + "d = input(\"Please enter values of denominator: \") \n", + "f = Fraction(n,d) \n", + "print 'Please enter another set of ' \n", + "n = input(\"numerator: \") \n", + "d = input(\"denominator: \") \n", + "print 'Creating fraction *pf : ' \n", + "pf = Fraction(n,d) \n", + "print 'Now a clone copy (f2) created from *pf: ' \n", + "f2 = Fraction(pf)\n", + "print 'Now another clone copy (*pf2) created from f:' \n", + "pf2 = Fraction(f) \n", + "print '*pf2 is being destroyed:' \n", + "del pf2\n", + "print '*pf is being destroyed:' \n", + "del pf \n", + "print 'now objects f2 and f automatically destroyed : '" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter values of numerator: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter values of denominator: 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "constructor sets numerator = 3 , denominator = 4\n", + "Please enter another set of \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "numerator: 5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "denominator: 6\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Creating fraction *pf : \n", + "constructor sets numerator = 5 , denominator = 6\n", + "Now a clone copy (f2) created from *pf: \n", + "copy constructor sets numerator = 5 , denominator = 6\n", + "Now another clone copy (*pf2) created from f:\n", + "copy constructor sets numerator = 3 , denominator = 4\n", + "*pf2 is being destroyed:\n", + "*pf is being destroyed:\n", + "now objects f2 and f automatically destroyed : \n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.11, page no:234" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def Memfail(self,s):\n", + " print \"Sorry Unable to allocate memory\"\n", + " sys.exit(0)\n", + "\n", + "MAX_SIZE = 60 + 1\n", + "\n", + "MAX_SIZE=[[0 for col in range(MAX_SIZE)]for row in range(MAX_SIZE)]\n", + "nChar=0\n", + "chArr=\"Hello\"\n", + "\n", + "print \"Please input a string( 60 characters max.): \",chArr\n", + "\n", + "nChar=len(chArr)+1\n", + "szStr=chArr\n", + "print \"required memory space for\",nChar,\n", + "print \"characters\"\n", + "chArr=szStr #string copy\n", + "szStr=chArr\n", + "print \"String copied in allocated space: \",szStr\n", + "print \"Memory space dellocated\"\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please input a string( 60 characters max.): Hello\n", + "required memory space for 6 characters\n", + "String copied in allocated space: Hello\n", + "Memory space dellocated\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.12, page no:236" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction:\n", + " \n", + " def __init__(self,a=0,b=1): #constructor\n", + " self.__num=a\n", + " self.__denom=b\n", + " \n", + " def __del__(self): #destructor\n", + " pass\n", + " \n", + " def GetValue(self,a,b):\n", + " a=self.__num\n", + " b=self.__denom\n", + " return self.__num,self.__denom\n", + "n=4\n", + "d=5\n", + "f=Fraction(4,5)\n", + "n,d=f.GetValue(n,d)" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.13, page no:239" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class X:\n", + " __sa=20 #initialising static member\n", + " a = None\n", + " def __init__(self):\n", + " self.a=None #public member\n", + " def f(self,b):\n", + " a=30\n", + " print \"Global a= \",b\n", + " print \"Local a= \",a\n", + " print \"Nonstatic member a= \",self.a\n", + " print \"static member sa= \",self.__sa\n", + "\n", + "a=10\n", + "\n", + "aXobj=X()\n", + "aXobj.a=40\n", + "aXobj.f(a)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Global a= 10\n", + "Local a= 30\n", + "Nonstatic member a= 40\n", + "static member sa= 20\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [], + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/sample_notebooks/NitamoniDas/Chapter_8_Data_Abstraction_through_Classes_and_User_Defined_Data_Types.ipynb b/sample_notebooks/NitamoniDas/Chapter_8_Data_Abstraction_through_Classes_and_User_Defined_Data_Types.ipynb new file mode 100755 index 00000000..7eb86cba --- /dev/null +++ b/sample_notebooks/NitamoniDas/Chapter_8_Data_Abstraction_through_Classes_and_User_Defined_Data_Types.ipynb @@ -0,0 +1,809 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:cceaf517d4ab644356e85522addd8c655c7a27bbc143387c58a9d5656be364df" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.1, page no: 208" + ] + }, + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 8: Data Abstraction through Classes and User-Defined Data Types " + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Super:\n", + " def __init__(self):\n", + " self.__IntegerData=None #private member\n", + " #public functions\n", + " def SetData(self,i):\n", + " self.__IntegerData=i #refer to IntegerData\n", + " def ShowData(self):\n", + " print \"Data is \",self.__IntegerData,' '\n", + "\n", + "ob1=Super()\n", + "ob2=Super()\n", + "ob1.SetData(1000)\n", + "ob2.SetData(2000)\n", + "ob1.ShowData()\n", + "ob2.ShowData()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Data is 1000 \n", + "Data is 2000 \n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.2, page no:211" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class X:\n", + " def __init__(self):\n", + " self.a=None #private members\n", + " self.b=None #private members\n", + " \n", + "#no structure type present in python \n", + "x = X()\n", + "\n", + "x.a=0\n", + "x.b=1\n", + "print \"x.a=\",x.a,\",x.b=\",x.b" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "x.a= 0 ,x.b= 1\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.3, page no:214" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction:\n", + " \n", + " def SetValue(self,a,b): #public functions\n", + " self.__num=a \n", + " self.__denom=b \n", + " def GetValue(self,a,b):\n", + " a=self.__num\n", + " b=self.__denom\n", + " \n", + "f=Fraction()\n", + "n=input(\"Enter n: \") #user input\n", + "d=input(\"Enter d: \") #user input\n", + "print \"enter the numerator and denominator: \", ' ',n,d\n", + "\n", + "f.SetValue(n,d) #call function SetValue\n", + "print \"Numerator value set: \", ' ',n\n", + "print \"Denominator value set: \", ' ',d\n", + "f.GetValue(n,d) #call function GetData\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter n: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter d: 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "enter the numerator and denominator: 3 4\n", + "Numerator value set: 3\n", + "Denominator value set: 4\n", + "Numerator value retrieved: 3\n", + "Denominator value retrieved: 4\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.4, page no:216" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class SimpleClass:\n", + " def __init__(self):\n", + " self.__i=None #private member\n", + " \n", + " def __init__(self):\n", + " self.__i=500 #constructor\n", + " \n", + " def SetData(self,d):\n", + " self.__i=d\n", + " \n", + " def GetData(self):\n", + " return self.__i\n", + " \n", + "#Initializing\n", + "s1=SimpleClass()\n", + "s2=SimpleClass()\n", + "print \"s1 has data: \",s1.GetData()\n", + "print \"s2 has data: \",s2.GetData()\n", + "s1.SetData(1000) #function call\n", + "s2.SetData(2000)\n", + "print \"s1 has data: \",s1.GetData(),' '\n", + "print \"s2 has data: \", s2.GetData(),' '\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "s1 has data: 500\n", + "s2 has data: 500\n", + "s1 has data: 1000 \n", + "s2 has data: 2000 \n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.5, page no:217" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class SimpleClass:\n", + " __IntegerData=None\n", + " def __init__(self,data=None):\n", + " if data==None:\n", + " self.__IntegerData=500 #default constructor\n", + " else:\n", + " self.__IntegerData=data #parameterised constructor\n", + " \n", + " def SetData(self,d):\n", + " self.__IntegerData=d\n", + " \n", + " def GetData(self):\n", + " return self.__IntegerData\n", + " \n", + "#Initializing\n", + "s1=SimpleClass()\n", + "s2=SimpleClass()\n", + "s3=SimpleClass(400)\n", + "s4=SimpleClass(600)\n", + "print \"s1 has data: \",s1.GetData(),' '\n", + "print \"s2 has data: \",s2.GetData(),' '\n", + "print \"s3 has data: \",s3.GetData(),' '\n", + "print \"s4 has data: \",s4.GetData(),' '\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "s1 has data: 500 \n", + "s2 has data: 500 \n", + "s3 has data: 400 \n", + "s4 has data: 600 \n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.6, page no:218" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction: \n", + " def_init_num=None #private members\n", + " def_init_denom=None #private members\n", + " def __init__(self,a=0,b=1):\n", + " self.__num=a\n", + " self.__denom=b\n", + " print \"Numerator set inside constructor\",' ',n\n", + " print \"Denominator set inside constructor\",' ',d\n", + " \n", + " def SetValue(self,a,b):\n", + " self.__num=a\n", + " self.__denom=b\n", + " def GetValue(self,a,b):\n", + " a= self.__num\n", + " b= self.__denom\n", + " return a,b\n", + " \n", + " def __del__(self): #destructor\n", + " pass\n", + " \n", + "n=input(\"Enter n: \") #user input\n", + "d=input(\"Enter d: \") #user input\n", + "print \"Please enter value of numerator and denominator: \",' ',n,d\n", + "f=Fraction(n,d)\n", + "f.GetValue(n,d)\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter n: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter d: 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter value of numerator and denominator: 3 4\n", + "Numerator set inside constructor 3\n", + "Denominator set inside constructor 4\n", + "Numerator value retrieved: 3\n", + "Denominator value retrieved: 4\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.7, page no:221" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction:\n", + " \n", + " def __init__(self,a=0,b=1): #constructor\n", + " self.__num=a\n", + " self.__denom=b\n", + " print \"Numerator set inside constructor: \",self.__num\n", + " print \"Denominator set inside constructor: \",self.__denom \n", + " \n", + " def GetValue(self,a,b):\n", + " return self.__num,self.__denom\n", + "n=input(\"Enter n: \") #user input\n", + "d=input(\"Enter d: \") #user input\n", + "print \"Please enter the value of the numerator and denominator: \",n,d\n", + "f=Fraction(n,d)\n", + "n,d=f.GetValue(n,d)\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d\n", + "n=input(\"Please enter the value of numerator only: \")\n", + "f1=Fraction(n)\n", + "n,d=f1.GetValue(n,d)\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d\n", + "print 'ok..now I will create a fraction-no input please'\n", + "f2=Fraction()\n", + "n,d=f2.GetValue(n,d)\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter n: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter d: 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter the value of the numerator and denominator: 3 4\n", + "Numerator set inside constructor: 3\n", + "Denominator set inside constructor: 4\n", + "Numerator value retrieved: 3\n", + "Denominator value retrieved: 4\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter the value of numerator only: 3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Numerator set inside constructor: 3\n", + "Denominator set inside constructor: 1\n", + "Numerator value retrieved: 3\n", + "Denominator value retrieved: 1\n", + "ok..now I will create a fraction-no input please\n", + "Numerator set inside constructor: 0\n", + "Denominator set inside constructor: 1\n", + "Numerator value retrieved: 0\n", + "Denominator value retrieved: 1\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.8, page no:223" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction:\n", + " def_init_num=None #private members\n", + " def_init_denom=None #private members\n", + " \n", + " def __init__(self,anotherFraction=None): \n", + " if anotherFraction==None: #normal constructor\n", + " self.__num=anotherFraction\n", + " self.__denom=anotherFraction\n", + " else: #copy constructor\n", + " self.__num=anotherFraction.self.__num\n", + " self.__denom=anotherFraction.self.__denom\n", + " \n", + " \n", + " #public functions\n", + " def SetValue(self,a,b):\n", + " self.__num=a\n", + " self.__denom=b#refer to IntegerData\n", + " def GetValue(self,a,b):\n", + " a=self.__num\n", + " b=self.__denom\n", + " return a,b\n", + " \n", + "f=Fraction()\n", + "n=input(\"Enter n: \") #user input\n", + "d=input(\"Enter d: \") #user input\n", + "print \"enter the numerator and denominator: \", ' ',n,d\n", + "\n", + "f.SetValue(n,d) #call function SetValue\n", + "print \"Numerator value set: \", ' ',n\n", + "print \"Denominator value set: \", ' ',d\n", + "f.GetValue(n,d) #call function GetData\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d\n", + "print \"Now a second clone copy is being created: \",''\n", + "f1=f\n", + "f1.GetValue(n,d)\n", + "print \"Clone's numerator value retrieved: \", ' ',n\n", + "print \"Clone's denominator value retrieved: \", ' ',d" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter n: 5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter d: 6\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "enter the numerator and denominator: 5 6\n", + "Numerator value set: 5\n", + "Denominator value set: 6\n", + "Numerator value retrieved: 5\n", + "Denominator value retrieved: 6\n", + "Now a second clone copy is being created: \n", + "Clone's numerator value retrieved: 5\n", + "Clone's denominator value retrieved: 6\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.9, page no:229" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def MyNewHandler():\n", + " print \"Sorry operator new failed to allocate memory\"\n", + " exit(0)\n", + " \n", + "def _set_new_handler(s):\n", + " s()\n", + "#In python there is no in-built _set_new_handler function, so i made this function and passed MyNewHandler function as a parameters\n", + "_set_new_handler(MyNewHandler)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Sorry operator new failed to allocate memory\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.10, page no:230" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from ctypes import * \n", + "class Fraction: \n", + " def __init__(self,a=0,b=1): \n", + " if isinstance(a,int): \n", + " c = c_int(a) \n", + " d = c_int(b) \n", + " self.__num = pointer(c) \n", + " self.__denom = pointer(d) \n", + " print 'constructor sets numerator = ', self.__num[0] , ', denominator = ', self.__denom[0] \n", + " else:\n", + " c=c_int(a.__num[0])\n", + " d = c_int(a.__denom[0])\n", + " self.__num = pointer(c) \n", + " self.__denom = pointer(d)\n", + " print 'copy constructor sets numerator = ', self.__num[0] , ', denominator = ', self.__denom[0] \n", + " \n", + " def __del__(self): \n", + " print 'destructor deallocates numerator = ', self.__num[0] , ', denominator = ', self.__denom[0] \n", + " \n", + "n = input(\"Please enter values of numerator: \") \n", + "d = input(\"Please enter values of denominator: \") \n", + "f = Fraction(n,d) \n", + "print 'Please enter another set of ' \n", + "n = input(\"numerator: \") \n", + "d = input(\"denominator: \") \n", + "print 'Creating fraction *pf : ' \n", + "pf = Fraction(n,d) \n", + "print 'Now a clone copy (f2) created from *pf: ' \n", + "f2 = Fraction(pf)\n", + "print 'Now another clone copy (*pf2) created from f:' \n", + "pf2 = Fraction(f) \n", + "print '*pf2 is being destroyed:' \n", + "del pf2\n", + "print '*pf is being destroyed:' \n", + "del pf \n", + "print 'now objects f2 and f automatically destroyed : '" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter values of numerator: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter values of denominator: 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "constructor sets numerator = 3 , denominator = 4\n", + "Please enter another set of \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "numerator: 5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "denominator: 6\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Creating fraction *pf : \n", + "constructor sets numerator = 5 , denominator = 6\n", + "Now a clone copy (f2) created from *pf: \n", + "copy constructor sets numerator = 5 , denominator = 6\n", + "Now another clone copy (*pf2) created from f:\n", + "copy constructor sets numerator = 3 , denominator = 4\n", + "*pf2 is being destroyed:\n", + "*pf is being destroyed:\n", + "now objects f2 and f automatically destroyed : \n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.11, page no:234" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def Memfail(self,s):\n", + " print \"Sorry Unable to allocate memory\"\n", + " sys.exit(0)\n", + "\n", + "MAX_SIZE = 60 + 1\n", + "\n", + "MAX_SIZE=[[0 for col in range(MAX_SIZE)]for row in range(MAX_SIZE)]\n", + "nChar=0\n", + "chArr=\"Hello\"\n", + "\n", + "print \"Please input a string( 60 characters max.): \",chArr\n", + "\n", + "nChar=len(chArr)+1\n", + "szStr=chArr\n", + "print \"required memory space for\",nChar,\n", + "print \"characters\"\n", + "chArr=szStr #string copy\n", + "szStr=chArr\n", + "print \"String copied in allocated space: \",szStr\n", + "print \"Memory space dellocated\"\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please input a string( 60 characters max.): Hello\n", + "required memory space for 6 characters\n", + "String copied in allocated space: Hello\n", + "Memory space dellocated\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.12, page no:236" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction:\n", + " \n", + " def __init__(self,a=0,b=1): #constructor\n", + " self.__num=a\n", + " self.__denom=b\n", + " \n", + " def __del__(self): #destructor\n", + " pass\n", + " \n", + " def GetValue(self,a,b):\n", + " a=self.__num\n", + " b=self.__denom\n", + " return self.__num,self.__denom\n", + "n=4\n", + "d=5\n", + "f=Fraction(4,5)\n", + "n,d=f.GetValue(n,d)" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.13, page no:239" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class X:\n", + " __sa=20 #initialising static member\n", + " a = None\n", + " def __init__(self):\n", + " self.a=None #public member\n", + " def f(self,b):\n", + " a=30\n", + " print \"Global a= \",b\n", + " print \"Local a= \",a\n", + " print \"Nonstatic member a= \",self.a\n", + " print \"static member sa= \",self.__sa\n", + "\n", + "a=10\n", + "\n", + "aXobj=X()\n", + "aXobj.a=40\n", + "aXobj.f(a)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Global a= 10\n", + "Local a= 30\n", + "Nonstatic member a= 40\n", + "static member sa= 20\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [], + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/sample_notebooks/NitamoniDas/Chapter_8_Data_Abstraction_through_Classes_and_User_Defined_Data_Types_1.ipynb b/sample_notebooks/NitamoniDas/Chapter_8_Data_Abstraction_through_Classes_and_User_Defined_Data_Types_1.ipynb new file mode 100755 index 00000000..7eb86cba --- /dev/null +++ b/sample_notebooks/NitamoniDas/Chapter_8_Data_Abstraction_through_Classes_and_User_Defined_Data_Types_1.ipynb @@ -0,0 +1,809 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:cceaf517d4ab644356e85522addd8c655c7a27bbc143387c58a9d5656be364df" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.1, page no: 208" + ] + }, + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 8: Data Abstraction through Classes and User-Defined Data Types " + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Super:\n", + " def __init__(self):\n", + " self.__IntegerData=None #private member\n", + " #public functions\n", + " def SetData(self,i):\n", + " self.__IntegerData=i #refer to IntegerData\n", + " def ShowData(self):\n", + " print \"Data is \",self.__IntegerData,' '\n", + "\n", + "ob1=Super()\n", + "ob2=Super()\n", + "ob1.SetData(1000)\n", + "ob2.SetData(2000)\n", + "ob1.ShowData()\n", + "ob2.ShowData()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Data is 1000 \n", + "Data is 2000 \n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.2, page no:211" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class X:\n", + " def __init__(self):\n", + " self.a=None #private members\n", + " self.b=None #private members\n", + " \n", + "#no structure type present in python \n", + "x = X()\n", + "\n", + "x.a=0\n", + "x.b=1\n", + "print \"x.a=\",x.a,\",x.b=\",x.b" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "x.a= 0 ,x.b= 1\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.3, page no:214" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction:\n", + " \n", + " def SetValue(self,a,b): #public functions\n", + " self.__num=a \n", + " self.__denom=b \n", + " def GetValue(self,a,b):\n", + " a=self.__num\n", + " b=self.__denom\n", + " \n", + "f=Fraction()\n", + "n=input(\"Enter n: \") #user input\n", + "d=input(\"Enter d: \") #user input\n", + "print \"enter the numerator and denominator: \", ' ',n,d\n", + "\n", + "f.SetValue(n,d) #call function SetValue\n", + "print \"Numerator value set: \", ' ',n\n", + "print \"Denominator value set: \", ' ',d\n", + "f.GetValue(n,d) #call function GetData\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter n: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter d: 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "enter the numerator and denominator: 3 4\n", + "Numerator value set: 3\n", + "Denominator value set: 4\n", + "Numerator value retrieved: 3\n", + "Denominator value retrieved: 4\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.4, page no:216" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class SimpleClass:\n", + " def __init__(self):\n", + " self.__i=None #private member\n", + " \n", + " def __init__(self):\n", + " self.__i=500 #constructor\n", + " \n", + " def SetData(self,d):\n", + " self.__i=d\n", + " \n", + " def GetData(self):\n", + " return self.__i\n", + " \n", + "#Initializing\n", + "s1=SimpleClass()\n", + "s2=SimpleClass()\n", + "print \"s1 has data: \",s1.GetData()\n", + "print \"s2 has data: \",s2.GetData()\n", + "s1.SetData(1000) #function call\n", + "s2.SetData(2000)\n", + "print \"s1 has data: \",s1.GetData(),' '\n", + "print \"s2 has data: \", s2.GetData(),' '\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "s1 has data: 500\n", + "s2 has data: 500\n", + "s1 has data: 1000 \n", + "s2 has data: 2000 \n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.5, page no:217" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class SimpleClass:\n", + " __IntegerData=None\n", + " def __init__(self,data=None):\n", + " if data==None:\n", + " self.__IntegerData=500 #default constructor\n", + " else:\n", + " self.__IntegerData=data #parameterised constructor\n", + " \n", + " def SetData(self,d):\n", + " self.__IntegerData=d\n", + " \n", + " def GetData(self):\n", + " return self.__IntegerData\n", + " \n", + "#Initializing\n", + "s1=SimpleClass()\n", + "s2=SimpleClass()\n", + "s3=SimpleClass(400)\n", + "s4=SimpleClass(600)\n", + "print \"s1 has data: \",s1.GetData(),' '\n", + "print \"s2 has data: \",s2.GetData(),' '\n", + "print \"s3 has data: \",s3.GetData(),' '\n", + "print \"s4 has data: \",s4.GetData(),' '\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "s1 has data: 500 \n", + "s2 has data: 500 \n", + "s3 has data: 400 \n", + "s4 has data: 600 \n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.6, page no:218" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction: \n", + " def_init_num=None #private members\n", + " def_init_denom=None #private members\n", + " def __init__(self,a=0,b=1):\n", + " self.__num=a\n", + " self.__denom=b\n", + " print \"Numerator set inside constructor\",' ',n\n", + " print \"Denominator set inside constructor\",' ',d\n", + " \n", + " def SetValue(self,a,b):\n", + " self.__num=a\n", + " self.__denom=b\n", + " def GetValue(self,a,b):\n", + " a= self.__num\n", + " b= self.__denom\n", + " return a,b\n", + " \n", + " def __del__(self): #destructor\n", + " pass\n", + " \n", + "n=input(\"Enter n: \") #user input\n", + "d=input(\"Enter d: \") #user input\n", + "print \"Please enter value of numerator and denominator: \",' ',n,d\n", + "f=Fraction(n,d)\n", + "f.GetValue(n,d)\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter n: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter d: 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter value of numerator and denominator: 3 4\n", + "Numerator set inside constructor 3\n", + "Denominator set inside constructor 4\n", + "Numerator value retrieved: 3\n", + "Denominator value retrieved: 4\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.7, page no:221" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction:\n", + " \n", + " def __init__(self,a=0,b=1): #constructor\n", + " self.__num=a\n", + " self.__denom=b\n", + " print \"Numerator set inside constructor: \",self.__num\n", + " print \"Denominator set inside constructor: \",self.__denom \n", + " \n", + " def GetValue(self,a,b):\n", + " return self.__num,self.__denom\n", + "n=input(\"Enter n: \") #user input\n", + "d=input(\"Enter d: \") #user input\n", + "print \"Please enter the value of the numerator and denominator: \",n,d\n", + "f=Fraction(n,d)\n", + "n,d=f.GetValue(n,d)\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d\n", + "n=input(\"Please enter the value of numerator only: \")\n", + "f1=Fraction(n)\n", + "n,d=f1.GetValue(n,d)\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d\n", + "print 'ok..now I will create a fraction-no input please'\n", + "f2=Fraction()\n", + "n,d=f2.GetValue(n,d)\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter n: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter d: 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter the value of the numerator and denominator: 3 4\n", + "Numerator set inside constructor: 3\n", + "Denominator set inside constructor: 4\n", + "Numerator value retrieved: 3\n", + "Denominator value retrieved: 4\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter the value of numerator only: 3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Numerator set inside constructor: 3\n", + "Denominator set inside constructor: 1\n", + "Numerator value retrieved: 3\n", + "Denominator value retrieved: 1\n", + "ok..now I will create a fraction-no input please\n", + "Numerator set inside constructor: 0\n", + "Denominator set inside constructor: 1\n", + "Numerator value retrieved: 0\n", + "Denominator value retrieved: 1\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.8, page no:223" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction:\n", + " def_init_num=None #private members\n", + " def_init_denom=None #private members\n", + " \n", + " def __init__(self,anotherFraction=None): \n", + " if anotherFraction==None: #normal constructor\n", + " self.__num=anotherFraction\n", + " self.__denom=anotherFraction\n", + " else: #copy constructor\n", + " self.__num=anotherFraction.self.__num\n", + " self.__denom=anotherFraction.self.__denom\n", + " \n", + " \n", + " #public functions\n", + " def SetValue(self,a,b):\n", + " self.__num=a\n", + " self.__denom=b#refer to IntegerData\n", + " def GetValue(self,a,b):\n", + " a=self.__num\n", + " b=self.__denom\n", + " return a,b\n", + " \n", + "f=Fraction()\n", + "n=input(\"Enter n: \") #user input\n", + "d=input(\"Enter d: \") #user input\n", + "print \"enter the numerator and denominator: \", ' ',n,d\n", + "\n", + "f.SetValue(n,d) #call function SetValue\n", + "print \"Numerator value set: \", ' ',n\n", + "print \"Denominator value set: \", ' ',d\n", + "f.GetValue(n,d) #call function GetData\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d\n", + "print \"Now a second clone copy is being created: \",''\n", + "f1=f\n", + "f1.GetValue(n,d)\n", + "print \"Clone's numerator value retrieved: \", ' ',n\n", + "print \"Clone's denominator value retrieved: \", ' ',d" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter n: 5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter d: 6\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "enter the numerator and denominator: 5 6\n", + "Numerator value set: 5\n", + "Denominator value set: 6\n", + "Numerator value retrieved: 5\n", + "Denominator value retrieved: 6\n", + "Now a second clone copy is being created: \n", + "Clone's numerator value retrieved: 5\n", + "Clone's denominator value retrieved: 6\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.9, page no:229" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def MyNewHandler():\n", + " print \"Sorry operator new failed to allocate memory\"\n", + " exit(0)\n", + " \n", + "def _set_new_handler(s):\n", + " s()\n", + "#In python there is no in-built _set_new_handler function, so i made this function and passed MyNewHandler function as a parameters\n", + "_set_new_handler(MyNewHandler)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Sorry operator new failed to allocate memory\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.10, page no:230" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from ctypes import * \n", + "class Fraction: \n", + " def __init__(self,a=0,b=1): \n", + " if isinstance(a,int): \n", + " c = c_int(a) \n", + " d = c_int(b) \n", + " self.__num = pointer(c) \n", + " self.__denom = pointer(d) \n", + " print 'constructor sets numerator = ', self.__num[0] , ', denominator = ', self.__denom[0] \n", + " else:\n", + " c=c_int(a.__num[0])\n", + " d = c_int(a.__denom[0])\n", + " self.__num = pointer(c) \n", + " self.__denom = pointer(d)\n", + " print 'copy constructor sets numerator = ', self.__num[0] , ', denominator = ', self.__denom[0] \n", + " \n", + " def __del__(self): \n", + " print 'destructor deallocates numerator = ', self.__num[0] , ', denominator = ', self.__denom[0] \n", + " \n", + "n = input(\"Please enter values of numerator: \") \n", + "d = input(\"Please enter values of denominator: \") \n", + "f = Fraction(n,d) \n", + "print 'Please enter another set of ' \n", + "n = input(\"numerator: \") \n", + "d = input(\"denominator: \") \n", + "print 'Creating fraction *pf : ' \n", + "pf = Fraction(n,d) \n", + "print 'Now a clone copy (f2) created from *pf: ' \n", + "f2 = Fraction(pf)\n", + "print 'Now another clone copy (*pf2) created from f:' \n", + "pf2 = Fraction(f) \n", + "print '*pf2 is being destroyed:' \n", + "del pf2\n", + "print '*pf is being destroyed:' \n", + "del pf \n", + "print 'now objects f2 and f automatically destroyed : '" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter values of numerator: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter values of denominator: 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "constructor sets numerator = 3 , denominator = 4\n", + "Please enter another set of \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "numerator: 5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "denominator: 6\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Creating fraction *pf : \n", + "constructor sets numerator = 5 , denominator = 6\n", + "Now a clone copy (f2) created from *pf: \n", + "copy constructor sets numerator = 5 , denominator = 6\n", + "Now another clone copy (*pf2) created from f:\n", + "copy constructor sets numerator = 3 , denominator = 4\n", + "*pf2 is being destroyed:\n", + "*pf is being destroyed:\n", + "now objects f2 and f automatically destroyed : \n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.11, page no:234" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def Memfail(self,s):\n", + " print \"Sorry Unable to allocate memory\"\n", + " sys.exit(0)\n", + "\n", + "MAX_SIZE = 60 + 1\n", + "\n", + "MAX_SIZE=[[0 for col in range(MAX_SIZE)]for row in range(MAX_SIZE)]\n", + "nChar=0\n", + "chArr=\"Hello\"\n", + "\n", + "print \"Please input a string( 60 characters max.): \",chArr\n", + "\n", + "nChar=len(chArr)+1\n", + "szStr=chArr\n", + "print \"required memory space for\",nChar,\n", + "print \"characters\"\n", + "chArr=szStr #string copy\n", + "szStr=chArr\n", + "print \"String copied in allocated space: \",szStr\n", + "print \"Memory space dellocated\"\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please input a string( 60 characters max.): Hello\n", + "required memory space for 6 characters\n", + "String copied in allocated space: Hello\n", + "Memory space dellocated\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.12, page no:236" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction:\n", + " \n", + " def __init__(self,a=0,b=1): #constructor\n", + " self.__num=a\n", + " self.__denom=b\n", + " \n", + " def __del__(self): #destructor\n", + " pass\n", + " \n", + " def GetValue(self,a,b):\n", + " a=self.__num\n", + " b=self.__denom\n", + " return self.__num,self.__denom\n", + "n=4\n", + "d=5\n", + "f=Fraction(4,5)\n", + "n,d=f.GetValue(n,d)" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.13, page no:239" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class X:\n", + " __sa=20 #initialising static member\n", + " a = None\n", + " def __init__(self):\n", + " self.a=None #public member\n", + " def f(self,b):\n", + " a=30\n", + " print \"Global a= \",b\n", + " print \"Local a= \",a\n", + " print \"Nonstatic member a= \",self.a\n", + " print \"static member sa= \",self.__sa\n", + "\n", + "a=10\n", + "\n", + "aXobj=X()\n", + "aXobj.a=40\n", + "aXobj.f(a)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Global a= 10\n", + "Local a= 30\n", + "Nonstatic member a= 40\n", + "static member sa= 20\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [], + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/sample_notebooks/NitamoniDas/Chapter_8_Data_Abstraction_through_Classes_and_User_Defined_Data_Types_2.ipynb b/sample_notebooks/NitamoniDas/Chapter_8_Data_Abstraction_through_Classes_and_User_Defined_Data_Types_2.ipynb new file mode 100755 index 00000000..7eb86cba --- /dev/null +++ b/sample_notebooks/NitamoniDas/Chapter_8_Data_Abstraction_through_Classes_and_User_Defined_Data_Types_2.ipynb @@ -0,0 +1,809 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:cceaf517d4ab644356e85522addd8c655c7a27bbc143387c58a9d5656be364df" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.1, page no: 208" + ] + }, + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 8: Data Abstraction through Classes and User-Defined Data Types " + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Super:\n", + " def __init__(self):\n", + " self.__IntegerData=None #private member\n", + " #public functions\n", + " def SetData(self,i):\n", + " self.__IntegerData=i #refer to IntegerData\n", + " def ShowData(self):\n", + " print \"Data is \",self.__IntegerData,' '\n", + "\n", + "ob1=Super()\n", + "ob2=Super()\n", + "ob1.SetData(1000)\n", + "ob2.SetData(2000)\n", + "ob1.ShowData()\n", + "ob2.ShowData()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Data is 1000 \n", + "Data is 2000 \n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.2, page no:211" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class X:\n", + " def __init__(self):\n", + " self.a=None #private members\n", + " self.b=None #private members\n", + " \n", + "#no structure type present in python \n", + "x = X()\n", + "\n", + "x.a=0\n", + "x.b=1\n", + "print \"x.a=\",x.a,\",x.b=\",x.b" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "x.a= 0 ,x.b= 1\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.3, page no:214" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction:\n", + " \n", + " def SetValue(self,a,b): #public functions\n", + " self.__num=a \n", + " self.__denom=b \n", + " def GetValue(self,a,b):\n", + " a=self.__num\n", + " b=self.__denom\n", + " \n", + "f=Fraction()\n", + "n=input(\"Enter n: \") #user input\n", + "d=input(\"Enter d: \") #user input\n", + "print \"enter the numerator and denominator: \", ' ',n,d\n", + "\n", + "f.SetValue(n,d) #call function SetValue\n", + "print \"Numerator value set: \", ' ',n\n", + "print \"Denominator value set: \", ' ',d\n", + "f.GetValue(n,d) #call function GetData\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter n: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter d: 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "enter the numerator and denominator: 3 4\n", + "Numerator value set: 3\n", + "Denominator value set: 4\n", + "Numerator value retrieved: 3\n", + "Denominator value retrieved: 4\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.4, page no:216" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class SimpleClass:\n", + " def __init__(self):\n", + " self.__i=None #private member\n", + " \n", + " def __init__(self):\n", + " self.__i=500 #constructor\n", + " \n", + " def SetData(self,d):\n", + " self.__i=d\n", + " \n", + " def GetData(self):\n", + " return self.__i\n", + " \n", + "#Initializing\n", + "s1=SimpleClass()\n", + "s2=SimpleClass()\n", + "print \"s1 has data: \",s1.GetData()\n", + "print \"s2 has data: \",s2.GetData()\n", + "s1.SetData(1000) #function call\n", + "s2.SetData(2000)\n", + "print \"s1 has data: \",s1.GetData(),' '\n", + "print \"s2 has data: \", s2.GetData(),' '\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "s1 has data: 500\n", + "s2 has data: 500\n", + "s1 has data: 1000 \n", + "s2 has data: 2000 \n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.5, page no:217" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class SimpleClass:\n", + " __IntegerData=None\n", + " def __init__(self,data=None):\n", + " if data==None:\n", + " self.__IntegerData=500 #default constructor\n", + " else:\n", + " self.__IntegerData=data #parameterised constructor\n", + " \n", + " def SetData(self,d):\n", + " self.__IntegerData=d\n", + " \n", + " def GetData(self):\n", + " return self.__IntegerData\n", + " \n", + "#Initializing\n", + "s1=SimpleClass()\n", + "s2=SimpleClass()\n", + "s3=SimpleClass(400)\n", + "s4=SimpleClass(600)\n", + "print \"s1 has data: \",s1.GetData(),' '\n", + "print \"s2 has data: \",s2.GetData(),' '\n", + "print \"s3 has data: \",s3.GetData(),' '\n", + "print \"s4 has data: \",s4.GetData(),' '\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "s1 has data: 500 \n", + "s2 has data: 500 \n", + "s3 has data: 400 \n", + "s4 has data: 600 \n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.6, page no:218" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction: \n", + " def_init_num=None #private members\n", + " def_init_denom=None #private members\n", + " def __init__(self,a=0,b=1):\n", + " self.__num=a\n", + " self.__denom=b\n", + " print \"Numerator set inside constructor\",' ',n\n", + " print \"Denominator set inside constructor\",' ',d\n", + " \n", + " def SetValue(self,a,b):\n", + " self.__num=a\n", + " self.__denom=b\n", + " def GetValue(self,a,b):\n", + " a= self.__num\n", + " b= self.__denom\n", + " return a,b\n", + " \n", + " def __del__(self): #destructor\n", + " pass\n", + " \n", + "n=input(\"Enter n: \") #user input\n", + "d=input(\"Enter d: \") #user input\n", + "print \"Please enter value of numerator and denominator: \",' ',n,d\n", + "f=Fraction(n,d)\n", + "f.GetValue(n,d)\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter n: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter d: 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter value of numerator and denominator: 3 4\n", + "Numerator set inside constructor 3\n", + "Denominator set inside constructor 4\n", + "Numerator value retrieved: 3\n", + "Denominator value retrieved: 4\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.7, page no:221" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction:\n", + " \n", + " def __init__(self,a=0,b=1): #constructor\n", + " self.__num=a\n", + " self.__denom=b\n", + " print \"Numerator set inside constructor: \",self.__num\n", + " print \"Denominator set inside constructor: \",self.__denom \n", + " \n", + " def GetValue(self,a,b):\n", + " return self.__num,self.__denom\n", + "n=input(\"Enter n: \") #user input\n", + "d=input(\"Enter d: \") #user input\n", + "print \"Please enter the value of the numerator and denominator: \",n,d\n", + "f=Fraction(n,d)\n", + "n,d=f.GetValue(n,d)\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d\n", + "n=input(\"Please enter the value of numerator only: \")\n", + "f1=Fraction(n)\n", + "n,d=f1.GetValue(n,d)\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d\n", + "print 'ok..now I will create a fraction-no input please'\n", + "f2=Fraction()\n", + "n,d=f2.GetValue(n,d)\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter n: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter d: 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter the value of the numerator and denominator: 3 4\n", + "Numerator set inside constructor: 3\n", + "Denominator set inside constructor: 4\n", + "Numerator value retrieved: 3\n", + "Denominator value retrieved: 4\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter the value of numerator only: 3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Numerator set inside constructor: 3\n", + "Denominator set inside constructor: 1\n", + "Numerator value retrieved: 3\n", + "Denominator value retrieved: 1\n", + "ok..now I will create a fraction-no input please\n", + "Numerator set inside constructor: 0\n", + "Denominator set inside constructor: 1\n", + "Numerator value retrieved: 0\n", + "Denominator value retrieved: 1\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.8, page no:223" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction:\n", + " def_init_num=None #private members\n", + " def_init_denom=None #private members\n", + " \n", + " def __init__(self,anotherFraction=None): \n", + " if anotherFraction==None: #normal constructor\n", + " self.__num=anotherFraction\n", + " self.__denom=anotherFraction\n", + " else: #copy constructor\n", + " self.__num=anotherFraction.self.__num\n", + " self.__denom=anotherFraction.self.__denom\n", + " \n", + " \n", + " #public functions\n", + " def SetValue(self,a,b):\n", + " self.__num=a\n", + " self.__denom=b#refer to IntegerData\n", + " def GetValue(self,a,b):\n", + " a=self.__num\n", + " b=self.__denom\n", + " return a,b\n", + " \n", + "f=Fraction()\n", + "n=input(\"Enter n: \") #user input\n", + "d=input(\"Enter d: \") #user input\n", + "print \"enter the numerator and denominator: \", ' ',n,d\n", + "\n", + "f.SetValue(n,d) #call function SetValue\n", + "print \"Numerator value set: \", ' ',n\n", + "print \"Denominator value set: \", ' ',d\n", + "f.GetValue(n,d) #call function GetData\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d\n", + "print \"Now a second clone copy is being created: \",''\n", + "f1=f\n", + "f1.GetValue(n,d)\n", + "print \"Clone's numerator value retrieved: \", ' ',n\n", + "print \"Clone's denominator value retrieved: \", ' ',d" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter n: 5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter d: 6\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "enter the numerator and denominator: 5 6\n", + "Numerator value set: 5\n", + "Denominator value set: 6\n", + "Numerator value retrieved: 5\n", + "Denominator value retrieved: 6\n", + "Now a second clone copy is being created: \n", + "Clone's numerator value retrieved: 5\n", + "Clone's denominator value retrieved: 6\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.9, page no:229" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def MyNewHandler():\n", + " print \"Sorry operator new failed to allocate memory\"\n", + " exit(0)\n", + " \n", + "def _set_new_handler(s):\n", + " s()\n", + "#In python there is no in-built _set_new_handler function, so i made this function and passed MyNewHandler function as a parameters\n", + "_set_new_handler(MyNewHandler)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Sorry operator new failed to allocate memory\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.10, page no:230" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from ctypes import * \n", + "class Fraction: \n", + " def __init__(self,a=0,b=1): \n", + " if isinstance(a,int): \n", + " c = c_int(a) \n", + " d = c_int(b) \n", + " self.__num = pointer(c) \n", + " self.__denom = pointer(d) \n", + " print 'constructor sets numerator = ', self.__num[0] , ', denominator = ', self.__denom[0] \n", + " else:\n", + " c=c_int(a.__num[0])\n", + " d = c_int(a.__denom[0])\n", + " self.__num = pointer(c) \n", + " self.__denom = pointer(d)\n", + " print 'copy constructor sets numerator = ', self.__num[0] , ', denominator = ', self.__denom[0] \n", + " \n", + " def __del__(self): \n", + " print 'destructor deallocates numerator = ', self.__num[0] , ', denominator = ', self.__denom[0] \n", + " \n", + "n = input(\"Please enter values of numerator: \") \n", + "d = input(\"Please enter values of denominator: \") \n", + "f = Fraction(n,d) \n", + "print 'Please enter another set of ' \n", + "n = input(\"numerator: \") \n", + "d = input(\"denominator: \") \n", + "print 'Creating fraction *pf : ' \n", + "pf = Fraction(n,d) \n", + "print 'Now a clone copy (f2) created from *pf: ' \n", + "f2 = Fraction(pf)\n", + "print 'Now another clone copy (*pf2) created from f:' \n", + "pf2 = Fraction(f) \n", + "print '*pf2 is being destroyed:' \n", + "del pf2\n", + "print '*pf is being destroyed:' \n", + "del pf \n", + "print 'now objects f2 and f automatically destroyed : '" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter values of numerator: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter values of denominator: 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "constructor sets numerator = 3 , denominator = 4\n", + "Please enter another set of \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "numerator: 5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "denominator: 6\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Creating fraction *pf : \n", + "constructor sets numerator = 5 , denominator = 6\n", + "Now a clone copy (f2) created from *pf: \n", + "copy constructor sets numerator = 5 , denominator = 6\n", + "Now another clone copy (*pf2) created from f:\n", + "copy constructor sets numerator = 3 , denominator = 4\n", + "*pf2 is being destroyed:\n", + "*pf is being destroyed:\n", + "now objects f2 and f automatically destroyed : \n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.11, page no:234" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def Memfail(self,s):\n", + " print \"Sorry Unable to allocate memory\"\n", + " sys.exit(0)\n", + "\n", + "MAX_SIZE = 60 + 1\n", + "\n", + "MAX_SIZE=[[0 for col in range(MAX_SIZE)]for row in range(MAX_SIZE)]\n", + "nChar=0\n", + "chArr=\"Hello\"\n", + "\n", + "print \"Please input a string( 60 characters max.): \",chArr\n", + "\n", + "nChar=len(chArr)+1\n", + "szStr=chArr\n", + "print \"required memory space for\",nChar,\n", + "print \"characters\"\n", + "chArr=szStr #string copy\n", + "szStr=chArr\n", + "print \"String copied in allocated space: \",szStr\n", + "print \"Memory space dellocated\"\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please input a string( 60 characters max.): Hello\n", + "required memory space for 6 characters\n", + "String copied in allocated space: Hello\n", + "Memory space dellocated\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.12, page no:236" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction:\n", + " \n", + " def __init__(self,a=0,b=1): #constructor\n", + " self.__num=a\n", + " self.__denom=b\n", + " \n", + " def __del__(self): #destructor\n", + " pass\n", + " \n", + " def GetValue(self,a,b):\n", + " a=self.__num\n", + " b=self.__denom\n", + " return self.__num,self.__denom\n", + "n=4\n", + "d=5\n", + "f=Fraction(4,5)\n", + "n,d=f.GetValue(n,d)" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.13, page no:239" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class X:\n", + " __sa=20 #initialising static member\n", + " a = None\n", + " def __init__(self):\n", + " self.a=None #public member\n", + " def f(self,b):\n", + " a=30\n", + " print \"Global a= \",b\n", + " print \"Local a= \",a\n", + " print \"Nonstatic member a= \",self.a\n", + " print \"static member sa= \",self.__sa\n", + "\n", + "a=10\n", + "\n", + "aXobj=X()\n", + "aXobj.a=40\n", + "aXobj.f(a)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Global a= 10\n", + "Local a= 30\n", + "Nonstatic member a= 40\n", + "static member sa= 20\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [], + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/sample_notebooks/NitamoniDas/Chapter_8_Data_Abstraction_through_Classes_and_User_Defined_Data_Types_3.ipynb b/sample_notebooks/NitamoniDas/Chapter_8_Data_Abstraction_through_Classes_and_User_Defined_Data_Types_3.ipynb new file mode 100755 index 00000000..7eb86cba --- /dev/null +++ b/sample_notebooks/NitamoniDas/Chapter_8_Data_Abstraction_through_Classes_and_User_Defined_Data_Types_3.ipynb @@ -0,0 +1,809 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:cceaf517d4ab644356e85522addd8c655c7a27bbc143387c58a9d5656be364df" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.1, page no: 208" + ] + }, + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 8: Data Abstraction through Classes and User-Defined Data Types " + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Super:\n", + " def __init__(self):\n", + " self.__IntegerData=None #private member\n", + " #public functions\n", + " def SetData(self,i):\n", + " self.__IntegerData=i #refer to IntegerData\n", + " def ShowData(self):\n", + " print \"Data is \",self.__IntegerData,' '\n", + "\n", + "ob1=Super()\n", + "ob2=Super()\n", + "ob1.SetData(1000)\n", + "ob2.SetData(2000)\n", + "ob1.ShowData()\n", + "ob2.ShowData()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Data is 1000 \n", + "Data is 2000 \n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.2, page no:211" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class X:\n", + " def __init__(self):\n", + " self.a=None #private members\n", + " self.b=None #private members\n", + " \n", + "#no structure type present in python \n", + "x = X()\n", + "\n", + "x.a=0\n", + "x.b=1\n", + "print \"x.a=\",x.a,\",x.b=\",x.b" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "x.a= 0 ,x.b= 1\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.3, page no:214" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction:\n", + " \n", + " def SetValue(self,a,b): #public functions\n", + " self.__num=a \n", + " self.__denom=b \n", + " def GetValue(self,a,b):\n", + " a=self.__num\n", + " b=self.__denom\n", + " \n", + "f=Fraction()\n", + "n=input(\"Enter n: \") #user input\n", + "d=input(\"Enter d: \") #user input\n", + "print \"enter the numerator and denominator: \", ' ',n,d\n", + "\n", + "f.SetValue(n,d) #call function SetValue\n", + "print \"Numerator value set: \", ' ',n\n", + "print \"Denominator value set: \", ' ',d\n", + "f.GetValue(n,d) #call function GetData\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter n: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter d: 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "enter the numerator and denominator: 3 4\n", + "Numerator value set: 3\n", + "Denominator value set: 4\n", + "Numerator value retrieved: 3\n", + "Denominator value retrieved: 4\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.4, page no:216" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class SimpleClass:\n", + " def __init__(self):\n", + " self.__i=None #private member\n", + " \n", + " def __init__(self):\n", + " self.__i=500 #constructor\n", + " \n", + " def SetData(self,d):\n", + " self.__i=d\n", + " \n", + " def GetData(self):\n", + " return self.__i\n", + " \n", + "#Initializing\n", + "s1=SimpleClass()\n", + "s2=SimpleClass()\n", + "print \"s1 has data: \",s1.GetData()\n", + "print \"s2 has data: \",s2.GetData()\n", + "s1.SetData(1000) #function call\n", + "s2.SetData(2000)\n", + "print \"s1 has data: \",s1.GetData(),' '\n", + "print \"s2 has data: \", s2.GetData(),' '\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "s1 has data: 500\n", + "s2 has data: 500\n", + "s1 has data: 1000 \n", + "s2 has data: 2000 \n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.5, page no:217" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class SimpleClass:\n", + " __IntegerData=None\n", + " def __init__(self,data=None):\n", + " if data==None:\n", + " self.__IntegerData=500 #default constructor\n", + " else:\n", + " self.__IntegerData=data #parameterised constructor\n", + " \n", + " def SetData(self,d):\n", + " self.__IntegerData=d\n", + " \n", + " def GetData(self):\n", + " return self.__IntegerData\n", + " \n", + "#Initializing\n", + "s1=SimpleClass()\n", + "s2=SimpleClass()\n", + "s3=SimpleClass(400)\n", + "s4=SimpleClass(600)\n", + "print \"s1 has data: \",s1.GetData(),' '\n", + "print \"s2 has data: \",s2.GetData(),' '\n", + "print \"s3 has data: \",s3.GetData(),' '\n", + "print \"s4 has data: \",s4.GetData(),' '\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "s1 has data: 500 \n", + "s2 has data: 500 \n", + "s3 has data: 400 \n", + "s4 has data: 600 \n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.6, page no:218" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction: \n", + " def_init_num=None #private members\n", + " def_init_denom=None #private members\n", + " def __init__(self,a=0,b=1):\n", + " self.__num=a\n", + " self.__denom=b\n", + " print \"Numerator set inside constructor\",' ',n\n", + " print \"Denominator set inside constructor\",' ',d\n", + " \n", + " def SetValue(self,a,b):\n", + " self.__num=a\n", + " self.__denom=b\n", + " def GetValue(self,a,b):\n", + " a= self.__num\n", + " b= self.__denom\n", + " return a,b\n", + " \n", + " def __del__(self): #destructor\n", + " pass\n", + " \n", + "n=input(\"Enter n: \") #user input\n", + "d=input(\"Enter d: \") #user input\n", + "print \"Please enter value of numerator and denominator: \",' ',n,d\n", + "f=Fraction(n,d)\n", + "f.GetValue(n,d)\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter n: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter d: 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter value of numerator and denominator: 3 4\n", + "Numerator set inside constructor 3\n", + "Denominator set inside constructor 4\n", + "Numerator value retrieved: 3\n", + "Denominator value retrieved: 4\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.7, page no:221" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction:\n", + " \n", + " def __init__(self,a=0,b=1): #constructor\n", + " self.__num=a\n", + " self.__denom=b\n", + " print \"Numerator set inside constructor: \",self.__num\n", + " print \"Denominator set inside constructor: \",self.__denom \n", + " \n", + " def GetValue(self,a,b):\n", + " return self.__num,self.__denom\n", + "n=input(\"Enter n: \") #user input\n", + "d=input(\"Enter d: \") #user input\n", + "print \"Please enter the value of the numerator and denominator: \",n,d\n", + "f=Fraction(n,d)\n", + "n,d=f.GetValue(n,d)\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d\n", + "n=input(\"Please enter the value of numerator only: \")\n", + "f1=Fraction(n)\n", + "n,d=f1.GetValue(n,d)\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d\n", + "print 'ok..now I will create a fraction-no input please'\n", + "f2=Fraction()\n", + "n,d=f2.GetValue(n,d)\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter n: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter d: 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter the value of the numerator and denominator: 3 4\n", + "Numerator set inside constructor: 3\n", + "Denominator set inside constructor: 4\n", + "Numerator value retrieved: 3\n", + "Denominator value retrieved: 4\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter the value of numerator only: 3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Numerator set inside constructor: 3\n", + "Denominator set inside constructor: 1\n", + "Numerator value retrieved: 3\n", + "Denominator value retrieved: 1\n", + "ok..now I will create a fraction-no input please\n", + "Numerator set inside constructor: 0\n", + "Denominator set inside constructor: 1\n", + "Numerator value retrieved: 0\n", + "Denominator value retrieved: 1\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.8, page no:223" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction:\n", + " def_init_num=None #private members\n", + " def_init_denom=None #private members\n", + " \n", + " def __init__(self,anotherFraction=None): \n", + " if anotherFraction==None: #normal constructor\n", + " self.__num=anotherFraction\n", + " self.__denom=anotherFraction\n", + " else: #copy constructor\n", + " self.__num=anotherFraction.self.__num\n", + " self.__denom=anotherFraction.self.__denom\n", + " \n", + " \n", + " #public functions\n", + " def SetValue(self,a,b):\n", + " self.__num=a\n", + " self.__denom=b#refer to IntegerData\n", + " def GetValue(self,a,b):\n", + " a=self.__num\n", + " b=self.__denom\n", + " return a,b\n", + " \n", + "f=Fraction()\n", + "n=input(\"Enter n: \") #user input\n", + "d=input(\"Enter d: \") #user input\n", + "print \"enter the numerator and denominator: \", ' ',n,d\n", + "\n", + "f.SetValue(n,d) #call function SetValue\n", + "print \"Numerator value set: \", ' ',n\n", + "print \"Denominator value set: \", ' ',d\n", + "f.GetValue(n,d) #call function GetData\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d\n", + "print \"Now a second clone copy is being created: \",''\n", + "f1=f\n", + "f1.GetValue(n,d)\n", + "print \"Clone's numerator value retrieved: \", ' ',n\n", + "print \"Clone's denominator value retrieved: \", ' ',d" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter n: 5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter d: 6\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "enter the numerator and denominator: 5 6\n", + "Numerator value set: 5\n", + "Denominator value set: 6\n", + "Numerator value retrieved: 5\n", + "Denominator value retrieved: 6\n", + "Now a second clone copy is being created: \n", + "Clone's numerator value retrieved: 5\n", + "Clone's denominator value retrieved: 6\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.9, page no:229" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def MyNewHandler():\n", + " print \"Sorry operator new failed to allocate memory\"\n", + " exit(0)\n", + " \n", + "def _set_new_handler(s):\n", + " s()\n", + "#In python there is no in-built _set_new_handler function, so i made this function and passed MyNewHandler function as a parameters\n", + "_set_new_handler(MyNewHandler)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Sorry operator new failed to allocate memory\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.10, page no:230" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from ctypes import * \n", + "class Fraction: \n", + " def __init__(self,a=0,b=1): \n", + " if isinstance(a,int): \n", + " c = c_int(a) \n", + " d = c_int(b) \n", + " self.__num = pointer(c) \n", + " self.__denom = pointer(d) \n", + " print 'constructor sets numerator = ', self.__num[0] , ', denominator = ', self.__denom[0] \n", + " else:\n", + " c=c_int(a.__num[0])\n", + " d = c_int(a.__denom[0])\n", + " self.__num = pointer(c) \n", + " self.__denom = pointer(d)\n", + " print 'copy constructor sets numerator = ', self.__num[0] , ', denominator = ', self.__denom[0] \n", + " \n", + " def __del__(self): \n", + " print 'destructor deallocates numerator = ', self.__num[0] , ', denominator = ', self.__denom[0] \n", + " \n", + "n = input(\"Please enter values of numerator: \") \n", + "d = input(\"Please enter values of denominator: \") \n", + "f = Fraction(n,d) \n", + "print 'Please enter another set of ' \n", + "n = input(\"numerator: \") \n", + "d = input(\"denominator: \") \n", + "print 'Creating fraction *pf : ' \n", + "pf = Fraction(n,d) \n", + "print 'Now a clone copy (f2) created from *pf: ' \n", + "f2 = Fraction(pf)\n", + "print 'Now another clone copy (*pf2) created from f:' \n", + "pf2 = Fraction(f) \n", + "print '*pf2 is being destroyed:' \n", + "del pf2\n", + "print '*pf is being destroyed:' \n", + "del pf \n", + "print 'now objects f2 and f automatically destroyed : '" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter values of numerator: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter values of denominator: 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "constructor sets numerator = 3 , denominator = 4\n", + "Please enter another set of \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "numerator: 5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "denominator: 6\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Creating fraction *pf : \n", + "constructor sets numerator = 5 , denominator = 6\n", + "Now a clone copy (f2) created from *pf: \n", + "copy constructor sets numerator = 5 , denominator = 6\n", + "Now another clone copy (*pf2) created from f:\n", + "copy constructor sets numerator = 3 , denominator = 4\n", + "*pf2 is being destroyed:\n", + "*pf is being destroyed:\n", + "now objects f2 and f automatically destroyed : \n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.11, page no:234" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def Memfail(self,s):\n", + " print \"Sorry Unable to allocate memory\"\n", + " sys.exit(0)\n", + "\n", + "MAX_SIZE = 60 + 1\n", + "\n", + "MAX_SIZE=[[0 for col in range(MAX_SIZE)]for row in range(MAX_SIZE)]\n", + "nChar=0\n", + "chArr=\"Hello\"\n", + "\n", + "print \"Please input a string( 60 characters max.): \",chArr\n", + "\n", + "nChar=len(chArr)+1\n", + "szStr=chArr\n", + "print \"required memory space for\",nChar,\n", + "print \"characters\"\n", + "chArr=szStr #string copy\n", + "szStr=chArr\n", + "print \"String copied in allocated space: \",szStr\n", + "print \"Memory space dellocated\"\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please input a string( 60 characters max.): Hello\n", + "required memory space for 6 characters\n", + "String copied in allocated space: Hello\n", + "Memory space dellocated\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.12, page no:236" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction:\n", + " \n", + " def __init__(self,a=0,b=1): #constructor\n", + " self.__num=a\n", + " self.__denom=b\n", + " \n", + " def __del__(self): #destructor\n", + " pass\n", + " \n", + " def GetValue(self,a,b):\n", + " a=self.__num\n", + " b=self.__denom\n", + " return self.__num,self.__denom\n", + "n=4\n", + "d=5\n", + "f=Fraction(4,5)\n", + "n,d=f.GetValue(n,d)" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.13, page no:239" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class X:\n", + " __sa=20 #initialising static member\n", + " a = None\n", + " def __init__(self):\n", + " self.a=None #public member\n", + " def f(self,b):\n", + " a=30\n", + " print \"Global a= \",b\n", + " print \"Local a= \",a\n", + " print \"Nonstatic member a= \",self.a\n", + " print \"static member sa= \",self.__sa\n", + "\n", + "a=10\n", + "\n", + "aXobj=X()\n", + "aXobj.a=40\n", + "aXobj.f(a)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Global a= 10\n", + "Local a= 30\n", + "Nonstatic member a= 40\n", + "static member sa= 20\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [], + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/sample_notebooks/NitamoniDas/Chapter_8_Data_Abstraction_through_Classes_and_User_Defined_Data_Types_4.ipynb b/sample_notebooks/NitamoniDas/Chapter_8_Data_Abstraction_through_Classes_and_User_Defined_Data_Types_4.ipynb new file mode 100755 index 00000000..7eb86cba --- /dev/null +++ b/sample_notebooks/NitamoniDas/Chapter_8_Data_Abstraction_through_Classes_and_User_Defined_Data_Types_4.ipynb @@ -0,0 +1,809 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:cceaf517d4ab644356e85522addd8c655c7a27bbc143387c58a9d5656be364df" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.1, page no: 208" + ] + }, + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 8: Data Abstraction through Classes and User-Defined Data Types " + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Super:\n", + " def __init__(self):\n", + " self.__IntegerData=None #private member\n", + " #public functions\n", + " def SetData(self,i):\n", + " self.__IntegerData=i #refer to IntegerData\n", + " def ShowData(self):\n", + " print \"Data is \",self.__IntegerData,' '\n", + "\n", + "ob1=Super()\n", + "ob2=Super()\n", + "ob1.SetData(1000)\n", + "ob2.SetData(2000)\n", + "ob1.ShowData()\n", + "ob2.ShowData()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Data is 1000 \n", + "Data is 2000 \n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.2, page no:211" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class X:\n", + " def __init__(self):\n", + " self.a=None #private members\n", + " self.b=None #private members\n", + " \n", + "#no structure type present in python \n", + "x = X()\n", + "\n", + "x.a=0\n", + "x.b=1\n", + "print \"x.a=\",x.a,\",x.b=\",x.b" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "x.a= 0 ,x.b= 1\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.3, page no:214" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction:\n", + " \n", + " def SetValue(self,a,b): #public functions\n", + " self.__num=a \n", + " self.__denom=b \n", + " def GetValue(self,a,b):\n", + " a=self.__num\n", + " b=self.__denom\n", + " \n", + "f=Fraction()\n", + "n=input(\"Enter n: \") #user input\n", + "d=input(\"Enter d: \") #user input\n", + "print \"enter the numerator and denominator: \", ' ',n,d\n", + "\n", + "f.SetValue(n,d) #call function SetValue\n", + "print \"Numerator value set: \", ' ',n\n", + "print \"Denominator value set: \", ' ',d\n", + "f.GetValue(n,d) #call function GetData\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter n: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter d: 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "enter the numerator and denominator: 3 4\n", + "Numerator value set: 3\n", + "Denominator value set: 4\n", + "Numerator value retrieved: 3\n", + "Denominator value retrieved: 4\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.4, page no:216" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class SimpleClass:\n", + " def __init__(self):\n", + " self.__i=None #private member\n", + " \n", + " def __init__(self):\n", + " self.__i=500 #constructor\n", + " \n", + " def SetData(self,d):\n", + " self.__i=d\n", + " \n", + " def GetData(self):\n", + " return self.__i\n", + " \n", + "#Initializing\n", + "s1=SimpleClass()\n", + "s2=SimpleClass()\n", + "print \"s1 has data: \",s1.GetData()\n", + "print \"s2 has data: \",s2.GetData()\n", + "s1.SetData(1000) #function call\n", + "s2.SetData(2000)\n", + "print \"s1 has data: \",s1.GetData(),' '\n", + "print \"s2 has data: \", s2.GetData(),' '\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "s1 has data: 500\n", + "s2 has data: 500\n", + "s1 has data: 1000 \n", + "s2 has data: 2000 \n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.5, page no:217" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class SimpleClass:\n", + " __IntegerData=None\n", + " def __init__(self,data=None):\n", + " if data==None:\n", + " self.__IntegerData=500 #default constructor\n", + " else:\n", + " self.__IntegerData=data #parameterised constructor\n", + " \n", + " def SetData(self,d):\n", + " self.__IntegerData=d\n", + " \n", + " def GetData(self):\n", + " return self.__IntegerData\n", + " \n", + "#Initializing\n", + "s1=SimpleClass()\n", + "s2=SimpleClass()\n", + "s3=SimpleClass(400)\n", + "s4=SimpleClass(600)\n", + "print \"s1 has data: \",s1.GetData(),' '\n", + "print \"s2 has data: \",s2.GetData(),' '\n", + "print \"s3 has data: \",s3.GetData(),' '\n", + "print \"s4 has data: \",s4.GetData(),' '\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "s1 has data: 500 \n", + "s2 has data: 500 \n", + "s3 has data: 400 \n", + "s4 has data: 600 \n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.6, page no:218" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction: \n", + " def_init_num=None #private members\n", + " def_init_denom=None #private members\n", + " def __init__(self,a=0,b=1):\n", + " self.__num=a\n", + " self.__denom=b\n", + " print \"Numerator set inside constructor\",' ',n\n", + " print \"Denominator set inside constructor\",' ',d\n", + " \n", + " def SetValue(self,a,b):\n", + " self.__num=a\n", + " self.__denom=b\n", + " def GetValue(self,a,b):\n", + " a= self.__num\n", + " b= self.__denom\n", + " return a,b\n", + " \n", + " def __del__(self): #destructor\n", + " pass\n", + " \n", + "n=input(\"Enter n: \") #user input\n", + "d=input(\"Enter d: \") #user input\n", + "print \"Please enter value of numerator and denominator: \",' ',n,d\n", + "f=Fraction(n,d)\n", + "f.GetValue(n,d)\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter n: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter d: 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter value of numerator and denominator: 3 4\n", + "Numerator set inside constructor 3\n", + "Denominator set inside constructor 4\n", + "Numerator value retrieved: 3\n", + "Denominator value retrieved: 4\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.7, page no:221" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction:\n", + " \n", + " def __init__(self,a=0,b=1): #constructor\n", + " self.__num=a\n", + " self.__denom=b\n", + " print \"Numerator set inside constructor: \",self.__num\n", + " print \"Denominator set inside constructor: \",self.__denom \n", + " \n", + " def GetValue(self,a,b):\n", + " return self.__num,self.__denom\n", + "n=input(\"Enter n: \") #user input\n", + "d=input(\"Enter d: \") #user input\n", + "print \"Please enter the value of the numerator and denominator: \",n,d\n", + "f=Fraction(n,d)\n", + "n,d=f.GetValue(n,d)\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d\n", + "n=input(\"Please enter the value of numerator only: \")\n", + "f1=Fraction(n)\n", + "n,d=f1.GetValue(n,d)\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d\n", + "print 'ok..now I will create a fraction-no input please'\n", + "f2=Fraction()\n", + "n,d=f2.GetValue(n,d)\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter n: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter d: 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter the value of the numerator and denominator: 3 4\n", + "Numerator set inside constructor: 3\n", + "Denominator set inside constructor: 4\n", + "Numerator value retrieved: 3\n", + "Denominator value retrieved: 4\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter the value of numerator only: 3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Numerator set inside constructor: 3\n", + "Denominator set inside constructor: 1\n", + "Numerator value retrieved: 3\n", + "Denominator value retrieved: 1\n", + "ok..now I will create a fraction-no input please\n", + "Numerator set inside constructor: 0\n", + "Denominator set inside constructor: 1\n", + "Numerator value retrieved: 0\n", + "Denominator value retrieved: 1\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.8, page no:223" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction:\n", + " def_init_num=None #private members\n", + " def_init_denom=None #private members\n", + " \n", + " def __init__(self,anotherFraction=None): \n", + " if anotherFraction==None: #normal constructor\n", + " self.__num=anotherFraction\n", + " self.__denom=anotherFraction\n", + " else: #copy constructor\n", + " self.__num=anotherFraction.self.__num\n", + " self.__denom=anotherFraction.self.__denom\n", + " \n", + " \n", + " #public functions\n", + " def SetValue(self,a,b):\n", + " self.__num=a\n", + " self.__denom=b#refer to IntegerData\n", + " def GetValue(self,a,b):\n", + " a=self.__num\n", + " b=self.__denom\n", + " return a,b\n", + " \n", + "f=Fraction()\n", + "n=input(\"Enter n: \") #user input\n", + "d=input(\"Enter d: \") #user input\n", + "print \"enter the numerator and denominator: \", ' ',n,d\n", + "\n", + "f.SetValue(n,d) #call function SetValue\n", + "print \"Numerator value set: \", ' ',n\n", + "print \"Denominator value set: \", ' ',d\n", + "f.GetValue(n,d) #call function GetData\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d\n", + "print \"Now a second clone copy is being created: \",''\n", + "f1=f\n", + "f1.GetValue(n,d)\n", + "print \"Clone's numerator value retrieved: \", ' ',n\n", + "print \"Clone's denominator value retrieved: \", ' ',d" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter n: 5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter d: 6\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "enter the numerator and denominator: 5 6\n", + "Numerator value set: 5\n", + "Denominator value set: 6\n", + "Numerator value retrieved: 5\n", + "Denominator value retrieved: 6\n", + "Now a second clone copy is being created: \n", + "Clone's numerator value retrieved: 5\n", + "Clone's denominator value retrieved: 6\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.9, page no:229" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def MyNewHandler():\n", + " print \"Sorry operator new failed to allocate memory\"\n", + " exit(0)\n", + " \n", + "def _set_new_handler(s):\n", + " s()\n", + "#In python there is no in-built _set_new_handler function, so i made this function and passed MyNewHandler function as a parameters\n", + "_set_new_handler(MyNewHandler)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Sorry operator new failed to allocate memory\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.10, page no:230" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from ctypes import * \n", + "class Fraction: \n", + " def __init__(self,a=0,b=1): \n", + " if isinstance(a,int): \n", + " c = c_int(a) \n", + " d = c_int(b) \n", + " self.__num = pointer(c) \n", + " self.__denom = pointer(d) \n", + " print 'constructor sets numerator = ', self.__num[0] , ', denominator = ', self.__denom[0] \n", + " else:\n", + " c=c_int(a.__num[0])\n", + " d = c_int(a.__denom[0])\n", + " self.__num = pointer(c) \n", + " self.__denom = pointer(d)\n", + " print 'copy constructor sets numerator = ', self.__num[0] , ', denominator = ', self.__denom[0] \n", + " \n", + " def __del__(self): \n", + " print 'destructor deallocates numerator = ', self.__num[0] , ', denominator = ', self.__denom[0] \n", + " \n", + "n = input(\"Please enter values of numerator: \") \n", + "d = input(\"Please enter values of denominator: \") \n", + "f = Fraction(n,d) \n", + "print 'Please enter another set of ' \n", + "n = input(\"numerator: \") \n", + "d = input(\"denominator: \") \n", + "print 'Creating fraction *pf : ' \n", + "pf = Fraction(n,d) \n", + "print 'Now a clone copy (f2) created from *pf: ' \n", + "f2 = Fraction(pf)\n", + "print 'Now another clone copy (*pf2) created from f:' \n", + "pf2 = Fraction(f) \n", + "print '*pf2 is being destroyed:' \n", + "del pf2\n", + "print '*pf is being destroyed:' \n", + "del pf \n", + "print 'now objects f2 and f automatically destroyed : '" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter values of numerator: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter values of denominator: 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "constructor sets numerator = 3 , denominator = 4\n", + "Please enter another set of \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "numerator: 5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "denominator: 6\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Creating fraction *pf : \n", + "constructor sets numerator = 5 , denominator = 6\n", + "Now a clone copy (f2) created from *pf: \n", + "copy constructor sets numerator = 5 , denominator = 6\n", + "Now another clone copy (*pf2) created from f:\n", + "copy constructor sets numerator = 3 , denominator = 4\n", + "*pf2 is being destroyed:\n", + "*pf is being destroyed:\n", + "now objects f2 and f automatically destroyed : \n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.11, page no:234" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def Memfail(self,s):\n", + " print \"Sorry Unable to allocate memory\"\n", + " sys.exit(0)\n", + "\n", + "MAX_SIZE = 60 + 1\n", + "\n", + "MAX_SIZE=[[0 for col in range(MAX_SIZE)]for row in range(MAX_SIZE)]\n", + "nChar=0\n", + "chArr=\"Hello\"\n", + "\n", + "print \"Please input a string( 60 characters max.): \",chArr\n", + "\n", + "nChar=len(chArr)+1\n", + "szStr=chArr\n", + "print \"required memory space for\",nChar,\n", + "print \"characters\"\n", + "chArr=szStr #string copy\n", + "szStr=chArr\n", + "print \"String copied in allocated space: \",szStr\n", + "print \"Memory space dellocated\"\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please input a string( 60 characters max.): Hello\n", + "required memory space for 6 characters\n", + "String copied in allocated space: Hello\n", + "Memory space dellocated\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.12, page no:236" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction:\n", + " \n", + " def __init__(self,a=0,b=1): #constructor\n", + " self.__num=a\n", + " self.__denom=b\n", + " \n", + " def __del__(self): #destructor\n", + " pass\n", + " \n", + " def GetValue(self,a,b):\n", + " a=self.__num\n", + " b=self.__denom\n", + " return self.__num,self.__denom\n", + "n=4\n", + "d=5\n", + "f=Fraction(4,5)\n", + "n,d=f.GetValue(n,d)" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.13, page no:239" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class X:\n", + " __sa=20 #initialising static member\n", + " a = None\n", + " def __init__(self):\n", + " self.a=None #public member\n", + " def f(self,b):\n", + " a=30\n", + " print \"Global a= \",b\n", + " print \"Local a= \",a\n", + " print \"Nonstatic member a= \",self.a\n", + " print \"static member sa= \",self.__sa\n", + "\n", + "a=10\n", + "\n", + "aXobj=X()\n", + "aXobj.a=40\n", + "aXobj.f(a)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Global a= 10\n", + "Local a= 30\n", + "Nonstatic member a= 40\n", + "static member sa= 20\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [], + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/sample_notebooks/NitamoniDas/Chapter_8_Data_Abstraction_through_Classes_and_User_Defined_Data_Types_5.ipynb b/sample_notebooks/NitamoniDas/Chapter_8_Data_Abstraction_through_Classes_and_User_Defined_Data_Types_5.ipynb new file mode 100755 index 00000000..7eb86cba --- /dev/null +++ b/sample_notebooks/NitamoniDas/Chapter_8_Data_Abstraction_through_Classes_and_User_Defined_Data_Types_5.ipynb @@ -0,0 +1,809 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:cceaf517d4ab644356e85522addd8c655c7a27bbc143387c58a9d5656be364df" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.1, page no: 208" + ] + }, + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 8: Data Abstraction through Classes and User-Defined Data Types " + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Super:\n", + " def __init__(self):\n", + " self.__IntegerData=None #private member\n", + " #public functions\n", + " def SetData(self,i):\n", + " self.__IntegerData=i #refer to IntegerData\n", + " def ShowData(self):\n", + " print \"Data is \",self.__IntegerData,' '\n", + "\n", + "ob1=Super()\n", + "ob2=Super()\n", + "ob1.SetData(1000)\n", + "ob2.SetData(2000)\n", + "ob1.ShowData()\n", + "ob2.ShowData()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Data is 1000 \n", + "Data is 2000 \n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.2, page no:211" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class X:\n", + " def __init__(self):\n", + " self.a=None #private members\n", + " self.b=None #private members\n", + " \n", + "#no structure type present in python \n", + "x = X()\n", + "\n", + "x.a=0\n", + "x.b=1\n", + "print \"x.a=\",x.a,\",x.b=\",x.b" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "x.a= 0 ,x.b= 1\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.3, page no:214" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction:\n", + " \n", + " def SetValue(self,a,b): #public functions\n", + " self.__num=a \n", + " self.__denom=b \n", + " def GetValue(self,a,b):\n", + " a=self.__num\n", + " b=self.__denom\n", + " \n", + "f=Fraction()\n", + "n=input(\"Enter n: \") #user input\n", + "d=input(\"Enter d: \") #user input\n", + "print \"enter the numerator and denominator: \", ' ',n,d\n", + "\n", + "f.SetValue(n,d) #call function SetValue\n", + "print \"Numerator value set: \", ' ',n\n", + "print \"Denominator value set: \", ' ',d\n", + "f.GetValue(n,d) #call function GetData\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter n: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter d: 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "enter the numerator and denominator: 3 4\n", + "Numerator value set: 3\n", + "Denominator value set: 4\n", + "Numerator value retrieved: 3\n", + "Denominator value retrieved: 4\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.4, page no:216" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class SimpleClass:\n", + " def __init__(self):\n", + " self.__i=None #private member\n", + " \n", + " def __init__(self):\n", + " self.__i=500 #constructor\n", + " \n", + " def SetData(self,d):\n", + " self.__i=d\n", + " \n", + " def GetData(self):\n", + " return self.__i\n", + " \n", + "#Initializing\n", + "s1=SimpleClass()\n", + "s2=SimpleClass()\n", + "print \"s1 has data: \",s1.GetData()\n", + "print \"s2 has data: \",s2.GetData()\n", + "s1.SetData(1000) #function call\n", + "s2.SetData(2000)\n", + "print \"s1 has data: \",s1.GetData(),' '\n", + "print \"s2 has data: \", s2.GetData(),' '\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "s1 has data: 500\n", + "s2 has data: 500\n", + "s1 has data: 1000 \n", + "s2 has data: 2000 \n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.5, page no:217" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class SimpleClass:\n", + " __IntegerData=None\n", + " def __init__(self,data=None):\n", + " if data==None:\n", + " self.__IntegerData=500 #default constructor\n", + " else:\n", + " self.__IntegerData=data #parameterised constructor\n", + " \n", + " def SetData(self,d):\n", + " self.__IntegerData=d\n", + " \n", + " def GetData(self):\n", + " return self.__IntegerData\n", + " \n", + "#Initializing\n", + "s1=SimpleClass()\n", + "s2=SimpleClass()\n", + "s3=SimpleClass(400)\n", + "s4=SimpleClass(600)\n", + "print \"s1 has data: \",s1.GetData(),' '\n", + "print \"s2 has data: \",s2.GetData(),' '\n", + "print \"s3 has data: \",s3.GetData(),' '\n", + "print \"s4 has data: \",s4.GetData(),' '\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "s1 has data: 500 \n", + "s2 has data: 500 \n", + "s3 has data: 400 \n", + "s4 has data: 600 \n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.6, page no:218" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction: \n", + " def_init_num=None #private members\n", + " def_init_denom=None #private members\n", + " def __init__(self,a=0,b=1):\n", + " self.__num=a\n", + " self.__denom=b\n", + " print \"Numerator set inside constructor\",' ',n\n", + " print \"Denominator set inside constructor\",' ',d\n", + " \n", + " def SetValue(self,a,b):\n", + " self.__num=a\n", + " self.__denom=b\n", + " def GetValue(self,a,b):\n", + " a= self.__num\n", + " b= self.__denom\n", + " return a,b\n", + " \n", + " def __del__(self): #destructor\n", + " pass\n", + " \n", + "n=input(\"Enter n: \") #user input\n", + "d=input(\"Enter d: \") #user input\n", + "print \"Please enter value of numerator and denominator: \",' ',n,d\n", + "f=Fraction(n,d)\n", + "f.GetValue(n,d)\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter n: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter d: 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter value of numerator and denominator: 3 4\n", + "Numerator set inside constructor 3\n", + "Denominator set inside constructor 4\n", + "Numerator value retrieved: 3\n", + "Denominator value retrieved: 4\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.7, page no:221" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction:\n", + " \n", + " def __init__(self,a=0,b=1): #constructor\n", + " self.__num=a\n", + " self.__denom=b\n", + " print \"Numerator set inside constructor: \",self.__num\n", + " print \"Denominator set inside constructor: \",self.__denom \n", + " \n", + " def GetValue(self,a,b):\n", + " return self.__num,self.__denom\n", + "n=input(\"Enter n: \") #user input\n", + "d=input(\"Enter d: \") #user input\n", + "print \"Please enter the value of the numerator and denominator: \",n,d\n", + "f=Fraction(n,d)\n", + "n,d=f.GetValue(n,d)\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d\n", + "n=input(\"Please enter the value of numerator only: \")\n", + "f1=Fraction(n)\n", + "n,d=f1.GetValue(n,d)\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d\n", + "print 'ok..now I will create a fraction-no input please'\n", + "f2=Fraction()\n", + "n,d=f2.GetValue(n,d)\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter n: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter d: 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter the value of the numerator and denominator: 3 4\n", + "Numerator set inside constructor: 3\n", + "Denominator set inside constructor: 4\n", + "Numerator value retrieved: 3\n", + "Denominator value retrieved: 4\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter the value of numerator only: 3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Numerator set inside constructor: 3\n", + "Denominator set inside constructor: 1\n", + "Numerator value retrieved: 3\n", + "Denominator value retrieved: 1\n", + "ok..now I will create a fraction-no input please\n", + "Numerator set inside constructor: 0\n", + "Denominator set inside constructor: 1\n", + "Numerator value retrieved: 0\n", + "Denominator value retrieved: 1\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.8, page no:223" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction:\n", + " def_init_num=None #private members\n", + " def_init_denom=None #private members\n", + " \n", + " def __init__(self,anotherFraction=None): \n", + " if anotherFraction==None: #normal constructor\n", + " self.__num=anotherFraction\n", + " self.__denom=anotherFraction\n", + " else: #copy constructor\n", + " self.__num=anotherFraction.self.__num\n", + " self.__denom=anotherFraction.self.__denom\n", + " \n", + " \n", + " #public functions\n", + " def SetValue(self,a,b):\n", + " self.__num=a\n", + " self.__denom=b#refer to IntegerData\n", + " def GetValue(self,a,b):\n", + " a=self.__num\n", + " b=self.__denom\n", + " return a,b\n", + " \n", + "f=Fraction()\n", + "n=input(\"Enter n: \") #user input\n", + "d=input(\"Enter d: \") #user input\n", + "print \"enter the numerator and denominator: \", ' ',n,d\n", + "\n", + "f.SetValue(n,d) #call function SetValue\n", + "print \"Numerator value set: \", ' ',n\n", + "print \"Denominator value set: \", ' ',d\n", + "f.GetValue(n,d) #call function GetData\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d\n", + "print \"Now a second clone copy is being created: \",''\n", + "f1=f\n", + "f1.GetValue(n,d)\n", + "print \"Clone's numerator value retrieved: \", ' ',n\n", + "print \"Clone's denominator value retrieved: \", ' ',d" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter n: 5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter d: 6\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "enter the numerator and denominator: 5 6\n", + "Numerator value set: 5\n", + "Denominator value set: 6\n", + "Numerator value retrieved: 5\n", + "Denominator value retrieved: 6\n", + "Now a second clone copy is being created: \n", + "Clone's numerator value retrieved: 5\n", + "Clone's denominator value retrieved: 6\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.9, page no:229" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def MyNewHandler():\n", + " print \"Sorry operator new failed to allocate memory\"\n", + " exit(0)\n", + " \n", + "def _set_new_handler(s):\n", + " s()\n", + "#In python there is no in-built _set_new_handler function, so i made this function and passed MyNewHandler function as a parameters\n", + "_set_new_handler(MyNewHandler)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Sorry operator new failed to allocate memory\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.10, page no:230" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from ctypes import * \n", + "class Fraction: \n", + " def __init__(self,a=0,b=1): \n", + " if isinstance(a,int): \n", + " c = c_int(a) \n", + " d = c_int(b) \n", + " self.__num = pointer(c) \n", + " self.__denom = pointer(d) \n", + " print 'constructor sets numerator = ', self.__num[0] , ', denominator = ', self.__denom[0] \n", + " else:\n", + " c=c_int(a.__num[0])\n", + " d = c_int(a.__denom[0])\n", + " self.__num = pointer(c) \n", + " self.__denom = pointer(d)\n", + " print 'copy constructor sets numerator = ', self.__num[0] , ', denominator = ', self.__denom[0] \n", + " \n", + " def __del__(self): \n", + " print 'destructor deallocates numerator = ', self.__num[0] , ', denominator = ', self.__denom[0] \n", + " \n", + "n = input(\"Please enter values of numerator: \") \n", + "d = input(\"Please enter values of denominator: \") \n", + "f = Fraction(n,d) \n", + "print 'Please enter another set of ' \n", + "n = input(\"numerator: \") \n", + "d = input(\"denominator: \") \n", + "print 'Creating fraction *pf : ' \n", + "pf = Fraction(n,d) \n", + "print 'Now a clone copy (f2) created from *pf: ' \n", + "f2 = Fraction(pf)\n", + "print 'Now another clone copy (*pf2) created from f:' \n", + "pf2 = Fraction(f) \n", + "print '*pf2 is being destroyed:' \n", + "del pf2\n", + "print '*pf is being destroyed:' \n", + "del pf \n", + "print 'now objects f2 and f automatically destroyed : '" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter values of numerator: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter values of denominator: 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "constructor sets numerator = 3 , denominator = 4\n", + "Please enter another set of \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "numerator: 5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "denominator: 6\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Creating fraction *pf : \n", + "constructor sets numerator = 5 , denominator = 6\n", + "Now a clone copy (f2) created from *pf: \n", + "copy constructor sets numerator = 5 , denominator = 6\n", + "Now another clone copy (*pf2) created from f:\n", + "copy constructor sets numerator = 3 , denominator = 4\n", + "*pf2 is being destroyed:\n", + "*pf is being destroyed:\n", + "now objects f2 and f automatically destroyed : \n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.11, page no:234" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def Memfail(self,s):\n", + " print \"Sorry Unable to allocate memory\"\n", + " sys.exit(0)\n", + "\n", + "MAX_SIZE = 60 + 1\n", + "\n", + "MAX_SIZE=[[0 for col in range(MAX_SIZE)]for row in range(MAX_SIZE)]\n", + "nChar=0\n", + "chArr=\"Hello\"\n", + "\n", + "print \"Please input a string( 60 characters max.): \",chArr\n", + "\n", + "nChar=len(chArr)+1\n", + "szStr=chArr\n", + "print \"required memory space for\",nChar,\n", + "print \"characters\"\n", + "chArr=szStr #string copy\n", + "szStr=chArr\n", + "print \"String copied in allocated space: \",szStr\n", + "print \"Memory space dellocated\"\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please input a string( 60 characters max.): Hello\n", + "required memory space for 6 characters\n", + "String copied in allocated space: Hello\n", + "Memory space dellocated\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.12, page no:236" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction:\n", + " \n", + " def __init__(self,a=0,b=1): #constructor\n", + " self.__num=a\n", + " self.__denom=b\n", + " \n", + " def __del__(self): #destructor\n", + " pass\n", + " \n", + " def GetValue(self,a,b):\n", + " a=self.__num\n", + " b=self.__denom\n", + " return self.__num,self.__denom\n", + "n=4\n", + "d=5\n", + "f=Fraction(4,5)\n", + "n,d=f.GetValue(n,d)" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.13, page no:239" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class X:\n", + " __sa=20 #initialising static member\n", + " a = None\n", + " def __init__(self):\n", + " self.a=None #public member\n", + " def f(self,b):\n", + " a=30\n", + " print \"Global a= \",b\n", + " print \"Local a= \",a\n", + " print \"Nonstatic member a= \",self.a\n", + " print \"static member sa= \",self.__sa\n", + "\n", + "a=10\n", + "\n", + "aXobj=X()\n", + "aXobj.a=40\n", + "aXobj.f(a)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Global a= 10\n", + "Local a= 30\n", + "Nonstatic member a= 40\n", + "static member sa= 20\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [], + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/sample_notebooks/NitamoniDas/Chapter_8_Data_Abstraction_through_Classes_and_User_Defined_Data_Types_6.ipynb b/sample_notebooks/NitamoniDas/Chapter_8_Data_Abstraction_through_Classes_and_User_Defined_Data_Types_6.ipynb new file mode 100755 index 00000000..7eb86cba --- /dev/null +++ b/sample_notebooks/NitamoniDas/Chapter_8_Data_Abstraction_through_Classes_and_User_Defined_Data_Types_6.ipynb @@ -0,0 +1,809 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:cceaf517d4ab644356e85522addd8c655c7a27bbc143387c58a9d5656be364df" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.1, page no: 208" + ] + }, + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 8: Data Abstraction through Classes and User-Defined Data Types " + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Super:\n", + " def __init__(self):\n", + " self.__IntegerData=None #private member\n", + " #public functions\n", + " def SetData(self,i):\n", + " self.__IntegerData=i #refer to IntegerData\n", + " def ShowData(self):\n", + " print \"Data is \",self.__IntegerData,' '\n", + "\n", + "ob1=Super()\n", + "ob2=Super()\n", + "ob1.SetData(1000)\n", + "ob2.SetData(2000)\n", + "ob1.ShowData()\n", + "ob2.ShowData()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Data is 1000 \n", + "Data is 2000 \n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.2, page no:211" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class X:\n", + " def __init__(self):\n", + " self.a=None #private members\n", + " self.b=None #private members\n", + " \n", + "#no structure type present in python \n", + "x = X()\n", + "\n", + "x.a=0\n", + "x.b=1\n", + "print \"x.a=\",x.a,\",x.b=\",x.b" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "x.a= 0 ,x.b= 1\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.3, page no:214" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction:\n", + " \n", + " def SetValue(self,a,b): #public functions\n", + " self.__num=a \n", + " self.__denom=b \n", + " def GetValue(self,a,b):\n", + " a=self.__num\n", + " b=self.__denom\n", + " \n", + "f=Fraction()\n", + "n=input(\"Enter n: \") #user input\n", + "d=input(\"Enter d: \") #user input\n", + "print \"enter the numerator and denominator: \", ' ',n,d\n", + "\n", + "f.SetValue(n,d) #call function SetValue\n", + "print \"Numerator value set: \", ' ',n\n", + "print \"Denominator value set: \", ' ',d\n", + "f.GetValue(n,d) #call function GetData\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter n: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter d: 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "enter the numerator and denominator: 3 4\n", + "Numerator value set: 3\n", + "Denominator value set: 4\n", + "Numerator value retrieved: 3\n", + "Denominator value retrieved: 4\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.4, page no:216" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class SimpleClass:\n", + " def __init__(self):\n", + " self.__i=None #private member\n", + " \n", + " def __init__(self):\n", + " self.__i=500 #constructor\n", + " \n", + " def SetData(self,d):\n", + " self.__i=d\n", + " \n", + " def GetData(self):\n", + " return self.__i\n", + " \n", + "#Initializing\n", + "s1=SimpleClass()\n", + "s2=SimpleClass()\n", + "print \"s1 has data: \",s1.GetData()\n", + "print \"s2 has data: \",s2.GetData()\n", + "s1.SetData(1000) #function call\n", + "s2.SetData(2000)\n", + "print \"s1 has data: \",s1.GetData(),' '\n", + "print \"s2 has data: \", s2.GetData(),' '\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "s1 has data: 500\n", + "s2 has data: 500\n", + "s1 has data: 1000 \n", + "s2 has data: 2000 \n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.5, page no:217" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class SimpleClass:\n", + " __IntegerData=None\n", + " def __init__(self,data=None):\n", + " if data==None:\n", + " self.__IntegerData=500 #default constructor\n", + " else:\n", + " self.__IntegerData=data #parameterised constructor\n", + " \n", + " def SetData(self,d):\n", + " self.__IntegerData=d\n", + " \n", + " def GetData(self):\n", + " return self.__IntegerData\n", + " \n", + "#Initializing\n", + "s1=SimpleClass()\n", + "s2=SimpleClass()\n", + "s3=SimpleClass(400)\n", + "s4=SimpleClass(600)\n", + "print \"s1 has data: \",s1.GetData(),' '\n", + "print \"s2 has data: \",s2.GetData(),' '\n", + "print \"s3 has data: \",s3.GetData(),' '\n", + "print \"s4 has data: \",s4.GetData(),' '\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "s1 has data: 500 \n", + "s2 has data: 500 \n", + "s3 has data: 400 \n", + "s4 has data: 600 \n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.6, page no:218" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction: \n", + " def_init_num=None #private members\n", + " def_init_denom=None #private members\n", + " def __init__(self,a=0,b=1):\n", + " self.__num=a\n", + " self.__denom=b\n", + " print \"Numerator set inside constructor\",' ',n\n", + " print \"Denominator set inside constructor\",' ',d\n", + " \n", + " def SetValue(self,a,b):\n", + " self.__num=a\n", + " self.__denom=b\n", + " def GetValue(self,a,b):\n", + " a= self.__num\n", + " b= self.__denom\n", + " return a,b\n", + " \n", + " def __del__(self): #destructor\n", + " pass\n", + " \n", + "n=input(\"Enter n: \") #user input\n", + "d=input(\"Enter d: \") #user input\n", + "print \"Please enter value of numerator and denominator: \",' ',n,d\n", + "f=Fraction(n,d)\n", + "f.GetValue(n,d)\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter n: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter d: 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter value of numerator and denominator: 3 4\n", + "Numerator set inside constructor 3\n", + "Denominator set inside constructor 4\n", + "Numerator value retrieved: 3\n", + "Denominator value retrieved: 4\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.7, page no:221" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction:\n", + " \n", + " def __init__(self,a=0,b=1): #constructor\n", + " self.__num=a\n", + " self.__denom=b\n", + " print \"Numerator set inside constructor: \",self.__num\n", + " print \"Denominator set inside constructor: \",self.__denom \n", + " \n", + " def GetValue(self,a,b):\n", + " return self.__num,self.__denom\n", + "n=input(\"Enter n: \") #user input\n", + "d=input(\"Enter d: \") #user input\n", + "print \"Please enter the value of the numerator and denominator: \",n,d\n", + "f=Fraction(n,d)\n", + "n,d=f.GetValue(n,d)\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d\n", + "n=input(\"Please enter the value of numerator only: \")\n", + "f1=Fraction(n)\n", + "n,d=f1.GetValue(n,d)\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d\n", + "print 'ok..now I will create a fraction-no input please'\n", + "f2=Fraction()\n", + "n,d=f2.GetValue(n,d)\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter n: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter d: 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter the value of the numerator and denominator: 3 4\n", + "Numerator set inside constructor: 3\n", + "Denominator set inside constructor: 4\n", + "Numerator value retrieved: 3\n", + "Denominator value retrieved: 4\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter the value of numerator only: 3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Numerator set inside constructor: 3\n", + "Denominator set inside constructor: 1\n", + "Numerator value retrieved: 3\n", + "Denominator value retrieved: 1\n", + "ok..now I will create a fraction-no input please\n", + "Numerator set inside constructor: 0\n", + "Denominator set inside constructor: 1\n", + "Numerator value retrieved: 0\n", + "Denominator value retrieved: 1\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.8, page no:223" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction:\n", + " def_init_num=None #private members\n", + " def_init_denom=None #private members\n", + " \n", + " def __init__(self,anotherFraction=None): \n", + " if anotherFraction==None: #normal constructor\n", + " self.__num=anotherFraction\n", + " self.__denom=anotherFraction\n", + " else: #copy constructor\n", + " self.__num=anotherFraction.self.__num\n", + " self.__denom=anotherFraction.self.__denom\n", + " \n", + " \n", + " #public functions\n", + " def SetValue(self,a,b):\n", + " self.__num=a\n", + " self.__denom=b#refer to IntegerData\n", + " def GetValue(self,a,b):\n", + " a=self.__num\n", + " b=self.__denom\n", + " return a,b\n", + " \n", + "f=Fraction()\n", + "n=input(\"Enter n: \") #user input\n", + "d=input(\"Enter d: \") #user input\n", + "print \"enter the numerator and denominator: \", ' ',n,d\n", + "\n", + "f.SetValue(n,d) #call function SetValue\n", + "print \"Numerator value set: \", ' ',n\n", + "print \"Denominator value set: \", ' ',d\n", + "f.GetValue(n,d) #call function GetData\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d\n", + "print \"Now a second clone copy is being created: \",''\n", + "f1=f\n", + "f1.GetValue(n,d)\n", + "print \"Clone's numerator value retrieved: \", ' ',n\n", + "print \"Clone's denominator value retrieved: \", ' ',d" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter n: 5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter d: 6\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "enter the numerator and denominator: 5 6\n", + "Numerator value set: 5\n", + "Denominator value set: 6\n", + "Numerator value retrieved: 5\n", + "Denominator value retrieved: 6\n", + "Now a second clone copy is being created: \n", + "Clone's numerator value retrieved: 5\n", + "Clone's denominator value retrieved: 6\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.9, page no:229" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def MyNewHandler():\n", + " print \"Sorry operator new failed to allocate memory\"\n", + " exit(0)\n", + " \n", + "def _set_new_handler(s):\n", + " s()\n", + "#In python there is no in-built _set_new_handler function, so i made this function and passed MyNewHandler function as a parameters\n", + "_set_new_handler(MyNewHandler)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Sorry operator new failed to allocate memory\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.10, page no:230" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from ctypes import * \n", + "class Fraction: \n", + " def __init__(self,a=0,b=1): \n", + " if isinstance(a,int): \n", + " c = c_int(a) \n", + " d = c_int(b) \n", + " self.__num = pointer(c) \n", + " self.__denom = pointer(d) \n", + " print 'constructor sets numerator = ', self.__num[0] , ', denominator = ', self.__denom[0] \n", + " else:\n", + " c=c_int(a.__num[0])\n", + " d = c_int(a.__denom[0])\n", + " self.__num = pointer(c) \n", + " self.__denom = pointer(d)\n", + " print 'copy constructor sets numerator = ', self.__num[0] , ', denominator = ', self.__denom[0] \n", + " \n", + " def __del__(self): \n", + " print 'destructor deallocates numerator = ', self.__num[0] , ', denominator = ', self.__denom[0] \n", + " \n", + "n = input(\"Please enter values of numerator: \") \n", + "d = input(\"Please enter values of denominator: \") \n", + "f = Fraction(n,d) \n", + "print 'Please enter another set of ' \n", + "n = input(\"numerator: \") \n", + "d = input(\"denominator: \") \n", + "print 'Creating fraction *pf : ' \n", + "pf = Fraction(n,d) \n", + "print 'Now a clone copy (f2) created from *pf: ' \n", + "f2 = Fraction(pf)\n", + "print 'Now another clone copy (*pf2) created from f:' \n", + "pf2 = Fraction(f) \n", + "print '*pf2 is being destroyed:' \n", + "del pf2\n", + "print '*pf is being destroyed:' \n", + "del pf \n", + "print 'now objects f2 and f automatically destroyed : '" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter values of numerator: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter values of denominator: 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "constructor sets numerator = 3 , denominator = 4\n", + "Please enter another set of \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "numerator: 5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "denominator: 6\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Creating fraction *pf : \n", + "constructor sets numerator = 5 , denominator = 6\n", + "Now a clone copy (f2) created from *pf: \n", + "copy constructor sets numerator = 5 , denominator = 6\n", + "Now another clone copy (*pf2) created from f:\n", + "copy constructor sets numerator = 3 , denominator = 4\n", + "*pf2 is being destroyed:\n", + "*pf is being destroyed:\n", + "now objects f2 and f automatically destroyed : \n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.11, page no:234" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def Memfail(self,s):\n", + " print \"Sorry Unable to allocate memory\"\n", + " sys.exit(0)\n", + "\n", + "MAX_SIZE = 60 + 1\n", + "\n", + "MAX_SIZE=[[0 for col in range(MAX_SIZE)]for row in range(MAX_SIZE)]\n", + "nChar=0\n", + "chArr=\"Hello\"\n", + "\n", + "print \"Please input a string( 60 characters max.): \",chArr\n", + "\n", + "nChar=len(chArr)+1\n", + "szStr=chArr\n", + "print \"required memory space for\",nChar,\n", + "print \"characters\"\n", + "chArr=szStr #string copy\n", + "szStr=chArr\n", + "print \"String copied in allocated space: \",szStr\n", + "print \"Memory space dellocated\"\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please input a string( 60 characters max.): Hello\n", + "required memory space for 6 characters\n", + "String copied in allocated space: Hello\n", + "Memory space dellocated\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.12, page no:236" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction:\n", + " \n", + " def __init__(self,a=0,b=1): #constructor\n", + " self.__num=a\n", + " self.__denom=b\n", + " \n", + " def __del__(self): #destructor\n", + " pass\n", + " \n", + " def GetValue(self,a,b):\n", + " a=self.__num\n", + " b=self.__denom\n", + " return self.__num,self.__denom\n", + "n=4\n", + "d=5\n", + "f=Fraction(4,5)\n", + "n,d=f.GetValue(n,d)" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.13, page no:239" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class X:\n", + " __sa=20 #initialising static member\n", + " a = None\n", + " def __init__(self):\n", + " self.a=None #public member\n", + " def f(self,b):\n", + " a=30\n", + " print \"Global a= \",b\n", + " print \"Local a= \",a\n", + " print \"Nonstatic member a= \",self.a\n", + " print \"static member sa= \",self.__sa\n", + "\n", + "a=10\n", + "\n", + "aXobj=X()\n", + "aXobj.a=40\n", + "aXobj.f(a)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Global a= 10\n", + "Local a= 30\n", + "Nonstatic member a= 40\n", + "static member sa= 20\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [], + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/sample_notebooks/NitamoniDas/chapter_8_Data_Abstraction_through_Classes.ipynb b/sample_notebooks/NitamoniDas/chapter_8_Data_Abstraction_through_Classes.ipynb new file mode 100755 index 00000000..1083b19a --- /dev/null +++ b/sample_notebooks/NitamoniDas/chapter_8_Data_Abstraction_through_Classes.ipynb @@ -0,0 +1,809 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:dbbd8ed92141bcc804ff2fbeacf8bb85f122dc6e196e1d830291a4a449a3a439" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 8: Data Abstraction through Classes and User-Defined Data Types " + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.1, page no: 208" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Super:\n", + " def __init__(self):\n", + " self.__IntegerData=None #private member\n", + " #public functions\n", + " def SetData(self,i):\n", + " self.__IntegerData=i #refer to IntegerData\n", + " def ShowData(self):\n", + " print \"Data is \",self.__IntegerData,' '\n", + "\n", + "ob1=Super()\n", + "ob2=Super()\n", + "ob1.SetData(1000)\n", + "ob2.SetData(2000)\n", + "ob1.ShowData()\n", + "ob2.ShowData()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Data is 1000 \n", + "Data is 2000 \n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.2, page no:211" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class X:\n", + " def __init__(self):\n", + " self.a=None #private members\n", + " self.b=None #private members\n", + " \n", + "#no structure type present in python \n", + "x = X()\n", + "\n", + "x.a=0\n", + "x.b=1\n", + "print \"x.a=\",x.a,\",x.b=\",x.b" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "x.a= 0 ,x.b= 1\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.3, page no:214" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction:\n", + " \n", + " def SetValue(self,a,b): #public functions\n", + " self.__num=a \n", + " self.__denom=b \n", + " def GetValue(self,a,b):\n", + " a=self.__num\n", + " b=self.__denom\n", + " \n", + "f=Fraction()\n", + "n=input(\"Enter n: \") #user input\n", + "d=input(\"Enter d: \") #user input\n", + "print \"enter the numerator and denominator: \", ' ',n,d\n", + "\n", + "f.SetValue(n,d) #call function SetValue\n", + "print \"Numerator value set: \", ' ',n\n", + "print \"Denominator value set: \", ' ',d\n", + "f.GetValue(n,d) #call function GetData\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter n: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter d: 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "enter the numerator and denominator: 3 4\n", + "Numerator value set: 3\n", + "Denominator value set: 4\n", + "Numerator value retrieved: 3\n", + "Denominator value retrieved: 4\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.4, page no:216" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class SimpleClass:\n", + " def __init__(self):\n", + " self.__i=None #private member\n", + " \n", + " def __init__(self):\n", + " self.__i=500 #constructor\n", + " \n", + " def SetData(self,d):\n", + " self.__i=d\n", + " \n", + " def GetData(self):\n", + " return self.__i\n", + " \n", + "#Initializing\n", + "s1=SimpleClass()\n", + "s2=SimpleClass()\n", + "print \"s1 has data: \",s1.GetData()\n", + "print \"s2 has data: \",s2.GetData()\n", + "s1.SetData(1000) #function call\n", + "s2.SetData(2000)\n", + "print \"s1 has data: \",s1.GetData(),' '\n", + "print \"s2 has data: \", s2.GetData(),' '\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "s1 has data: 500\n", + "s2 has data: 500\n", + "s1 has data: 1000 \n", + "s2 has data: 2000 \n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.5, page no:217" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class SimpleClass:\n", + " __IntegerData=None\n", + " def __init__(self,data=None):\n", + " if data==None:\n", + " self.__IntegerData=500 #default constructor\n", + " else:\n", + " self.__IntegerData=data #parameterised constructor\n", + " \n", + " def SetData(self,d):\n", + " self.__IntegerData=d\n", + " \n", + " def GetData(self):\n", + " return self.__IntegerData\n", + " \n", + "#Initializing\n", + "s1=SimpleClass()\n", + "s2=SimpleClass()\n", + "s3=SimpleClass(400)\n", + "s4=SimpleClass(600)\n", + "print \"s1 has data: \",s1.GetData(),' '\n", + "print \"s2 has data: \",s2.GetData(),' '\n", + "print \"s3 has data: \",s3.GetData(),' '\n", + "print \"s4 has data: \",s4.GetData(),' '\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "s1 has data: 500 \n", + "s2 has data: 500 \n", + "s3 has data: 400 \n", + "s4 has data: 600 \n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.6, page no:218" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction: \n", + " def_init_num=None #private members\n", + " def_init_denom=None #private members\n", + " def __init__(self,a=0,b=1):\n", + " self.__num=a\n", + " self.__denom=b\n", + " print \"Numerator set inside constructor\",' ',n\n", + " print \"Denominator set inside constructor\",' ',d\n", + " \n", + " def SetValue(self,a,b):\n", + " self.__num=a\n", + " self.__denom=b\n", + " def GetValue(self,a,b):\n", + " a= self.__num\n", + " b= self.__denom\n", + " return a,b\n", + " \n", + " def __del__(self): #destructor\n", + " pass\n", + " \n", + "n=input(\"Enter n: \") #user input\n", + "d=input(\"Enter d: \") #user input\n", + "print \"Please enter value of numerator and denominator: \",' ',n,d\n", + "f=Fraction(n,d)\n", + "f.GetValue(n,d)\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter n: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter d: 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter value of numerator and denominator: 3 4\n", + "Numerator set inside constructor 3\n", + "Denominator set inside constructor 4\n", + "Numerator value retrieved: 3\n", + "Denominator value retrieved: 4\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.7, page no:221" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction:\n", + " \n", + " def __init__(self,a=0,b=1): #constructor\n", + " self.__num=a\n", + " self.__denom=b\n", + " print \"Numerator set inside constructor: \",self.__num\n", + " print \"Denominator set inside constructor: \",self.__denom \n", + " \n", + " def GetValue(self,a,b):\n", + " return self.__num,self.__denom\n", + "n=input(\"Enter n: \") #user input\n", + "d=input(\"Enter d: \") #user input\n", + "print \"Please enter the value of the numerator and denominator: \",n,d\n", + "f=Fraction(n,d)\n", + "n,d=f.GetValue(n,d)\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d\n", + "n=input(\"Please enter the value of numerator only: \")\n", + "f1=Fraction(n)\n", + "n,d=f1.GetValue(n,d)\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d\n", + "print 'ok..now I will create a fraction-no input please'\n", + "f2=Fraction()\n", + "n,d=f2.GetValue(n,d)\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter n: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter d: 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter the value of the numerator and denominator: 3 4\n", + "Numerator set inside constructor: 3\n", + "Denominator set inside constructor: 4\n", + "Numerator value retrieved: 3\n", + "Denominator value retrieved: 4\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter the value of numerator only: 3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Numerator set inside constructor: 3\n", + "Denominator set inside constructor: 1\n", + "Numerator value retrieved: 3\n", + "Denominator value retrieved: 1\n", + "ok..now I will create a fraction-no input please\n", + "Numerator set inside constructor: 0\n", + "Denominator set inside constructor: 1\n", + "Numerator value retrieved: 0\n", + "Denominator value retrieved: 1\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.8, page no:223" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction:\n", + " def_init_num=None #private members\n", + " def_init_denom=None #private members\n", + " \n", + " def __init__(self,anotherFraction=None): \n", + " if anotherFraction==None: #normal constructor\n", + " self.__num=anotherFraction\n", + " self.__denom=anotherFraction\n", + " else: #copy constructor\n", + " self.__num=anotherFraction.self.__num\n", + " self.__denom=anotherFraction.self.__denom\n", + " \n", + " \n", + " #public functions\n", + " def SetValue(self,a,b):\n", + " self.__num=a\n", + " self.__denom=b#refer to IntegerData\n", + " def GetValue(self,a,b):\n", + " a=self.__num\n", + " b=self.__denom\n", + " return a,b\n", + " \n", + "f=Fraction()\n", + "n=input(\"Enter n: \") #user input\n", + "d=input(\"Enter d: \") #user input\n", + "print \"enter the numerator and denominator: \", ' ',n,d\n", + "\n", + "f.SetValue(n,d) #call function SetValue\n", + "print \"Numerator value set: \", ' ',n\n", + "print \"Denominator value set: \", ' ',d\n", + "f.GetValue(n,d) #call function GetData\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d\n", + "print \"Now a second clone copy is being created: \",''\n", + "f1=f\n", + "f1.GetValue(n,d)\n", + "print \"Clone's numerator value retrieved: \", ' ',n\n", + "print \"Clone's denominator value retrieved: \", ' ',d" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter n: 5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter d: 6\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "enter the numerator and denominator: 5 6\n", + "Numerator value set: 5\n", + "Denominator value set: 6\n", + "Numerator value retrieved: 5\n", + "Denominator value retrieved: 6\n", + "Now a second clone copy is being created: \n", + "Clone's numerator value retrieved: 5\n", + "Clone's denominator value retrieved: 6\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.9, page no:229" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def MyNewHandler():\n", + " print \"Sorry operator new failed to allocate memory\"\n", + " exit(0)\n", + " \n", + "def _set_new_handler(s):\n", + " s()\n", + "#In python there is no in-built _set_new_handler function, so i made this function and passed MyNewHandler function as a parameters\n", + "_set_new_handler(MyNewHandler)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Sorry operator new failed to allocate memory\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.10, page no:230" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from ctypes import * \n", + "class Fraction: \n", + " def __init__(self,a=0,b=1): \n", + " if isinstance(a,int): \n", + " c = c_int(a) \n", + " d = c_int(b) \n", + " self.__num = pointer(c) \n", + " self.__denom = pointer(d) \n", + " print 'constructor sets numerator = ', self.__num[0] , ', denominator = ', self.__denom[0] \n", + " else:\n", + " c=c_int(a.__num[0])\n", + " d = c_int(a.__denom[0])\n", + " self.__num = pointer(c) \n", + " self.__denom = pointer(d)\n", + " print 'copy constructor sets numerator = ', self.__num[0] , ', denominator = ', self.__denom[0] \n", + " \n", + " def __del__(self): \n", + " print 'destructor deallocates numerator = ', self.__num[0] , ', denominator = ', self.__denom[0] \n", + " \n", + "n = input(\"Please enter values of numerator: \") \n", + "d = input(\"Please enter values of denominator: \") \n", + "f = Fraction(n,d) \n", + "print 'Please enter another set of ' \n", + "n = input(\"numerator: \") \n", + "d = input(\"denominator: \") \n", + "print 'Creating fraction *pf : ' \n", + "pf = Fraction(n,d) \n", + "print 'Now a clone copy (f2) created from *pf: ' \n", + "f2 = Fraction(pf)\n", + "print 'Now another clone copy (*pf2) created from f:' \n", + "pf2 = Fraction(f) \n", + "print '*pf2 is being destroyed:' \n", + "del pf2\n", + "print '*pf is being destroyed:' \n", + "del pf \n", + "print 'now objects f2 and f automatically destroyed : '" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter values of numerator: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter values of denominator: 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "constructor sets numerator = 3 , denominator = 4\n", + "Please enter another set of \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "numerator: 5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "denominator: 6\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Creating fraction *pf : \n", + "constructor sets numerator = 5 , denominator = 6\n", + "Now a clone copy (f2) created from *pf: \n", + "copy constructor sets numerator = 5 , denominator = 6\n", + "Now another clone copy (*pf2) created from f:\n", + "copy constructor sets numerator = 3 , denominator = 4\n", + "*pf2 is being destroyed:\n", + "*pf is being destroyed:\n", + "now objects f2 and f automatically destroyed : \n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.11, page no:234" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def Memfail(self,s):\n", + " print \"Sorry Unable to allocate memory\"\n", + " sys.exit(0)\n", + "\n", + "MAX_SIZE = 60 + 1\n", + "\n", + "MAX_SIZE=[[0 for col in range(MAX_SIZE)]for row in range(MAX_SIZE)]\n", + "nChar=0\n", + "chArr=\"Hello\"\n", + "\n", + "print \"Please input a string( 60 characters max.): \",chArr\n", + "\n", + "nChar=len(chArr)+1\n", + "szStr=chArr\n", + "print \"required memory space for\",nChar,\n", + "print \"characters\"\n", + "chArr=szStr #string copy\n", + "szStr=chArr\n", + "print \"String copied in allocated space: \",szStr\n", + "print \"Memory space dellocated\"\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please input a string( 60 characters max.): Hello\n", + "required memory space for 6 characters\n", + "String copied in allocated space: Hello\n", + "Memory space dellocated\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.12, page no:236" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction:\n", + " \n", + " def __init__(self,a=0,b=1): #constructor\n", + " self.__num=a\n", + " self.__denom=b\n", + " \n", + " def __del__(self): #destructor\n", + " pass\n", + " \n", + " def GetValue(self,a,b):\n", + " a=self.__num\n", + " b=self.__denom\n", + " return self.__num,self.__denom\n", + "n=4\n", + "d=5\n", + "f=Fraction(4,5)\n", + "n,d=f.GetValue(n,d)" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.13, page no:239" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class X:\n", + " __sa=20 #initialising static member\n", + " a = None\n", + " def __init__(self):\n", + " self.a=None #public member\n", + " def f(self,b):\n", + " a=30\n", + " print \"Global a= \",b\n", + " print \"Local a= \",a\n", + " print \"Nonstatic member a= \",self.a\n", + " print \"static member sa= \",self.__sa\n", + "\n", + "a=10\n", + "\n", + "aXobj=X()\n", + "aXobj.a=40\n", + "aXobj.f(a)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Global a= 10\n", + "Local a= 30\n", + "Nonstatic member a= 40\n", + "static member sa= 20\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [], + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/sample_notebooks/NitamoniDas/chapter_8_Data_Abstraction_through_Classes_and_User-Defined_Data_Types_(1).ipynb b/sample_notebooks/NitamoniDas/chapter_8_Data_Abstraction_through_Classes_and_User-Defined_Data_Types_(1).ipynb new file mode 100755 index 00000000..1083b19a --- /dev/null +++ b/sample_notebooks/NitamoniDas/chapter_8_Data_Abstraction_through_Classes_and_User-Defined_Data_Types_(1).ipynb @@ -0,0 +1,809 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:dbbd8ed92141bcc804ff2fbeacf8bb85f122dc6e196e1d830291a4a449a3a439" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 8: Data Abstraction through Classes and User-Defined Data Types " + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.1, page no: 208" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Super:\n", + " def __init__(self):\n", + " self.__IntegerData=None #private member\n", + " #public functions\n", + " def SetData(self,i):\n", + " self.__IntegerData=i #refer to IntegerData\n", + " def ShowData(self):\n", + " print \"Data is \",self.__IntegerData,' '\n", + "\n", + "ob1=Super()\n", + "ob2=Super()\n", + "ob1.SetData(1000)\n", + "ob2.SetData(2000)\n", + "ob1.ShowData()\n", + "ob2.ShowData()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Data is 1000 \n", + "Data is 2000 \n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.2, page no:211" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class X:\n", + " def __init__(self):\n", + " self.a=None #private members\n", + " self.b=None #private members\n", + " \n", + "#no structure type present in python \n", + "x = X()\n", + "\n", + "x.a=0\n", + "x.b=1\n", + "print \"x.a=\",x.a,\",x.b=\",x.b" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "x.a= 0 ,x.b= 1\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.3, page no:214" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction:\n", + " \n", + " def SetValue(self,a,b): #public functions\n", + " self.__num=a \n", + " self.__denom=b \n", + " def GetValue(self,a,b):\n", + " a=self.__num\n", + " b=self.__denom\n", + " \n", + "f=Fraction()\n", + "n=input(\"Enter n: \") #user input\n", + "d=input(\"Enter d: \") #user input\n", + "print \"enter the numerator and denominator: \", ' ',n,d\n", + "\n", + "f.SetValue(n,d) #call function SetValue\n", + "print \"Numerator value set: \", ' ',n\n", + "print \"Denominator value set: \", ' ',d\n", + "f.GetValue(n,d) #call function GetData\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter n: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter d: 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "enter the numerator and denominator: 3 4\n", + "Numerator value set: 3\n", + "Denominator value set: 4\n", + "Numerator value retrieved: 3\n", + "Denominator value retrieved: 4\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.4, page no:216" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class SimpleClass:\n", + " def __init__(self):\n", + " self.__i=None #private member\n", + " \n", + " def __init__(self):\n", + " self.__i=500 #constructor\n", + " \n", + " def SetData(self,d):\n", + " self.__i=d\n", + " \n", + " def GetData(self):\n", + " return self.__i\n", + " \n", + "#Initializing\n", + "s1=SimpleClass()\n", + "s2=SimpleClass()\n", + "print \"s1 has data: \",s1.GetData()\n", + "print \"s2 has data: \",s2.GetData()\n", + "s1.SetData(1000) #function call\n", + "s2.SetData(2000)\n", + "print \"s1 has data: \",s1.GetData(),' '\n", + "print \"s2 has data: \", s2.GetData(),' '\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "s1 has data: 500\n", + "s2 has data: 500\n", + "s1 has data: 1000 \n", + "s2 has data: 2000 \n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.5, page no:217" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class SimpleClass:\n", + " __IntegerData=None\n", + " def __init__(self,data=None):\n", + " if data==None:\n", + " self.__IntegerData=500 #default constructor\n", + " else:\n", + " self.__IntegerData=data #parameterised constructor\n", + " \n", + " def SetData(self,d):\n", + " self.__IntegerData=d\n", + " \n", + " def GetData(self):\n", + " return self.__IntegerData\n", + " \n", + "#Initializing\n", + "s1=SimpleClass()\n", + "s2=SimpleClass()\n", + "s3=SimpleClass(400)\n", + "s4=SimpleClass(600)\n", + "print \"s1 has data: \",s1.GetData(),' '\n", + "print \"s2 has data: \",s2.GetData(),' '\n", + "print \"s3 has data: \",s3.GetData(),' '\n", + "print \"s4 has data: \",s4.GetData(),' '\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "s1 has data: 500 \n", + "s2 has data: 500 \n", + "s3 has data: 400 \n", + "s4 has data: 600 \n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.6, page no:218" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction: \n", + " def_init_num=None #private members\n", + " def_init_denom=None #private members\n", + " def __init__(self,a=0,b=1):\n", + " self.__num=a\n", + " self.__denom=b\n", + " print \"Numerator set inside constructor\",' ',n\n", + " print \"Denominator set inside constructor\",' ',d\n", + " \n", + " def SetValue(self,a,b):\n", + " self.__num=a\n", + " self.__denom=b\n", + " def GetValue(self,a,b):\n", + " a= self.__num\n", + " b= self.__denom\n", + " return a,b\n", + " \n", + " def __del__(self): #destructor\n", + " pass\n", + " \n", + "n=input(\"Enter n: \") #user input\n", + "d=input(\"Enter d: \") #user input\n", + "print \"Please enter value of numerator and denominator: \",' ',n,d\n", + "f=Fraction(n,d)\n", + "f.GetValue(n,d)\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter n: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter d: 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter value of numerator and denominator: 3 4\n", + "Numerator set inside constructor 3\n", + "Denominator set inside constructor 4\n", + "Numerator value retrieved: 3\n", + "Denominator value retrieved: 4\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.7, page no:221" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction:\n", + " \n", + " def __init__(self,a=0,b=1): #constructor\n", + " self.__num=a\n", + " self.__denom=b\n", + " print \"Numerator set inside constructor: \",self.__num\n", + " print \"Denominator set inside constructor: \",self.__denom \n", + " \n", + " def GetValue(self,a,b):\n", + " return self.__num,self.__denom\n", + "n=input(\"Enter n: \") #user input\n", + "d=input(\"Enter d: \") #user input\n", + "print \"Please enter the value of the numerator and denominator: \",n,d\n", + "f=Fraction(n,d)\n", + "n,d=f.GetValue(n,d)\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d\n", + "n=input(\"Please enter the value of numerator only: \")\n", + "f1=Fraction(n)\n", + "n,d=f1.GetValue(n,d)\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d\n", + "print 'ok..now I will create a fraction-no input please'\n", + "f2=Fraction()\n", + "n,d=f2.GetValue(n,d)\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter n: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter d: 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter the value of the numerator and denominator: 3 4\n", + "Numerator set inside constructor: 3\n", + "Denominator set inside constructor: 4\n", + "Numerator value retrieved: 3\n", + "Denominator value retrieved: 4\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter the value of numerator only: 3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Numerator set inside constructor: 3\n", + "Denominator set inside constructor: 1\n", + "Numerator value retrieved: 3\n", + "Denominator value retrieved: 1\n", + "ok..now I will create a fraction-no input please\n", + "Numerator set inside constructor: 0\n", + "Denominator set inside constructor: 1\n", + "Numerator value retrieved: 0\n", + "Denominator value retrieved: 1\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.8, page no:223" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction:\n", + " def_init_num=None #private members\n", + " def_init_denom=None #private members\n", + " \n", + " def __init__(self,anotherFraction=None): \n", + " if anotherFraction==None: #normal constructor\n", + " self.__num=anotherFraction\n", + " self.__denom=anotherFraction\n", + " else: #copy constructor\n", + " self.__num=anotherFraction.self.__num\n", + " self.__denom=anotherFraction.self.__denom\n", + " \n", + " \n", + " #public functions\n", + " def SetValue(self,a,b):\n", + " self.__num=a\n", + " self.__denom=b#refer to IntegerData\n", + " def GetValue(self,a,b):\n", + " a=self.__num\n", + " b=self.__denom\n", + " return a,b\n", + " \n", + "f=Fraction()\n", + "n=input(\"Enter n: \") #user input\n", + "d=input(\"Enter d: \") #user input\n", + "print \"enter the numerator and denominator: \", ' ',n,d\n", + "\n", + "f.SetValue(n,d) #call function SetValue\n", + "print \"Numerator value set: \", ' ',n\n", + "print \"Denominator value set: \", ' ',d\n", + "f.GetValue(n,d) #call function GetData\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d\n", + "print \"Now a second clone copy is being created: \",''\n", + "f1=f\n", + "f1.GetValue(n,d)\n", + "print \"Clone's numerator value retrieved: \", ' ',n\n", + "print \"Clone's denominator value retrieved: \", ' ',d" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter n: 5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter d: 6\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "enter the numerator and denominator: 5 6\n", + "Numerator value set: 5\n", + "Denominator value set: 6\n", + "Numerator value retrieved: 5\n", + "Denominator value retrieved: 6\n", + "Now a second clone copy is being created: \n", + "Clone's numerator value retrieved: 5\n", + "Clone's denominator value retrieved: 6\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.9, page no:229" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def MyNewHandler():\n", + " print \"Sorry operator new failed to allocate memory\"\n", + " exit(0)\n", + " \n", + "def _set_new_handler(s):\n", + " s()\n", + "#In python there is no in-built _set_new_handler function, so i made this function and passed MyNewHandler function as a parameters\n", + "_set_new_handler(MyNewHandler)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Sorry operator new failed to allocate memory\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.10, page no:230" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from ctypes import * \n", + "class Fraction: \n", + " def __init__(self,a=0,b=1): \n", + " if isinstance(a,int): \n", + " c = c_int(a) \n", + " d = c_int(b) \n", + " self.__num = pointer(c) \n", + " self.__denom = pointer(d) \n", + " print 'constructor sets numerator = ', self.__num[0] , ', denominator = ', self.__denom[0] \n", + " else:\n", + " c=c_int(a.__num[0])\n", + " d = c_int(a.__denom[0])\n", + " self.__num = pointer(c) \n", + " self.__denom = pointer(d)\n", + " print 'copy constructor sets numerator = ', self.__num[0] , ', denominator = ', self.__denom[0] \n", + " \n", + " def __del__(self): \n", + " print 'destructor deallocates numerator = ', self.__num[0] , ', denominator = ', self.__denom[0] \n", + " \n", + "n = input(\"Please enter values of numerator: \") \n", + "d = input(\"Please enter values of denominator: \") \n", + "f = Fraction(n,d) \n", + "print 'Please enter another set of ' \n", + "n = input(\"numerator: \") \n", + "d = input(\"denominator: \") \n", + "print 'Creating fraction *pf : ' \n", + "pf = Fraction(n,d) \n", + "print 'Now a clone copy (f2) created from *pf: ' \n", + "f2 = Fraction(pf)\n", + "print 'Now another clone copy (*pf2) created from f:' \n", + "pf2 = Fraction(f) \n", + "print '*pf2 is being destroyed:' \n", + "del pf2\n", + "print '*pf is being destroyed:' \n", + "del pf \n", + "print 'now objects f2 and f automatically destroyed : '" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter values of numerator: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter values of denominator: 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "constructor sets numerator = 3 , denominator = 4\n", + "Please enter another set of \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "numerator: 5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "denominator: 6\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Creating fraction *pf : \n", + "constructor sets numerator = 5 , denominator = 6\n", + "Now a clone copy (f2) created from *pf: \n", + "copy constructor sets numerator = 5 , denominator = 6\n", + "Now another clone copy (*pf2) created from f:\n", + "copy constructor sets numerator = 3 , denominator = 4\n", + "*pf2 is being destroyed:\n", + "*pf is being destroyed:\n", + "now objects f2 and f automatically destroyed : \n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.11, page no:234" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def Memfail(self,s):\n", + " print \"Sorry Unable to allocate memory\"\n", + " sys.exit(0)\n", + "\n", + "MAX_SIZE = 60 + 1\n", + "\n", + "MAX_SIZE=[[0 for col in range(MAX_SIZE)]for row in range(MAX_SIZE)]\n", + "nChar=0\n", + "chArr=\"Hello\"\n", + "\n", + "print \"Please input a string( 60 characters max.): \",chArr\n", + "\n", + "nChar=len(chArr)+1\n", + "szStr=chArr\n", + "print \"required memory space for\",nChar,\n", + "print \"characters\"\n", + "chArr=szStr #string copy\n", + "szStr=chArr\n", + "print \"String copied in allocated space: \",szStr\n", + "print \"Memory space dellocated\"\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please input a string( 60 characters max.): Hello\n", + "required memory space for 6 characters\n", + "String copied in allocated space: Hello\n", + "Memory space dellocated\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.12, page no:236" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction:\n", + " \n", + " def __init__(self,a=0,b=1): #constructor\n", + " self.__num=a\n", + " self.__denom=b\n", + " \n", + " def __del__(self): #destructor\n", + " pass\n", + " \n", + " def GetValue(self,a,b):\n", + " a=self.__num\n", + " b=self.__denom\n", + " return self.__num,self.__denom\n", + "n=4\n", + "d=5\n", + "f=Fraction(4,5)\n", + "n,d=f.GetValue(n,d)" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.13, page no:239" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class X:\n", + " __sa=20 #initialising static member\n", + " a = None\n", + " def __init__(self):\n", + " self.a=None #public member\n", + " def f(self,b):\n", + " a=30\n", + " print \"Global a= \",b\n", + " print \"Local a= \",a\n", + " print \"Nonstatic member a= \",self.a\n", + " print \"static member sa= \",self.__sa\n", + "\n", + "a=10\n", + "\n", + "aXobj=X()\n", + "aXobj.a=40\n", + "aXobj.f(a)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Global a= 10\n", + "Local a= 30\n", + "Nonstatic member a= 40\n", + "static member sa= 20\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [], + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/sample_notebooks/NitamoniDas/chapter_8_Data_Abstraction_through_Classes_and_User-Defined_Data_Types_10.ipynb b/sample_notebooks/NitamoniDas/chapter_8_Data_Abstraction_through_Classes_and_User-Defined_Data_Types_10.ipynb new file mode 100755 index 00000000..1083b19a --- /dev/null +++ b/sample_notebooks/NitamoniDas/chapter_8_Data_Abstraction_through_Classes_and_User-Defined_Data_Types_10.ipynb @@ -0,0 +1,809 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:dbbd8ed92141bcc804ff2fbeacf8bb85f122dc6e196e1d830291a4a449a3a439" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 8: Data Abstraction through Classes and User-Defined Data Types " + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.1, page no: 208" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Super:\n", + " def __init__(self):\n", + " self.__IntegerData=None #private member\n", + " #public functions\n", + " def SetData(self,i):\n", + " self.__IntegerData=i #refer to IntegerData\n", + " def ShowData(self):\n", + " print \"Data is \",self.__IntegerData,' '\n", + "\n", + "ob1=Super()\n", + "ob2=Super()\n", + "ob1.SetData(1000)\n", + "ob2.SetData(2000)\n", + "ob1.ShowData()\n", + "ob2.ShowData()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Data is 1000 \n", + "Data is 2000 \n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.2, page no:211" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class X:\n", + " def __init__(self):\n", + " self.a=None #private members\n", + " self.b=None #private members\n", + " \n", + "#no structure type present in python \n", + "x = X()\n", + "\n", + "x.a=0\n", + "x.b=1\n", + "print \"x.a=\",x.a,\",x.b=\",x.b" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "x.a= 0 ,x.b= 1\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.3, page no:214" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction:\n", + " \n", + " def SetValue(self,a,b): #public functions\n", + " self.__num=a \n", + " self.__denom=b \n", + " def GetValue(self,a,b):\n", + " a=self.__num\n", + " b=self.__denom\n", + " \n", + "f=Fraction()\n", + "n=input(\"Enter n: \") #user input\n", + "d=input(\"Enter d: \") #user input\n", + "print \"enter the numerator and denominator: \", ' ',n,d\n", + "\n", + "f.SetValue(n,d) #call function SetValue\n", + "print \"Numerator value set: \", ' ',n\n", + "print \"Denominator value set: \", ' ',d\n", + "f.GetValue(n,d) #call function GetData\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter n: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter d: 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "enter the numerator and denominator: 3 4\n", + "Numerator value set: 3\n", + "Denominator value set: 4\n", + "Numerator value retrieved: 3\n", + "Denominator value retrieved: 4\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.4, page no:216" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class SimpleClass:\n", + " def __init__(self):\n", + " self.__i=None #private member\n", + " \n", + " def __init__(self):\n", + " self.__i=500 #constructor\n", + " \n", + " def SetData(self,d):\n", + " self.__i=d\n", + " \n", + " def GetData(self):\n", + " return self.__i\n", + " \n", + "#Initializing\n", + "s1=SimpleClass()\n", + "s2=SimpleClass()\n", + "print \"s1 has data: \",s1.GetData()\n", + "print \"s2 has data: \",s2.GetData()\n", + "s1.SetData(1000) #function call\n", + "s2.SetData(2000)\n", + "print \"s1 has data: \",s1.GetData(),' '\n", + "print \"s2 has data: \", s2.GetData(),' '\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "s1 has data: 500\n", + "s2 has data: 500\n", + "s1 has data: 1000 \n", + "s2 has data: 2000 \n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.5, page no:217" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class SimpleClass:\n", + " __IntegerData=None\n", + " def __init__(self,data=None):\n", + " if data==None:\n", + " self.__IntegerData=500 #default constructor\n", + " else:\n", + " self.__IntegerData=data #parameterised constructor\n", + " \n", + " def SetData(self,d):\n", + " self.__IntegerData=d\n", + " \n", + " def GetData(self):\n", + " return self.__IntegerData\n", + " \n", + "#Initializing\n", + "s1=SimpleClass()\n", + "s2=SimpleClass()\n", + "s3=SimpleClass(400)\n", + "s4=SimpleClass(600)\n", + "print \"s1 has data: \",s1.GetData(),' '\n", + "print \"s2 has data: \",s2.GetData(),' '\n", + "print \"s3 has data: \",s3.GetData(),' '\n", + "print \"s4 has data: \",s4.GetData(),' '\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "s1 has data: 500 \n", + "s2 has data: 500 \n", + "s3 has data: 400 \n", + "s4 has data: 600 \n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.6, page no:218" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction: \n", + " def_init_num=None #private members\n", + " def_init_denom=None #private members\n", + " def __init__(self,a=0,b=1):\n", + " self.__num=a\n", + " self.__denom=b\n", + " print \"Numerator set inside constructor\",' ',n\n", + " print \"Denominator set inside constructor\",' ',d\n", + " \n", + " def SetValue(self,a,b):\n", + " self.__num=a\n", + " self.__denom=b\n", + " def GetValue(self,a,b):\n", + " a= self.__num\n", + " b= self.__denom\n", + " return a,b\n", + " \n", + " def __del__(self): #destructor\n", + " pass\n", + " \n", + "n=input(\"Enter n: \") #user input\n", + "d=input(\"Enter d: \") #user input\n", + "print \"Please enter value of numerator and denominator: \",' ',n,d\n", + "f=Fraction(n,d)\n", + "f.GetValue(n,d)\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter n: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter d: 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter value of numerator and denominator: 3 4\n", + "Numerator set inside constructor 3\n", + "Denominator set inside constructor 4\n", + "Numerator value retrieved: 3\n", + "Denominator value retrieved: 4\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.7, page no:221" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction:\n", + " \n", + " def __init__(self,a=0,b=1): #constructor\n", + " self.__num=a\n", + " self.__denom=b\n", + " print \"Numerator set inside constructor: \",self.__num\n", + " print \"Denominator set inside constructor: \",self.__denom \n", + " \n", + " def GetValue(self,a,b):\n", + " return self.__num,self.__denom\n", + "n=input(\"Enter n: \") #user input\n", + "d=input(\"Enter d: \") #user input\n", + "print \"Please enter the value of the numerator and denominator: \",n,d\n", + "f=Fraction(n,d)\n", + "n,d=f.GetValue(n,d)\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d\n", + "n=input(\"Please enter the value of numerator only: \")\n", + "f1=Fraction(n)\n", + "n,d=f1.GetValue(n,d)\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d\n", + "print 'ok..now I will create a fraction-no input please'\n", + "f2=Fraction()\n", + "n,d=f2.GetValue(n,d)\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter n: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter d: 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter the value of the numerator and denominator: 3 4\n", + "Numerator set inside constructor: 3\n", + "Denominator set inside constructor: 4\n", + "Numerator value retrieved: 3\n", + "Denominator value retrieved: 4\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter the value of numerator only: 3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Numerator set inside constructor: 3\n", + "Denominator set inside constructor: 1\n", + "Numerator value retrieved: 3\n", + "Denominator value retrieved: 1\n", + "ok..now I will create a fraction-no input please\n", + "Numerator set inside constructor: 0\n", + "Denominator set inside constructor: 1\n", + "Numerator value retrieved: 0\n", + "Denominator value retrieved: 1\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.8, page no:223" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction:\n", + " def_init_num=None #private members\n", + " def_init_denom=None #private members\n", + " \n", + " def __init__(self,anotherFraction=None): \n", + " if anotherFraction==None: #normal constructor\n", + " self.__num=anotherFraction\n", + " self.__denom=anotherFraction\n", + " else: #copy constructor\n", + " self.__num=anotherFraction.self.__num\n", + " self.__denom=anotherFraction.self.__denom\n", + " \n", + " \n", + " #public functions\n", + " def SetValue(self,a,b):\n", + " self.__num=a\n", + " self.__denom=b#refer to IntegerData\n", + " def GetValue(self,a,b):\n", + " a=self.__num\n", + " b=self.__denom\n", + " return a,b\n", + " \n", + "f=Fraction()\n", + "n=input(\"Enter n: \") #user input\n", + "d=input(\"Enter d: \") #user input\n", + "print \"enter the numerator and denominator: \", ' ',n,d\n", + "\n", + "f.SetValue(n,d) #call function SetValue\n", + "print \"Numerator value set: \", ' ',n\n", + "print \"Denominator value set: \", ' ',d\n", + "f.GetValue(n,d) #call function GetData\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d\n", + "print \"Now a second clone copy is being created: \",''\n", + "f1=f\n", + "f1.GetValue(n,d)\n", + "print \"Clone's numerator value retrieved: \", ' ',n\n", + "print \"Clone's denominator value retrieved: \", ' ',d" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter n: 5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter d: 6\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "enter the numerator and denominator: 5 6\n", + "Numerator value set: 5\n", + "Denominator value set: 6\n", + "Numerator value retrieved: 5\n", + "Denominator value retrieved: 6\n", + "Now a second clone copy is being created: \n", + "Clone's numerator value retrieved: 5\n", + "Clone's denominator value retrieved: 6\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.9, page no:229" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def MyNewHandler():\n", + " print \"Sorry operator new failed to allocate memory\"\n", + " exit(0)\n", + " \n", + "def _set_new_handler(s):\n", + " s()\n", + "#In python there is no in-built _set_new_handler function, so i made this function and passed MyNewHandler function as a parameters\n", + "_set_new_handler(MyNewHandler)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Sorry operator new failed to allocate memory\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.10, page no:230" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from ctypes import * \n", + "class Fraction: \n", + " def __init__(self,a=0,b=1): \n", + " if isinstance(a,int): \n", + " c = c_int(a) \n", + " d = c_int(b) \n", + " self.__num = pointer(c) \n", + " self.__denom = pointer(d) \n", + " print 'constructor sets numerator = ', self.__num[0] , ', denominator = ', self.__denom[0] \n", + " else:\n", + " c=c_int(a.__num[0])\n", + " d = c_int(a.__denom[0])\n", + " self.__num = pointer(c) \n", + " self.__denom = pointer(d)\n", + " print 'copy constructor sets numerator = ', self.__num[0] , ', denominator = ', self.__denom[0] \n", + " \n", + " def __del__(self): \n", + " print 'destructor deallocates numerator = ', self.__num[0] , ', denominator = ', self.__denom[0] \n", + " \n", + "n = input(\"Please enter values of numerator: \") \n", + "d = input(\"Please enter values of denominator: \") \n", + "f = Fraction(n,d) \n", + "print 'Please enter another set of ' \n", + "n = input(\"numerator: \") \n", + "d = input(\"denominator: \") \n", + "print 'Creating fraction *pf : ' \n", + "pf = Fraction(n,d) \n", + "print 'Now a clone copy (f2) created from *pf: ' \n", + "f2 = Fraction(pf)\n", + "print 'Now another clone copy (*pf2) created from f:' \n", + "pf2 = Fraction(f) \n", + "print '*pf2 is being destroyed:' \n", + "del pf2\n", + "print '*pf is being destroyed:' \n", + "del pf \n", + "print 'now objects f2 and f automatically destroyed : '" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter values of numerator: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter values of denominator: 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "constructor sets numerator = 3 , denominator = 4\n", + "Please enter another set of \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "numerator: 5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "denominator: 6\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Creating fraction *pf : \n", + "constructor sets numerator = 5 , denominator = 6\n", + "Now a clone copy (f2) created from *pf: \n", + "copy constructor sets numerator = 5 , denominator = 6\n", + "Now another clone copy (*pf2) created from f:\n", + "copy constructor sets numerator = 3 , denominator = 4\n", + "*pf2 is being destroyed:\n", + "*pf is being destroyed:\n", + "now objects f2 and f automatically destroyed : \n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.11, page no:234" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def Memfail(self,s):\n", + " print \"Sorry Unable to allocate memory\"\n", + " sys.exit(0)\n", + "\n", + "MAX_SIZE = 60 + 1\n", + "\n", + "MAX_SIZE=[[0 for col in range(MAX_SIZE)]for row in range(MAX_SIZE)]\n", + "nChar=0\n", + "chArr=\"Hello\"\n", + "\n", + "print \"Please input a string( 60 characters max.): \",chArr\n", + "\n", + "nChar=len(chArr)+1\n", + "szStr=chArr\n", + "print \"required memory space for\",nChar,\n", + "print \"characters\"\n", + "chArr=szStr #string copy\n", + "szStr=chArr\n", + "print \"String copied in allocated space: \",szStr\n", + "print \"Memory space dellocated\"\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please input a string( 60 characters max.): Hello\n", + "required memory space for 6 characters\n", + "String copied in allocated space: Hello\n", + "Memory space dellocated\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.12, page no:236" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction:\n", + " \n", + " def __init__(self,a=0,b=1): #constructor\n", + " self.__num=a\n", + " self.__denom=b\n", + " \n", + " def __del__(self): #destructor\n", + " pass\n", + " \n", + " def GetValue(self,a,b):\n", + " a=self.__num\n", + " b=self.__denom\n", + " return self.__num,self.__denom\n", + "n=4\n", + "d=5\n", + "f=Fraction(4,5)\n", + "n,d=f.GetValue(n,d)" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.13, page no:239" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class X:\n", + " __sa=20 #initialising static member\n", + " a = None\n", + " def __init__(self):\n", + " self.a=None #public member\n", + " def f(self,b):\n", + " a=30\n", + " print \"Global a= \",b\n", + " print \"Local a= \",a\n", + " print \"Nonstatic member a= \",self.a\n", + " print \"static member sa= \",self.__sa\n", + "\n", + "a=10\n", + "\n", + "aXobj=X()\n", + "aXobj.a=40\n", + "aXobj.f(a)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Global a= 10\n", + "Local a= 30\n", + "Nonstatic member a= 40\n", + "static member sa= 20\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [], + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/sample_notebooks/NitamoniDas/chapter_8_Data_Abstraction_through_Classes_and_User-Defined_Data_Types_2.ipynb b/sample_notebooks/NitamoniDas/chapter_8_Data_Abstraction_through_Classes_and_User-Defined_Data_Types_2.ipynb new file mode 100755 index 00000000..1083b19a --- /dev/null +++ b/sample_notebooks/NitamoniDas/chapter_8_Data_Abstraction_through_Classes_and_User-Defined_Data_Types_2.ipynb @@ -0,0 +1,809 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:dbbd8ed92141bcc804ff2fbeacf8bb85f122dc6e196e1d830291a4a449a3a439" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 8: Data Abstraction through Classes and User-Defined Data Types " + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.1, page no: 208" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Super:\n", + " def __init__(self):\n", + " self.__IntegerData=None #private member\n", + " #public functions\n", + " def SetData(self,i):\n", + " self.__IntegerData=i #refer to IntegerData\n", + " def ShowData(self):\n", + " print \"Data is \",self.__IntegerData,' '\n", + "\n", + "ob1=Super()\n", + "ob2=Super()\n", + "ob1.SetData(1000)\n", + "ob2.SetData(2000)\n", + "ob1.ShowData()\n", + "ob2.ShowData()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Data is 1000 \n", + "Data is 2000 \n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.2, page no:211" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class X:\n", + " def __init__(self):\n", + " self.a=None #private members\n", + " self.b=None #private members\n", + " \n", + "#no structure type present in python \n", + "x = X()\n", + "\n", + "x.a=0\n", + "x.b=1\n", + "print \"x.a=\",x.a,\",x.b=\",x.b" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "x.a= 0 ,x.b= 1\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.3, page no:214" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction:\n", + " \n", + " def SetValue(self,a,b): #public functions\n", + " self.__num=a \n", + " self.__denom=b \n", + " def GetValue(self,a,b):\n", + " a=self.__num\n", + " b=self.__denom\n", + " \n", + "f=Fraction()\n", + "n=input(\"Enter n: \") #user input\n", + "d=input(\"Enter d: \") #user input\n", + "print \"enter the numerator and denominator: \", ' ',n,d\n", + "\n", + "f.SetValue(n,d) #call function SetValue\n", + "print \"Numerator value set: \", ' ',n\n", + "print \"Denominator value set: \", ' ',d\n", + "f.GetValue(n,d) #call function GetData\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter n: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter d: 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "enter the numerator and denominator: 3 4\n", + "Numerator value set: 3\n", + "Denominator value set: 4\n", + "Numerator value retrieved: 3\n", + "Denominator value retrieved: 4\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.4, page no:216" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class SimpleClass:\n", + " def __init__(self):\n", + " self.__i=None #private member\n", + " \n", + " def __init__(self):\n", + " self.__i=500 #constructor\n", + " \n", + " def SetData(self,d):\n", + " self.__i=d\n", + " \n", + " def GetData(self):\n", + " return self.__i\n", + " \n", + "#Initializing\n", + "s1=SimpleClass()\n", + "s2=SimpleClass()\n", + "print \"s1 has data: \",s1.GetData()\n", + "print \"s2 has data: \",s2.GetData()\n", + "s1.SetData(1000) #function call\n", + "s2.SetData(2000)\n", + "print \"s1 has data: \",s1.GetData(),' '\n", + "print \"s2 has data: \", s2.GetData(),' '\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "s1 has data: 500\n", + "s2 has data: 500\n", + "s1 has data: 1000 \n", + "s2 has data: 2000 \n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.5, page no:217" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class SimpleClass:\n", + " __IntegerData=None\n", + " def __init__(self,data=None):\n", + " if data==None:\n", + " self.__IntegerData=500 #default constructor\n", + " else:\n", + " self.__IntegerData=data #parameterised constructor\n", + " \n", + " def SetData(self,d):\n", + " self.__IntegerData=d\n", + " \n", + " def GetData(self):\n", + " return self.__IntegerData\n", + " \n", + "#Initializing\n", + "s1=SimpleClass()\n", + "s2=SimpleClass()\n", + "s3=SimpleClass(400)\n", + "s4=SimpleClass(600)\n", + "print \"s1 has data: \",s1.GetData(),' '\n", + "print \"s2 has data: \",s2.GetData(),' '\n", + "print \"s3 has data: \",s3.GetData(),' '\n", + "print \"s4 has data: \",s4.GetData(),' '\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "s1 has data: 500 \n", + "s2 has data: 500 \n", + "s3 has data: 400 \n", + "s4 has data: 600 \n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.6, page no:218" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction: \n", + " def_init_num=None #private members\n", + " def_init_denom=None #private members\n", + " def __init__(self,a=0,b=1):\n", + " self.__num=a\n", + " self.__denom=b\n", + " print \"Numerator set inside constructor\",' ',n\n", + " print \"Denominator set inside constructor\",' ',d\n", + " \n", + " def SetValue(self,a,b):\n", + " self.__num=a\n", + " self.__denom=b\n", + " def GetValue(self,a,b):\n", + " a= self.__num\n", + " b= self.__denom\n", + " return a,b\n", + " \n", + " def __del__(self): #destructor\n", + " pass\n", + " \n", + "n=input(\"Enter n: \") #user input\n", + "d=input(\"Enter d: \") #user input\n", + "print \"Please enter value of numerator and denominator: \",' ',n,d\n", + "f=Fraction(n,d)\n", + "f.GetValue(n,d)\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter n: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter d: 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter value of numerator and denominator: 3 4\n", + "Numerator set inside constructor 3\n", + "Denominator set inside constructor 4\n", + "Numerator value retrieved: 3\n", + "Denominator value retrieved: 4\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.7, page no:221" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction:\n", + " \n", + " def __init__(self,a=0,b=1): #constructor\n", + " self.__num=a\n", + " self.__denom=b\n", + " print \"Numerator set inside constructor: \",self.__num\n", + " print \"Denominator set inside constructor: \",self.__denom \n", + " \n", + " def GetValue(self,a,b):\n", + " return self.__num,self.__denom\n", + "n=input(\"Enter n: \") #user input\n", + "d=input(\"Enter d: \") #user input\n", + "print \"Please enter the value of the numerator and denominator: \",n,d\n", + "f=Fraction(n,d)\n", + "n,d=f.GetValue(n,d)\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d\n", + "n=input(\"Please enter the value of numerator only: \")\n", + "f1=Fraction(n)\n", + "n,d=f1.GetValue(n,d)\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d\n", + "print 'ok..now I will create a fraction-no input please'\n", + "f2=Fraction()\n", + "n,d=f2.GetValue(n,d)\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter n: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter d: 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter the value of the numerator and denominator: 3 4\n", + "Numerator set inside constructor: 3\n", + "Denominator set inside constructor: 4\n", + "Numerator value retrieved: 3\n", + "Denominator value retrieved: 4\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter the value of numerator only: 3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Numerator set inside constructor: 3\n", + "Denominator set inside constructor: 1\n", + "Numerator value retrieved: 3\n", + "Denominator value retrieved: 1\n", + "ok..now I will create a fraction-no input please\n", + "Numerator set inside constructor: 0\n", + "Denominator set inside constructor: 1\n", + "Numerator value retrieved: 0\n", + "Denominator value retrieved: 1\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.8, page no:223" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction:\n", + " def_init_num=None #private members\n", + " def_init_denom=None #private members\n", + " \n", + " def __init__(self,anotherFraction=None): \n", + " if anotherFraction==None: #normal constructor\n", + " self.__num=anotherFraction\n", + " self.__denom=anotherFraction\n", + " else: #copy constructor\n", + " self.__num=anotherFraction.self.__num\n", + " self.__denom=anotherFraction.self.__denom\n", + " \n", + " \n", + " #public functions\n", + " def SetValue(self,a,b):\n", + " self.__num=a\n", + " self.__denom=b#refer to IntegerData\n", + " def GetValue(self,a,b):\n", + " a=self.__num\n", + " b=self.__denom\n", + " return a,b\n", + " \n", + "f=Fraction()\n", + "n=input(\"Enter n: \") #user input\n", + "d=input(\"Enter d: \") #user input\n", + "print \"enter the numerator and denominator: \", ' ',n,d\n", + "\n", + "f.SetValue(n,d) #call function SetValue\n", + "print \"Numerator value set: \", ' ',n\n", + "print \"Denominator value set: \", ' ',d\n", + "f.GetValue(n,d) #call function GetData\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d\n", + "print \"Now a second clone copy is being created: \",''\n", + "f1=f\n", + "f1.GetValue(n,d)\n", + "print \"Clone's numerator value retrieved: \", ' ',n\n", + "print \"Clone's denominator value retrieved: \", ' ',d" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter n: 5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter d: 6\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "enter the numerator and denominator: 5 6\n", + "Numerator value set: 5\n", + "Denominator value set: 6\n", + "Numerator value retrieved: 5\n", + "Denominator value retrieved: 6\n", + "Now a second clone copy is being created: \n", + "Clone's numerator value retrieved: 5\n", + "Clone's denominator value retrieved: 6\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.9, page no:229" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def MyNewHandler():\n", + " print \"Sorry operator new failed to allocate memory\"\n", + " exit(0)\n", + " \n", + "def _set_new_handler(s):\n", + " s()\n", + "#In python there is no in-built _set_new_handler function, so i made this function and passed MyNewHandler function as a parameters\n", + "_set_new_handler(MyNewHandler)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Sorry operator new failed to allocate memory\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.10, page no:230" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from ctypes import * \n", + "class Fraction: \n", + " def __init__(self,a=0,b=1): \n", + " if isinstance(a,int): \n", + " c = c_int(a) \n", + " d = c_int(b) \n", + " self.__num = pointer(c) \n", + " self.__denom = pointer(d) \n", + " print 'constructor sets numerator = ', self.__num[0] , ', denominator = ', self.__denom[0] \n", + " else:\n", + " c=c_int(a.__num[0])\n", + " d = c_int(a.__denom[0])\n", + " self.__num = pointer(c) \n", + " self.__denom = pointer(d)\n", + " print 'copy constructor sets numerator = ', self.__num[0] , ', denominator = ', self.__denom[0] \n", + " \n", + " def __del__(self): \n", + " print 'destructor deallocates numerator = ', self.__num[0] , ', denominator = ', self.__denom[0] \n", + " \n", + "n = input(\"Please enter values of numerator: \") \n", + "d = input(\"Please enter values of denominator: \") \n", + "f = Fraction(n,d) \n", + "print 'Please enter another set of ' \n", + "n = input(\"numerator: \") \n", + "d = input(\"denominator: \") \n", + "print 'Creating fraction *pf : ' \n", + "pf = Fraction(n,d) \n", + "print 'Now a clone copy (f2) created from *pf: ' \n", + "f2 = Fraction(pf)\n", + "print 'Now another clone copy (*pf2) created from f:' \n", + "pf2 = Fraction(f) \n", + "print '*pf2 is being destroyed:' \n", + "del pf2\n", + "print '*pf is being destroyed:' \n", + "del pf \n", + "print 'now objects f2 and f automatically destroyed : '" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter values of numerator: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter values of denominator: 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "constructor sets numerator = 3 , denominator = 4\n", + "Please enter another set of \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "numerator: 5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "denominator: 6\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Creating fraction *pf : \n", + "constructor sets numerator = 5 , denominator = 6\n", + "Now a clone copy (f2) created from *pf: \n", + "copy constructor sets numerator = 5 , denominator = 6\n", + "Now another clone copy (*pf2) created from f:\n", + "copy constructor sets numerator = 3 , denominator = 4\n", + "*pf2 is being destroyed:\n", + "*pf is being destroyed:\n", + "now objects f2 and f automatically destroyed : \n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.11, page no:234" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def Memfail(self,s):\n", + " print \"Sorry Unable to allocate memory\"\n", + " sys.exit(0)\n", + "\n", + "MAX_SIZE = 60 + 1\n", + "\n", + "MAX_SIZE=[[0 for col in range(MAX_SIZE)]for row in range(MAX_SIZE)]\n", + "nChar=0\n", + "chArr=\"Hello\"\n", + "\n", + "print \"Please input a string( 60 characters max.): \",chArr\n", + "\n", + "nChar=len(chArr)+1\n", + "szStr=chArr\n", + "print \"required memory space for\",nChar,\n", + "print \"characters\"\n", + "chArr=szStr #string copy\n", + "szStr=chArr\n", + "print \"String copied in allocated space: \",szStr\n", + "print \"Memory space dellocated\"\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please input a string( 60 characters max.): Hello\n", + "required memory space for 6 characters\n", + "String copied in allocated space: Hello\n", + "Memory space dellocated\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.12, page no:236" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction:\n", + " \n", + " def __init__(self,a=0,b=1): #constructor\n", + " self.__num=a\n", + " self.__denom=b\n", + " \n", + " def __del__(self): #destructor\n", + " pass\n", + " \n", + " def GetValue(self,a,b):\n", + " a=self.__num\n", + " b=self.__denom\n", + " return self.__num,self.__denom\n", + "n=4\n", + "d=5\n", + "f=Fraction(4,5)\n", + "n,d=f.GetValue(n,d)" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.13, page no:239" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class X:\n", + " __sa=20 #initialising static member\n", + " a = None\n", + " def __init__(self):\n", + " self.a=None #public member\n", + " def f(self,b):\n", + " a=30\n", + " print \"Global a= \",b\n", + " print \"Local a= \",a\n", + " print \"Nonstatic member a= \",self.a\n", + " print \"static member sa= \",self.__sa\n", + "\n", + "a=10\n", + "\n", + "aXobj=X()\n", + "aXobj.a=40\n", + "aXobj.f(a)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Global a= 10\n", + "Local a= 30\n", + "Nonstatic member a= 40\n", + "static member sa= 20\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [], + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/sample_notebooks/NitamoniDas/chapter_8_Data_Abstraction_through_Classes_and_User-Defined_Data_Types_3.ipynb b/sample_notebooks/NitamoniDas/chapter_8_Data_Abstraction_through_Classes_and_User-Defined_Data_Types_3.ipynb new file mode 100755 index 00000000..1083b19a --- /dev/null +++ b/sample_notebooks/NitamoniDas/chapter_8_Data_Abstraction_through_Classes_and_User-Defined_Data_Types_3.ipynb @@ -0,0 +1,809 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:dbbd8ed92141bcc804ff2fbeacf8bb85f122dc6e196e1d830291a4a449a3a439" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 8: Data Abstraction through Classes and User-Defined Data Types " + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.1, page no: 208" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Super:\n", + " def __init__(self):\n", + " self.__IntegerData=None #private member\n", + " #public functions\n", + " def SetData(self,i):\n", + " self.__IntegerData=i #refer to IntegerData\n", + " def ShowData(self):\n", + " print \"Data is \",self.__IntegerData,' '\n", + "\n", + "ob1=Super()\n", + "ob2=Super()\n", + "ob1.SetData(1000)\n", + "ob2.SetData(2000)\n", + "ob1.ShowData()\n", + "ob2.ShowData()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Data is 1000 \n", + "Data is 2000 \n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.2, page no:211" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class X:\n", + " def __init__(self):\n", + " self.a=None #private members\n", + " self.b=None #private members\n", + " \n", + "#no structure type present in python \n", + "x = X()\n", + "\n", + "x.a=0\n", + "x.b=1\n", + "print \"x.a=\",x.a,\",x.b=\",x.b" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "x.a= 0 ,x.b= 1\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.3, page no:214" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction:\n", + " \n", + " def SetValue(self,a,b): #public functions\n", + " self.__num=a \n", + " self.__denom=b \n", + " def GetValue(self,a,b):\n", + " a=self.__num\n", + " b=self.__denom\n", + " \n", + "f=Fraction()\n", + "n=input(\"Enter n: \") #user input\n", + "d=input(\"Enter d: \") #user input\n", + "print \"enter the numerator and denominator: \", ' ',n,d\n", + "\n", + "f.SetValue(n,d) #call function SetValue\n", + "print \"Numerator value set: \", ' ',n\n", + "print \"Denominator value set: \", ' ',d\n", + "f.GetValue(n,d) #call function GetData\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter n: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter d: 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "enter the numerator and denominator: 3 4\n", + "Numerator value set: 3\n", + "Denominator value set: 4\n", + "Numerator value retrieved: 3\n", + "Denominator value retrieved: 4\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.4, page no:216" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class SimpleClass:\n", + " def __init__(self):\n", + " self.__i=None #private member\n", + " \n", + " def __init__(self):\n", + " self.__i=500 #constructor\n", + " \n", + " def SetData(self,d):\n", + " self.__i=d\n", + " \n", + " def GetData(self):\n", + " return self.__i\n", + " \n", + "#Initializing\n", + "s1=SimpleClass()\n", + "s2=SimpleClass()\n", + "print \"s1 has data: \",s1.GetData()\n", + "print \"s2 has data: \",s2.GetData()\n", + "s1.SetData(1000) #function call\n", + "s2.SetData(2000)\n", + "print \"s1 has data: \",s1.GetData(),' '\n", + "print \"s2 has data: \", s2.GetData(),' '\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "s1 has data: 500\n", + "s2 has data: 500\n", + "s1 has data: 1000 \n", + "s2 has data: 2000 \n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.5, page no:217" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class SimpleClass:\n", + " __IntegerData=None\n", + " def __init__(self,data=None):\n", + " if data==None:\n", + " self.__IntegerData=500 #default constructor\n", + " else:\n", + " self.__IntegerData=data #parameterised constructor\n", + " \n", + " def SetData(self,d):\n", + " self.__IntegerData=d\n", + " \n", + " def GetData(self):\n", + " return self.__IntegerData\n", + " \n", + "#Initializing\n", + "s1=SimpleClass()\n", + "s2=SimpleClass()\n", + "s3=SimpleClass(400)\n", + "s4=SimpleClass(600)\n", + "print \"s1 has data: \",s1.GetData(),' '\n", + "print \"s2 has data: \",s2.GetData(),' '\n", + "print \"s3 has data: \",s3.GetData(),' '\n", + "print \"s4 has data: \",s4.GetData(),' '\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "s1 has data: 500 \n", + "s2 has data: 500 \n", + "s3 has data: 400 \n", + "s4 has data: 600 \n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.6, page no:218" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction: \n", + " def_init_num=None #private members\n", + " def_init_denom=None #private members\n", + " def __init__(self,a=0,b=1):\n", + " self.__num=a\n", + " self.__denom=b\n", + " print \"Numerator set inside constructor\",' ',n\n", + " print \"Denominator set inside constructor\",' ',d\n", + " \n", + " def SetValue(self,a,b):\n", + " self.__num=a\n", + " self.__denom=b\n", + " def GetValue(self,a,b):\n", + " a= self.__num\n", + " b= self.__denom\n", + " return a,b\n", + " \n", + " def __del__(self): #destructor\n", + " pass\n", + " \n", + "n=input(\"Enter n: \") #user input\n", + "d=input(\"Enter d: \") #user input\n", + "print \"Please enter value of numerator and denominator: \",' ',n,d\n", + "f=Fraction(n,d)\n", + "f.GetValue(n,d)\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter n: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter d: 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter value of numerator and denominator: 3 4\n", + "Numerator set inside constructor 3\n", + "Denominator set inside constructor 4\n", + "Numerator value retrieved: 3\n", + "Denominator value retrieved: 4\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.7, page no:221" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction:\n", + " \n", + " def __init__(self,a=0,b=1): #constructor\n", + " self.__num=a\n", + " self.__denom=b\n", + " print \"Numerator set inside constructor: \",self.__num\n", + " print \"Denominator set inside constructor: \",self.__denom \n", + " \n", + " def GetValue(self,a,b):\n", + " return self.__num,self.__denom\n", + "n=input(\"Enter n: \") #user input\n", + "d=input(\"Enter d: \") #user input\n", + "print \"Please enter the value of the numerator and denominator: \",n,d\n", + "f=Fraction(n,d)\n", + "n,d=f.GetValue(n,d)\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d\n", + "n=input(\"Please enter the value of numerator only: \")\n", + "f1=Fraction(n)\n", + "n,d=f1.GetValue(n,d)\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d\n", + "print 'ok..now I will create a fraction-no input please'\n", + "f2=Fraction()\n", + "n,d=f2.GetValue(n,d)\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter n: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter d: 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter the value of the numerator and denominator: 3 4\n", + "Numerator set inside constructor: 3\n", + "Denominator set inside constructor: 4\n", + "Numerator value retrieved: 3\n", + "Denominator value retrieved: 4\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter the value of numerator only: 3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Numerator set inside constructor: 3\n", + "Denominator set inside constructor: 1\n", + "Numerator value retrieved: 3\n", + "Denominator value retrieved: 1\n", + "ok..now I will create a fraction-no input please\n", + "Numerator set inside constructor: 0\n", + "Denominator set inside constructor: 1\n", + "Numerator value retrieved: 0\n", + "Denominator value retrieved: 1\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.8, page no:223" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction:\n", + " def_init_num=None #private members\n", + " def_init_denom=None #private members\n", + " \n", + " def __init__(self,anotherFraction=None): \n", + " if anotherFraction==None: #normal constructor\n", + " self.__num=anotherFraction\n", + " self.__denom=anotherFraction\n", + " else: #copy constructor\n", + " self.__num=anotherFraction.self.__num\n", + " self.__denom=anotherFraction.self.__denom\n", + " \n", + " \n", + " #public functions\n", + " def SetValue(self,a,b):\n", + " self.__num=a\n", + " self.__denom=b#refer to IntegerData\n", + " def GetValue(self,a,b):\n", + " a=self.__num\n", + " b=self.__denom\n", + " return a,b\n", + " \n", + "f=Fraction()\n", + "n=input(\"Enter n: \") #user input\n", + "d=input(\"Enter d: \") #user input\n", + "print \"enter the numerator and denominator: \", ' ',n,d\n", + "\n", + "f.SetValue(n,d) #call function SetValue\n", + "print \"Numerator value set: \", ' ',n\n", + "print \"Denominator value set: \", ' ',d\n", + "f.GetValue(n,d) #call function GetData\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d\n", + "print \"Now a second clone copy is being created: \",''\n", + "f1=f\n", + "f1.GetValue(n,d)\n", + "print \"Clone's numerator value retrieved: \", ' ',n\n", + "print \"Clone's denominator value retrieved: \", ' ',d" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter n: 5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter d: 6\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "enter the numerator and denominator: 5 6\n", + "Numerator value set: 5\n", + "Denominator value set: 6\n", + "Numerator value retrieved: 5\n", + "Denominator value retrieved: 6\n", + "Now a second clone copy is being created: \n", + "Clone's numerator value retrieved: 5\n", + "Clone's denominator value retrieved: 6\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.9, page no:229" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def MyNewHandler():\n", + " print \"Sorry operator new failed to allocate memory\"\n", + " exit(0)\n", + " \n", + "def _set_new_handler(s):\n", + " s()\n", + "#In python there is no in-built _set_new_handler function, so i made this function and passed MyNewHandler function as a parameters\n", + "_set_new_handler(MyNewHandler)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Sorry operator new failed to allocate memory\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.10, page no:230" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from ctypes import * \n", + "class Fraction: \n", + " def __init__(self,a=0,b=1): \n", + " if isinstance(a,int): \n", + " c = c_int(a) \n", + " d = c_int(b) \n", + " self.__num = pointer(c) \n", + " self.__denom = pointer(d) \n", + " print 'constructor sets numerator = ', self.__num[0] , ', denominator = ', self.__denom[0] \n", + " else:\n", + " c=c_int(a.__num[0])\n", + " d = c_int(a.__denom[0])\n", + " self.__num = pointer(c) \n", + " self.__denom = pointer(d)\n", + " print 'copy constructor sets numerator = ', self.__num[0] , ', denominator = ', self.__denom[0] \n", + " \n", + " def __del__(self): \n", + " print 'destructor deallocates numerator = ', self.__num[0] , ', denominator = ', self.__denom[0] \n", + " \n", + "n = input(\"Please enter values of numerator: \") \n", + "d = input(\"Please enter values of denominator: \") \n", + "f = Fraction(n,d) \n", + "print 'Please enter another set of ' \n", + "n = input(\"numerator: \") \n", + "d = input(\"denominator: \") \n", + "print 'Creating fraction *pf : ' \n", + "pf = Fraction(n,d) \n", + "print 'Now a clone copy (f2) created from *pf: ' \n", + "f2 = Fraction(pf)\n", + "print 'Now another clone copy (*pf2) created from f:' \n", + "pf2 = Fraction(f) \n", + "print '*pf2 is being destroyed:' \n", + "del pf2\n", + "print '*pf is being destroyed:' \n", + "del pf \n", + "print 'now objects f2 and f automatically destroyed : '" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter values of numerator: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter values of denominator: 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "constructor sets numerator = 3 , denominator = 4\n", + "Please enter another set of \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "numerator: 5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "denominator: 6\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Creating fraction *pf : \n", + "constructor sets numerator = 5 , denominator = 6\n", + "Now a clone copy (f2) created from *pf: \n", + "copy constructor sets numerator = 5 , denominator = 6\n", + "Now another clone copy (*pf2) created from f:\n", + "copy constructor sets numerator = 3 , denominator = 4\n", + "*pf2 is being destroyed:\n", + "*pf is being destroyed:\n", + "now objects f2 and f automatically destroyed : \n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.11, page no:234" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def Memfail(self,s):\n", + " print \"Sorry Unable to allocate memory\"\n", + " sys.exit(0)\n", + "\n", + "MAX_SIZE = 60 + 1\n", + "\n", + "MAX_SIZE=[[0 for col in range(MAX_SIZE)]for row in range(MAX_SIZE)]\n", + "nChar=0\n", + "chArr=\"Hello\"\n", + "\n", + "print \"Please input a string( 60 characters max.): \",chArr\n", + "\n", + "nChar=len(chArr)+1\n", + "szStr=chArr\n", + "print \"required memory space for\",nChar,\n", + "print \"characters\"\n", + "chArr=szStr #string copy\n", + "szStr=chArr\n", + "print \"String copied in allocated space: \",szStr\n", + "print \"Memory space dellocated\"\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please input a string( 60 characters max.): Hello\n", + "required memory space for 6 characters\n", + "String copied in allocated space: Hello\n", + "Memory space dellocated\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.12, page no:236" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction:\n", + " \n", + " def __init__(self,a=0,b=1): #constructor\n", + " self.__num=a\n", + " self.__denom=b\n", + " \n", + " def __del__(self): #destructor\n", + " pass\n", + " \n", + " def GetValue(self,a,b):\n", + " a=self.__num\n", + " b=self.__denom\n", + " return self.__num,self.__denom\n", + "n=4\n", + "d=5\n", + "f=Fraction(4,5)\n", + "n,d=f.GetValue(n,d)" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.13, page no:239" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class X:\n", + " __sa=20 #initialising static member\n", + " a = None\n", + " def __init__(self):\n", + " self.a=None #public member\n", + " def f(self,b):\n", + " a=30\n", + " print \"Global a= \",b\n", + " print \"Local a= \",a\n", + " print \"Nonstatic member a= \",self.a\n", + " print \"static member sa= \",self.__sa\n", + "\n", + "a=10\n", + "\n", + "aXobj=X()\n", + "aXobj.a=40\n", + "aXobj.f(a)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Global a= 10\n", + "Local a= 30\n", + "Nonstatic member a= 40\n", + "static member sa= 20\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [], + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/sample_notebooks/NitamoniDas/chapter_8_Data_Abstraction_through_Classes_and_User-Defined_Data_Types_4.ipynb b/sample_notebooks/NitamoniDas/chapter_8_Data_Abstraction_through_Classes_and_User-Defined_Data_Types_4.ipynb new file mode 100755 index 00000000..1083b19a --- /dev/null +++ b/sample_notebooks/NitamoniDas/chapter_8_Data_Abstraction_through_Classes_and_User-Defined_Data_Types_4.ipynb @@ -0,0 +1,809 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:dbbd8ed92141bcc804ff2fbeacf8bb85f122dc6e196e1d830291a4a449a3a439" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 8: Data Abstraction through Classes and User-Defined Data Types " + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.1, page no: 208" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Super:\n", + " def __init__(self):\n", + " self.__IntegerData=None #private member\n", + " #public functions\n", + " def SetData(self,i):\n", + " self.__IntegerData=i #refer to IntegerData\n", + " def ShowData(self):\n", + " print \"Data is \",self.__IntegerData,' '\n", + "\n", + "ob1=Super()\n", + "ob2=Super()\n", + "ob1.SetData(1000)\n", + "ob2.SetData(2000)\n", + "ob1.ShowData()\n", + "ob2.ShowData()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Data is 1000 \n", + "Data is 2000 \n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.2, page no:211" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class X:\n", + " def __init__(self):\n", + " self.a=None #private members\n", + " self.b=None #private members\n", + " \n", + "#no structure type present in python \n", + "x = X()\n", + "\n", + "x.a=0\n", + "x.b=1\n", + "print \"x.a=\",x.a,\",x.b=\",x.b" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "x.a= 0 ,x.b= 1\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.3, page no:214" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction:\n", + " \n", + " def SetValue(self,a,b): #public functions\n", + " self.__num=a \n", + " self.__denom=b \n", + " def GetValue(self,a,b):\n", + " a=self.__num\n", + " b=self.__denom\n", + " \n", + "f=Fraction()\n", + "n=input(\"Enter n: \") #user input\n", + "d=input(\"Enter d: \") #user input\n", + "print \"enter the numerator and denominator: \", ' ',n,d\n", + "\n", + "f.SetValue(n,d) #call function SetValue\n", + "print \"Numerator value set: \", ' ',n\n", + "print \"Denominator value set: \", ' ',d\n", + "f.GetValue(n,d) #call function GetData\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter n: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter d: 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "enter the numerator and denominator: 3 4\n", + "Numerator value set: 3\n", + "Denominator value set: 4\n", + "Numerator value retrieved: 3\n", + "Denominator value retrieved: 4\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.4, page no:216" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class SimpleClass:\n", + " def __init__(self):\n", + " self.__i=None #private member\n", + " \n", + " def __init__(self):\n", + " self.__i=500 #constructor\n", + " \n", + " def SetData(self,d):\n", + " self.__i=d\n", + " \n", + " def GetData(self):\n", + " return self.__i\n", + " \n", + "#Initializing\n", + "s1=SimpleClass()\n", + "s2=SimpleClass()\n", + "print \"s1 has data: \",s1.GetData()\n", + "print \"s2 has data: \",s2.GetData()\n", + "s1.SetData(1000) #function call\n", + "s2.SetData(2000)\n", + "print \"s1 has data: \",s1.GetData(),' '\n", + "print \"s2 has data: \", s2.GetData(),' '\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "s1 has data: 500\n", + "s2 has data: 500\n", + "s1 has data: 1000 \n", + "s2 has data: 2000 \n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.5, page no:217" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class SimpleClass:\n", + " __IntegerData=None\n", + " def __init__(self,data=None):\n", + " if data==None:\n", + " self.__IntegerData=500 #default constructor\n", + " else:\n", + " self.__IntegerData=data #parameterised constructor\n", + " \n", + " def SetData(self,d):\n", + " self.__IntegerData=d\n", + " \n", + " def GetData(self):\n", + " return self.__IntegerData\n", + " \n", + "#Initializing\n", + "s1=SimpleClass()\n", + "s2=SimpleClass()\n", + "s3=SimpleClass(400)\n", + "s4=SimpleClass(600)\n", + "print \"s1 has data: \",s1.GetData(),' '\n", + "print \"s2 has data: \",s2.GetData(),' '\n", + "print \"s3 has data: \",s3.GetData(),' '\n", + "print \"s4 has data: \",s4.GetData(),' '\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "s1 has data: 500 \n", + "s2 has data: 500 \n", + "s3 has data: 400 \n", + "s4 has data: 600 \n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.6, page no:218" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction: \n", + " def_init_num=None #private members\n", + " def_init_denom=None #private members\n", + " def __init__(self,a=0,b=1):\n", + " self.__num=a\n", + " self.__denom=b\n", + " print \"Numerator set inside constructor\",' ',n\n", + " print \"Denominator set inside constructor\",' ',d\n", + " \n", + " def SetValue(self,a,b):\n", + " self.__num=a\n", + " self.__denom=b\n", + " def GetValue(self,a,b):\n", + " a= self.__num\n", + " b= self.__denom\n", + " return a,b\n", + " \n", + " def __del__(self): #destructor\n", + " pass\n", + " \n", + "n=input(\"Enter n: \") #user input\n", + "d=input(\"Enter d: \") #user input\n", + "print \"Please enter value of numerator and denominator: \",' ',n,d\n", + "f=Fraction(n,d)\n", + "f.GetValue(n,d)\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter n: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter d: 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter value of numerator and denominator: 3 4\n", + "Numerator set inside constructor 3\n", + "Denominator set inside constructor 4\n", + "Numerator value retrieved: 3\n", + "Denominator value retrieved: 4\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.7, page no:221" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction:\n", + " \n", + " def __init__(self,a=0,b=1): #constructor\n", + " self.__num=a\n", + " self.__denom=b\n", + " print \"Numerator set inside constructor: \",self.__num\n", + " print \"Denominator set inside constructor: \",self.__denom \n", + " \n", + " def GetValue(self,a,b):\n", + " return self.__num,self.__denom\n", + "n=input(\"Enter n: \") #user input\n", + "d=input(\"Enter d: \") #user input\n", + "print \"Please enter the value of the numerator and denominator: \",n,d\n", + "f=Fraction(n,d)\n", + "n,d=f.GetValue(n,d)\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d\n", + "n=input(\"Please enter the value of numerator only: \")\n", + "f1=Fraction(n)\n", + "n,d=f1.GetValue(n,d)\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d\n", + "print 'ok..now I will create a fraction-no input please'\n", + "f2=Fraction()\n", + "n,d=f2.GetValue(n,d)\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter n: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter d: 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter the value of the numerator and denominator: 3 4\n", + "Numerator set inside constructor: 3\n", + "Denominator set inside constructor: 4\n", + "Numerator value retrieved: 3\n", + "Denominator value retrieved: 4\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter the value of numerator only: 3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Numerator set inside constructor: 3\n", + "Denominator set inside constructor: 1\n", + "Numerator value retrieved: 3\n", + "Denominator value retrieved: 1\n", + "ok..now I will create a fraction-no input please\n", + "Numerator set inside constructor: 0\n", + "Denominator set inside constructor: 1\n", + "Numerator value retrieved: 0\n", + "Denominator value retrieved: 1\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.8, page no:223" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction:\n", + " def_init_num=None #private members\n", + " def_init_denom=None #private members\n", + " \n", + " def __init__(self,anotherFraction=None): \n", + " if anotherFraction==None: #normal constructor\n", + " self.__num=anotherFraction\n", + " self.__denom=anotherFraction\n", + " else: #copy constructor\n", + " self.__num=anotherFraction.self.__num\n", + " self.__denom=anotherFraction.self.__denom\n", + " \n", + " \n", + " #public functions\n", + " def SetValue(self,a,b):\n", + " self.__num=a\n", + " self.__denom=b#refer to IntegerData\n", + " def GetValue(self,a,b):\n", + " a=self.__num\n", + " b=self.__denom\n", + " return a,b\n", + " \n", + "f=Fraction()\n", + "n=input(\"Enter n: \") #user input\n", + "d=input(\"Enter d: \") #user input\n", + "print \"enter the numerator and denominator: \", ' ',n,d\n", + "\n", + "f.SetValue(n,d) #call function SetValue\n", + "print \"Numerator value set: \", ' ',n\n", + "print \"Denominator value set: \", ' ',d\n", + "f.GetValue(n,d) #call function GetData\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d\n", + "print \"Now a second clone copy is being created: \",''\n", + "f1=f\n", + "f1.GetValue(n,d)\n", + "print \"Clone's numerator value retrieved: \", ' ',n\n", + "print \"Clone's denominator value retrieved: \", ' ',d" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter n: 5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter d: 6\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "enter the numerator and denominator: 5 6\n", + "Numerator value set: 5\n", + "Denominator value set: 6\n", + "Numerator value retrieved: 5\n", + "Denominator value retrieved: 6\n", + "Now a second clone copy is being created: \n", + "Clone's numerator value retrieved: 5\n", + "Clone's denominator value retrieved: 6\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.9, page no:229" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def MyNewHandler():\n", + " print \"Sorry operator new failed to allocate memory\"\n", + " exit(0)\n", + " \n", + "def _set_new_handler(s):\n", + " s()\n", + "#In python there is no in-built _set_new_handler function, so i made this function and passed MyNewHandler function as a parameters\n", + "_set_new_handler(MyNewHandler)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Sorry operator new failed to allocate memory\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.10, page no:230" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from ctypes import * \n", + "class Fraction: \n", + " def __init__(self,a=0,b=1): \n", + " if isinstance(a,int): \n", + " c = c_int(a) \n", + " d = c_int(b) \n", + " self.__num = pointer(c) \n", + " self.__denom = pointer(d) \n", + " print 'constructor sets numerator = ', self.__num[0] , ', denominator = ', self.__denom[0] \n", + " else:\n", + " c=c_int(a.__num[0])\n", + " d = c_int(a.__denom[0])\n", + " self.__num = pointer(c) \n", + " self.__denom = pointer(d)\n", + " print 'copy constructor sets numerator = ', self.__num[0] , ', denominator = ', self.__denom[0] \n", + " \n", + " def __del__(self): \n", + " print 'destructor deallocates numerator = ', self.__num[0] , ', denominator = ', self.__denom[0] \n", + " \n", + "n = input(\"Please enter values of numerator: \") \n", + "d = input(\"Please enter values of denominator: \") \n", + "f = Fraction(n,d) \n", + "print 'Please enter another set of ' \n", + "n = input(\"numerator: \") \n", + "d = input(\"denominator: \") \n", + "print 'Creating fraction *pf : ' \n", + "pf = Fraction(n,d) \n", + "print 'Now a clone copy (f2) created from *pf: ' \n", + "f2 = Fraction(pf)\n", + "print 'Now another clone copy (*pf2) created from f:' \n", + "pf2 = Fraction(f) \n", + "print '*pf2 is being destroyed:' \n", + "del pf2\n", + "print '*pf is being destroyed:' \n", + "del pf \n", + "print 'now objects f2 and f automatically destroyed : '" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter values of numerator: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter values of denominator: 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "constructor sets numerator = 3 , denominator = 4\n", + "Please enter another set of \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "numerator: 5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "denominator: 6\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Creating fraction *pf : \n", + "constructor sets numerator = 5 , denominator = 6\n", + "Now a clone copy (f2) created from *pf: \n", + "copy constructor sets numerator = 5 , denominator = 6\n", + "Now another clone copy (*pf2) created from f:\n", + "copy constructor sets numerator = 3 , denominator = 4\n", + "*pf2 is being destroyed:\n", + "*pf is being destroyed:\n", + "now objects f2 and f automatically destroyed : \n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.11, page no:234" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def Memfail(self,s):\n", + " print \"Sorry Unable to allocate memory\"\n", + " sys.exit(0)\n", + "\n", + "MAX_SIZE = 60 + 1\n", + "\n", + "MAX_SIZE=[[0 for col in range(MAX_SIZE)]for row in range(MAX_SIZE)]\n", + "nChar=0\n", + "chArr=\"Hello\"\n", + "\n", + "print \"Please input a string( 60 characters max.): \",chArr\n", + "\n", + "nChar=len(chArr)+1\n", + "szStr=chArr\n", + "print \"required memory space for\",nChar,\n", + "print \"characters\"\n", + "chArr=szStr #string copy\n", + "szStr=chArr\n", + "print \"String copied in allocated space: \",szStr\n", + "print \"Memory space dellocated\"\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please input a string( 60 characters max.): Hello\n", + "required memory space for 6 characters\n", + "String copied in allocated space: Hello\n", + "Memory space dellocated\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.12, page no:236" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction:\n", + " \n", + " def __init__(self,a=0,b=1): #constructor\n", + " self.__num=a\n", + " self.__denom=b\n", + " \n", + " def __del__(self): #destructor\n", + " pass\n", + " \n", + " def GetValue(self,a,b):\n", + " a=self.__num\n", + " b=self.__denom\n", + " return self.__num,self.__denom\n", + "n=4\n", + "d=5\n", + "f=Fraction(4,5)\n", + "n,d=f.GetValue(n,d)" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.13, page no:239" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class X:\n", + " __sa=20 #initialising static member\n", + " a = None\n", + " def __init__(self):\n", + " self.a=None #public member\n", + " def f(self,b):\n", + " a=30\n", + " print \"Global a= \",b\n", + " print \"Local a= \",a\n", + " print \"Nonstatic member a= \",self.a\n", + " print \"static member sa= \",self.__sa\n", + "\n", + "a=10\n", + "\n", + "aXobj=X()\n", + "aXobj.a=40\n", + "aXobj.f(a)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Global a= 10\n", + "Local a= 30\n", + "Nonstatic member a= 40\n", + "static member sa= 20\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [], + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/sample_notebooks/NitamoniDas/chapter_8_Data_Abstraction_through_Classes_and_User-Defined_Data_Types_5.ipynb b/sample_notebooks/NitamoniDas/chapter_8_Data_Abstraction_through_Classes_and_User-Defined_Data_Types_5.ipynb new file mode 100755 index 00000000..1083b19a --- /dev/null +++ b/sample_notebooks/NitamoniDas/chapter_8_Data_Abstraction_through_Classes_and_User-Defined_Data_Types_5.ipynb @@ -0,0 +1,809 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:dbbd8ed92141bcc804ff2fbeacf8bb85f122dc6e196e1d830291a4a449a3a439" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 8: Data Abstraction through Classes and User-Defined Data Types " + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.1, page no: 208" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Super:\n", + " def __init__(self):\n", + " self.__IntegerData=None #private member\n", + " #public functions\n", + " def SetData(self,i):\n", + " self.__IntegerData=i #refer to IntegerData\n", + " def ShowData(self):\n", + " print \"Data is \",self.__IntegerData,' '\n", + "\n", + "ob1=Super()\n", + "ob2=Super()\n", + "ob1.SetData(1000)\n", + "ob2.SetData(2000)\n", + "ob1.ShowData()\n", + "ob2.ShowData()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Data is 1000 \n", + "Data is 2000 \n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.2, page no:211" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class X:\n", + " def __init__(self):\n", + " self.a=None #private members\n", + " self.b=None #private members\n", + " \n", + "#no structure type present in python \n", + "x = X()\n", + "\n", + "x.a=0\n", + "x.b=1\n", + "print \"x.a=\",x.a,\",x.b=\",x.b" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "x.a= 0 ,x.b= 1\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.3, page no:214" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction:\n", + " \n", + " def SetValue(self,a,b): #public functions\n", + " self.__num=a \n", + " self.__denom=b \n", + " def GetValue(self,a,b):\n", + " a=self.__num\n", + " b=self.__denom\n", + " \n", + "f=Fraction()\n", + "n=input(\"Enter n: \") #user input\n", + "d=input(\"Enter d: \") #user input\n", + "print \"enter the numerator and denominator: \", ' ',n,d\n", + "\n", + "f.SetValue(n,d) #call function SetValue\n", + "print \"Numerator value set: \", ' ',n\n", + "print \"Denominator value set: \", ' ',d\n", + "f.GetValue(n,d) #call function GetData\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter n: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter d: 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "enter the numerator and denominator: 3 4\n", + "Numerator value set: 3\n", + "Denominator value set: 4\n", + "Numerator value retrieved: 3\n", + "Denominator value retrieved: 4\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.4, page no:216" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class SimpleClass:\n", + " def __init__(self):\n", + " self.__i=None #private member\n", + " \n", + " def __init__(self):\n", + " self.__i=500 #constructor\n", + " \n", + " def SetData(self,d):\n", + " self.__i=d\n", + " \n", + " def GetData(self):\n", + " return self.__i\n", + " \n", + "#Initializing\n", + "s1=SimpleClass()\n", + "s2=SimpleClass()\n", + "print \"s1 has data: \",s1.GetData()\n", + "print \"s2 has data: \",s2.GetData()\n", + "s1.SetData(1000) #function call\n", + "s2.SetData(2000)\n", + "print \"s1 has data: \",s1.GetData(),' '\n", + "print \"s2 has data: \", s2.GetData(),' '\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "s1 has data: 500\n", + "s2 has data: 500\n", + "s1 has data: 1000 \n", + "s2 has data: 2000 \n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.5, page no:217" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class SimpleClass:\n", + " __IntegerData=None\n", + " def __init__(self,data=None):\n", + " if data==None:\n", + " self.__IntegerData=500 #default constructor\n", + " else:\n", + " self.__IntegerData=data #parameterised constructor\n", + " \n", + " def SetData(self,d):\n", + " self.__IntegerData=d\n", + " \n", + " def GetData(self):\n", + " return self.__IntegerData\n", + " \n", + "#Initializing\n", + "s1=SimpleClass()\n", + "s2=SimpleClass()\n", + "s3=SimpleClass(400)\n", + "s4=SimpleClass(600)\n", + "print \"s1 has data: \",s1.GetData(),' '\n", + "print \"s2 has data: \",s2.GetData(),' '\n", + "print \"s3 has data: \",s3.GetData(),' '\n", + "print \"s4 has data: \",s4.GetData(),' '\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "s1 has data: 500 \n", + "s2 has data: 500 \n", + "s3 has data: 400 \n", + "s4 has data: 600 \n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.6, page no:218" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction: \n", + " def_init_num=None #private members\n", + " def_init_denom=None #private members\n", + " def __init__(self,a=0,b=1):\n", + " self.__num=a\n", + " self.__denom=b\n", + " print \"Numerator set inside constructor\",' ',n\n", + " print \"Denominator set inside constructor\",' ',d\n", + " \n", + " def SetValue(self,a,b):\n", + " self.__num=a\n", + " self.__denom=b\n", + " def GetValue(self,a,b):\n", + " a= self.__num\n", + " b= self.__denom\n", + " return a,b\n", + " \n", + " def __del__(self): #destructor\n", + " pass\n", + " \n", + "n=input(\"Enter n: \") #user input\n", + "d=input(\"Enter d: \") #user input\n", + "print \"Please enter value of numerator and denominator: \",' ',n,d\n", + "f=Fraction(n,d)\n", + "f.GetValue(n,d)\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter n: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter d: 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter value of numerator and denominator: 3 4\n", + "Numerator set inside constructor 3\n", + "Denominator set inside constructor 4\n", + "Numerator value retrieved: 3\n", + "Denominator value retrieved: 4\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.7, page no:221" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction:\n", + " \n", + " def __init__(self,a=0,b=1): #constructor\n", + " self.__num=a\n", + " self.__denom=b\n", + " print \"Numerator set inside constructor: \",self.__num\n", + " print \"Denominator set inside constructor: \",self.__denom \n", + " \n", + " def GetValue(self,a,b):\n", + " return self.__num,self.__denom\n", + "n=input(\"Enter n: \") #user input\n", + "d=input(\"Enter d: \") #user input\n", + "print \"Please enter the value of the numerator and denominator: \",n,d\n", + "f=Fraction(n,d)\n", + "n,d=f.GetValue(n,d)\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d\n", + "n=input(\"Please enter the value of numerator only: \")\n", + "f1=Fraction(n)\n", + "n,d=f1.GetValue(n,d)\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d\n", + "print 'ok..now I will create a fraction-no input please'\n", + "f2=Fraction()\n", + "n,d=f2.GetValue(n,d)\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter n: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter d: 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter the value of the numerator and denominator: 3 4\n", + "Numerator set inside constructor: 3\n", + "Denominator set inside constructor: 4\n", + "Numerator value retrieved: 3\n", + "Denominator value retrieved: 4\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter the value of numerator only: 3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Numerator set inside constructor: 3\n", + "Denominator set inside constructor: 1\n", + "Numerator value retrieved: 3\n", + "Denominator value retrieved: 1\n", + "ok..now I will create a fraction-no input please\n", + "Numerator set inside constructor: 0\n", + "Denominator set inside constructor: 1\n", + "Numerator value retrieved: 0\n", + "Denominator value retrieved: 1\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.8, page no:223" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction:\n", + " def_init_num=None #private members\n", + " def_init_denom=None #private members\n", + " \n", + " def __init__(self,anotherFraction=None): \n", + " if anotherFraction==None: #normal constructor\n", + " self.__num=anotherFraction\n", + " self.__denom=anotherFraction\n", + " else: #copy constructor\n", + " self.__num=anotherFraction.self.__num\n", + " self.__denom=anotherFraction.self.__denom\n", + " \n", + " \n", + " #public functions\n", + " def SetValue(self,a,b):\n", + " self.__num=a\n", + " self.__denom=b#refer to IntegerData\n", + " def GetValue(self,a,b):\n", + " a=self.__num\n", + " b=self.__denom\n", + " return a,b\n", + " \n", + "f=Fraction()\n", + "n=input(\"Enter n: \") #user input\n", + "d=input(\"Enter d: \") #user input\n", + "print \"enter the numerator and denominator: \", ' ',n,d\n", + "\n", + "f.SetValue(n,d) #call function SetValue\n", + "print \"Numerator value set: \", ' ',n\n", + "print \"Denominator value set: \", ' ',d\n", + "f.GetValue(n,d) #call function GetData\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d\n", + "print \"Now a second clone copy is being created: \",''\n", + "f1=f\n", + "f1.GetValue(n,d)\n", + "print \"Clone's numerator value retrieved: \", ' ',n\n", + "print \"Clone's denominator value retrieved: \", ' ',d" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter n: 5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter d: 6\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "enter the numerator and denominator: 5 6\n", + "Numerator value set: 5\n", + "Denominator value set: 6\n", + "Numerator value retrieved: 5\n", + "Denominator value retrieved: 6\n", + "Now a second clone copy is being created: \n", + "Clone's numerator value retrieved: 5\n", + "Clone's denominator value retrieved: 6\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.9, page no:229" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def MyNewHandler():\n", + " print \"Sorry operator new failed to allocate memory\"\n", + " exit(0)\n", + " \n", + "def _set_new_handler(s):\n", + " s()\n", + "#In python there is no in-built _set_new_handler function, so i made this function and passed MyNewHandler function as a parameters\n", + "_set_new_handler(MyNewHandler)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Sorry operator new failed to allocate memory\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.10, page no:230" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from ctypes import * \n", + "class Fraction: \n", + " def __init__(self,a=0,b=1): \n", + " if isinstance(a,int): \n", + " c = c_int(a) \n", + " d = c_int(b) \n", + " self.__num = pointer(c) \n", + " self.__denom = pointer(d) \n", + " print 'constructor sets numerator = ', self.__num[0] , ', denominator = ', self.__denom[0] \n", + " else:\n", + " c=c_int(a.__num[0])\n", + " d = c_int(a.__denom[0])\n", + " self.__num = pointer(c) \n", + " self.__denom = pointer(d)\n", + " print 'copy constructor sets numerator = ', self.__num[0] , ', denominator = ', self.__denom[0] \n", + " \n", + " def __del__(self): \n", + " print 'destructor deallocates numerator = ', self.__num[0] , ', denominator = ', self.__denom[0] \n", + " \n", + "n = input(\"Please enter values of numerator: \") \n", + "d = input(\"Please enter values of denominator: \") \n", + "f = Fraction(n,d) \n", + "print 'Please enter another set of ' \n", + "n = input(\"numerator: \") \n", + "d = input(\"denominator: \") \n", + "print 'Creating fraction *pf : ' \n", + "pf = Fraction(n,d) \n", + "print 'Now a clone copy (f2) created from *pf: ' \n", + "f2 = Fraction(pf)\n", + "print 'Now another clone copy (*pf2) created from f:' \n", + "pf2 = Fraction(f) \n", + "print '*pf2 is being destroyed:' \n", + "del pf2\n", + "print '*pf is being destroyed:' \n", + "del pf \n", + "print 'now objects f2 and f automatically destroyed : '" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter values of numerator: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter values of denominator: 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "constructor sets numerator = 3 , denominator = 4\n", + "Please enter another set of \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "numerator: 5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "denominator: 6\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Creating fraction *pf : \n", + "constructor sets numerator = 5 , denominator = 6\n", + "Now a clone copy (f2) created from *pf: \n", + "copy constructor sets numerator = 5 , denominator = 6\n", + "Now another clone copy (*pf2) created from f:\n", + "copy constructor sets numerator = 3 , denominator = 4\n", + "*pf2 is being destroyed:\n", + "*pf is being destroyed:\n", + "now objects f2 and f automatically destroyed : \n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.11, page no:234" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def Memfail(self,s):\n", + " print \"Sorry Unable to allocate memory\"\n", + " sys.exit(0)\n", + "\n", + "MAX_SIZE = 60 + 1\n", + "\n", + "MAX_SIZE=[[0 for col in range(MAX_SIZE)]for row in range(MAX_SIZE)]\n", + "nChar=0\n", + "chArr=\"Hello\"\n", + "\n", + "print \"Please input a string( 60 characters max.): \",chArr\n", + "\n", + "nChar=len(chArr)+1\n", + "szStr=chArr\n", + "print \"required memory space for\",nChar,\n", + "print \"characters\"\n", + "chArr=szStr #string copy\n", + "szStr=chArr\n", + "print \"String copied in allocated space: \",szStr\n", + "print \"Memory space dellocated\"\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please input a string( 60 characters max.): Hello\n", + "required memory space for 6 characters\n", + "String copied in allocated space: Hello\n", + "Memory space dellocated\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.12, page no:236" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction:\n", + " \n", + " def __init__(self,a=0,b=1): #constructor\n", + " self.__num=a\n", + " self.__denom=b\n", + " \n", + " def __del__(self): #destructor\n", + " pass\n", + " \n", + " def GetValue(self,a,b):\n", + " a=self.__num\n", + " b=self.__denom\n", + " return self.__num,self.__denom\n", + "n=4\n", + "d=5\n", + "f=Fraction(4,5)\n", + "n,d=f.GetValue(n,d)" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.13, page no:239" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class X:\n", + " __sa=20 #initialising static member\n", + " a = None\n", + " def __init__(self):\n", + " self.a=None #public member\n", + " def f(self,b):\n", + " a=30\n", + " print \"Global a= \",b\n", + " print \"Local a= \",a\n", + " print \"Nonstatic member a= \",self.a\n", + " print \"static member sa= \",self.__sa\n", + "\n", + "a=10\n", + "\n", + "aXobj=X()\n", + "aXobj.a=40\n", + "aXobj.f(a)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Global a= 10\n", + "Local a= 30\n", + "Nonstatic member a= 40\n", + "static member sa= 20\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [], + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/sample_notebooks/NitamoniDas/chapter_8_Data_Abstraction_through_Classes_and_User-Defined_Data_Types_6.ipynb b/sample_notebooks/NitamoniDas/chapter_8_Data_Abstraction_through_Classes_and_User-Defined_Data_Types_6.ipynb new file mode 100755 index 00000000..1083b19a --- /dev/null +++ b/sample_notebooks/NitamoniDas/chapter_8_Data_Abstraction_through_Classes_and_User-Defined_Data_Types_6.ipynb @@ -0,0 +1,809 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:dbbd8ed92141bcc804ff2fbeacf8bb85f122dc6e196e1d830291a4a449a3a439" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 8: Data Abstraction through Classes and User-Defined Data Types " + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.1, page no: 208" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Super:\n", + " def __init__(self):\n", + " self.__IntegerData=None #private member\n", + " #public functions\n", + " def SetData(self,i):\n", + " self.__IntegerData=i #refer to IntegerData\n", + " def ShowData(self):\n", + " print \"Data is \",self.__IntegerData,' '\n", + "\n", + "ob1=Super()\n", + "ob2=Super()\n", + "ob1.SetData(1000)\n", + "ob2.SetData(2000)\n", + "ob1.ShowData()\n", + "ob2.ShowData()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Data is 1000 \n", + "Data is 2000 \n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.2, page no:211" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class X:\n", + " def __init__(self):\n", + " self.a=None #private members\n", + " self.b=None #private members\n", + " \n", + "#no structure type present in python \n", + "x = X()\n", + "\n", + "x.a=0\n", + "x.b=1\n", + "print \"x.a=\",x.a,\",x.b=\",x.b" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "x.a= 0 ,x.b= 1\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.3, page no:214" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction:\n", + " \n", + " def SetValue(self,a,b): #public functions\n", + " self.__num=a \n", + " self.__denom=b \n", + " def GetValue(self,a,b):\n", + " a=self.__num\n", + " b=self.__denom\n", + " \n", + "f=Fraction()\n", + "n=input(\"Enter n: \") #user input\n", + "d=input(\"Enter d: \") #user input\n", + "print \"enter the numerator and denominator: \", ' ',n,d\n", + "\n", + "f.SetValue(n,d) #call function SetValue\n", + "print \"Numerator value set: \", ' ',n\n", + "print \"Denominator value set: \", ' ',d\n", + "f.GetValue(n,d) #call function GetData\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter n: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter d: 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "enter the numerator and denominator: 3 4\n", + "Numerator value set: 3\n", + "Denominator value set: 4\n", + "Numerator value retrieved: 3\n", + "Denominator value retrieved: 4\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.4, page no:216" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class SimpleClass:\n", + " def __init__(self):\n", + " self.__i=None #private member\n", + " \n", + " def __init__(self):\n", + " self.__i=500 #constructor\n", + " \n", + " def SetData(self,d):\n", + " self.__i=d\n", + " \n", + " def GetData(self):\n", + " return self.__i\n", + " \n", + "#Initializing\n", + "s1=SimpleClass()\n", + "s2=SimpleClass()\n", + "print \"s1 has data: \",s1.GetData()\n", + "print \"s2 has data: \",s2.GetData()\n", + "s1.SetData(1000) #function call\n", + "s2.SetData(2000)\n", + "print \"s1 has data: \",s1.GetData(),' '\n", + "print \"s2 has data: \", s2.GetData(),' '\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "s1 has data: 500\n", + "s2 has data: 500\n", + "s1 has data: 1000 \n", + "s2 has data: 2000 \n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.5, page no:217" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class SimpleClass:\n", + " __IntegerData=None\n", + " def __init__(self,data=None):\n", + " if data==None:\n", + " self.__IntegerData=500 #default constructor\n", + " else:\n", + " self.__IntegerData=data #parameterised constructor\n", + " \n", + " def SetData(self,d):\n", + " self.__IntegerData=d\n", + " \n", + " def GetData(self):\n", + " return self.__IntegerData\n", + " \n", + "#Initializing\n", + "s1=SimpleClass()\n", + "s2=SimpleClass()\n", + "s3=SimpleClass(400)\n", + "s4=SimpleClass(600)\n", + "print \"s1 has data: \",s1.GetData(),' '\n", + "print \"s2 has data: \",s2.GetData(),' '\n", + "print \"s3 has data: \",s3.GetData(),' '\n", + "print \"s4 has data: \",s4.GetData(),' '\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "s1 has data: 500 \n", + "s2 has data: 500 \n", + "s3 has data: 400 \n", + "s4 has data: 600 \n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.6, page no:218" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction: \n", + " def_init_num=None #private members\n", + " def_init_denom=None #private members\n", + " def __init__(self,a=0,b=1):\n", + " self.__num=a\n", + " self.__denom=b\n", + " print \"Numerator set inside constructor\",' ',n\n", + " print \"Denominator set inside constructor\",' ',d\n", + " \n", + " def SetValue(self,a,b):\n", + " self.__num=a\n", + " self.__denom=b\n", + " def GetValue(self,a,b):\n", + " a= self.__num\n", + " b= self.__denom\n", + " return a,b\n", + " \n", + " def __del__(self): #destructor\n", + " pass\n", + " \n", + "n=input(\"Enter n: \") #user input\n", + "d=input(\"Enter d: \") #user input\n", + "print \"Please enter value of numerator and denominator: \",' ',n,d\n", + "f=Fraction(n,d)\n", + "f.GetValue(n,d)\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter n: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter d: 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter value of numerator and denominator: 3 4\n", + "Numerator set inside constructor 3\n", + "Denominator set inside constructor 4\n", + "Numerator value retrieved: 3\n", + "Denominator value retrieved: 4\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.7, page no:221" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction:\n", + " \n", + " def __init__(self,a=0,b=1): #constructor\n", + " self.__num=a\n", + " self.__denom=b\n", + " print \"Numerator set inside constructor: \",self.__num\n", + " print \"Denominator set inside constructor: \",self.__denom \n", + " \n", + " def GetValue(self,a,b):\n", + " return self.__num,self.__denom\n", + "n=input(\"Enter n: \") #user input\n", + "d=input(\"Enter d: \") #user input\n", + "print \"Please enter the value of the numerator and denominator: \",n,d\n", + "f=Fraction(n,d)\n", + "n,d=f.GetValue(n,d)\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d\n", + "n=input(\"Please enter the value of numerator only: \")\n", + "f1=Fraction(n)\n", + "n,d=f1.GetValue(n,d)\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d\n", + "print 'ok..now I will create a fraction-no input please'\n", + "f2=Fraction()\n", + "n,d=f2.GetValue(n,d)\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter n: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter d: 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter the value of the numerator and denominator: 3 4\n", + "Numerator set inside constructor: 3\n", + "Denominator set inside constructor: 4\n", + "Numerator value retrieved: 3\n", + "Denominator value retrieved: 4\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter the value of numerator only: 3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Numerator set inside constructor: 3\n", + "Denominator set inside constructor: 1\n", + "Numerator value retrieved: 3\n", + "Denominator value retrieved: 1\n", + "ok..now I will create a fraction-no input please\n", + "Numerator set inside constructor: 0\n", + "Denominator set inside constructor: 1\n", + "Numerator value retrieved: 0\n", + "Denominator value retrieved: 1\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.8, page no:223" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction:\n", + " def_init_num=None #private members\n", + " def_init_denom=None #private members\n", + " \n", + " def __init__(self,anotherFraction=None): \n", + " if anotherFraction==None: #normal constructor\n", + " self.__num=anotherFraction\n", + " self.__denom=anotherFraction\n", + " else: #copy constructor\n", + " self.__num=anotherFraction.self.__num\n", + " self.__denom=anotherFraction.self.__denom\n", + " \n", + " \n", + " #public functions\n", + " def SetValue(self,a,b):\n", + " self.__num=a\n", + " self.__denom=b#refer to IntegerData\n", + " def GetValue(self,a,b):\n", + " a=self.__num\n", + " b=self.__denom\n", + " return a,b\n", + " \n", + "f=Fraction()\n", + "n=input(\"Enter n: \") #user input\n", + "d=input(\"Enter d: \") #user input\n", + "print \"enter the numerator and denominator: \", ' ',n,d\n", + "\n", + "f.SetValue(n,d) #call function SetValue\n", + "print \"Numerator value set: \", ' ',n\n", + "print \"Denominator value set: \", ' ',d\n", + "f.GetValue(n,d) #call function GetData\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d\n", + "print \"Now a second clone copy is being created: \",''\n", + "f1=f\n", + "f1.GetValue(n,d)\n", + "print \"Clone's numerator value retrieved: \", ' ',n\n", + "print \"Clone's denominator value retrieved: \", ' ',d" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter n: 5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter d: 6\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "enter the numerator and denominator: 5 6\n", + "Numerator value set: 5\n", + "Denominator value set: 6\n", + "Numerator value retrieved: 5\n", + "Denominator value retrieved: 6\n", + "Now a second clone copy is being created: \n", + "Clone's numerator value retrieved: 5\n", + "Clone's denominator value retrieved: 6\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.9, page no:229" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def MyNewHandler():\n", + " print \"Sorry operator new failed to allocate memory\"\n", + " exit(0)\n", + " \n", + "def _set_new_handler(s):\n", + " s()\n", + "#In python there is no in-built _set_new_handler function, so i made this function and passed MyNewHandler function as a parameters\n", + "_set_new_handler(MyNewHandler)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Sorry operator new failed to allocate memory\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.10, page no:230" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from ctypes import * \n", + "class Fraction: \n", + " def __init__(self,a=0,b=1): \n", + " if isinstance(a,int): \n", + " c = c_int(a) \n", + " d = c_int(b) \n", + " self.__num = pointer(c) \n", + " self.__denom = pointer(d) \n", + " print 'constructor sets numerator = ', self.__num[0] , ', denominator = ', self.__denom[0] \n", + " else:\n", + " c=c_int(a.__num[0])\n", + " d = c_int(a.__denom[0])\n", + " self.__num = pointer(c) \n", + " self.__denom = pointer(d)\n", + " print 'copy constructor sets numerator = ', self.__num[0] , ', denominator = ', self.__denom[0] \n", + " \n", + " def __del__(self): \n", + " print 'destructor deallocates numerator = ', self.__num[0] , ', denominator = ', self.__denom[0] \n", + " \n", + "n = input(\"Please enter values of numerator: \") \n", + "d = input(\"Please enter values of denominator: \") \n", + "f = Fraction(n,d) \n", + "print 'Please enter another set of ' \n", + "n = input(\"numerator: \") \n", + "d = input(\"denominator: \") \n", + "print 'Creating fraction *pf : ' \n", + "pf = Fraction(n,d) \n", + "print 'Now a clone copy (f2) created from *pf: ' \n", + "f2 = Fraction(pf)\n", + "print 'Now another clone copy (*pf2) created from f:' \n", + "pf2 = Fraction(f) \n", + "print '*pf2 is being destroyed:' \n", + "del pf2\n", + "print '*pf is being destroyed:' \n", + "del pf \n", + "print 'now objects f2 and f automatically destroyed : '" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter values of numerator: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter values of denominator: 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "constructor sets numerator = 3 , denominator = 4\n", + "Please enter another set of \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "numerator: 5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "denominator: 6\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Creating fraction *pf : \n", + "constructor sets numerator = 5 , denominator = 6\n", + "Now a clone copy (f2) created from *pf: \n", + "copy constructor sets numerator = 5 , denominator = 6\n", + "Now another clone copy (*pf2) created from f:\n", + "copy constructor sets numerator = 3 , denominator = 4\n", + "*pf2 is being destroyed:\n", + "*pf is being destroyed:\n", + "now objects f2 and f automatically destroyed : \n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.11, page no:234" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def Memfail(self,s):\n", + " print \"Sorry Unable to allocate memory\"\n", + " sys.exit(0)\n", + "\n", + "MAX_SIZE = 60 + 1\n", + "\n", + "MAX_SIZE=[[0 for col in range(MAX_SIZE)]for row in range(MAX_SIZE)]\n", + "nChar=0\n", + "chArr=\"Hello\"\n", + "\n", + "print \"Please input a string( 60 characters max.): \",chArr\n", + "\n", + "nChar=len(chArr)+1\n", + "szStr=chArr\n", + "print \"required memory space for\",nChar,\n", + "print \"characters\"\n", + "chArr=szStr #string copy\n", + "szStr=chArr\n", + "print \"String copied in allocated space: \",szStr\n", + "print \"Memory space dellocated\"\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please input a string( 60 characters max.): Hello\n", + "required memory space for 6 characters\n", + "String copied in allocated space: Hello\n", + "Memory space dellocated\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.12, page no:236" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction:\n", + " \n", + " def __init__(self,a=0,b=1): #constructor\n", + " self.__num=a\n", + " self.__denom=b\n", + " \n", + " def __del__(self): #destructor\n", + " pass\n", + " \n", + " def GetValue(self,a,b):\n", + " a=self.__num\n", + " b=self.__denom\n", + " return self.__num,self.__denom\n", + "n=4\n", + "d=5\n", + "f=Fraction(4,5)\n", + "n,d=f.GetValue(n,d)" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.13, page no:239" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class X:\n", + " __sa=20 #initialising static member\n", + " a = None\n", + " def __init__(self):\n", + " self.a=None #public member\n", + " def f(self,b):\n", + " a=30\n", + " print \"Global a= \",b\n", + " print \"Local a= \",a\n", + " print \"Nonstatic member a= \",self.a\n", + " print \"static member sa= \",self.__sa\n", + "\n", + "a=10\n", + "\n", + "aXobj=X()\n", + "aXobj.a=40\n", + "aXobj.f(a)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Global a= 10\n", + "Local a= 30\n", + "Nonstatic member a= 40\n", + "static member sa= 20\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [], + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/sample_notebooks/NitamoniDas/chapter_8_Data_Abstraction_through_Classes_and_User-Defined_Data_Types_7.ipynb b/sample_notebooks/NitamoniDas/chapter_8_Data_Abstraction_through_Classes_and_User-Defined_Data_Types_7.ipynb new file mode 100755 index 00000000..1083b19a --- /dev/null +++ b/sample_notebooks/NitamoniDas/chapter_8_Data_Abstraction_through_Classes_and_User-Defined_Data_Types_7.ipynb @@ -0,0 +1,809 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:dbbd8ed92141bcc804ff2fbeacf8bb85f122dc6e196e1d830291a4a449a3a439" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 8: Data Abstraction through Classes and User-Defined Data Types " + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.1, page no: 208" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Super:\n", + " def __init__(self):\n", + " self.__IntegerData=None #private member\n", + " #public functions\n", + " def SetData(self,i):\n", + " self.__IntegerData=i #refer to IntegerData\n", + " def ShowData(self):\n", + " print \"Data is \",self.__IntegerData,' '\n", + "\n", + "ob1=Super()\n", + "ob2=Super()\n", + "ob1.SetData(1000)\n", + "ob2.SetData(2000)\n", + "ob1.ShowData()\n", + "ob2.ShowData()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Data is 1000 \n", + "Data is 2000 \n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.2, page no:211" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class X:\n", + " def __init__(self):\n", + " self.a=None #private members\n", + " self.b=None #private members\n", + " \n", + "#no structure type present in python \n", + "x = X()\n", + "\n", + "x.a=0\n", + "x.b=1\n", + "print \"x.a=\",x.a,\",x.b=\",x.b" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "x.a= 0 ,x.b= 1\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.3, page no:214" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction:\n", + " \n", + " def SetValue(self,a,b): #public functions\n", + " self.__num=a \n", + " self.__denom=b \n", + " def GetValue(self,a,b):\n", + " a=self.__num\n", + " b=self.__denom\n", + " \n", + "f=Fraction()\n", + "n=input(\"Enter n: \") #user input\n", + "d=input(\"Enter d: \") #user input\n", + "print \"enter the numerator and denominator: \", ' ',n,d\n", + "\n", + "f.SetValue(n,d) #call function SetValue\n", + "print \"Numerator value set: \", ' ',n\n", + "print \"Denominator value set: \", ' ',d\n", + "f.GetValue(n,d) #call function GetData\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter n: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter d: 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "enter the numerator and denominator: 3 4\n", + "Numerator value set: 3\n", + "Denominator value set: 4\n", + "Numerator value retrieved: 3\n", + "Denominator value retrieved: 4\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.4, page no:216" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class SimpleClass:\n", + " def __init__(self):\n", + " self.__i=None #private member\n", + " \n", + " def __init__(self):\n", + " self.__i=500 #constructor\n", + " \n", + " def SetData(self,d):\n", + " self.__i=d\n", + " \n", + " def GetData(self):\n", + " return self.__i\n", + " \n", + "#Initializing\n", + "s1=SimpleClass()\n", + "s2=SimpleClass()\n", + "print \"s1 has data: \",s1.GetData()\n", + "print \"s2 has data: \",s2.GetData()\n", + "s1.SetData(1000) #function call\n", + "s2.SetData(2000)\n", + "print \"s1 has data: \",s1.GetData(),' '\n", + "print \"s2 has data: \", s2.GetData(),' '\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "s1 has data: 500\n", + "s2 has data: 500\n", + "s1 has data: 1000 \n", + "s2 has data: 2000 \n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.5, page no:217" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class SimpleClass:\n", + " __IntegerData=None\n", + " def __init__(self,data=None):\n", + " if data==None:\n", + " self.__IntegerData=500 #default constructor\n", + " else:\n", + " self.__IntegerData=data #parameterised constructor\n", + " \n", + " def SetData(self,d):\n", + " self.__IntegerData=d\n", + " \n", + " def GetData(self):\n", + " return self.__IntegerData\n", + " \n", + "#Initializing\n", + "s1=SimpleClass()\n", + "s2=SimpleClass()\n", + "s3=SimpleClass(400)\n", + "s4=SimpleClass(600)\n", + "print \"s1 has data: \",s1.GetData(),' '\n", + "print \"s2 has data: \",s2.GetData(),' '\n", + "print \"s3 has data: \",s3.GetData(),' '\n", + "print \"s4 has data: \",s4.GetData(),' '\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "s1 has data: 500 \n", + "s2 has data: 500 \n", + "s3 has data: 400 \n", + "s4 has data: 600 \n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.6, page no:218" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction: \n", + " def_init_num=None #private members\n", + " def_init_denom=None #private members\n", + " def __init__(self,a=0,b=1):\n", + " self.__num=a\n", + " self.__denom=b\n", + " print \"Numerator set inside constructor\",' ',n\n", + " print \"Denominator set inside constructor\",' ',d\n", + " \n", + " def SetValue(self,a,b):\n", + " self.__num=a\n", + " self.__denom=b\n", + " def GetValue(self,a,b):\n", + " a= self.__num\n", + " b= self.__denom\n", + " return a,b\n", + " \n", + " def __del__(self): #destructor\n", + " pass\n", + " \n", + "n=input(\"Enter n: \") #user input\n", + "d=input(\"Enter d: \") #user input\n", + "print \"Please enter value of numerator and denominator: \",' ',n,d\n", + "f=Fraction(n,d)\n", + "f.GetValue(n,d)\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter n: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter d: 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter value of numerator and denominator: 3 4\n", + "Numerator set inside constructor 3\n", + "Denominator set inside constructor 4\n", + "Numerator value retrieved: 3\n", + "Denominator value retrieved: 4\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.7, page no:221" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction:\n", + " \n", + " def __init__(self,a=0,b=1): #constructor\n", + " self.__num=a\n", + " self.__denom=b\n", + " print \"Numerator set inside constructor: \",self.__num\n", + " print \"Denominator set inside constructor: \",self.__denom \n", + " \n", + " def GetValue(self,a,b):\n", + " return self.__num,self.__denom\n", + "n=input(\"Enter n: \") #user input\n", + "d=input(\"Enter d: \") #user input\n", + "print \"Please enter the value of the numerator and denominator: \",n,d\n", + "f=Fraction(n,d)\n", + "n,d=f.GetValue(n,d)\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d\n", + "n=input(\"Please enter the value of numerator only: \")\n", + "f1=Fraction(n)\n", + "n,d=f1.GetValue(n,d)\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d\n", + "print 'ok..now I will create a fraction-no input please'\n", + "f2=Fraction()\n", + "n,d=f2.GetValue(n,d)\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter n: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter d: 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter the value of the numerator and denominator: 3 4\n", + "Numerator set inside constructor: 3\n", + "Denominator set inside constructor: 4\n", + "Numerator value retrieved: 3\n", + "Denominator value retrieved: 4\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter the value of numerator only: 3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Numerator set inside constructor: 3\n", + "Denominator set inside constructor: 1\n", + "Numerator value retrieved: 3\n", + "Denominator value retrieved: 1\n", + "ok..now I will create a fraction-no input please\n", + "Numerator set inside constructor: 0\n", + "Denominator set inside constructor: 1\n", + "Numerator value retrieved: 0\n", + "Denominator value retrieved: 1\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.8, page no:223" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction:\n", + " def_init_num=None #private members\n", + " def_init_denom=None #private members\n", + " \n", + " def __init__(self,anotherFraction=None): \n", + " if anotherFraction==None: #normal constructor\n", + " self.__num=anotherFraction\n", + " self.__denom=anotherFraction\n", + " else: #copy constructor\n", + " self.__num=anotherFraction.self.__num\n", + " self.__denom=anotherFraction.self.__denom\n", + " \n", + " \n", + " #public functions\n", + " def SetValue(self,a,b):\n", + " self.__num=a\n", + " self.__denom=b#refer to IntegerData\n", + " def GetValue(self,a,b):\n", + " a=self.__num\n", + " b=self.__denom\n", + " return a,b\n", + " \n", + "f=Fraction()\n", + "n=input(\"Enter n: \") #user input\n", + "d=input(\"Enter d: \") #user input\n", + "print \"enter the numerator and denominator: \", ' ',n,d\n", + "\n", + "f.SetValue(n,d) #call function SetValue\n", + "print \"Numerator value set: \", ' ',n\n", + "print \"Denominator value set: \", ' ',d\n", + "f.GetValue(n,d) #call function GetData\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d\n", + "print \"Now a second clone copy is being created: \",''\n", + "f1=f\n", + "f1.GetValue(n,d)\n", + "print \"Clone's numerator value retrieved: \", ' ',n\n", + "print \"Clone's denominator value retrieved: \", ' ',d" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter n: 5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter d: 6\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "enter the numerator and denominator: 5 6\n", + "Numerator value set: 5\n", + "Denominator value set: 6\n", + "Numerator value retrieved: 5\n", + "Denominator value retrieved: 6\n", + "Now a second clone copy is being created: \n", + "Clone's numerator value retrieved: 5\n", + "Clone's denominator value retrieved: 6\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.9, page no:229" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def MyNewHandler():\n", + " print \"Sorry operator new failed to allocate memory\"\n", + " exit(0)\n", + " \n", + "def _set_new_handler(s):\n", + " s()\n", + "#In python there is no in-built _set_new_handler function, so i made this function and passed MyNewHandler function as a parameters\n", + "_set_new_handler(MyNewHandler)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Sorry operator new failed to allocate memory\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.10, page no:230" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from ctypes import * \n", + "class Fraction: \n", + " def __init__(self,a=0,b=1): \n", + " if isinstance(a,int): \n", + " c = c_int(a) \n", + " d = c_int(b) \n", + " self.__num = pointer(c) \n", + " self.__denom = pointer(d) \n", + " print 'constructor sets numerator = ', self.__num[0] , ', denominator = ', self.__denom[0] \n", + " else:\n", + " c=c_int(a.__num[0])\n", + " d = c_int(a.__denom[0])\n", + " self.__num = pointer(c) \n", + " self.__denom = pointer(d)\n", + " print 'copy constructor sets numerator = ', self.__num[0] , ', denominator = ', self.__denom[0] \n", + " \n", + " def __del__(self): \n", + " print 'destructor deallocates numerator = ', self.__num[0] , ', denominator = ', self.__denom[0] \n", + " \n", + "n = input(\"Please enter values of numerator: \") \n", + "d = input(\"Please enter values of denominator: \") \n", + "f = Fraction(n,d) \n", + "print 'Please enter another set of ' \n", + "n = input(\"numerator: \") \n", + "d = input(\"denominator: \") \n", + "print 'Creating fraction *pf : ' \n", + "pf = Fraction(n,d) \n", + "print 'Now a clone copy (f2) created from *pf: ' \n", + "f2 = Fraction(pf)\n", + "print 'Now another clone copy (*pf2) created from f:' \n", + "pf2 = Fraction(f) \n", + "print '*pf2 is being destroyed:' \n", + "del pf2\n", + "print '*pf is being destroyed:' \n", + "del pf \n", + "print 'now objects f2 and f automatically destroyed : '" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter values of numerator: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter values of denominator: 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "constructor sets numerator = 3 , denominator = 4\n", + "Please enter another set of \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "numerator: 5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "denominator: 6\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Creating fraction *pf : \n", + "constructor sets numerator = 5 , denominator = 6\n", + "Now a clone copy (f2) created from *pf: \n", + "copy constructor sets numerator = 5 , denominator = 6\n", + "Now another clone copy (*pf2) created from f:\n", + "copy constructor sets numerator = 3 , denominator = 4\n", + "*pf2 is being destroyed:\n", + "*pf is being destroyed:\n", + "now objects f2 and f automatically destroyed : \n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.11, page no:234" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def Memfail(self,s):\n", + " print \"Sorry Unable to allocate memory\"\n", + " sys.exit(0)\n", + "\n", + "MAX_SIZE = 60 + 1\n", + "\n", + "MAX_SIZE=[[0 for col in range(MAX_SIZE)]for row in range(MAX_SIZE)]\n", + "nChar=0\n", + "chArr=\"Hello\"\n", + "\n", + "print \"Please input a string( 60 characters max.): \",chArr\n", + "\n", + "nChar=len(chArr)+1\n", + "szStr=chArr\n", + "print \"required memory space for\",nChar,\n", + "print \"characters\"\n", + "chArr=szStr #string copy\n", + "szStr=chArr\n", + "print \"String copied in allocated space: \",szStr\n", + "print \"Memory space dellocated\"\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please input a string( 60 characters max.): Hello\n", + "required memory space for 6 characters\n", + "String copied in allocated space: Hello\n", + "Memory space dellocated\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.12, page no:236" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction:\n", + " \n", + " def __init__(self,a=0,b=1): #constructor\n", + " self.__num=a\n", + " self.__denom=b\n", + " \n", + " def __del__(self): #destructor\n", + " pass\n", + " \n", + " def GetValue(self,a,b):\n", + " a=self.__num\n", + " b=self.__denom\n", + " return self.__num,self.__denom\n", + "n=4\n", + "d=5\n", + "f=Fraction(4,5)\n", + "n,d=f.GetValue(n,d)" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.13, page no:239" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class X:\n", + " __sa=20 #initialising static member\n", + " a = None\n", + " def __init__(self):\n", + " self.a=None #public member\n", + " def f(self,b):\n", + " a=30\n", + " print \"Global a= \",b\n", + " print \"Local a= \",a\n", + " print \"Nonstatic member a= \",self.a\n", + " print \"static member sa= \",self.__sa\n", + "\n", + "a=10\n", + "\n", + "aXobj=X()\n", + "aXobj.a=40\n", + "aXobj.f(a)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Global a= 10\n", + "Local a= 30\n", + "Nonstatic member a= 40\n", + "static member sa= 20\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [], + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/sample_notebooks/NitamoniDas/chapter_8_Data_Abstraction_through_Classes_and_User-Defined_Data_Types_8.ipynb b/sample_notebooks/NitamoniDas/chapter_8_Data_Abstraction_through_Classes_and_User-Defined_Data_Types_8.ipynb new file mode 100755 index 00000000..1083b19a --- /dev/null +++ b/sample_notebooks/NitamoniDas/chapter_8_Data_Abstraction_through_Classes_and_User-Defined_Data_Types_8.ipynb @@ -0,0 +1,809 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:dbbd8ed92141bcc804ff2fbeacf8bb85f122dc6e196e1d830291a4a449a3a439" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 8: Data Abstraction through Classes and User-Defined Data Types " + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.1, page no: 208" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Super:\n", + " def __init__(self):\n", + " self.__IntegerData=None #private member\n", + " #public functions\n", + " def SetData(self,i):\n", + " self.__IntegerData=i #refer to IntegerData\n", + " def ShowData(self):\n", + " print \"Data is \",self.__IntegerData,' '\n", + "\n", + "ob1=Super()\n", + "ob2=Super()\n", + "ob1.SetData(1000)\n", + "ob2.SetData(2000)\n", + "ob1.ShowData()\n", + "ob2.ShowData()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Data is 1000 \n", + "Data is 2000 \n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.2, page no:211" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class X:\n", + " def __init__(self):\n", + " self.a=None #private members\n", + " self.b=None #private members\n", + " \n", + "#no structure type present in python \n", + "x = X()\n", + "\n", + "x.a=0\n", + "x.b=1\n", + "print \"x.a=\",x.a,\",x.b=\",x.b" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "x.a= 0 ,x.b= 1\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.3, page no:214" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction:\n", + " \n", + " def SetValue(self,a,b): #public functions\n", + " self.__num=a \n", + " self.__denom=b \n", + " def GetValue(self,a,b):\n", + " a=self.__num\n", + " b=self.__denom\n", + " \n", + "f=Fraction()\n", + "n=input(\"Enter n: \") #user input\n", + "d=input(\"Enter d: \") #user input\n", + "print \"enter the numerator and denominator: \", ' ',n,d\n", + "\n", + "f.SetValue(n,d) #call function SetValue\n", + "print \"Numerator value set: \", ' ',n\n", + "print \"Denominator value set: \", ' ',d\n", + "f.GetValue(n,d) #call function GetData\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter n: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter d: 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "enter the numerator and denominator: 3 4\n", + "Numerator value set: 3\n", + "Denominator value set: 4\n", + "Numerator value retrieved: 3\n", + "Denominator value retrieved: 4\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.4, page no:216" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class SimpleClass:\n", + " def __init__(self):\n", + " self.__i=None #private member\n", + " \n", + " def __init__(self):\n", + " self.__i=500 #constructor\n", + " \n", + " def SetData(self,d):\n", + " self.__i=d\n", + " \n", + " def GetData(self):\n", + " return self.__i\n", + " \n", + "#Initializing\n", + "s1=SimpleClass()\n", + "s2=SimpleClass()\n", + "print \"s1 has data: \",s1.GetData()\n", + "print \"s2 has data: \",s2.GetData()\n", + "s1.SetData(1000) #function call\n", + "s2.SetData(2000)\n", + "print \"s1 has data: \",s1.GetData(),' '\n", + "print \"s2 has data: \", s2.GetData(),' '\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "s1 has data: 500\n", + "s2 has data: 500\n", + "s1 has data: 1000 \n", + "s2 has data: 2000 \n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.5, page no:217" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class SimpleClass:\n", + " __IntegerData=None\n", + " def __init__(self,data=None):\n", + " if data==None:\n", + " self.__IntegerData=500 #default constructor\n", + " else:\n", + " self.__IntegerData=data #parameterised constructor\n", + " \n", + " def SetData(self,d):\n", + " self.__IntegerData=d\n", + " \n", + " def GetData(self):\n", + " return self.__IntegerData\n", + " \n", + "#Initializing\n", + "s1=SimpleClass()\n", + "s2=SimpleClass()\n", + "s3=SimpleClass(400)\n", + "s4=SimpleClass(600)\n", + "print \"s1 has data: \",s1.GetData(),' '\n", + "print \"s2 has data: \",s2.GetData(),' '\n", + "print \"s3 has data: \",s3.GetData(),' '\n", + "print \"s4 has data: \",s4.GetData(),' '\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "s1 has data: 500 \n", + "s2 has data: 500 \n", + "s3 has data: 400 \n", + "s4 has data: 600 \n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.6, page no:218" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction: \n", + " def_init_num=None #private members\n", + " def_init_denom=None #private members\n", + " def __init__(self,a=0,b=1):\n", + " self.__num=a\n", + " self.__denom=b\n", + " print \"Numerator set inside constructor\",' ',n\n", + " print \"Denominator set inside constructor\",' ',d\n", + " \n", + " def SetValue(self,a,b):\n", + " self.__num=a\n", + " self.__denom=b\n", + " def GetValue(self,a,b):\n", + " a= self.__num\n", + " b= self.__denom\n", + " return a,b\n", + " \n", + " def __del__(self): #destructor\n", + " pass\n", + " \n", + "n=input(\"Enter n: \") #user input\n", + "d=input(\"Enter d: \") #user input\n", + "print \"Please enter value of numerator and denominator: \",' ',n,d\n", + "f=Fraction(n,d)\n", + "f.GetValue(n,d)\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter n: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter d: 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter value of numerator and denominator: 3 4\n", + "Numerator set inside constructor 3\n", + "Denominator set inside constructor 4\n", + "Numerator value retrieved: 3\n", + "Denominator value retrieved: 4\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.7, page no:221" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction:\n", + " \n", + " def __init__(self,a=0,b=1): #constructor\n", + " self.__num=a\n", + " self.__denom=b\n", + " print \"Numerator set inside constructor: \",self.__num\n", + " print \"Denominator set inside constructor: \",self.__denom \n", + " \n", + " def GetValue(self,a,b):\n", + " return self.__num,self.__denom\n", + "n=input(\"Enter n: \") #user input\n", + "d=input(\"Enter d: \") #user input\n", + "print \"Please enter the value of the numerator and denominator: \",n,d\n", + "f=Fraction(n,d)\n", + "n,d=f.GetValue(n,d)\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d\n", + "n=input(\"Please enter the value of numerator only: \")\n", + "f1=Fraction(n)\n", + "n,d=f1.GetValue(n,d)\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d\n", + "print 'ok..now I will create a fraction-no input please'\n", + "f2=Fraction()\n", + "n,d=f2.GetValue(n,d)\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter n: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter d: 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter the value of the numerator and denominator: 3 4\n", + "Numerator set inside constructor: 3\n", + "Denominator set inside constructor: 4\n", + "Numerator value retrieved: 3\n", + "Denominator value retrieved: 4\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter the value of numerator only: 3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Numerator set inside constructor: 3\n", + "Denominator set inside constructor: 1\n", + "Numerator value retrieved: 3\n", + "Denominator value retrieved: 1\n", + "ok..now I will create a fraction-no input please\n", + "Numerator set inside constructor: 0\n", + "Denominator set inside constructor: 1\n", + "Numerator value retrieved: 0\n", + "Denominator value retrieved: 1\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.8, page no:223" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction:\n", + " def_init_num=None #private members\n", + " def_init_denom=None #private members\n", + " \n", + " def __init__(self,anotherFraction=None): \n", + " if anotherFraction==None: #normal constructor\n", + " self.__num=anotherFraction\n", + " self.__denom=anotherFraction\n", + " else: #copy constructor\n", + " self.__num=anotherFraction.self.__num\n", + " self.__denom=anotherFraction.self.__denom\n", + " \n", + " \n", + " #public functions\n", + " def SetValue(self,a,b):\n", + " self.__num=a\n", + " self.__denom=b#refer to IntegerData\n", + " def GetValue(self,a,b):\n", + " a=self.__num\n", + " b=self.__denom\n", + " return a,b\n", + " \n", + "f=Fraction()\n", + "n=input(\"Enter n: \") #user input\n", + "d=input(\"Enter d: \") #user input\n", + "print \"enter the numerator and denominator: \", ' ',n,d\n", + "\n", + "f.SetValue(n,d) #call function SetValue\n", + "print \"Numerator value set: \", ' ',n\n", + "print \"Denominator value set: \", ' ',d\n", + "f.GetValue(n,d) #call function GetData\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d\n", + "print \"Now a second clone copy is being created: \",''\n", + "f1=f\n", + "f1.GetValue(n,d)\n", + "print \"Clone's numerator value retrieved: \", ' ',n\n", + "print \"Clone's denominator value retrieved: \", ' ',d" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter n: 5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter d: 6\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "enter the numerator and denominator: 5 6\n", + "Numerator value set: 5\n", + "Denominator value set: 6\n", + "Numerator value retrieved: 5\n", + "Denominator value retrieved: 6\n", + "Now a second clone copy is being created: \n", + "Clone's numerator value retrieved: 5\n", + "Clone's denominator value retrieved: 6\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.9, page no:229" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def MyNewHandler():\n", + " print \"Sorry operator new failed to allocate memory\"\n", + " exit(0)\n", + " \n", + "def _set_new_handler(s):\n", + " s()\n", + "#In python there is no in-built _set_new_handler function, so i made this function and passed MyNewHandler function as a parameters\n", + "_set_new_handler(MyNewHandler)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Sorry operator new failed to allocate memory\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.10, page no:230" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from ctypes import * \n", + "class Fraction: \n", + " def __init__(self,a=0,b=1): \n", + " if isinstance(a,int): \n", + " c = c_int(a) \n", + " d = c_int(b) \n", + " self.__num = pointer(c) \n", + " self.__denom = pointer(d) \n", + " print 'constructor sets numerator = ', self.__num[0] , ', denominator = ', self.__denom[0] \n", + " else:\n", + " c=c_int(a.__num[0])\n", + " d = c_int(a.__denom[0])\n", + " self.__num = pointer(c) \n", + " self.__denom = pointer(d)\n", + " print 'copy constructor sets numerator = ', self.__num[0] , ', denominator = ', self.__denom[0] \n", + " \n", + " def __del__(self): \n", + " print 'destructor deallocates numerator = ', self.__num[0] , ', denominator = ', self.__denom[0] \n", + " \n", + "n = input(\"Please enter values of numerator: \") \n", + "d = input(\"Please enter values of denominator: \") \n", + "f = Fraction(n,d) \n", + "print 'Please enter another set of ' \n", + "n = input(\"numerator: \") \n", + "d = input(\"denominator: \") \n", + "print 'Creating fraction *pf : ' \n", + "pf = Fraction(n,d) \n", + "print 'Now a clone copy (f2) created from *pf: ' \n", + "f2 = Fraction(pf)\n", + "print 'Now another clone copy (*pf2) created from f:' \n", + "pf2 = Fraction(f) \n", + "print '*pf2 is being destroyed:' \n", + "del pf2\n", + "print '*pf is being destroyed:' \n", + "del pf \n", + "print 'now objects f2 and f automatically destroyed : '" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter values of numerator: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter values of denominator: 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "constructor sets numerator = 3 , denominator = 4\n", + "Please enter another set of \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "numerator: 5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "denominator: 6\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Creating fraction *pf : \n", + "constructor sets numerator = 5 , denominator = 6\n", + "Now a clone copy (f2) created from *pf: \n", + "copy constructor sets numerator = 5 , denominator = 6\n", + "Now another clone copy (*pf2) created from f:\n", + "copy constructor sets numerator = 3 , denominator = 4\n", + "*pf2 is being destroyed:\n", + "*pf is being destroyed:\n", + "now objects f2 and f automatically destroyed : \n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.11, page no:234" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def Memfail(self,s):\n", + " print \"Sorry Unable to allocate memory\"\n", + " sys.exit(0)\n", + "\n", + "MAX_SIZE = 60 + 1\n", + "\n", + "MAX_SIZE=[[0 for col in range(MAX_SIZE)]for row in range(MAX_SIZE)]\n", + "nChar=0\n", + "chArr=\"Hello\"\n", + "\n", + "print \"Please input a string( 60 characters max.): \",chArr\n", + "\n", + "nChar=len(chArr)+1\n", + "szStr=chArr\n", + "print \"required memory space for\",nChar,\n", + "print \"characters\"\n", + "chArr=szStr #string copy\n", + "szStr=chArr\n", + "print \"String copied in allocated space: \",szStr\n", + "print \"Memory space dellocated\"\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please input a string( 60 characters max.): Hello\n", + "required memory space for 6 characters\n", + "String copied in allocated space: Hello\n", + "Memory space dellocated\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.12, page no:236" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction:\n", + " \n", + " def __init__(self,a=0,b=1): #constructor\n", + " self.__num=a\n", + " self.__denom=b\n", + " \n", + " def __del__(self): #destructor\n", + " pass\n", + " \n", + " def GetValue(self,a,b):\n", + " a=self.__num\n", + " b=self.__denom\n", + " return self.__num,self.__denom\n", + "n=4\n", + "d=5\n", + "f=Fraction(4,5)\n", + "n,d=f.GetValue(n,d)" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.13, page no:239" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class X:\n", + " __sa=20 #initialising static member\n", + " a = None\n", + " def __init__(self):\n", + " self.a=None #public member\n", + " def f(self,b):\n", + " a=30\n", + " print \"Global a= \",b\n", + " print \"Local a= \",a\n", + " print \"Nonstatic member a= \",self.a\n", + " print \"static member sa= \",self.__sa\n", + "\n", + "a=10\n", + "\n", + "aXobj=X()\n", + "aXobj.a=40\n", + "aXobj.f(a)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Global a= 10\n", + "Local a= 30\n", + "Nonstatic member a= 40\n", + "static member sa= 20\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [], + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/sample_notebooks/NitamoniDas/chapter_8_Data_Abstraction_through_Classes_and_User-Defined_Data_Types_9.ipynb b/sample_notebooks/NitamoniDas/chapter_8_Data_Abstraction_through_Classes_and_User-Defined_Data_Types_9.ipynb new file mode 100755 index 00000000..1083b19a --- /dev/null +++ b/sample_notebooks/NitamoniDas/chapter_8_Data_Abstraction_through_Classes_and_User-Defined_Data_Types_9.ipynb @@ -0,0 +1,809 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:dbbd8ed92141bcc804ff2fbeacf8bb85f122dc6e196e1d830291a4a449a3a439" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 8: Data Abstraction through Classes and User-Defined Data Types " + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.1, page no: 208" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Super:\n", + " def __init__(self):\n", + " self.__IntegerData=None #private member\n", + " #public functions\n", + " def SetData(self,i):\n", + " self.__IntegerData=i #refer to IntegerData\n", + " def ShowData(self):\n", + " print \"Data is \",self.__IntegerData,' '\n", + "\n", + "ob1=Super()\n", + "ob2=Super()\n", + "ob1.SetData(1000)\n", + "ob2.SetData(2000)\n", + "ob1.ShowData()\n", + "ob2.ShowData()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Data is 1000 \n", + "Data is 2000 \n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.2, page no:211" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class X:\n", + " def __init__(self):\n", + " self.a=None #private members\n", + " self.b=None #private members\n", + " \n", + "#no structure type present in python \n", + "x = X()\n", + "\n", + "x.a=0\n", + "x.b=1\n", + "print \"x.a=\",x.a,\",x.b=\",x.b" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "x.a= 0 ,x.b= 1\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.3, page no:214" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction:\n", + " \n", + " def SetValue(self,a,b): #public functions\n", + " self.__num=a \n", + " self.__denom=b \n", + " def GetValue(self,a,b):\n", + " a=self.__num\n", + " b=self.__denom\n", + " \n", + "f=Fraction()\n", + "n=input(\"Enter n: \") #user input\n", + "d=input(\"Enter d: \") #user input\n", + "print \"enter the numerator and denominator: \", ' ',n,d\n", + "\n", + "f.SetValue(n,d) #call function SetValue\n", + "print \"Numerator value set: \", ' ',n\n", + "print \"Denominator value set: \", ' ',d\n", + "f.GetValue(n,d) #call function GetData\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter n: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter d: 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "enter the numerator and denominator: 3 4\n", + "Numerator value set: 3\n", + "Denominator value set: 4\n", + "Numerator value retrieved: 3\n", + "Denominator value retrieved: 4\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.4, page no:216" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class SimpleClass:\n", + " def __init__(self):\n", + " self.__i=None #private member\n", + " \n", + " def __init__(self):\n", + " self.__i=500 #constructor\n", + " \n", + " def SetData(self,d):\n", + " self.__i=d\n", + " \n", + " def GetData(self):\n", + " return self.__i\n", + " \n", + "#Initializing\n", + "s1=SimpleClass()\n", + "s2=SimpleClass()\n", + "print \"s1 has data: \",s1.GetData()\n", + "print \"s2 has data: \",s2.GetData()\n", + "s1.SetData(1000) #function call\n", + "s2.SetData(2000)\n", + "print \"s1 has data: \",s1.GetData(),' '\n", + "print \"s2 has data: \", s2.GetData(),' '\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "s1 has data: 500\n", + "s2 has data: 500\n", + "s1 has data: 1000 \n", + "s2 has data: 2000 \n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.5, page no:217" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class SimpleClass:\n", + " __IntegerData=None\n", + " def __init__(self,data=None):\n", + " if data==None:\n", + " self.__IntegerData=500 #default constructor\n", + " else:\n", + " self.__IntegerData=data #parameterised constructor\n", + " \n", + " def SetData(self,d):\n", + " self.__IntegerData=d\n", + " \n", + " def GetData(self):\n", + " return self.__IntegerData\n", + " \n", + "#Initializing\n", + "s1=SimpleClass()\n", + "s2=SimpleClass()\n", + "s3=SimpleClass(400)\n", + "s4=SimpleClass(600)\n", + "print \"s1 has data: \",s1.GetData(),' '\n", + "print \"s2 has data: \",s2.GetData(),' '\n", + "print \"s3 has data: \",s3.GetData(),' '\n", + "print \"s4 has data: \",s4.GetData(),' '\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "s1 has data: 500 \n", + "s2 has data: 500 \n", + "s3 has data: 400 \n", + "s4 has data: 600 \n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.6, page no:218" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction: \n", + " def_init_num=None #private members\n", + " def_init_denom=None #private members\n", + " def __init__(self,a=0,b=1):\n", + " self.__num=a\n", + " self.__denom=b\n", + " print \"Numerator set inside constructor\",' ',n\n", + " print \"Denominator set inside constructor\",' ',d\n", + " \n", + " def SetValue(self,a,b):\n", + " self.__num=a\n", + " self.__denom=b\n", + " def GetValue(self,a,b):\n", + " a= self.__num\n", + " b= self.__denom\n", + " return a,b\n", + " \n", + " def __del__(self): #destructor\n", + " pass\n", + " \n", + "n=input(\"Enter n: \") #user input\n", + "d=input(\"Enter d: \") #user input\n", + "print \"Please enter value of numerator and denominator: \",' ',n,d\n", + "f=Fraction(n,d)\n", + "f.GetValue(n,d)\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter n: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter d: 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter value of numerator and denominator: 3 4\n", + "Numerator set inside constructor 3\n", + "Denominator set inside constructor 4\n", + "Numerator value retrieved: 3\n", + "Denominator value retrieved: 4\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.7, page no:221" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction:\n", + " \n", + " def __init__(self,a=0,b=1): #constructor\n", + " self.__num=a\n", + " self.__denom=b\n", + " print \"Numerator set inside constructor: \",self.__num\n", + " print \"Denominator set inside constructor: \",self.__denom \n", + " \n", + " def GetValue(self,a,b):\n", + " return self.__num,self.__denom\n", + "n=input(\"Enter n: \") #user input\n", + "d=input(\"Enter d: \") #user input\n", + "print \"Please enter the value of the numerator and denominator: \",n,d\n", + "f=Fraction(n,d)\n", + "n,d=f.GetValue(n,d)\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d\n", + "n=input(\"Please enter the value of numerator only: \")\n", + "f1=Fraction(n)\n", + "n,d=f1.GetValue(n,d)\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d\n", + "print 'ok..now I will create a fraction-no input please'\n", + "f2=Fraction()\n", + "n,d=f2.GetValue(n,d)\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter n: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter d: 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter the value of the numerator and denominator: 3 4\n", + "Numerator set inside constructor: 3\n", + "Denominator set inside constructor: 4\n", + "Numerator value retrieved: 3\n", + "Denominator value retrieved: 4\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter the value of numerator only: 3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Numerator set inside constructor: 3\n", + "Denominator set inside constructor: 1\n", + "Numerator value retrieved: 3\n", + "Denominator value retrieved: 1\n", + "ok..now I will create a fraction-no input please\n", + "Numerator set inside constructor: 0\n", + "Denominator set inside constructor: 1\n", + "Numerator value retrieved: 0\n", + "Denominator value retrieved: 1\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.8, page no:223" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction:\n", + " def_init_num=None #private members\n", + " def_init_denom=None #private members\n", + " \n", + " def __init__(self,anotherFraction=None): \n", + " if anotherFraction==None: #normal constructor\n", + " self.__num=anotherFraction\n", + " self.__denom=anotherFraction\n", + " else: #copy constructor\n", + " self.__num=anotherFraction.self.__num\n", + " self.__denom=anotherFraction.self.__denom\n", + " \n", + " \n", + " #public functions\n", + " def SetValue(self,a,b):\n", + " self.__num=a\n", + " self.__denom=b#refer to IntegerData\n", + " def GetValue(self,a,b):\n", + " a=self.__num\n", + " b=self.__denom\n", + " return a,b\n", + " \n", + "f=Fraction()\n", + "n=input(\"Enter n: \") #user input\n", + "d=input(\"Enter d: \") #user input\n", + "print \"enter the numerator and denominator: \", ' ',n,d\n", + "\n", + "f.SetValue(n,d) #call function SetValue\n", + "print \"Numerator value set: \", ' ',n\n", + "print \"Denominator value set: \", ' ',d\n", + "f.GetValue(n,d) #call function GetData\n", + "print \"Numerator value retrieved: \", ' ',n\n", + "print \"Denominator value retrieved: \", ' ',d\n", + "print \"Now a second clone copy is being created: \",''\n", + "f1=f\n", + "f1.GetValue(n,d)\n", + "print \"Clone's numerator value retrieved: \", ' ',n\n", + "print \"Clone's denominator value retrieved: \", ' ',d" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter n: 5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter d: 6\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "enter the numerator and denominator: 5 6\n", + "Numerator value set: 5\n", + "Denominator value set: 6\n", + "Numerator value retrieved: 5\n", + "Denominator value retrieved: 6\n", + "Now a second clone copy is being created: \n", + "Clone's numerator value retrieved: 5\n", + "Clone's denominator value retrieved: 6\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.9, page no:229" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def MyNewHandler():\n", + " print \"Sorry operator new failed to allocate memory\"\n", + " exit(0)\n", + " \n", + "def _set_new_handler(s):\n", + " s()\n", + "#In python there is no in-built _set_new_handler function, so i made this function and passed MyNewHandler function as a parameters\n", + "_set_new_handler(MyNewHandler)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Sorry operator new failed to allocate memory\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.10, page no:230" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from ctypes import * \n", + "class Fraction: \n", + " def __init__(self,a=0,b=1): \n", + " if isinstance(a,int): \n", + " c = c_int(a) \n", + " d = c_int(b) \n", + " self.__num = pointer(c) \n", + " self.__denom = pointer(d) \n", + " print 'constructor sets numerator = ', self.__num[0] , ', denominator = ', self.__denom[0] \n", + " else:\n", + " c=c_int(a.__num[0])\n", + " d = c_int(a.__denom[0])\n", + " self.__num = pointer(c) \n", + " self.__denom = pointer(d)\n", + " print 'copy constructor sets numerator = ', self.__num[0] , ', denominator = ', self.__denom[0] \n", + " \n", + " def __del__(self): \n", + " print 'destructor deallocates numerator = ', self.__num[0] , ', denominator = ', self.__denom[0] \n", + " \n", + "n = input(\"Please enter values of numerator: \") \n", + "d = input(\"Please enter values of denominator: \") \n", + "f = Fraction(n,d) \n", + "print 'Please enter another set of ' \n", + "n = input(\"numerator: \") \n", + "d = input(\"denominator: \") \n", + "print 'Creating fraction *pf : ' \n", + "pf = Fraction(n,d) \n", + "print 'Now a clone copy (f2) created from *pf: ' \n", + "f2 = Fraction(pf)\n", + "print 'Now another clone copy (*pf2) created from f:' \n", + "pf2 = Fraction(f) \n", + "print '*pf2 is being destroyed:' \n", + "del pf2\n", + "print '*pf is being destroyed:' \n", + "del pf \n", + "print 'now objects f2 and f automatically destroyed : '" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter values of numerator: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter values of denominator: 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "constructor sets numerator = 3 , denominator = 4\n", + "Please enter another set of \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "numerator: 5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "denominator: 6\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Creating fraction *pf : \n", + "constructor sets numerator = 5 , denominator = 6\n", + "Now a clone copy (f2) created from *pf: \n", + "copy constructor sets numerator = 5 , denominator = 6\n", + "Now another clone copy (*pf2) created from f:\n", + "copy constructor sets numerator = 3 , denominator = 4\n", + "*pf2 is being destroyed:\n", + "*pf is being destroyed:\n", + "now objects f2 and f automatically destroyed : \n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.11, page no:234" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def Memfail(self,s):\n", + " print \"Sorry Unable to allocate memory\"\n", + " sys.exit(0)\n", + "\n", + "MAX_SIZE = 60 + 1\n", + "\n", + "MAX_SIZE=[[0 for col in range(MAX_SIZE)]for row in range(MAX_SIZE)]\n", + "nChar=0\n", + "chArr=\"Hello\"\n", + "\n", + "print \"Please input a string( 60 characters max.): \",chArr\n", + "\n", + "nChar=len(chArr)+1\n", + "szStr=chArr\n", + "print \"required memory space for\",nChar,\n", + "print \"characters\"\n", + "chArr=szStr #string copy\n", + "szStr=chArr\n", + "print \"String copied in allocated space: \",szStr\n", + "print \"Memory space dellocated\"\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please input a string( 60 characters max.): Hello\n", + "required memory space for 6 characters\n", + "String copied in allocated space: Hello\n", + "Memory space dellocated\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.12, page no:236" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Fraction:\n", + " \n", + " def __init__(self,a=0,b=1): #constructor\n", + " self.__num=a\n", + " self.__denom=b\n", + " \n", + " def __del__(self): #destructor\n", + " pass\n", + " \n", + " def GetValue(self,a,b):\n", + " a=self.__num\n", + " b=self.__denom\n", + " return self.__num,self.__denom\n", + "n=4\n", + "d=5\n", + "f=Fraction(4,5)\n", + "n,d=f.GetValue(n,d)" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program Source Code 8.13, page no:239" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class X:\n", + " __sa=20 #initialising static member\n", + " a = None\n", + " def __init__(self):\n", + " self.a=None #public member\n", + " def f(self,b):\n", + " a=30\n", + " print \"Global a= \",b\n", + " print \"Local a= \",a\n", + " print \"Nonstatic member a= \",self.a\n", + " print \"static member sa= \",self.__sa\n", + "\n", + "a=10\n", + "\n", + "aXobj=X()\n", + "aXobj.a=40\n", + "aXobj.f(a)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Global a= 10\n", + "Local a= 30\n", + "Nonstatic member a= 40\n", + "static member sa= 20\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [], + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/sample_notebooks/Prashanth Kumar Immady/Chapter_1.ipynb b/sample_notebooks/Prashanth Kumar Immady/Chapter_1.ipynb new file mode 100755 index 00000000..3756f226 --- /dev/null +++ b/sample_notebooks/Prashanth Kumar Immady/Chapter_1.ipynb @@ -0,0 +1,307 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:b71c5763a44a53c43becf4b38bc4dc6ca15fcaba00869ac6f1172cbb9803b804" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 1 - ELECTRONIC MATERIALS AND COMPONENTS" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E1 - Pg 43" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Calculate the value of resistor\n", + "print '%s' %(\"Refer to the figure 1.52\")\n", + "print '%s' %(\"Hold the resistor as shown in the figure such that tolerance is on your extreme right.\")\n", + "print '%s' %(\"Now the value of the resistor is equal to\")\n", + "print '%s' %(\" Red Black Blue Gold\")\n", + "print '%s' %(\" 2 0 6 (+/-)5%\")\n", + "red=2. #red value\n", + "blk=0 #black value\n", + "blu=6. #blue value\n", + "gld=5. #gold value\n", + "value_res=(red*10.+blk)*10.**blu #value of resistor\n", + "print '%s %.0f %s %.0f' %(\"\\n The value of resistor is\",value_res,\"ohm (+/-)\",gld)\n", + "per_val=0.05*value_res\n", + "pos_value_res=value_res+per_val #positive range of resistor\n", + "neg_value_res=value_res-per_val #negative range of resistor\n", + "print '%s %.0f %s %.0f %s ' %(\"\\n The value of resistor is\",neg_value_res*1e-6,\" Mohm and\",pos_value_res*1e-6,\"Mohm\")\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Refer to the figure 1.52\n", + "Hold the resistor as shown in the figure such that tolerance is on your extreme right.\n", + "Now the value of the resistor is equal to\n", + " Red Black Blue Gold\n", + " 2 0 6 (+/-)5%\n", + "\n", + " The value of resistor is 20000000 ohm (+/-) 5\n", + "\n", + " The value of resistor is 19 Mohm and 21 Mohm \n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E2 - Pg 43" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Calculate the value of the resistor\n", + "print '%s' %(\"With the help of colour coding table, one finds\")\n", + "print '%s' %(\" 1st_Band 2nd_Band 3rd_Band 4th_Band\")\n", + "print(\" Yellow Violet Orange Gold\")\n", + "print '%s' %(\" 4 7 10**3 (+/-)5%\")\n", + "yel=4. #yellow value\n", + "vio=7. #violet value\n", + "org=1e3 #orange value\n", + "gld=5. #gold value in %\n", + "val_res=(yel*10.+vio)*org\n", + "print '%s %.2f %s %.0f' %(\"\\n The value of resistor is\",val_res*1e-3,\"kohm (+/-)\",gld)\n", + "gld_ab=0.05 #absolute gold value\n", + "per_val=gld_ab*val_res\n", + "print '%s %.0f %s'%(\"\\n Now, 5%% of 47k_ohm =\",per_val, \"ohm\")\n", + "range_high=val_res+per_val #higher range\n", + "range_low=val_res-per_val #lower range\n", + "print'%s %.2f %s %.2f %s %.2f %s %.2f %s'%(\"\\n\\n Thus resistance should be within the range\",val_res*1e-3, \"kohm(+/-)\",per_val*1e-3 ,\"Kohm\\n or between\",range_low*1e-3 ,\"kohm and\",range_high*1e-3, \"kohm\")\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "With the help of colour coding table, one finds\n", + " 1st_Band 2nd_Band 3rd_Band 4th_Band\n", + " Yellow Violet Orange Gold\n", + " 4 7 10**3 (+/-)5%\n", + "\n", + " The value of resistor is 47.00 kohm (+/-) 5\n", + "\n", + " Now, 5%% of 47k_ohm = 2350 ohm\n", + "\n", + "\n", + " Thus resistance should be within the range 47.00 kohm(+/-) 2.35 Kohm\n", + " or between 44.65 kohm and 49.35 kohm\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E3 - Pg 44" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Calculate the value of the resistor\n", + "print '%s' %(\"The specification of the resistor from the color coding table is as follows\")\n", + "print '%s' %(\" 1st_Band 2nd_Band 3rd_Band 4th_Band\")\n", + "print '%s' %(\" Gray Blue Gold Silver\")\n", + "print '%s' %(\" 8 6 10**(-1) (+/-)10%\")\n", + "gray=8. #gray value\n", + "blu=6. #blue value\n", + "gld=10.**-1 #gold value\n", + "sil=10. #silver value in %\n", + "val_res=(gray*10.+blu)*gld\n", + "print '%s %.2f %s %.2f' %(\"\\n The value of resistor is\",val_res, \"ohm (+/-)\",sil)\n", + "sil_ab=0.1 #absolute gold value\n", + "per_val=sil_ab*val_res\n", + "print '%s %.2f %s' %(\"\\n Now, 10%% of 8.6 ohm =\",per_val,\" ohm\")\n", + "range_high=val_res+per_val #higher range\n", + "range_low=val_res-per_val #lower range\n", + "print '%s %.2f %s %.2f %s %.2f %s %.2f %s' %(\"\\n\\n Obviously resistance should lie within the range\",val_res,\"ohm(+/-)\",per_val,\"ohm\\n or between\",range_high,\"ohm and\",range_low,\"ohm\")\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The specification of the resistor from the color coding table is as follows\n", + " 1st_Band 2nd_Band 3rd_Band 4th_Band\n", + " Gray Blue Gold Silver\n", + " 8 6 10**(-1) (+/-)10%\n", + "\n", + " The value of resistor is 8.60 ohm (+/-) 10.00\n", + "\n", + " Now, 10%% of 8.6 ohm = 0.86 ohm\n", + "\n", + "\n", + " Obviously resistance should lie within the range 8.60 ohm(+/-) 0.86 ohm\n", + " or between 9.46 ohm and 7.74 ohm\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E4 - Pg 44" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Calculate the load voltage and load current\n", + "print '%s' %(\"Refer to the figure 1.53\")\n", + "Vs=2. #supply voltage in V\n", + "Rs=1. #resistance in ohm\n", + "Is=Vs/Rs\n", + "print '%s %.2f %s' %(\"\\n Current Is =\",Is,\" A \\n\")\n", + "print '%s' %(\" Internal resistance remains the same but is now connected in parralel with the current sourceIS,as shown in Figure 1.51(a)\")\n", + "print '%s' %(\" Now,we connect a load resistance R_L=1 ohm across the terminals of two representations ,and find I_L and V_L. From Figure 1.54(b) and using the current-divider concept,one obtains\")\n", + "RL=1. #load resistance in ohm\n", + "IL=Is*(Rs/(Rs+RL)) #load current using current-divider\n", + "VL=IL*RL #load voltage\n", + "print '%s %.0f %s' %(\"\\n Load voltage =\",VL,\"V\")\n", + "print '%s %.0f %s' %(\"\\n Load current =\",IL,\"A \\n\")\n", + "print '%s' %(\"From equation 53(b),using the voltage-divider concept,one obtains\")\n", + "VD_vl=Vs*(RL/(RL+Rs)) #load voltage using voltage divider \n", + "VD_il=VL/RL #load current\n", + "print '%s %.0f %s' %(\"\\n Load voltage =\",VD_vl,\"V\")\n", + "print '%s %.0f %s' %(\"\\n Load current =\",VD_il,\"A \\n\")\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Refer to the figure 1.53\n", + "\n", + " Current Is = 2.00 A \n", + "\n", + " Internal resistance remains the same but is now connected in parralel with the current sourceIS,as shown in Figure 1.51(a)\n", + " Now,we connect a load resistance R_L=1 ohm across the terminals of two representations ,and find I_L and V_L. From Figure 1.54(b) and using the current-divider concept,one obtains\n", + "\n", + " Load voltage = 1 V\n", + "\n", + " Load current = 1 A \n", + "\n", + "From equation 53(b),using the voltage-divider concept,one obtains\n", + "\n", + " Load voltage = 1 V\n", + "\n", + " Load current = 1 A \n", + "\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E5 - Pg 45" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Calculate the percentage variation in current, current for two extreme values R_L\n", + "print '%s' %(\"Refer to the figure 1.55\")\n", + "print '%s' %(\"(a) R_L varies from 1 ohm to 10 ohm.\")\n", + "print '%s' %(\"Currents for two extreme values of R_L are\")\n", + "Vs=10. #supply voltage\n", + "RL1=1. #resistance RL1\n", + "Rs=100. #source resistance\n", + "IL1=(Vs/(RL1+Rs))\n", + "RL2=10.\n", + "IL2=(Vs/(RL2+Rs))\n", + "per_var_cur=((IL1-IL2)/IL1)*100.\n", + "print '%s %.2f %s' %(\"\\n Percentage variation in current =\",per_var_cur,\"\\n\")#answer in the text book took a .3 decimal round off value\n", + "print '%s' %(\" Now,load voltage for the two extreme values of R_L are\")\n", + "VL1=IL1*RL1\n", + "VL2=IL2*RL2\n", + "per_var_vol=((VL2-VL1)/VL2)*100.\n", + "print '%s %.2f %s' %(\"\\n Percentage variation in current =\",per_var_vol,\"\\n\")\n", + "\n", + "print '%s' %(\"(b) R_L varies from 1 k-ohm to 10 k-ohm(Figure 1.55(b))\")\n", + "print '%s' %(\"Currents for the two extreme values R_L are\")\n", + "RL11=1000.\n", + "IL11=(Vs/(RL11+Rs))\n", + "RL22=10000.\n", + "IL22=(Vs/(RL22+Rs)) #mistake in book value\n", + "per_var_cur11=((IL11-IL22)/IL11)*100.\n", + "print '%s %.2f %s' %(\"\\n Percentage variation in current =\",per_var_cur11,\"\\n\") #mistake in book value\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Refer to the figure 1.55\n", + "(a) R_L varies from 1 ohm to 10 ohm.\n", + "Currents for two extreme values of R_L are\n", + "\n", + " Percentage variation in current = 8.18 \n", + "\n", + " Now,load voltage for the two extreme values of R_L are\n", + "\n", + " Percentage variation in current = 89.11 \n", + "\n", + "(b) R_L varies from 1 k-ohm to 10 k-ohm(Figure 1.55(b))\n", + "Currents for the two extreme values R_L are\n", + "\n", + " Percentage variation in current = 89.11 \n", + "\n" + ] + } + ], + "prompt_number": 3 + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/sample_notebooks/Prashanth Kumar Immady/Chapter_1_1.ipynb b/sample_notebooks/Prashanth Kumar Immady/Chapter_1_1.ipynb new file mode 100755 index 00000000..3756f226 --- /dev/null +++ b/sample_notebooks/Prashanth Kumar Immady/Chapter_1_1.ipynb @@ -0,0 +1,307 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:b71c5763a44a53c43becf4b38bc4dc6ca15fcaba00869ac6f1172cbb9803b804" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 1 - ELECTRONIC MATERIALS AND COMPONENTS" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E1 - Pg 43" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Calculate the value of resistor\n", + "print '%s' %(\"Refer to the figure 1.52\")\n", + "print '%s' %(\"Hold the resistor as shown in the figure such that tolerance is on your extreme right.\")\n", + "print '%s' %(\"Now the value of the resistor is equal to\")\n", + "print '%s' %(\" Red Black Blue Gold\")\n", + "print '%s' %(\" 2 0 6 (+/-)5%\")\n", + "red=2. #red value\n", + "blk=0 #black value\n", + "blu=6. #blue value\n", + "gld=5. #gold value\n", + "value_res=(red*10.+blk)*10.**blu #value of resistor\n", + "print '%s %.0f %s %.0f' %(\"\\n The value of resistor is\",value_res,\"ohm (+/-)\",gld)\n", + "per_val=0.05*value_res\n", + "pos_value_res=value_res+per_val #positive range of resistor\n", + "neg_value_res=value_res-per_val #negative range of resistor\n", + "print '%s %.0f %s %.0f %s ' %(\"\\n The value of resistor is\",neg_value_res*1e-6,\" Mohm and\",pos_value_res*1e-6,\"Mohm\")\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Refer to the figure 1.52\n", + "Hold the resistor as shown in the figure such that tolerance is on your extreme right.\n", + "Now the value of the resistor is equal to\n", + " Red Black Blue Gold\n", + " 2 0 6 (+/-)5%\n", + "\n", + " The value of resistor is 20000000 ohm (+/-) 5\n", + "\n", + " The value of resistor is 19 Mohm and 21 Mohm \n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E2 - Pg 43" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Calculate the value of the resistor\n", + "print '%s' %(\"With the help of colour coding table, one finds\")\n", + "print '%s' %(\" 1st_Band 2nd_Band 3rd_Band 4th_Band\")\n", + "print(\" Yellow Violet Orange Gold\")\n", + "print '%s' %(\" 4 7 10**3 (+/-)5%\")\n", + "yel=4. #yellow value\n", + "vio=7. #violet value\n", + "org=1e3 #orange value\n", + "gld=5. #gold value in %\n", + "val_res=(yel*10.+vio)*org\n", + "print '%s %.2f %s %.0f' %(\"\\n The value of resistor is\",val_res*1e-3,\"kohm (+/-)\",gld)\n", + "gld_ab=0.05 #absolute gold value\n", + "per_val=gld_ab*val_res\n", + "print '%s %.0f %s'%(\"\\n Now, 5%% of 47k_ohm =\",per_val, \"ohm\")\n", + "range_high=val_res+per_val #higher range\n", + "range_low=val_res-per_val #lower range\n", + "print'%s %.2f %s %.2f %s %.2f %s %.2f %s'%(\"\\n\\n Thus resistance should be within the range\",val_res*1e-3, \"kohm(+/-)\",per_val*1e-3 ,\"Kohm\\n or between\",range_low*1e-3 ,\"kohm and\",range_high*1e-3, \"kohm\")\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "With the help of colour coding table, one finds\n", + " 1st_Band 2nd_Band 3rd_Band 4th_Band\n", + " Yellow Violet Orange Gold\n", + " 4 7 10**3 (+/-)5%\n", + "\n", + " The value of resistor is 47.00 kohm (+/-) 5\n", + "\n", + " Now, 5%% of 47k_ohm = 2350 ohm\n", + "\n", + "\n", + " Thus resistance should be within the range 47.00 kohm(+/-) 2.35 Kohm\n", + " or between 44.65 kohm and 49.35 kohm\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E3 - Pg 44" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Calculate the value of the resistor\n", + "print '%s' %(\"The specification of the resistor from the color coding table is as follows\")\n", + "print '%s' %(\" 1st_Band 2nd_Band 3rd_Band 4th_Band\")\n", + "print '%s' %(\" Gray Blue Gold Silver\")\n", + "print '%s' %(\" 8 6 10**(-1) (+/-)10%\")\n", + "gray=8. #gray value\n", + "blu=6. #blue value\n", + "gld=10.**-1 #gold value\n", + "sil=10. #silver value in %\n", + "val_res=(gray*10.+blu)*gld\n", + "print '%s %.2f %s %.2f' %(\"\\n The value of resistor is\",val_res, \"ohm (+/-)\",sil)\n", + "sil_ab=0.1 #absolute gold value\n", + "per_val=sil_ab*val_res\n", + "print '%s %.2f %s' %(\"\\n Now, 10%% of 8.6 ohm =\",per_val,\" ohm\")\n", + "range_high=val_res+per_val #higher range\n", + "range_low=val_res-per_val #lower range\n", + "print '%s %.2f %s %.2f %s %.2f %s %.2f %s' %(\"\\n\\n Obviously resistance should lie within the range\",val_res,\"ohm(+/-)\",per_val,\"ohm\\n or between\",range_high,\"ohm and\",range_low,\"ohm\")\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The specification of the resistor from the color coding table is as follows\n", + " 1st_Band 2nd_Band 3rd_Band 4th_Band\n", + " Gray Blue Gold Silver\n", + " 8 6 10**(-1) (+/-)10%\n", + "\n", + " The value of resistor is 8.60 ohm (+/-) 10.00\n", + "\n", + " Now, 10%% of 8.6 ohm = 0.86 ohm\n", + "\n", + "\n", + " Obviously resistance should lie within the range 8.60 ohm(+/-) 0.86 ohm\n", + " or between 9.46 ohm and 7.74 ohm\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E4 - Pg 44" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Calculate the load voltage and load current\n", + "print '%s' %(\"Refer to the figure 1.53\")\n", + "Vs=2. #supply voltage in V\n", + "Rs=1. #resistance in ohm\n", + "Is=Vs/Rs\n", + "print '%s %.2f %s' %(\"\\n Current Is =\",Is,\" A \\n\")\n", + "print '%s' %(\" Internal resistance remains the same but is now connected in parralel with the current sourceIS,as shown in Figure 1.51(a)\")\n", + "print '%s' %(\" Now,we connect a load resistance R_L=1 ohm across the terminals of two representations ,and find I_L and V_L. From Figure 1.54(b) and using the current-divider concept,one obtains\")\n", + "RL=1. #load resistance in ohm\n", + "IL=Is*(Rs/(Rs+RL)) #load current using current-divider\n", + "VL=IL*RL #load voltage\n", + "print '%s %.0f %s' %(\"\\n Load voltage =\",VL,\"V\")\n", + "print '%s %.0f %s' %(\"\\n Load current =\",IL,\"A \\n\")\n", + "print '%s' %(\"From equation 53(b),using the voltage-divider concept,one obtains\")\n", + "VD_vl=Vs*(RL/(RL+Rs)) #load voltage using voltage divider \n", + "VD_il=VL/RL #load current\n", + "print '%s %.0f %s' %(\"\\n Load voltage =\",VD_vl,\"V\")\n", + "print '%s %.0f %s' %(\"\\n Load current =\",VD_il,\"A \\n\")\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Refer to the figure 1.53\n", + "\n", + " Current Is = 2.00 A \n", + "\n", + " Internal resistance remains the same but is now connected in parralel with the current sourceIS,as shown in Figure 1.51(a)\n", + " Now,we connect a load resistance R_L=1 ohm across the terminals of two representations ,and find I_L and V_L. From Figure 1.54(b) and using the current-divider concept,one obtains\n", + "\n", + " Load voltage = 1 V\n", + "\n", + " Load current = 1 A \n", + "\n", + "From equation 53(b),using the voltage-divider concept,one obtains\n", + "\n", + " Load voltage = 1 V\n", + "\n", + " Load current = 1 A \n", + "\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E5 - Pg 45" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Calculate the percentage variation in current, current for two extreme values R_L\n", + "print '%s' %(\"Refer to the figure 1.55\")\n", + "print '%s' %(\"(a) R_L varies from 1 ohm to 10 ohm.\")\n", + "print '%s' %(\"Currents for two extreme values of R_L are\")\n", + "Vs=10. #supply voltage\n", + "RL1=1. #resistance RL1\n", + "Rs=100. #source resistance\n", + "IL1=(Vs/(RL1+Rs))\n", + "RL2=10.\n", + "IL2=(Vs/(RL2+Rs))\n", + "per_var_cur=((IL1-IL2)/IL1)*100.\n", + "print '%s %.2f %s' %(\"\\n Percentage variation in current =\",per_var_cur,\"\\n\")#answer in the text book took a .3 decimal round off value\n", + "print '%s' %(\" Now,load voltage for the two extreme values of R_L are\")\n", + "VL1=IL1*RL1\n", + "VL2=IL2*RL2\n", + "per_var_vol=((VL2-VL1)/VL2)*100.\n", + "print '%s %.2f %s' %(\"\\n Percentage variation in current =\",per_var_vol,\"\\n\")\n", + "\n", + "print '%s' %(\"(b) R_L varies from 1 k-ohm to 10 k-ohm(Figure 1.55(b))\")\n", + "print '%s' %(\"Currents for the two extreme values R_L are\")\n", + "RL11=1000.\n", + "IL11=(Vs/(RL11+Rs))\n", + "RL22=10000.\n", + "IL22=(Vs/(RL22+Rs)) #mistake in book value\n", + "per_var_cur11=((IL11-IL22)/IL11)*100.\n", + "print '%s %.2f %s' %(\"\\n Percentage variation in current =\",per_var_cur11,\"\\n\") #mistake in book value\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Refer to the figure 1.55\n", + "(a) R_L varies from 1 ohm to 10 ohm.\n", + "Currents for two extreme values of R_L are\n", + "\n", + " Percentage variation in current = 8.18 \n", + "\n", + " Now,load voltage for the two extreme values of R_L are\n", + "\n", + " Percentage variation in current = 89.11 \n", + "\n", + "(b) R_L varies from 1 k-ohm to 10 k-ohm(Figure 1.55(b))\n", + "Currents for the two extreme values R_L are\n", + "\n", + " Percentage variation in current = 89.11 \n", + "\n" + ] + } + ], + "prompt_number": 3 + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/sample_notebooks/Prashanth Kumar Immady/Chapter_1_2.ipynb b/sample_notebooks/Prashanth Kumar Immady/Chapter_1_2.ipynb new file mode 100755 index 00000000..3756f226 --- /dev/null +++ b/sample_notebooks/Prashanth Kumar Immady/Chapter_1_2.ipynb @@ -0,0 +1,307 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:b71c5763a44a53c43becf4b38bc4dc6ca15fcaba00869ac6f1172cbb9803b804" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 1 - ELECTRONIC MATERIALS AND COMPONENTS" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E1 - Pg 43" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Calculate the value of resistor\n", + "print '%s' %(\"Refer to the figure 1.52\")\n", + "print '%s' %(\"Hold the resistor as shown in the figure such that tolerance is on your extreme right.\")\n", + "print '%s' %(\"Now the value of the resistor is equal to\")\n", + "print '%s' %(\" Red Black Blue Gold\")\n", + "print '%s' %(\" 2 0 6 (+/-)5%\")\n", + "red=2. #red value\n", + "blk=0 #black value\n", + "blu=6. #blue value\n", + "gld=5. #gold value\n", + "value_res=(red*10.+blk)*10.**blu #value of resistor\n", + "print '%s %.0f %s %.0f' %(\"\\n The value of resistor is\",value_res,\"ohm (+/-)\",gld)\n", + "per_val=0.05*value_res\n", + "pos_value_res=value_res+per_val #positive range of resistor\n", + "neg_value_res=value_res-per_val #negative range of resistor\n", + "print '%s %.0f %s %.0f %s ' %(\"\\n The value of resistor is\",neg_value_res*1e-6,\" Mohm and\",pos_value_res*1e-6,\"Mohm\")\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Refer to the figure 1.52\n", + "Hold the resistor as shown in the figure such that tolerance is on your extreme right.\n", + "Now the value of the resistor is equal to\n", + " Red Black Blue Gold\n", + " 2 0 6 (+/-)5%\n", + "\n", + " The value of resistor is 20000000 ohm (+/-) 5\n", + "\n", + " The value of resistor is 19 Mohm and 21 Mohm \n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E2 - Pg 43" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Calculate the value of the resistor\n", + "print '%s' %(\"With the help of colour coding table, one finds\")\n", + "print '%s' %(\" 1st_Band 2nd_Band 3rd_Band 4th_Band\")\n", + "print(\" Yellow Violet Orange Gold\")\n", + "print '%s' %(\" 4 7 10**3 (+/-)5%\")\n", + "yel=4. #yellow value\n", + "vio=7. #violet value\n", + "org=1e3 #orange value\n", + "gld=5. #gold value in %\n", + "val_res=(yel*10.+vio)*org\n", + "print '%s %.2f %s %.0f' %(\"\\n The value of resistor is\",val_res*1e-3,\"kohm (+/-)\",gld)\n", + "gld_ab=0.05 #absolute gold value\n", + "per_val=gld_ab*val_res\n", + "print '%s %.0f %s'%(\"\\n Now, 5%% of 47k_ohm =\",per_val, \"ohm\")\n", + "range_high=val_res+per_val #higher range\n", + "range_low=val_res-per_val #lower range\n", + "print'%s %.2f %s %.2f %s %.2f %s %.2f %s'%(\"\\n\\n Thus resistance should be within the range\",val_res*1e-3, \"kohm(+/-)\",per_val*1e-3 ,\"Kohm\\n or between\",range_low*1e-3 ,\"kohm and\",range_high*1e-3, \"kohm\")\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "With the help of colour coding table, one finds\n", + " 1st_Band 2nd_Band 3rd_Band 4th_Band\n", + " Yellow Violet Orange Gold\n", + " 4 7 10**3 (+/-)5%\n", + "\n", + " The value of resistor is 47.00 kohm (+/-) 5\n", + "\n", + " Now, 5%% of 47k_ohm = 2350 ohm\n", + "\n", + "\n", + " Thus resistance should be within the range 47.00 kohm(+/-) 2.35 Kohm\n", + " or between 44.65 kohm and 49.35 kohm\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E3 - Pg 44" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Calculate the value of the resistor\n", + "print '%s' %(\"The specification of the resistor from the color coding table is as follows\")\n", + "print '%s' %(\" 1st_Band 2nd_Band 3rd_Band 4th_Band\")\n", + "print '%s' %(\" Gray Blue Gold Silver\")\n", + "print '%s' %(\" 8 6 10**(-1) (+/-)10%\")\n", + "gray=8. #gray value\n", + "blu=6. #blue value\n", + "gld=10.**-1 #gold value\n", + "sil=10. #silver value in %\n", + "val_res=(gray*10.+blu)*gld\n", + "print '%s %.2f %s %.2f' %(\"\\n The value of resistor is\",val_res, \"ohm (+/-)\",sil)\n", + "sil_ab=0.1 #absolute gold value\n", + "per_val=sil_ab*val_res\n", + "print '%s %.2f %s' %(\"\\n Now, 10%% of 8.6 ohm =\",per_val,\" ohm\")\n", + "range_high=val_res+per_val #higher range\n", + "range_low=val_res-per_val #lower range\n", + "print '%s %.2f %s %.2f %s %.2f %s %.2f %s' %(\"\\n\\n Obviously resistance should lie within the range\",val_res,\"ohm(+/-)\",per_val,\"ohm\\n or between\",range_high,\"ohm and\",range_low,\"ohm\")\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The specification of the resistor from the color coding table is as follows\n", + " 1st_Band 2nd_Band 3rd_Band 4th_Band\n", + " Gray Blue Gold Silver\n", + " 8 6 10**(-1) (+/-)10%\n", + "\n", + " The value of resistor is 8.60 ohm (+/-) 10.00\n", + "\n", + " Now, 10%% of 8.6 ohm = 0.86 ohm\n", + "\n", + "\n", + " Obviously resistance should lie within the range 8.60 ohm(+/-) 0.86 ohm\n", + " or between 9.46 ohm and 7.74 ohm\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E4 - Pg 44" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Calculate the load voltage and load current\n", + "print '%s' %(\"Refer to the figure 1.53\")\n", + "Vs=2. #supply voltage in V\n", + "Rs=1. #resistance in ohm\n", + "Is=Vs/Rs\n", + "print '%s %.2f %s' %(\"\\n Current Is =\",Is,\" A \\n\")\n", + "print '%s' %(\" Internal resistance remains the same but is now connected in parralel with the current sourceIS,as shown in Figure 1.51(a)\")\n", + "print '%s' %(\" Now,we connect a load resistance R_L=1 ohm across the terminals of two representations ,and find I_L and V_L. From Figure 1.54(b) and using the current-divider concept,one obtains\")\n", + "RL=1. #load resistance in ohm\n", + "IL=Is*(Rs/(Rs+RL)) #load current using current-divider\n", + "VL=IL*RL #load voltage\n", + "print '%s %.0f %s' %(\"\\n Load voltage =\",VL,\"V\")\n", + "print '%s %.0f %s' %(\"\\n Load current =\",IL,\"A \\n\")\n", + "print '%s' %(\"From equation 53(b),using the voltage-divider concept,one obtains\")\n", + "VD_vl=Vs*(RL/(RL+Rs)) #load voltage using voltage divider \n", + "VD_il=VL/RL #load current\n", + "print '%s %.0f %s' %(\"\\n Load voltage =\",VD_vl,\"V\")\n", + "print '%s %.0f %s' %(\"\\n Load current =\",VD_il,\"A \\n\")\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Refer to the figure 1.53\n", + "\n", + " Current Is = 2.00 A \n", + "\n", + " Internal resistance remains the same but is now connected in parralel with the current sourceIS,as shown in Figure 1.51(a)\n", + " Now,we connect a load resistance R_L=1 ohm across the terminals of two representations ,and find I_L and V_L. From Figure 1.54(b) and using the current-divider concept,one obtains\n", + "\n", + " Load voltage = 1 V\n", + "\n", + " Load current = 1 A \n", + "\n", + "From equation 53(b),using the voltage-divider concept,one obtains\n", + "\n", + " Load voltage = 1 V\n", + "\n", + " Load current = 1 A \n", + "\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E5 - Pg 45" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Calculate the percentage variation in current, current for two extreme values R_L\n", + "print '%s' %(\"Refer to the figure 1.55\")\n", + "print '%s' %(\"(a) R_L varies from 1 ohm to 10 ohm.\")\n", + "print '%s' %(\"Currents for two extreme values of R_L are\")\n", + "Vs=10. #supply voltage\n", + "RL1=1. #resistance RL1\n", + "Rs=100. #source resistance\n", + "IL1=(Vs/(RL1+Rs))\n", + "RL2=10.\n", + "IL2=(Vs/(RL2+Rs))\n", + "per_var_cur=((IL1-IL2)/IL1)*100.\n", + "print '%s %.2f %s' %(\"\\n Percentage variation in current =\",per_var_cur,\"\\n\")#answer in the text book took a .3 decimal round off value\n", + "print '%s' %(\" Now,load voltage for the two extreme values of R_L are\")\n", + "VL1=IL1*RL1\n", + "VL2=IL2*RL2\n", + "per_var_vol=((VL2-VL1)/VL2)*100.\n", + "print '%s %.2f %s' %(\"\\n Percentage variation in current =\",per_var_vol,\"\\n\")\n", + "\n", + "print '%s' %(\"(b) R_L varies from 1 k-ohm to 10 k-ohm(Figure 1.55(b))\")\n", + "print '%s' %(\"Currents for the two extreme values R_L are\")\n", + "RL11=1000.\n", + "IL11=(Vs/(RL11+Rs))\n", + "RL22=10000.\n", + "IL22=(Vs/(RL22+Rs)) #mistake in book value\n", + "per_var_cur11=((IL11-IL22)/IL11)*100.\n", + "print '%s %.2f %s' %(\"\\n Percentage variation in current =\",per_var_cur11,\"\\n\") #mistake in book value\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Refer to the figure 1.55\n", + "(a) R_L varies from 1 ohm to 10 ohm.\n", + "Currents for two extreme values of R_L are\n", + "\n", + " Percentage variation in current = 8.18 \n", + "\n", + " Now,load voltage for the two extreme values of R_L are\n", + "\n", + " Percentage variation in current = 89.11 \n", + "\n", + "(b) R_L varies from 1 k-ohm to 10 k-ohm(Figure 1.55(b))\n", + "Currents for the two extreme values R_L are\n", + "\n", + " Percentage variation in current = 89.11 \n", + "\n" + ] + } + ], + "prompt_number": 3 + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/sample_notebooks/SPANDANAARROJU/Chapter4.ipynb b/sample_notebooks/SPANDANAARROJU/Chapter4.ipynb new file mode 100755 index 00000000..f4145c55 --- /dev/null +++ b/sample_notebooks/SPANDANAARROJU/Chapter4.ipynb @@ -0,0 +1,553 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:e9b50f0b4ca0520935774156fedb1fdaaf2b2fd5241b8184a650d42b25d657cd" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "4: Interference" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example number 4.1, Page number 69" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#importing modules\n", + "import math\n", + "from __future__ import division\n", + "\n", + "#Variable declaration\n", + "i=40; #angle of incidence(degrees)\n", + "mew=1.2; #refractive index\n", + "t=0.23; #thickness of the film(micro m)\n", + "\n", + "#Calculation\n", + "i=i*math.pi/180; #angle of incidence(radian)\n", + "r=math.asin(math.sin(i)/mew); #angle of refraction(radian)\n", + "lambda1=(2*mew*t*math.cos(r))*10**3; #wavelength absent(nm) \n", + "lambda2=lambda1/2;\n", + "\n", + "#Result\n", + "print \"The wavelength absent is\",round(lambda1,1),\"nm\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The wavelength absent is 466.1 nm\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example number 4.2, Page number 69" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#importing modules\n", + "import math\n", + "from __future__ import division\n", + "\n", + "#Variable declaration\n", + "lambda1=400*10**-9; #wavelength 1(m)\n", + "lambda2=600*10**-9; #wavelength 2(m)\n", + "#2*t=n*lambda\n", + "n=150; \n", + "\n", + "#Calculation \n", + "t=((n*lambda2)/2)*10**6; #thickness of the air film(micro meter)\n", + "\n", + "#Result\n", + "print \"The thickness of the air film is\",t,\"micro m\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The thickness of the air film is 45.0 micro m\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example number 4.3, Page number 70" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#importing modules\n", + "import math\n", + "from __future__ import division\n", + "\n", + "#Variable declaration\n", + "lamda=600*10**-9; #wavelength(m)\n", + "mew=2;\n", + "theta=0.025; #wedge-angle(degrees)\n", + "\n", + "#Calculation \n", + "theta=theta*math.pi/180; #wedge-angle(radian)\n", + "x=(lamda/(2*mew*math.sin(theta)))*10**2; #bandwidth(cm)\n", + "\n", + "#Result\n", + "print \"The bandwidth is\",round(x,3),\"cm\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The bandwidth is 0.034 cm\n" + ] + } + ], + "prompt_number": 10 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example number 4.4, Page number 70" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#importing modules\n", + "import math\n", + "from __future__ import division\n", + "\n", + "#Variable declaration\n", + "xair=0.15; #bandwidth of air(cm)\n", + "xliq=0.115; #bandwidth of liquid(cm)\n", + "mewair=1; #refractive index of air\n", + "\n", + "#Calculation \n", + "mewliq=(xair*mewair)/xliq; #refractive index of liquid\n", + "\n", + "#Result\n", + "print \"The refractive index of liquid is\",round(mewliq,1)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The refractive index of liquid is 1.3\n" + ] + } + ], + "prompt_number": 12 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example number 4.5, Page number 70" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#importing modules\n", + "import math\n", + "from __future__ import division\n", + "\n", + "#Variable declaration\n", + "n=9;\n", + "lamda=589*10**-9; #wavelength of light used(m)\n", + "R=0.95; #radius of curvature of lens(m)\n", + "mew=1;\n", + "\n", + "#Calculation \n", + "D=(math.sqrt((4*n*lamda*R)/mew))*10**2; #diameter of the ninth dark ring(m)\n", + "\n", + "#Result\n", + "print \"The diameter of the ninth dark ring is\",round(D,2),\"cm\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The diameter of the ninth dark ring is 0.45 cm\n" + ] + } + ], + "prompt_number": 15 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example number 4.6, Page number 70" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#importing modules\n", + "import math\n", + "from __future__ import division\n", + "\n", + "#Variable declaration\n", + "x=0.055; #distance in fringe shift(mm)\n", + "n=200; #number of fringes\n", + "\n", + "#Calculation \n", + "lamda=((2*x)/n)*10**6; #wavelength(nm)\n", + "\n", + "#Result\n", + "print \"The wavelength of light used is\",lamda,\"nm\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The wavelength of light used is 550.0 nm\n" + ] + } + ], + "prompt_number": 17 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example number 4.7, Page number 70" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#importing modules\n", + "import math\n", + "from __future__ import division\n", + "\n", + "#Variable declaration\n", + "n=50; #number of fringes\n", + "lamda=500*10**-9; #wavelength of light used(m)\n", + "mew=1.5; #refractive index of the plate\n", + "\n", + "#Calculation \n", + "t=((n*lamda)/(2*(mew-1)))*10**6; #thickness of the plate(micro meter)\n", + "\n", + "#Result\n", + "print \"The thickness of the plate is\",t,\"micro m\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The thickness of the plate is 25.0 micro m\n" + ] + } + ], + "prompt_number": 20 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example number 4.8, Page number 70" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#importing modules\n", + "import math\n", + "from __future__ import division\n", + "\n", + "#Variable declaration\n", + "lamda=550*10**-9; #wavelength(m)\n", + "mew=1.38; #refractive index\n", + "\n", + "#Calculation \n", + "t=(lamda/(4*mew))*10**9; #thickness(nm)\n", + "\n", + "#Result\n", + "print \"The minimum thickness of the plate for normal incidence of light is\",round(t,3),\"nm\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The minimum thickness of the plate for normal incidence of light is 99.638 nm\n" + ] + } + ], + "prompt_number": 23 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example number 4.9, Page number 70" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#importing modules\n", + "import math\n", + "from __future__ import division\n", + "\n", + "#Variable declaration\n", + "i=35; #angle of incidence(degrees)\n", + "mew=1.4; #refractive index\n", + "n=50; \n", + "lamda=459*10**-9; #wavelength(m)\n", + "\n", + "#Calculation \n", + "i=i*math.pi/180; #angle of incidence(radian)\n", + "r=math.asin(math.sin(i)/mew); #angle of refraction(radian)\n", + "#2*mew*cos(r)=n*lambda\n", + "#n(459)=(n+1)450\n", + "t=(n*lamda/(2*mew*math.cos(r)))*10**6; #thickness of the film(micro meter)\n", + "\n", + "#Result\n", + "print \"The thickness of the film is\",round(t,3),\"micro m\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The thickness of the film is 8.985 micro m\n" + ] + } + ], + "prompt_number": 26 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example number 4.10, Page number 71" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#importing modules\n", + "import math\n", + "from __future__ import division\n", + "\n", + "#Variable declaration\n", + "lamda=500*10**-9; #wavelength(m)\n", + "x=0.07; #observed band width(cm)\n", + "mew=1; #refractive index\n", + "\n", + "#Calculation \n", + "theta=(math.asin(lamda/(2*mew*x)))*10**2; #wedge angle(radian)\n", + "theta=theta*180/math.pi; #wedge angle(degrees)\n", + "\n", + "#Result\n", + "print \"The wedge angle is\",round(theta,2),\"degrees\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The wedge angle is 0.02 degrees\n" + ] + } + ], + "prompt_number": 31 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example number 4.11, Page number 71" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#importing modules\n", + "import math\n", + "from __future__ import division\n", + "\n", + "#Variable declaration\n", + "dair=0.42; #diameter of certain rings(cm)\n", + "dliq=0.38; #diameter of rings when liquid is introduced(cm)\n", + "\n", + "#Calculation \n", + "mew=dair**2/dliq**2; #refractive index of liquid\n", + "\n", + "#Result\n", + "print \"The refravtive index of liquid is\",round(mew,2)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The refravtive index of liquid is 1.22\n" + ] + } + ], + "prompt_number": 33 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example number 4.12, Page number 71" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#importing modules\n", + "import math\n", + "from __future__ import division\n", + "\n", + "#Variable declaration\n", + "m=8; #eigth ring\n", + "n=3; #third ring\n", + "dm=0.4; #diameter of the eigth ring(cm)\n", + "dn=0.2; #diameter of the third ring(cm)\n", + "R=101; #Radius of curvature(cm)\n", + "\n", + "#Calculation \n", + "lamda=(((dm**2)-(dn**2))/(4*R*(m-n))); #wavelength of light(cm) \n", + "\n", + "#Result\n", + "print \"The wavelength of light used is\",round(lamda*10**5,4),\"*10**-5 cm\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The wavelength of light used is 5.9406 *10**-5 cm\n" + ] + } + ], + "prompt_number": 39 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example number 4.13, Page number 71" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#importing modules\n", + "import math\n", + "from __future__ import division\n", + "\n", + "#Variable declaration\n", + "mew=1.38; #refractive index of magnesium floride\n", + "t=175; #thickness of coating of magnesium fluoride(nm)\n", + "\n", + "#Calculation \n", + "lamda=4*t*mew; #wavelength(nm)\n", + "\n", + "#Result\n", + "print \"The wavelength which has high transmission is\",lamda,\"nm\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The wavelength which has high transmission is 966.0 nm\n" + ] + } + ], + "prompt_number": 41 + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/sample_notebooks/SUMITRATHI/chapterno1.ipynb b/sample_notebooks/SUMITRATHI/chapterno1.ipynb new file mode 100755 index 00000000..5ff2285e --- /dev/null +++ b/sample_notebooks/SUMITRATHI/chapterno1.ipynb @@ -0,0 +1,126 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:a1ed470be69235951f179c73fc3f7daca02bf5e071f528ddce3fb4f1444cb8ef" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 1: INTRODUCTORY DIGITAL CONCEPTS" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1-1,Page No-6" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Variable Declaration\n", + "T=10*10**-3\n", + "tw=1*10**-3\n", + "\n", + "#Calculations\n", + "#Part A\n", + "f=1/T\n", + "\n", + "#Part C\n", + "Duty_Cycle=(tw/T)*100\n", + "\n", + "\n", + "#Results\n", + "print\"The Period is measured from the edge of the next pulse. In this case T is measured from leading edge to leading edge,as indicated.T equals 10*10^-3\"\n", + "print\"f=\",f,\"Hz\"\n", + "print\"Duty Cycle=\",Duty_Cycle,\"%\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The Period is measured from the edge of the next pulse. In this case T is measured from leading edge to leading edge,as indicated.T equals 10*10^-3\n", + "f= 100.0 Hz\n", + "Duty Cycle= 10.0 %\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1-2, Page No-8" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Variable Declaration\n", + "f=100*10**-3\n", + "time=8\n", + "T=1/f\n", + "print\"Since the frequency of the clock is 100kHz,the period is\",T,\"usec\"\n", + "print\"It takes 10*10**-6to transfer each bit in the waveform.The total transfer time for 8 bits is time\"\n", + "print\"Time is\",time,\"usec\"\n", + "\n", + "print\"To detrmine the sequence of bits,examine the waveform during each bit time.If waveform A is HIGH during the bit time, a 1 is transferred. If waveform A is LOW during the bit time,a0 is transferred. The bit sequence is illustrated .The left mosst bit is the first to be transferred.\"\n", + "\n", + "print\"A parallel transfer would take 10*10**-6 for all eight bits.\"\n", + " \n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Since the frequency of the clock is 100kHz,the period is 10.0 usec\n", + "It takes 10*10**-6to transfer each bit in the waveform.The total transfer time for 8 bits is time\n", + "Time is 8 usec\n", + "To detrmine the sequence of bits,examine the waveform during each bit time.If waveform A is HIGH during the bit time, a 1 is transferred. If waveform A is LOW during the bit time,a0 is transferred. The bit sequence is illustrated .The left mosst bit is the first to be transferred.\n", + "A parallel transfer would take 10*10**-6 for all eight bits.\n" + ] + } + ], + "prompt_number": 14 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 8 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [], + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/sample_notebooks/SrutiGoyal/Chapter_11-_Object_Initialization_and_Clean-Up_2.ipynb b/sample_notebooks/SrutiGoyal/Chapter_11-_Object_Initialization_and_Clean-Up_2.ipynb new file mode 100755 index 00000000..d3bdda01 --- /dev/null +++ b/sample_notebooks/SrutiGoyal/Chapter_11-_Object_Initialization_and_Clean-Up_2.ipynb @@ -0,0 +1,1779 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:64b755d597a2016634dadbbc81a598a60446119cc89f4f94fd9140b5bc077b77" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 11: Object Initialization and clean up" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example- bag.cpp, Page-392" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "MAX_ITEMS=25\n", + "def show(self):\n", + " for i in range(self.ItemCount):\n", + " print self._Bag__contents[i],\n", + "class Bag:\n", + " __contents=[int]*MAX_ITEMS\n", + " __ItemCount=int\n", + " def SetEmpty(self):\n", + " self.ItemCount=0\n", + " def put(self,item):\n", + " self._Bag__contents[self.ItemCount]=item\n", + " self.ItemCount+=1\n", + " show=show\n", + "bag=Bag() #object of class Bag\n", + "bag.SetEmpty() #initialize the object\n", + "while 1:\n", + " item=int(raw_input(\"\\nEnter Item Number to be put into the bag <0-no item>: \"))\n", + " if item==0:\n", + " break\n", + " bag.put(item)\n", + " print \"Items in bag:\",\n", + " bag.show()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "Enter Item Number to be put into the bag <0-no item>: 1\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Items in bag: 1" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "Enter Item Number to be put into the bag <0-no item>: 3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Items in bag: 1 3" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "Enter Item Number to be put into the bag <0-no item>: 2\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Items in bag: 1 3 2" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "Enter Item Number to be put into the bag <0-no item>: 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Items in bag: 1 3 2 4" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "Enter Item Number to be put into the bag <0-no item>: 0\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example- newbag.cpp, Page-395" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "MAX_ITEMS=25 #size of array contents\n", + "def show(self):\n", + " for i in range(self.ItemCount):\n", + " print self._Bag__contents[i],\n", + "class Bag:\n", + " __contents=[int]*MAX_ITEMS #int 1D array\n", + " __ItemCount=int\n", + " def __init__(self): #Constructor\n", + " self.ItemCount=0\n", + " def put(self,item): #member function defined inside the class\n", + " self._Bag__contents[self.ItemCount]=item\n", + " self.ItemCount+=1\n", + " show=show #member function defined outside the class\n", + "bag=Bag() #object of class Bag\n", + "while 1:\n", + " item=int(raw_input(\"\\nEnter Item Number to be put into the bag <0-no item>: \"))\n", + " if item==0:\n", + " break\n", + " bag.put(item)\n", + " print \"Items in bag:\",\n", + " bag.show()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "Enter Item Number to be put into the bag <0-no item>: 1\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Items in bag: 1" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "Enter Item Number to be put into the bag <0-no item>: 3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Items in bag: 1 3" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "Enter Item Number to be put into the bag <0-no item>: 2\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Items in bag: 1 3 2" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "Enter Item Number to be put into the bag <0-no item>: 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Items in bag: 1 3 2 4" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "Enter Item Number to be put into the bag <0-no item>: 0\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example-test1.cpp, Page-396" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def __init__(self):\n", + " print \"Constructor of class test called\"\n", + "class Test:\n", + " __init__=__init__ #Constructor\n", + "G=Test()\n", + "def func():\n", + " L=Test()\n", + " print \"Here's function func()\"\n", + "X=Test()\n", + "print \"main() function\"\n", + "func()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Constructor of class test called\n", + "Constructor of class test called\n", + "main() function\n", + "Constructor of class test called\n", + "Here's function func()\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example- giftbag.cpp, Page- 398" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "MAX_ITEMS=25\n", + "def show(self):\n", + " if self.ItemCount:\n", + " for i in range(self.ItemCount):\n", + " print self._Bag__contents[i],\n", + " else:\n", + " print \"Nil\"\n", + "class Bag:\n", + " __contents=[int]*MAX_ITEMS\n", + " __ItemCount=int\n", + " def __init__(self, item=None): #parameterized constructor: Python does not support overloading of functions\n", + " if isinstance(item, int):\n", + " self._Bag__contents[0]=item\n", + " self.ItemCount=1\n", + " else:\n", + " self.ItemCount=0\n", + " def put(self,item):\n", + " self._Bag__contents[self.ItemCount]=item\n", + " self.ItemCount+=1\n", + " show=show\n", + "bag1=Bag()\n", + "bag2=Bag(4) #object created using the parameterized constructor\n", + "print \"Gifted bag1 initially has:\",\n", + "bag1.show()\n", + "print \"Gifted bag2 initially has:\",\n", + "bag2.show()\n", + "while 1:\n", + " item=int(raw_input(\"\\nEnter Item Number to be put into the bag <0-no item>: \"))\n", + " if item==0:\n", + " break\n", + " bag2.put(item)\n", + " print \"Items in bag2:\",\n", + " bag2.show()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Gifted bag1 initially has: Nil\n", + "Gifted bag2 initially has: 4" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "Enter Item Number to be put into the bag <0-no item>: 1\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Items in bag2: 4 1" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "Enter Item Number to be put into the bag <0-no item>: 2\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Items in bag2: 4 1 2" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "Enter Item Number to be put into the bag <0-no item>: 3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Items in bag2: 4 1 2 3" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "Enter Item Number to be put into the bag <0-no item>: 0\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example-test.cpp, Page-400 " + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def __init__(self):\n", + " print \"Constructor of class Test called\"\n", + "def __del__(self):\n", + " print \"Destructor of class Test called\"\n", + "class Test:\n", + " __init__=__init__ #Constructor\n", + " __del__=__del__ #Destructor\n", + "x=Test()\n", + "print \"Terminating main\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Constructor of class Test called\n", + "Destructor of class Test called\n", + "Terminating main\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example-count.cpp, Page-401" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "nobjects=0\n", + "nobj_alive=0\n", + "class MyClass:\n", + " def __init__(self):\n", + " global nobjects #using the global nobjects\n", + " global nobj_alive #using the global nobj_alive\n", + " nobjects+=1\n", + " nobj_alive+=1\n", + " def __del__(self):\n", + " global nobj_alive #using the global nobjects\n", + " nobj_alive-=1\n", + " def show(self):\n", + " global nobjects\n", + " global nobj_alive\n", + " print \"Total number of objects created: \", nobjects\n", + " print \"Number of objects currently alive: \", nobj_alive\n", + "obj1=MyClass()\n", + "obj1.show()\n", + "def func():\n", + " obj1=MyClass()\n", + " obj2=MyClass()\n", + " obj2.show()\n", + " del obj1\n", + " del obj2\n", + "func()\n", + "obj1.show()\n", + "obj2=MyClass()\n", + "obj3=MyClass()\n", + "obj2.show()\n", + "del obj1\n", + "del obj2\n", + "del obj3" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Total number of objects created: 1\n", + "Number of objects currently alive: 1\n", + "Total number of objects created: 3\n", + "Number of objects currently alive: 3\n", + "Total number of objects created: 3\n", + "Number of objects currently alive: 1\n", + "Total number of objects created: 5\n", + "Number of objects currently alive: 3\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Example-account.cpp, Page- 403" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def MoneyTransfer(self, acc , amount):\n", + " self._AccClass__balance=self._AccClass__balance-amount\n", + " acc._AccClass__balance=acc._AccClass__balance + amount\n", + "class AccClass:\n", + " __accno=int\n", + " __balance=float\n", + " def __init__(self, an=None, bal=0.0):\n", + " if isinstance(an, int):\n", + " self.accno=an\n", + " self.__balance=bal\n", + " else:\n", + " self.accno=raw_input(\"Enter account number for acc1 object: \")\n", + " self.__balance=float(raw_input(\"Enter the balance: \"))\n", + " def display(self):\n", + " print \"Acoount number is: \", self.accno\n", + " print \"Balance is: \", self.__balance\n", + " MoneyTransfer=MoneyTransfer\n", + "acc1=AccClass()\n", + "acc2=AccClass(10)\n", + "acc3=AccClass(20, 750.5)\n", + "print \"Acoount information...\"\n", + "acc1.display()\n", + "acc2.display()\n", + "acc3.display()\n", + "trans_money=float(raw_input(\"How much money is to be transferred from acc3 to acc1: \"))\n", + "acc3.MoneyTransfer(acc1, trans_money)\n", + "print \"Updated information about accounts...\"\n", + "acc1.display()\n", + "acc2.display()\n", + "acc3.display()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter account number for acc1 object: 1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the balance: 100\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Acoount information...\n", + "Acoount number is: 1\n", + "Balance is: 100.0\n", + "Acoount number is: 10\n", + "Balance is: 0.0\n", + "Acoount number is: 20\n", + "Balance is: 750.5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "How much money is to be transferred from acc3 to acc1: 200\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Updated information about accounts...\n", + "Acoount number is: 1\n", + "Balance is: 300.0\n", + "Acoount number is: 10\n", + "Balance is: 0.0\n", + "Acoount number is: 20\n", + "Balance is: 550.5\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example-test2.cpp. Page- 405" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def __init__(self, NameIn=None):\n", + " if isinstance(NameIn, str):\n", + " self.name=NameIn\n", + " print \"Test Object \", NameIn, \" created\"\n", + " else:\n", + " self.name=\"unnamed\"\n", + " print \"Test object 'unnamed' created\"\n", + "def __del__(self):\n", + " print \"Test Object \", self.name, \" destroyed\"\n", + " del self.name\n", + "class Test:\n", + " __name=[str]\n", + " __init__=__init__\n", + " __del__=__del__\n", + "g=Test(\"global\")\n", + "def func():\n", + " l=Test(\"func\")\n", + " print \"here's function func()\"\n", + "x=Test(\"main\")\n", + "func()\n", + "print \"main() function - termination\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Test Object global created\n", + "Test Object global destroyed\n", + "Test Object main created\n", + "Test Object main destroyed\n", + "Test Object func created\n", + "here's function func()\n", + "Test Object func destroyed\n", + "main() function - termination\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example-complex1.cpp, Page- 407" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import math\n", + "def add (self, c2):\n", + " temp=Complex()\n", + " temp._Complex__real=self._Complex__real+c2._Complex__real\n", + " temp._Complex__imag=self._Complex__imag+c2._Complex__imag\n", + " return temp\n", + "class Complex:\n", + " __real=float\n", + " __imag=float\n", + " def __init__(self, real_in=None, imag_in=0.0):\n", + " if isinstance(real_in, float):\n", + " self.__real=real_in\n", + " self.__imag=imag_in\n", + " else:\n", + " self.__real=self.__imag=0.0\n", + " def show(self, msg):\n", + " print msg, \n", + " print self.__real,\n", + " if self.__imag<0:\n", + " print \"-i\",\n", + " else:\n", + " print \"+i\",\n", + " print math.fabs(self.__imag) #print absolute value\n", + " add=add\n", + "c1=Complex(1.5,2.0)\n", + "c2=Complex(2.2)\n", + "c3=Complex()\n", + "c1.show(\"c1=\")\n", + "c2.show(\"c2=\")\n", + "c3=c1.add(c2)\n", + "c3.show(\"c3=c1.add(c2):\")" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "c1= 1.5 +i 2.0\n", + "c2= 2.2 +i 0.0\n", + "c3=c1.add(c2): 3.7 +i 2.0\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example- noname.cpp, Page- 410" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class nameless:\n", + " __a=int\n", + " def __init__(self):\n", + " print \"Constructor\"\n", + " def __del__(self):\n", + " print \"Destructor\"\n", + "nameless() #nameless object created\n", + "n1=nameless()\n", + "n2=nameless()\n", + "print \"Program terminates\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Constructor\n", + "Destructor\n", + "Constructor\n", + "Destructor\n", + "Constructor\n", + "Destructor\n", + "Program terminates\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example-name.cpp, Page-411" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def show(self, msg):\n", + " print msg\n", + " print \"First Name: \", self._name__first\n", + " if self._name__middle[0]:\n", + " print \"Middle Name: \", self._name__middle\n", + " if self._name__last[0]:\n", + " print \"Last Name: \", self._name__last\n", + "class name:\n", + " __first=[None]*15\n", + " __middle=[None]*15\n", + " __last=[None]*15\n", + " def __init__(self, FirstName=None, MiddleName=None, LastName=None):\n", + " if isinstance(LastName, str):\n", + " self.__last=LastName\n", + " self.__middle=MiddleName\n", + " self.__first=FirstName\n", + " elif isinstance(MiddleName, str):\n", + " self.__middle=MiddleName\n", + " self.__first=FirstName\n", + " elif isinstance(FirstName, str):\n", + " self.__first=FirstName\n", + " else:\n", + " self.__last='\\0' #initialized to NULL\n", + " self.__middle='\\0'\n", + " self.__first='\\0'\n", + " show=show\n", + "n1=name()\n", + "n2=name()\n", + "n3=name()\n", + "n1=name(\"Rajkumar\")\n", + "n2=name(\"Savithri\", \"S\")\n", + "n3=name(\"Veugopal\", \"K\", \"R\")\n", + "n1.show(\"First prson details...\")\n", + "n2.show(\"Second prson details...\")\n", + "n3.show(\"Third prson details...\")" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "First prson details...\n", + "First Name: Rajkumar\n", + "Second prson details...\n", + "First Name: Savithri\n", + "Middle Name: S\n", + "Third prson details...\n", + "First Name: Veugopal\n", + "Middle Name: K\n", + "Last Name: R\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example-vector1.cpp, Page-413" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def read(self):\n", + " for i in range(self._vector__sz):\n", + " print \"Enter vector [\", i, \"]? \",\n", + " self._vector__v[i]=int(raw_input())\n", + "def show_sum(self):\n", + " Sum=0\n", + " for i in range(self._vector__sz):\n", + " Sum+=self._vector__v[i]\n", + " print \"Vector sum= \", Sum\n", + "class vector:\n", + " __v=[int] #array of type integer\n", + " __sz=int\n", + " def __init__(self, size):\n", + " self.__sz= size\n", + " self.__v=[int]*size #dynamically allocating size to integer array\n", + " def __del__(self):\n", + " del self.__v\n", + " read=read\n", + " show_sum=show_sum\n", + "count = int\n", + "count=int(raw_input(\"How many elements are there in the vector: \"))\n", + "v1= vector(count)\n", + "v1.read()\n", + "v1.show_sum()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "How many elements are there in the vector: 5\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter vector [ 0 ]? " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter vector [ 1 ]? " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter vector [ 2 ]? " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter vector [ 3 ]? " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter vector [ 4 ]? " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Vector sum= 15\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example-vector2.cpp, Page-415" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def show(self):\n", + " for i in range(self._vector__size):\n", + " print self.elem(i), \", \",\n", + "class vector:\n", + " __v=[int]\n", + " __size=int\n", + " def __init__(self, vector_size):\n", + " if isinstance(vector_size, int):\n", + " self.__size= vector_size\n", + " self.__v=[int]*vector_size\n", + " else:\n", + " print \"Copy construcor invoked\"\n", + " self.__size=vector_size.__size\n", + " self.__v=[int]*vector_size.__size\n", + " for i in range(vector_size.__size):\n", + " self.__v[i]=vector_size.__v[i]\n", + " def elem(self,i):\n", + " if i>=self.__size:\n", + " print \"Error: Out of Range\"\n", + " return -1\n", + " return self.__v[i]\n", + " def __del__(self):\n", + " del self.__v\n", + " show=show\n", + "v1=vector(5)\n", + "v2=vector(5)\n", + "for i in range(5):\n", + " if v2.elem(i)!=-1:\n", + " v2._vector__v[i]=i+1\n", + "v1=v2\n", + "v3=vector(v2)\n", + "print \"Vector v1: \",\n", + "v1.show()\n", + "print \"\\nvector v2: \",\n", + "v2.show()\n", + "print \"\\nvector v3: \",\n", + "v3.show()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Copy construcor invoked\n", + "Vector v1: 1 , 2 , 3 , 4 , 5 , \n", + "vector v2: 1 , 2 , 3 , 4 , 5 , \n", + "vector v3: 1 , 2 , 3 , 4 , 5 , \n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example-matrix.cpp, Page-418" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "TRUE=1\n", + "FALSE=0\n", + "def __del__(self):\n", + " for i in range(self._matrix__MaxRow):\n", + " del self._matrix__p[i]\n", + " del self._matrix__p\n", + "def add(self, a, b):\n", + " self._matrix__MaxRow=a._matrix__MaxRow\n", + " self._matrix__MaxCol=a._matrix__MaxCol\n", + " if (a._matrix__MaxRow!=b._matrix__MaxRow)|(a._matrix__MaxCol!=b._matrix__MaxCol):\n", + " print \"Error: invalid matrix order for addition\"\n", + " return\n", + " for i in range(self._matrix__MaxRow):\n", + " for j in range(self._matrix__MaxCol):\n", + " self._matrix__p[i][j]=a._matrix__p[i][j]+b._matrix__p[i][j]\n", + "def sub(self, a, b):\n", + " self._matrix__MaxRow=a._matrix__MaxRow\n", + " self._matrix__MaxCol=a._matrix__MaxCol\n", + " if (a._matrix__MaxRow!=b._matrix__MaxRow)|(a._matrix__MaxCol!=b._matrix__MaxCol):\n", + " print \"Error: invalid matrix order for subtraction\"\n", + " return\n", + " for i in range(self._matrix__MaxRow):\n", + " for j in range(self._matrix__MaxCol):\n", + " self._matrix__p[i][j]=a._matrix__p[i][j]-b._matrix__p[i][j]\n", + "def mul(self, a, b):\n", + " self._matrix__MaxRow=a._matrix__MaxRow\n", + " self._matrix__MaxCol=a._matrix__MaxCol\n", + " if (a._matrix__MaxCol!=b._matrix__MaxRow):\n", + " print \"Error: invalid matrix order for multiplication\"\n", + " return\n", + " for i in range(a._matrix__MaxRow):\n", + " for j in range(b._matrix__MaxCol):\n", + " self._matrix__p[i][j]=0\n", + " for k in range(a._matrix__MaxCol):\n", + " self._matrix__p[i][j]+=a._matrix__p[i][j]*b._matrix__p[i][j]\n", + "def eql(self, b):\n", + " for i in range(self._matrix__MaxRow):\n", + " for j in range(self._matrix__MaxCol):\n", + " if self._matrix__p[i][i]!=b._matrix__p[i][j]:\n", + " return 0\n", + " return 1\n", + "def read(self):\n", + " for i in range(self._matrix__MaxRow):\n", + " for j in range(self._matrix__MaxCol):\n", + " print \"Matrix[\", i, \",\",j,\"] =? \",\n", + " self._matrix__p[i][j]=int(raw_input())\n", + "def show(self):\n", + " for i in range(self._matrix__MaxRow):\n", + " for j in range(self._matrix__MaxCol):\n", + " print self._matrix__p[i][j], \" \",\n", + " print \"\"\n", + "class matrix:\n", + " __MaxRow=int\n", + " __MaxCol=int\n", + " __p=[int]\n", + " def __init__(self, row=0, col=0):\n", + " self.__MaxRow=row\n", + " self.__MaxCol=col\n", + " if row>0:\n", + " self.__p=[[int]*self.__MaxCol]*self.__MaxRow\n", + " __del__=__del__\n", + " read=read\n", + " show=show\n", + " add=add\n", + " sub=sub\n", + " mul=mul\n", + " eql=eql\n", + "print \"Enter Matrix A details...\"\n", + "m=int(raw_input(\"How many rows? \"))\n", + "n=int(raw_input(\"How many columns? \"))\n", + "a=matrix(m,n)\n", + "a.read()\n", + "print \"Enter Matrix B details...\"\n", + "p=int(raw_input(\"How many rows? \"))\n", + "q=int(raw_input(\"How many columns? \"))\n", + "b=matrix(p,q)\n", + "b.read()\n", + "print \"Matrix A is...\"\n", + "a.show()\n", + "print \"Matrix B is...\"\n", + "b.show()\n", + "c=matrix(m,n)\n", + "c.add(a,b)\n", + "print \"C=A+B...\"\n", + "c.show()\n", + "d=matrix(m,n)\n", + "d.sub(a,b)\n", + "print \"D=A-B...\"\n", + "d.show()\n", + "e=matrix(m,q)\n", + "e.mul(a,b)\n", + "print \"E=A*B...\"\n", + "e.show()\n", + "print \"(Is matrix A equal to matrix B)? \",\n", + "if(a.eql(b)):\n", + " print \"Yes\"\n", + "else:\n", + " print \"No\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter Matrix A details...\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "How many rows? 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "How many columns? 3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Matrix[ 0 , 0 ] =? " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Matrix[ 0 , 1 ] =? " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Matrix[ 0 , 2 ] =? " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Matrix[ 1 , 0 ] =? " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Matrix[ 1 , 1 ] =? " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Matrix[ 1 , 2 ] =? " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Matrix[ 2 , 0 ] =? " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Matrix[ 2 , 1 ] =? " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Matrix[ 2 , 2 ] =? " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter Matrix B details...\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "How many rows? 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "How many columns? 3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Matrix[ 0 , 0 ] =? " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Matrix[ 0 , 1 ] =? " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Matrix[ 0 , 2 ] =? " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Matrix[ 1 , 0 ] =? " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Matrix[ 1 , 1 ] =? " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Matrix[ 1 , 2 ] =? " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Matrix[ 2 , 0 ] =? " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Matrix[ 2 , 1 ] =? " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Matrix[ 2 , 2 ] =? " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Matrix A is...\n", + "2 2 2 \n", + "2 2 2 \n", + "2 2 2 \n", + "Matrix B is...\n", + "1 1 1 \n", + "1 1 1 \n", + "1 1 1 \n", + "C=A+B...\n", + "3 3 3 \n", + "3 3 3 \n", + "3 3 3 \n", + "D=A-B...\n", + "1 1 1 \n", + "1 1 1 \n", + "1 1 1 \n", + "E=A*B...\n", + "6 6 6 \n", + "6 6 6 \n", + "6 6 6 \n", + "(Is matrix A equal to matrix B)? No\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example-person.cpp, Page-423" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def __init__(self, NameIn, AddressIn, PhoneIn):\n", + " self._Person__name=NameIn\n", + " self._Person__address=AddressIn\n", + " self._Person__phone=PhoneIn\n", + "#inline\n", + "def __del__(self):\n", + " del self._Person__name\n", + " del self._Person__address\n", + " del self._Person__phone\n", + "def getname(self):\n", + " return self._Person__name\n", + "def getaddress(self):\n", + " return self._Person__address\n", + "def getphone(self):\n", + " return self._Person__phone\n", + "def changename(self, NameIn):\n", + " if(self._Person__name):\n", + " del self._Person__name\n", + " self._Person__name=NameIn\n", + "class Person:\n", + " __name=[str]\n", + " __address=[str]\n", + " __phone=[str]\n", + " __init__=__init__\n", + " __del__=__del__\n", + " getname=getname\n", + " getaddress=getaddress\n", + " getphone=getphone\n", + " changename=changename\n", + "def printperson(p):\n", + " if(p.getname()):\n", + " print \"Name: \", p.getname()\n", + " if(p.getaddress()):\n", + " print \"Address: \", p.getaddress()\n", + " if(p.getphone()):\n", + " print \"Phone: \", p.getphone()\n", + "me=Person(\"Rajkumar\", \"E-mail: raj@cdabc.erne.in\", \"91-080-5584271\")\n", + "printperson(me)\n", + "you=Person(\"XYZ\", \"-not sure-\", \"-not sure-\")\n", + "print \"You XYZ by default...\"\n", + "printperson(you)\n", + "you.changename(\"ABC\")\n", + "print \"You changed XYZ to ABC...\"\n", + "printperson(you)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Name: Rajkumar\n", + "Address: E-mail: raj@cdabc.erne.in\n", + "Phone: 91-080-5584271\n", + "You XYZ by default...\n", + "Name: XYZ\n", + "Address: -not sure-\n", + "Phone: -not sure-\n", + "You changed XYZ to ABC...\n", + "Name: ABC\n", + "Address: -not sure-\n", + "Phone: -not sure-\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example-graph.cpp, Page-425" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def __init__(self):\n", + " if(self._Graphics__nobjects[0]==False):\n", + " self._Graphics__setgraphicsmode()\n", + " self._Graphics__nobjects[0]+=1\n", + "def __del__(self):\n", + " self._Graphics__nobjects[0]-=1\n", + " if(self._Graphics__nobjects[0]==False):\n", + " self._Graphics__settextmode()\n", + "class Graphics:\n", + " __nobjects=[0]\n", + " def __setgraphicsmode(self):\n", + " pass\n", + " def __settextmode(self):\n", + " pass\n", + " __init__=__init__\n", + " __del__=__del__\n", + " def getcount(self):\n", + " return self.__nobjects[0]\n", + "def my_func():\n", + " obj=Graphics()\n", + " print \"No. of Graphics' objects while in my_func=\", obj.getcount()\n", + "obj1=Graphics()\n", + "print \"No. of Graphics' objects before in my_func=\", obj1.getcount()\n", + "my_func()\n", + "print \"No. of Graphics' objects after in my_func=\", obj1.getcount()\n", + "obj2=Graphics()\n", + "obj3=Graphics()\n", + "obj4=Graphics()\n", + "print \"Value of static member nobjects after all 3 more objects...\"\n", + "print \"In obj1= \", obj1.getcount()\n", + "print \"In obj2= \", obj2.getcount()\n", + "print \"In obj3= \", obj3.getcount()\n", + "print \"In obj4= \", obj4.getcount()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "No. of Graphics' objects before in my_func= 1\n", + "No. of Graphics' objects while in my_func= 2\n", + "No. of Graphics' objects after in my_func= 1\n", + "Value of static member nobjects after all 3 more objects...\n", + "In obj1= 4\n", + "In obj2= 4\n", + "In obj3= 4\n", + "In obj4= 4\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example Page-428" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def distance(self, a, b):\n", + " self.x=a.x-b.x\n", + " self.y=a.y-b.y\n", + "def display(self):\n", + " print \"x= \",self.x\n", + " print \"y= \", self.y\n", + "class point:\n", + " __x=int\n", + " __y=int\n", + " def __init__(self, a=None, b=None):\n", + " if isinstance(a, int):\n", + " self.x=a\n", + " self.y=b\n", + " else:\n", + " self.x=self.y=0\n", + " def __del__(self):\n", + " pass\n", + " distance=distance\n", + " display=display\n", + "p1=point(40,18)\n", + "p2=point(12,9)\n", + "p3=point()\n", + "p3.distance(p1,p2)\n", + "print \"Coordinates of P1: \"\n", + "p1.display()\n", + "print \"Coordinates of P2: \"\n", + "p2.display()\n", + "print \"distance between P1 and P2: \"\n", + "p3.display()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Coordinates of P1: \n", + "x= 40\n", + "y= 18\n", + "Coordinates of P2: \n", + "x= 12\n", + "y= 9\n", + "distance between P1 and P2: \n", + "x= 28\n", + "y= 9\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example Page-430" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def display(self):\n", + " print \"a =\", self.a,\n", + " print \"b =\", self.b\n", + "class data:\n", + " __a=int\n", + " __b=float\n", + " def __init__(self, x=None, y=None):\n", + " if isinstance(x, int):\n", + " self.a=x\n", + " self.b=y\n", + " elif isinstance(x, data):\n", + " self.a=x.a\n", + " self.b=x.b\n", + " else:\n", + " self.a=0\n", + " self.b=0\n", + " display=display\n", + "d1=data()\n", + "d2=data(12,9.9)\n", + "d3=data(d2)\n", + "print \"For default constructor: \"\n", + "d1.display()\n", + "print\"For parameterized constructor: \"\n", + "d2.display()\n", + "print \"For Copy Constructor: \"\n", + "d3.display()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "For default constructor: \n", + "a = 0 b = 0\n", + "For parameterized constructor: \n", + "a = 12 b = 9.9\n", + "For Copy Constructor: \n", + "a = 12 b = 9.9\n" + ] + } + ], + "prompt_number": 1 + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/sample_notebooks/Tarun KumarDas/Chapter9_1.ipynb b/sample_notebooks/Tarun KumarDas/Chapter9_1.ipynb new file mode 100755 index 00000000..9ad66918 --- /dev/null +++ b/sample_notebooks/Tarun KumarDas/Chapter9_1.ipynb @@ -0,0 +1,227 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:e51e8cf0773e9d18bdd072a1fa1bf98f99abe047f89cd3c17683371d37f7815d" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "VALVE DIAGRAMS AND VALVE GEARS" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 9.5 Page 150" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#initialisation of variable\n", + "from math import pi,sqrt,acos,asin,atan,cos,sin,tan\n", + "p=20#in\n", + "l=100#in\n", + "r=120#r.p.m\n", + "v=3.5#in\n", + "l2=1#in\n", + "l3=1/8#in\n", + "v1=1.44#omega in/sec\n", + "\n", + "#CALCULATIONS\n", + "V=p*(1.06/1.166)#omega in./sec\n", + "R=(V/v1)#omega in/sec\n", + "\n", + "#RESULTS\n", + "print\"The ratio of velocity of the piston to the velocity is\",round(R,3),\"Omega in/sec\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The ratio of velocity of the piston to the velocity is 12.626 Omega in/sec\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 9.7 Page 151" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#initialisation of variable\n", + "from math import pi,sqrt,acos,asin,atan,cos,sin,tan\n", + "v=0.6#in\n", + "m=1.0#in\n", + "t=0.75#in\n", + "p=4#in\n", + "\n", + "#CALCULATIONS\n", + "D=t/m#in\n", + "A=(p*m/D)#in\n", + "\n", + "#RESULTS\n", + "print\"the travel and laps of the value is\",round(A,3),\"in\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "the travel and laps of the value is 5.333 in\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 9.10 Page 154" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#initialisation of variable\n", + "from math import pi,sqrt,acos,asin,atan,cos,sin,tan\n", + "l=1.5#in\n", + "p=4.0#in\n", + "v=0.98#in\n", + "\n", + "#CALCULATIONS\n", + "T=(l*p/v)#in\n", + "\n", + "#RESULTS\n", + "print\"the particulars of a value and it eccentric is\",round(T,3),\"in\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "the particulars of a value and it eccentric is 6.122 in\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 9.12 Page 156" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#initialisation of variable\n", + "from math import pi,sqrt,acos,asin,atan,cos,sin,tan\n", + "p=1/10#in\n", + "v1=3/4#in\n", + "v2=3/5#in\n", + "m=1*1/2#in\n", + "l=4#cranks\n", + "a1=1.25#in\n", + "a2=0.7#in\n", + "\n", + "#CALCULATIONS\n", + "C=a1/a2#in\n", + "A=l*a1/a2#in\n", + "S=(A/2-a1)#in\n", + "\n", + "#RESULTS\n", + "print\"the travel of the value is\",round(S,3),\"in\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "the travel of the value is 2.321 in\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 9.17 Page 161" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#initialisation of variable\n", + "from math import pi,sqrt,acos,asin,atan,cos,sin,tan\n", + "v=3*1/2#in\n", + "a=30#degree\n", + "l=0.8#in\n", + "v1=0.2#in\n", + "L=0.13#in\n", + "m=1.075#in\n", + "d=0.58#in\n", + "p=1.875#in\n", + "\n", + "#CALCULATIONS\n", + "V=(p-d)#in\n", + "P=V+1.25#in\n", + "\n", + "#RESULTS\n", + "print\"the main value and the maximum opening to steam is\",round(P,3),\"in\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "the main value and the maximum opening to steam is 2.545 in\n" + ] + } + ], + "prompt_number": 5 + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/sample_notebooks/Tarun KumarDas/Chapter9_2.ipynb b/sample_notebooks/Tarun KumarDas/Chapter9_2.ipynb new file mode 100755 index 00000000..320477a8 --- /dev/null +++ b/sample_notebooks/Tarun KumarDas/Chapter9_2.ipynb @@ -0,0 +1,227 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:023bb2358c5a7a6740b1b9a08290e11f954e790555a353817bbe84615e4679e6" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "CHAPTER 9: VALVE DIAGRAMS AND VALVE GEARS" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 9.5 Page 150" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#initialisation of variable\n", + "from math import pi,sqrt,acos,asin,atan,cos,sin,tan\n", + "p=20#in\n", + "l=100#in\n", + "r=120#r.p.m\n", + "v=3.5#in\n", + "l2=1#in\n", + "l3=1/8#in\n", + "v1=1.44#omega in/sec\n", + "\n", + "#CALCULATIONS\n", + "V=p*(1.06/1.166)#omega in./sec\n", + "R=(V/v1)#omega in/sec\n", + "\n", + "#RESULTS\n", + "print\"The ratio of velocity of the piston to the velocity is\",round(R,3),\"Omega in/sec\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The ratio of velocity of the piston to the velocity is 12.626 Omega in/sec\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 9.7 Page 151" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#initialisation of variable\n", + "from math import pi,sqrt,acos,asin,atan,cos,sin,tan\n", + "v=0.6#in\n", + "m=1.0#in\n", + "t=0.75#in\n", + "p=4#in\n", + "\n", + "#CALCULATIONS\n", + "D=t/m#in\n", + "A=(p*m/D)#in\n", + "\n", + "#RESULTS\n", + "print\"the travel and laps of the value is\",round(A,3),\"in\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "the travel and laps of the value is 5.333 in\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 9.10 Page 154" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#initialisation of variable\n", + "from math import pi,sqrt,acos,asin,atan,cos,sin,tan\n", + "l=1.5#in\n", + "p=4.0#in\n", + "v=0.98#in\n", + "\n", + "#CALCULATIONS\n", + "T=(l*p/v)#in\n", + "\n", + "#RESULTS\n", + "print\"the particulars of a value and it eccentric is\",round(T,3),\"in\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "the particulars of a value and it eccentric is 6.122 in\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 9.12 Page 156" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#initialisation of variable\n", + "from math import pi,sqrt,acos,asin,atan,cos,sin,tan\n", + "p=1/10#in\n", + "v1=3/4#in\n", + "v2=3/5#in\n", + "m=1*1/2#in\n", + "l=4#cranks\n", + "a1=1.25#in\n", + "a2=0.7#in\n", + "\n", + "#CALCULATIONS\n", + "C=a1/a2#in\n", + "A=l*a1/a2#in\n", + "S=(A/2-a1)#in\n", + "\n", + "#RESULTS\n", + "print\"the travel of the value is\",round(S,3),\"in\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "the travel of the value is 2.321 in\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 9.17 Page 161" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#initialisation of variable\n", + "from math import pi,sqrt,acos,asin,atan,cos,sin,tan\n", + "v=3*1/2#in\n", + "a=30#degree\n", + "l=0.8#in\n", + "v1=0.2#in\n", + "L=0.13#in\n", + "m=1.075#in\n", + "d=0.58#in\n", + "p=1.875#in\n", + "\n", + "#CALCULATIONS\n", + "V=(p-d)#in\n", + "P=V+1.25#in\n", + "\n", + "#RESULTS\n", + "print\"the main value and the maximum opening to steam is\",round(P,3),\"in\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "the main value and the maximum opening to steam is 2.545 in\n" + ] + } + ], + "prompt_number": 5 + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/sample_notebooks/Tarun KumarDas/Chapter9_3.ipynb b/sample_notebooks/Tarun KumarDas/Chapter9_3.ipynb new file mode 100755 index 00000000..320477a8 --- /dev/null +++ b/sample_notebooks/Tarun KumarDas/Chapter9_3.ipynb @@ -0,0 +1,227 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:023bb2358c5a7a6740b1b9a08290e11f954e790555a353817bbe84615e4679e6" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "CHAPTER 9: VALVE DIAGRAMS AND VALVE GEARS" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 9.5 Page 150" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#initialisation of variable\n", + "from math import pi,sqrt,acos,asin,atan,cos,sin,tan\n", + "p=20#in\n", + "l=100#in\n", + "r=120#r.p.m\n", + "v=3.5#in\n", + "l2=1#in\n", + "l3=1/8#in\n", + "v1=1.44#omega in/sec\n", + "\n", + "#CALCULATIONS\n", + "V=p*(1.06/1.166)#omega in./sec\n", + "R=(V/v1)#omega in/sec\n", + "\n", + "#RESULTS\n", + "print\"The ratio of velocity of the piston to the velocity is\",round(R,3),\"Omega in/sec\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The ratio of velocity of the piston to the velocity is 12.626 Omega in/sec\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 9.7 Page 151" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#initialisation of variable\n", + "from math import pi,sqrt,acos,asin,atan,cos,sin,tan\n", + "v=0.6#in\n", + "m=1.0#in\n", + "t=0.75#in\n", + "p=4#in\n", + "\n", + "#CALCULATIONS\n", + "D=t/m#in\n", + "A=(p*m/D)#in\n", + "\n", + "#RESULTS\n", + "print\"the travel and laps of the value is\",round(A,3),\"in\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "the travel and laps of the value is 5.333 in\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 9.10 Page 154" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#initialisation of variable\n", + "from math import pi,sqrt,acos,asin,atan,cos,sin,tan\n", + "l=1.5#in\n", + "p=4.0#in\n", + "v=0.98#in\n", + "\n", + "#CALCULATIONS\n", + "T=(l*p/v)#in\n", + "\n", + "#RESULTS\n", + "print\"the particulars of a value and it eccentric is\",round(T,3),\"in\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "the particulars of a value and it eccentric is 6.122 in\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 9.12 Page 156" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#initialisation of variable\n", + "from math import pi,sqrt,acos,asin,atan,cos,sin,tan\n", + "p=1/10#in\n", + "v1=3/4#in\n", + "v2=3/5#in\n", + "m=1*1/2#in\n", + "l=4#cranks\n", + "a1=1.25#in\n", + "a2=0.7#in\n", + "\n", + "#CALCULATIONS\n", + "C=a1/a2#in\n", + "A=l*a1/a2#in\n", + "S=(A/2-a1)#in\n", + "\n", + "#RESULTS\n", + "print\"the travel of the value is\",round(S,3),\"in\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "the travel of the value is 2.321 in\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 9.17 Page 161" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#initialisation of variable\n", + "from math import pi,sqrt,acos,asin,atan,cos,sin,tan\n", + "v=3*1/2#in\n", + "a=30#degree\n", + "l=0.8#in\n", + "v1=0.2#in\n", + "L=0.13#in\n", + "m=1.075#in\n", + "d=0.58#in\n", + "p=1.875#in\n", + "\n", + "#CALCULATIONS\n", + "V=(p-d)#in\n", + "P=V+1.25#in\n", + "\n", + "#RESULTS\n", + "print\"the main value and the maximum opening to steam is\",round(P,3),\"in\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "the main value and the maximum opening to steam is 2.545 in\n" + ] + } + ], + "prompt_number": 5 + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/sample_notebooks/Tarun KumarDas/Chapter9_4.ipynb b/sample_notebooks/Tarun KumarDas/Chapter9_4.ipynb new file mode 100755 index 00000000..320477a8 --- /dev/null +++ b/sample_notebooks/Tarun KumarDas/Chapter9_4.ipynb @@ -0,0 +1,227 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:023bb2358c5a7a6740b1b9a08290e11f954e790555a353817bbe84615e4679e6" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "CHAPTER 9: VALVE DIAGRAMS AND VALVE GEARS" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 9.5 Page 150" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#initialisation of variable\n", + "from math import pi,sqrt,acos,asin,atan,cos,sin,tan\n", + "p=20#in\n", + "l=100#in\n", + "r=120#r.p.m\n", + "v=3.5#in\n", + "l2=1#in\n", + "l3=1/8#in\n", + "v1=1.44#omega in/sec\n", + "\n", + "#CALCULATIONS\n", + "V=p*(1.06/1.166)#omega in./sec\n", + "R=(V/v1)#omega in/sec\n", + "\n", + "#RESULTS\n", + "print\"The ratio of velocity of the piston to the velocity is\",round(R,3),\"Omega in/sec\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The ratio of velocity of the piston to the velocity is 12.626 Omega in/sec\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 9.7 Page 151" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#initialisation of variable\n", + "from math import pi,sqrt,acos,asin,atan,cos,sin,tan\n", + "v=0.6#in\n", + "m=1.0#in\n", + "t=0.75#in\n", + "p=4#in\n", + "\n", + "#CALCULATIONS\n", + "D=t/m#in\n", + "A=(p*m/D)#in\n", + "\n", + "#RESULTS\n", + "print\"the travel and laps of the value is\",round(A,3),\"in\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "the travel and laps of the value is 5.333 in\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 9.10 Page 154" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#initialisation of variable\n", + "from math import pi,sqrt,acos,asin,atan,cos,sin,tan\n", + "l=1.5#in\n", + "p=4.0#in\n", + "v=0.98#in\n", + "\n", + "#CALCULATIONS\n", + "T=(l*p/v)#in\n", + "\n", + "#RESULTS\n", + "print\"the particulars of a value and it eccentric is\",round(T,3),\"in\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "the particulars of a value and it eccentric is 6.122 in\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 9.12 Page 156" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#initialisation of variable\n", + "from math import pi,sqrt,acos,asin,atan,cos,sin,tan\n", + "p=1/10#in\n", + "v1=3/4#in\n", + "v2=3/5#in\n", + "m=1*1/2#in\n", + "l=4#cranks\n", + "a1=1.25#in\n", + "a2=0.7#in\n", + "\n", + "#CALCULATIONS\n", + "C=a1/a2#in\n", + "A=l*a1/a2#in\n", + "S=(A/2-a1)#in\n", + "\n", + "#RESULTS\n", + "print\"the travel of the value is\",round(S,3),\"in\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "the travel of the value is 2.321 in\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 9.17 Page 161" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#initialisation of variable\n", + "from math import pi,sqrt,acos,asin,atan,cos,sin,tan\n", + "v=3*1/2#in\n", + "a=30#degree\n", + "l=0.8#in\n", + "v1=0.2#in\n", + "L=0.13#in\n", + "m=1.075#in\n", + "d=0.58#in\n", + "p=1.875#in\n", + "\n", + "#CALCULATIONS\n", + "V=(p-d)#in\n", + "P=V+1.25#in\n", + "\n", + "#RESULTS\n", + "print\"the main value and the maximum opening to steam is\",round(P,3),\"in\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "the main value and the maximum opening to steam is 2.545 in\n" + ] + } + ], + "prompt_number": 5 + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/sample_notebooks/Tarun KumarDas/Chapter9_5.ipynb b/sample_notebooks/Tarun KumarDas/Chapter9_5.ipynb new file mode 100755 index 00000000..320477a8 --- /dev/null +++ b/sample_notebooks/Tarun KumarDas/Chapter9_5.ipynb @@ -0,0 +1,227 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:023bb2358c5a7a6740b1b9a08290e11f954e790555a353817bbe84615e4679e6" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "CHAPTER 9: VALVE DIAGRAMS AND VALVE GEARS" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 9.5 Page 150" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#initialisation of variable\n", + "from math import pi,sqrt,acos,asin,atan,cos,sin,tan\n", + "p=20#in\n", + "l=100#in\n", + "r=120#r.p.m\n", + "v=3.5#in\n", + "l2=1#in\n", + "l3=1/8#in\n", + "v1=1.44#omega in/sec\n", + "\n", + "#CALCULATIONS\n", + "V=p*(1.06/1.166)#omega in./sec\n", + "R=(V/v1)#omega in/sec\n", + "\n", + "#RESULTS\n", + "print\"The ratio of velocity of the piston to the velocity is\",round(R,3),\"Omega in/sec\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The ratio of velocity of the piston to the velocity is 12.626 Omega in/sec\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 9.7 Page 151" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#initialisation of variable\n", + "from math import pi,sqrt,acos,asin,atan,cos,sin,tan\n", + "v=0.6#in\n", + "m=1.0#in\n", + "t=0.75#in\n", + "p=4#in\n", + "\n", + "#CALCULATIONS\n", + "D=t/m#in\n", + "A=(p*m/D)#in\n", + "\n", + "#RESULTS\n", + "print\"the travel and laps of the value is\",round(A,3),\"in\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "the travel and laps of the value is 5.333 in\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 9.10 Page 154" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#initialisation of variable\n", + "from math import pi,sqrt,acos,asin,atan,cos,sin,tan\n", + "l=1.5#in\n", + "p=4.0#in\n", + "v=0.98#in\n", + "\n", + "#CALCULATIONS\n", + "T=(l*p/v)#in\n", + "\n", + "#RESULTS\n", + "print\"the particulars of a value and it eccentric is\",round(T,3),\"in\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "the particulars of a value and it eccentric is 6.122 in\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 9.12 Page 156" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#initialisation of variable\n", + "from math import pi,sqrt,acos,asin,atan,cos,sin,tan\n", + "p=1/10#in\n", + "v1=3/4#in\n", + "v2=3/5#in\n", + "m=1*1/2#in\n", + "l=4#cranks\n", + "a1=1.25#in\n", + "a2=0.7#in\n", + "\n", + "#CALCULATIONS\n", + "C=a1/a2#in\n", + "A=l*a1/a2#in\n", + "S=(A/2-a1)#in\n", + "\n", + "#RESULTS\n", + "print\"the travel of the value is\",round(S,3),\"in\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "the travel of the value is 2.321 in\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 9.17 Page 161" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#initialisation of variable\n", + "from math import pi,sqrt,acos,asin,atan,cos,sin,tan\n", + "v=3*1/2#in\n", + "a=30#degree\n", + "l=0.8#in\n", + "v1=0.2#in\n", + "L=0.13#in\n", + "m=1.075#in\n", + "d=0.58#in\n", + "p=1.875#in\n", + "\n", + "#CALCULATIONS\n", + "V=(p-d)#in\n", + "P=V+1.25#in\n", + "\n", + "#RESULTS\n", + "print\"the main value and the maximum opening to steam is\",round(P,3),\"in\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "the main value and the maximum opening to steam is 2.545 in\n" + ] + } + ], + "prompt_number": 5 + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/sample_notebooks/VidyaSri/Chapter_1.ipynb b/sample_notebooks/VidyaSri/Chapter_1.ipynb new file mode 100755 index 00000000..7a5ae89d --- /dev/null +++ b/sample_notebooks/VidyaSri/Chapter_1.ipynb @@ -0,0 +1,389 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:e7689de630b2babf67b8065d816d51134502d1f0c04e82cddeaf0e3e2d421795" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 1 - PULSE FUNDAMENTALS " + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E1 - Pg 12" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Caption:Find (a)Pulse amplitude (b)PRF (c)PW (d)Duty cycle and (e)M/S ratio\n", + "v=1.#Vertical scale(Volt per division)\n", + "h=0.1#Horizontal scale(Milli sec per division)\n", + "pv=3.5#Amplitude of pulse in divisions\n", + "t=6.#Time in divisions\n", + "pw=2.5#Width of pulse\n", + "P=pv*v\n", + "print '%s %.1f' %('(a)Pulse Amplitude (in volts)=',P)\n", + "T=t*h\n", + "prf=(1/T)*1000\n", + "print '%s %.4f' %('(b)PRF(in pps)=',prf)\n", + "p=pw*h\n", + "print '%s %.2f' %('(c)PW (in ms)=',p)\n", + "sw=pv*h\n", + "d=(p/T)*100\n", + "print '%s %.6f' %('(d)Duty cycle(in %)=',d)\n", + "m=p/sw\n", + "print '%s %.7f' %('(e)M/S ratio=',m)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "(a)Pulse Amplitude (in volts)= 3.5\n", + "(b)PRF(in pps)= 1666.6667\n", + "(c)PW (in ms)= 0.25\n", + "(d)Duty cycle(in %)= 41.666667\n", + "(e)M/S ratio= 0.7142857\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E2 - Pg 13" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Caption:Determine (a)Pulse amplitude,tilt,rise time,fall time,PW,PRF,mark to space ratio,and duty cycle (b)tilt\n", + "vs=100.#Vertical scale(in mv/divisions)\n", + "hs=100.#Horizontal scale(in micro sec/division)\n", + "e1=380.#first peak of waveform(in mv)\n", + "e2=350.#second peak of waveform(in mv)\n", + "E=(e1+e2)/2.\n", + "t=(e1-e2)*100./E\n", + "tr=0.3*hs\n", + "tf=0.4*hs\n", + "T=5.*hs\n", + "prf=10.**6./T\n", + "pw=2.2*hs\n", + "sw=2.8*hs\n", + "ms=pw/sw\n", + "dc=(pw*100.)/T\n", + "print '%s %.f'%('(a)Pulse Amplitude(in mv)=',E)\n", + "print '%s %.7f'%('(a)tilt(in %)',t)\n", + "print '%s %.f'%('(a)rise time(in micro sec)=',tr)\n", + "print '%s %.f'%('(a)fall time(in micro sec)=',tf)\n", + "print '%s %.f'%('(a)PW(in micro sec)=',pw)\n", + "print '%s %.f'%('(a)PRF(in pps)=',prf)\n", + "print '%s %.7f'%('(a)M/s ratio=',ms)\n", + "print '%s %.f' %('(a)Duty cycle(in %)=',dc)\n", + "eb=0.5*vs\n", + "ee=2.25*vs\n", + "tb=eb*100./ee\n", + "print '%s %.6f' %('(b)Tilt(in %)=',tb)\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "(a)Pulse Amplitude(in mv)= 365\n", + "(a)tilt(in %) 8.2191781\n", + "(a)rise time(in micro sec)= 30\n", + "(a)fall time(in micro sec)= 40\n", + "(a)PW(in micro sec)= 220\n", + "(a)PRF(in pps)= 2000\n", + "(a)M/s ratio= 0.7857143\n", + "(a)Duty cycle(in %)= 44\n", + "(b)Tilt(in %)= 22.222222\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E3 - Pg 13" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Caption:Determine average voltage level\n", + "vs=2.#Vertical scale(V/div)\n", + "hs=1.#Horizontal scale(ms/div)\n", + "v1=8.#Amplitude of signal in (+)ve direction (in volts)\n", + "v2=-1.#Amplitude of signal in (-)ve direction (in volts)\n", + "t1=0.8#Horizontal divisions for v1\n", + "t2=2.2#Horizontal divisions for v2\n", + "T=3.*hs\n", + "T1=t1*hs\n", + "T2=t2*hs\n", + "Va=((T1*v1)+(T2*v2))/T\n", + "print '%s %.1f' %('Average voltage (in volts)=',Va)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Average voltage (in volts)= 1.4\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E4 - Pg 14" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Caption:Determine the upper 3db frequency of the amplifier\n", + "tr=1.#Rise time(in micro sec)\n", + "fu=0.35*10**6/tr\n", + "print '%s %.f' %('The upper 3db frequency of the amplifier(in hertz)=',fu)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The upper 3db frequency of the amplifier(in hertz)= 350000\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E5 - Pg 14" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Caption:Determine (a)Minimum upper cut frequency (b)Minimum pulse width and duty cycle\n", + "prf=1.5#in Khz\n", + "dc=3.#Duty cycle(in %)\n", + "pa=1.5#Amplitude of pulse(in Khz)\n", + "fu=1.#High frequency limit(in Mhz)\n", + "tr=10.#Rise time(in %)\n", + "pw=(dc/100.)*10.**3./pa\n", + "Tr=(tr/100.)*pw\n", + "fh=0.35*10.**6./Tr\n", + "print '%s %.1f' %('(a)Minimum upper cut frequency(in hertz)=',fh)\n", + "Tr2=0.35*10.**(-6.)/fu\n", + "Pw=10.*Tr2\n", + "dc=Pw*100.*(pa*1000.)\n", + "print '%s %.7f' %('(b)Pulse width(in sec) and Duty cycle(in %)=',Pw)\n", + "print '%s %.3f' %('(b)Duty cycle(in %)=',dc)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "(a)Minimum upper cut frequency(in hertz)= 175000.0\n", + "(b)Pulse width(in sec) and Duty cycle(in %)= 0.0000035\n", + "(b)Duty cycle(in %)= 0.525\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E6 - Pg 17" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Caption:Calculate (a)Rise time in output waveform (b)Minimum upper cut off frequency and print '%s %.2f' %layed rise time\n", + "import math\n", + "tr=10.#Rise time of input waveform(in micro sec)\n", + "fu=350.#Upper cut off frequency(in KHz)\n", + "ti=100.#Input rise time(in ns)\n", + "trc=0.35*(10.**(-3.))/350.\n", + "tro=math.sqrt(((tr)*(10.**(-6.)))**2.+(trc**2.))*10.**6.\n", + "print '%s %.6f' %('(a)Rise Time(in Micro sec)=',tro)\n", + "tc=ti*(10.**(-9.))/3.\n", + "fh=0.35*10.**(-6.)/tc\n", + "Tro=math.sqrt((ti*(10.**(-9.)))**2.+(tc**2.))*10.**9.\n", + "print '%s %.1f' %('(b)Minimum upper cut off frequency(in Mhz) and rise time(in ns)=',fh)\n", + "print '%s %.5f' %('(b)rise time(in ns)=',Tro)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "(a)Rise Time(in Micro sec)= 10.049876\n", + "(b)Minimum upper cut off frequency(in Mhz) and rise time(in ns)= 10.5\n", + "(b)rise time(in ns)= 105.40926\n" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E7 - Pg 18" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Caption:Calculate lowest input frequency \n", + "import math\n", + "fl=10.#Lower cutoff frequency(in hertz)\n", + "t=0.02#Tilt on output waveform\n", + "f=math.pi*fl/(t*1000)\n", + "print '%s %.7f' %('Lowest input frequency(in Khz)=',f)\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Lowest input frequency(in Khz)= 1.5707963\n" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E8 - Pg 18" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Caption:Determine (a)upper cutoff frequency (b)lower cutoff frequency\n", + "import math\n", + "f=1.#frequency of square wave(in khz)\n", + "tr=200.#rise time of output(in ns)\n", + "t=0.03#fractional tilt\n", + "fh=0.35*10.**3./tr\n", + "print '%s %.2f' %('(a)upper cutoff frequency(in mhz)=',fh) \n", + "fl=f*t*1000./math.pi\n", + "print '%s %.7f' %('(b)Lower cutoff frequency(in hz)=',fl)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "(a)upper cutoff frequency(in mhz)= 1.75\n", + "(b)Lower cutoff frequency(in hz)= 9.5492966\n" + ] + } + ], + "prompt_number": 8 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example E9 - Pg 21" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Caption:Determine upper and lower Frequencies\n", + "import math\n", + "tr=30.#Rise time(in micro sec)\n", + "PRF=2000.#Pulse repetition Frequency(in pps)\n", + "t=0.082#Tilt(in %)\n", + "Pw=220.#Pulse width(in micro sec)\n", + "fh=0.35*10.**(6.)/tr\n", + "fl=t*10.**6./(2.*math.pi*Pw)\n", + "print '%s %.3f' %('Upper frequency(in hz)=',fh)\n", + "print '%s %.6f' %('lower frequency(in hz)=',fl)\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Upper frequency(in hz)= 11666.667\n", + "lower frequency(in hz)= 59.321388\n" + ] + } + ], + "prompt_number": 11 + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/sample_notebooks/VikasPrasad/Chapter_01.ipynb b/sample_notebooks/VikasPrasad/Chapter_01.ipynb new file mode 100755 index 00000000..b5226e07 --- /dev/null +++ b/sample_notebooks/VikasPrasad/Chapter_01.ipynb @@ -0,0 +1,170 @@ +{ + "metadata": { + "name": "" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "1: Overview of Data Structures and Algorithms" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1: Page 20" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Thermostat:\n", + "\t\"\"\"A simple thermostat class\"\"\"\n", + " \n", + "\t#since private instance variables don't exist in Python,\n", + " #hence using a convention: name prefixed with an underscore, to treat them as non-public part\n", + "\t_currentTemp = 0.0\n", + "\t_desiredTemp = 0.0\n", + "\n", + "\tdef furnace_on(self):\n", + "\t\t#method body goes here\n", + "\t\tpass\n", + "\n", + "\tdef furnace_off(self):\n", + "\t\t#method body goes here\n", + "\t\tpass\n", + " \n", + "#end class Thermostat" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2: Page 22" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#demonstrates basic OOP syntax\n", + "\n", + "class BankAccount:\n", + "\t\"\"\"A simple bank account class\"\"\"\n", + "\t\n", + "\tdef __init__(self, openingBalance):\t#special method to create objects\n", + " #with instances customized to a specific initial state\n", + "\t\t\n", + " #since private instance variables don't exist in Python,\n", + " #hence using a convention: name prefixed with an underscore, to treat them as non-public part\n", + "\t self._balance = openingBalance\t#account balance\n", + "\n", + "\tdef deposit(self, amount):\t#makes deposit\n", + "\t\tself._balance = self._balance + amount\n", + "\n", + "\tdef withdraw(self, amount):\t#makes withdrawl\n", + "\t\tself._balance = self._balance - amount\n", + "\n", + "\tdef display(self):\t#displays balance\n", + "\t\tprint 'Balance=', self._balance\n", + " \n", + "#end class BankAccount\n", + "\n", + "ba1 = BankAccount(100.00)\t#create account\n", + "\n", + "print 'Before transactions, ',\n", + "ba1.display()\t#display balance\n", + "\n", + "ba1.deposit(74.35)\t#make deposit\n", + "ba1.withdraw(20.00)\t#make withdrawl\n", + "\n", + "print 'After transactions, ',\n", + "ba1.display()\t#display balance\n", + "#end" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Before transactions, Balance= 100.0\n", + "After transactions, Balance= 154.35\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 3: Page 25" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#creating string objects\n", + "str1 = ''\n", + "str2 = 'George'\n", + "\n", + "#verifying results\n", + "print str1\n", + "print str2\n", + "\n", + "#naming string object\n", + "str3 = 'amanuensis'\n", + "\n", + "#verifying results\n", + "print str3\n", + "\n", + "#accessing specific characters from string using [] operator\n", + "ch1 = str2[3]\n", + "\n", + "#verifying results\n", + "print ch1\n", + "\n", + "#finding and printing number of characters in a string\n", + "print len(str1)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "George\n", + "amanuensis\n", + "r\n", + "0\n" + ] + } + ], + "prompt_number": 3 + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/sample_notebooks/VikasPrasad/Chapter_01_1.ipynb b/sample_notebooks/VikasPrasad/Chapter_01_1.ipynb new file mode 100755 index 00000000..b5226e07 --- /dev/null +++ b/sample_notebooks/VikasPrasad/Chapter_01_1.ipynb @@ -0,0 +1,170 @@ +{ + "metadata": { + "name": "" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "1: Overview of Data Structures and Algorithms" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1: Page 20" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Thermostat:\n", + "\t\"\"\"A simple thermostat class\"\"\"\n", + " \n", + "\t#since private instance variables don't exist in Python,\n", + " #hence using a convention: name prefixed with an underscore, to treat them as non-public part\n", + "\t_currentTemp = 0.0\n", + "\t_desiredTemp = 0.0\n", + "\n", + "\tdef furnace_on(self):\n", + "\t\t#method body goes here\n", + "\t\tpass\n", + "\n", + "\tdef furnace_off(self):\n", + "\t\t#method body goes here\n", + "\t\tpass\n", + " \n", + "#end class Thermostat" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2: Page 22" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#demonstrates basic OOP syntax\n", + "\n", + "class BankAccount:\n", + "\t\"\"\"A simple bank account class\"\"\"\n", + "\t\n", + "\tdef __init__(self, openingBalance):\t#special method to create objects\n", + " #with instances customized to a specific initial state\n", + "\t\t\n", + " #since private instance variables don't exist in Python,\n", + " #hence using a convention: name prefixed with an underscore, to treat them as non-public part\n", + "\t self._balance = openingBalance\t#account balance\n", + "\n", + "\tdef deposit(self, amount):\t#makes deposit\n", + "\t\tself._balance = self._balance + amount\n", + "\n", + "\tdef withdraw(self, amount):\t#makes withdrawl\n", + "\t\tself._balance = self._balance - amount\n", + "\n", + "\tdef display(self):\t#displays balance\n", + "\t\tprint 'Balance=', self._balance\n", + " \n", + "#end class BankAccount\n", + "\n", + "ba1 = BankAccount(100.00)\t#create account\n", + "\n", + "print 'Before transactions, ',\n", + "ba1.display()\t#display balance\n", + "\n", + "ba1.deposit(74.35)\t#make deposit\n", + "ba1.withdraw(20.00)\t#make withdrawl\n", + "\n", + "print 'After transactions, ',\n", + "ba1.display()\t#display balance\n", + "#end" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Before transactions, Balance= 100.0\n", + "After transactions, Balance= 154.35\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 3: Page 25" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#creating string objects\n", + "str1 = ''\n", + "str2 = 'George'\n", + "\n", + "#verifying results\n", + "print str1\n", + "print str2\n", + "\n", + "#naming string object\n", + "str3 = 'amanuensis'\n", + "\n", + "#verifying results\n", + "print str3\n", + "\n", + "#accessing specific characters from string using [] operator\n", + "ch1 = str2[3]\n", + "\n", + "#verifying results\n", + "print ch1\n", + "\n", + "#finding and printing number of characters in a string\n", + "print len(str1)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "George\n", + "amanuensis\n", + "r\n", + "0\n" + ] + } + ], + "prompt_number": 3 + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/sample_notebooks/VikasPrasad/Chapter_01_2.ipynb b/sample_notebooks/VikasPrasad/Chapter_01_2.ipynb new file mode 100755 index 00000000..b5226e07 --- /dev/null +++ b/sample_notebooks/VikasPrasad/Chapter_01_2.ipynb @@ -0,0 +1,170 @@ +{ + "metadata": { + "name": "" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "1: Overview of Data Structures and Algorithms" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1: Page 20" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Thermostat:\n", + "\t\"\"\"A simple thermostat class\"\"\"\n", + " \n", + "\t#since private instance variables don't exist in Python,\n", + " #hence using a convention: name prefixed with an underscore, to treat them as non-public part\n", + "\t_currentTemp = 0.0\n", + "\t_desiredTemp = 0.0\n", + "\n", + "\tdef furnace_on(self):\n", + "\t\t#method body goes here\n", + "\t\tpass\n", + "\n", + "\tdef furnace_off(self):\n", + "\t\t#method body goes here\n", + "\t\tpass\n", + " \n", + "#end class Thermostat" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2: Page 22" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#demonstrates basic OOP syntax\n", + "\n", + "class BankAccount:\n", + "\t\"\"\"A simple bank account class\"\"\"\n", + "\t\n", + "\tdef __init__(self, openingBalance):\t#special method to create objects\n", + " #with instances customized to a specific initial state\n", + "\t\t\n", + " #since private instance variables don't exist in Python,\n", + " #hence using a convention: name prefixed with an underscore, to treat them as non-public part\n", + "\t self._balance = openingBalance\t#account balance\n", + "\n", + "\tdef deposit(self, amount):\t#makes deposit\n", + "\t\tself._balance = self._balance + amount\n", + "\n", + "\tdef withdraw(self, amount):\t#makes withdrawl\n", + "\t\tself._balance = self._balance - amount\n", + "\n", + "\tdef display(self):\t#displays balance\n", + "\t\tprint 'Balance=', self._balance\n", + " \n", + "#end class BankAccount\n", + "\n", + "ba1 = BankAccount(100.00)\t#create account\n", + "\n", + "print 'Before transactions, ',\n", + "ba1.display()\t#display balance\n", + "\n", + "ba1.deposit(74.35)\t#make deposit\n", + "ba1.withdraw(20.00)\t#make withdrawl\n", + "\n", + "print 'After transactions, ',\n", + "ba1.display()\t#display balance\n", + "#end" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Before transactions, Balance= 100.0\n", + "After transactions, Balance= 154.35\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 3: Page 25" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#creating string objects\n", + "str1 = ''\n", + "str2 = 'George'\n", + "\n", + "#verifying results\n", + "print str1\n", + "print str2\n", + "\n", + "#naming string object\n", + "str3 = 'amanuensis'\n", + "\n", + "#verifying results\n", + "print str3\n", + "\n", + "#accessing specific characters from string using [] operator\n", + "ch1 = str2[3]\n", + "\n", + "#verifying results\n", + "print ch1\n", + "\n", + "#finding and printing number of characters in a string\n", + "print len(str1)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "George\n", + "amanuensis\n", + "r\n", + "0\n" + ] + } + ], + "prompt_number": 3 + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/sample_notebooks/VikasPrasad/chapter1_2.ipynb b/sample_notebooks/VikasPrasad/chapter1_2.ipynb new file mode 100755 index 00000000..eca44a51 --- /dev/null +++ b/sample_notebooks/VikasPrasad/chapter1_2.ipynb @@ -0,0 +1,165 @@ +{ + "metadata": { + "name": "" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "1: Overview of Data Structures and Algorithms" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1: Page 20" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Thermostat:\n", + "\t\"\"\"A simple thermostat class\"\"\"\n", + "\t#since private instance variables don't exist in Python, hence using a convention: name prefixed with an underscore,\n", + "\t#to treat them as non-public part\n", + "\t_currentTemp = 0.0\n", + "\t_desiredTemp = 0.0\n", + "\n", + "\tdef furnace_on(self):\n", + "\t\t#method body goes here\n", + "\t\tpass\n", + "\n", + "\tdef furnace_off(self):\n", + "\t\t#method body goes here\n", + "\t\tpass\n", + "#end class Thermostat" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2: Page 22" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#demonstrates basic OOP syntax\n", + "\n", + "class BankAccount:\n", + "\t\"\"\"A simple bank account class\"\"\"\n", + "\t\n", + "\tdef __init__(self, openingBalance):\t#special method to cerate objects with instances customized to a specific initial state\n", + "\t\t#since private instance variables don't exist in Python, hence using a convention: name prefixed with an underscore,\n", + "\t\t#to treat them as non-public part\n", + "\t self._balance = openingBalance\t#account balance\n", + "\n", + "\tdef deposit(self, amount):\t#makes deposit\n", + "\t\tself._balance = self._balance + amount\n", + "\n", + "\tdef withdraw(self, amount):\t#makes withdrawl\n", + "\t\tself._balance = self._balance - amount\n", + "\n", + "\tdef display(self):\t#displays balance\n", + "\t\tprint 'Balance=', self._balance\n", + "#end class BankAccount\n", + "\n", + "ba1 = BankAccount(100.00)\t#create account\n", + "\n", + "print 'Before transactions, ',\n", + "ba1.display()\t#display balance\n", + "\n", + "ba1.deposit(74.35)\t#make deposit\n", + "ba1.withdraw(20.00)\t#make withdrawl\n", + "\n", + "print 'After transactions, ',\n", + "ba1.display()\t#display balance\n", + "#end" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Before transactions, Balance= 100.0\n", + "After transactions, Balance= 154.35\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 3: Page 25" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#creating string objects\n", + "str1 = ''\n", + "str2 = 'George'\n", + "\n", + "#verifying results\n", + "print str1\n", + "print str2\n", + "\n", + "#naming string object\n", + "str3 = 'amanuensis'\n", + "\n", + "#verifying results\n", + "print str3\n", + "\n", + "#accessing specific characters from string using [] operator\n", + "ch1 = str2[3]\n", + "\n", + "#verifying results\n", + "print ch1\n", + "\n", + "#finding and printing number of characters in a string\n", + "print len(str1)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "George\n", + "amanuensis\n", + "r\n", + "0\n" + ] + } + ], + "prompt_number": 3 + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/sample_notebooks/VikasPrasad/chapter1_3.ipynb b/sample_notebooks/VikasPrasad/chapter1_3.ipynb new file mode 100755 index 00000000..eca44a51 --- /dev/null +++ b/sample_notebooks/VikasPrasad/chapter1_3.ipynb @@ -0,0 +1,165 @@ +{ + "metadata": { + "name": "" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "1: Overview of Data Structures and Algorithms" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1: Page 20" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Thermostat:\n", + "\t\"\"\"A simple thermostat class\"\"\"\n", + "\t#since private instance variables don't exist in Python, hence using a convention: name prefixed with an underscore,\n", + "\t#to treat them as non-public part\n", + "\t_currentTemp = 0.0\n", + "\t_desiredTemp = 0.0\n", + "\n", + "\tdef furnace_on(self):\n", + "\t\t#method body goes here\n", + "\t\tpass\n", + "\n", + "\tdef furnace_off(self):\n", + "\t\t#method body goes here\n", + "\t\tpass\n", + "#end class Thermostat" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2: Page 22" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#demonstrates basic OOP syntax\n", + "\n", + "class BankAccount:\n", + "\t\"\"\"A simple bank account class\"\"\"\n", + "\t\n", + "\tdef __init__(self, openingBalance):\t#special method to cerate objects with instances customized to a specific initial state\n", + "\t\t#since private instance variables don't exist in Python, hence using a convention: name prefixed with an underscore,\n", + "\t\t#to treat them as non-public part\n", + "\t self._balance = openingBalance\t#account balance\n", + "\n", + "\tdef deposit(self, amount):\t#makes deposit\n", + "\t\tself._balance = self._balance + amount\n", + "\n", + "\tdef withdraw(self, amount):\t#makes withdrawl\n", + "\t\tself._balance = self._balance - amount\n", + "\n", + "\tdef display(self):\t#displays balance\n", + "\t\tprint 'Balance=', self._balance\n", + "#end class BankAccount\n", + "\n", + "ba1 = BankAccount(100.00)\t#create account\n", + "\n", + "print 'Before transactions, ',\n", + "ba1.display()\t#display balance\n", + "\n", + "ba1.deposit(74.35)\t#make deposit\n", + "ba1.withdraw(20.00)\t#make withdrawl\n", + "\n", + "print 'After transactions, ',\n", + "ba1.display()\t#display balance\n", + "#end" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Before transactions, Balance= 100.0\n", + "After transactions, Balance= 154.35\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 3: Page 25" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#creating string objects\n", + "str1 = ''\n", + "str2 = 'George'\n", + "\n", + "#verifying results\n", + "print str1\n", + "print str2\n", + "\n", + "#naming string object\n", + "str3 = 'amanuensis'\n", + "\n", + "#verifying results\n", + "print str3\n", + "\n", + "#accessing specific characters from string using [] operator\n", + "ch1 = str2[3]\n", + "\n", + "#verifying results\n", + "print ch1\n", + "\n", + "#finding and printing number of characters in a string\n", + "print len(str1)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "George\n", + "amanuensis\n", + "r\n", + "0\n" + ] + } + ], + "prompt_number": 3 + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/sample_notebooks/VikasPrasad/chapter1_4.ipynb b/sample_notebooks/VikasPrasad/chapter1_4.ipynb new file mode 100755 index 00000000..eca44a51 --- /dev/null +++ b/sample_notebooks/VikasPrasad/chapter1_4.ipynb @@ -0,0 +1,165 @@ +{ + "metadata": { + "name": "" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "1: Overview of Data Structures and Algorithms" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1: Page 20" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Thermostat:\n", + "\t\"\"\"A simple thermostat class\"\"\"\n", + "\t#since private instance variables don't exist in Python, hence using a convention: name prefixed with an underscore,\n", + "\t#to treat them as non-public part\n", + "\t_currentTemp = 0.0\n", + "\t_desiredTemp = 0.0\n", + "\n", + "\tdef furnace_on(self):\n", + "\t\t#method body goes here\n", + "\t\tpass\n", + "\n", + "\tdef furnace_off(self):\n", + "\t\t#method body goes here\n", + "\t\tpass\n", + "#end class Thermostat" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2: Page 22" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#demonstrates basic OOP syntax\n", + "\n", + "class BankAccount:\n", + "\t\"\"\"A simple bank account class\"\"\"\n", + "\t\n", + "\tdef __init__(self, openingBalance):\t#special method to cerate objects with instances customized to a specific initial state\n", + "\t\t#since private instance variables don't exist in Python, hence using a convention: name prefixed with an underscore,\n", + "\t\t#to treat them as non-public part\n", + "\t self._balance = openingBalance\t#account balance\n", + "\n", + "\tdef deposit(self, amount):\t#makes deposit\n", + "\t\tself._balance = self._balance + amount\n", + "\n", + "\tdef withdraw(self, amount):\t#makes withdrawl\n", + "\t\tself._balance = self._balance - amount\n", + "\n", + "\tdef display(self):\t#displays balance\n", + "\t\tprint 'Balance=', self._balance\n", + "#end class BankAccount\n", + "\n", + "ba1 = BankAccount(100.00)\t#create account\n", + "\n", + "print 'Before transactions, ',\n", + "ba1.display()\t#display balance\n", + "\n", + "ba1.deposit(74.35)\t#make deposit\n", + "ba1.withdraw(20.00)\t#make withdrawl\n", + "\n", + "print 'After transactions, ',\n", + "ba1.display()\t#display balance\n", + "#end" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Before transactions, Balance= 100.0\n", + "After transactions, Balance= 154.35\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 3: Page 25" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#creating string objects\n", + "str1 = ''\n", + "str2 = 'George'\n", + "\n", + "#verifying results\n", + "print str1\n", + "print str2\n", + "\n", + "#naming string object\n", + "str3 = 'amanuensis'\n", + "\n", + "#verifying results\n", + "print str3\n", + "\n", + "#accessing specific characters from string using [] operator\n", + "ch1 = str2[3]\n", + "\n", + "#verifying results\n", + "print ch1\n", + "\n", + "#finding and printing number of characters in a string\n", + "print len(str1)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "George\n", + "amanuensis\n", + "r\n", + "0\n" + ] + } + ], + "prompt_number": 3 + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/sample_notebooks/VikasPrasad/chapter1_5.ipynb b/sample_notebooks/VikasPrasad/chapter1_5.ipynb new file mode 100755 index 00000000..eca44a51 --- /dev/null +++ b/sample_notebooks/VikasPrasad/chapter1_5.ipynb @@ -0,0 +1,165 @@ +{ + "metadata": { + "name": "" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "1: Overview of Data Structures and Algorithms" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1: Page 20" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Thermostat:\n", + "\t\"\"\"A simple thermostat class\"\"\"\n", + "\t#since private instance variables don't exist in Python, hence using a convention: name prefixed with an underscore,\n", + "\t#to treat them as non-public part\n", + "\t_currentTemp = 0.0\n", + "\t_desiredTemp = 0.0\n", + "\n", + "\tdef furnace_on(self):\n", + "\t\t#method body goes here\n", + "\t\tpass\n", + "\n", + "\tdef furnace_off(self):\n", + "\t\t#method body goes here\n", + "\t\tpass\n", + "#end class Thermostat" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2: Page 22" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#demonstrates basic OOP syntax\n", + "\n", + "class BankAccount:\n", + "\t\"\"\"A simple bank account class\"\"\"\n", + "\t\n", + "\tdef __init__(self, openingBalance):\t#special method to cerate objects with instances customized to a specific initial state\n", + "\t\t#since private instance variables don't exist in Python, hence using a convention: name prefixed with an underscore,\n", + "\t\t#to treat them as non-public part\n", + "\t self._balance = openingBalance\t#account balance\n", + "\n", + "\tdef deposit(self, amount):\t#makes deposit\n", + "\t\tself._balance = self._balance + amount\n", + "\n", + "\tdef withdraw(self, amount):\t#makes withdrawl\n", + "\t\tself._balance = self._balance - amount\n", + "\n", + "\tdef display(self):\t#displays balance\n", + "\t\tprint 'Balance=', self._balance\n", + "#end class BankAccount\n", + "\n", + "ba1 = BankAccount(100.00)\t#create account\n", + "\n", + "print 'Before transactions, ',\n", + "ba1.display()\t#display balance\n", + "\n", + "ba1.deposit(74.35)\t#make deposit\n", + "ba1.withdraw(20.00)\t#make withdrawl\n", + "\n", + "print 'After transactions, ',\n", + "ba1.display()\t#display balance\n", + "#end" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Before transactions, Balance= 100.0\n", + "After transactions, Balance= 154.35\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 3: Page 25" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#creating string objects\n", + "str1 = ''\n", + "str2 = 'George'\n", + "\n", + "#verifying results\n", + "print str1\n", + "print str2\n", + "\n", + "#naming string object\n", + "str3 = 'amanuensis'\n", + "\n", + "#verifying results\n", + "print str3\n", + "\n", + "#accessing specific characters from string using [] operator\n", + "ch1 = str2[3]\n", + "\n", + "#verifying results\n", + "print ch1\n", + "\n", + "#finding and printing number of characters in a string\n", + "print len(str1)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "George\n", + "amanuensis\n", + "r\n", + "0\n" + ] + } + ], + "prompt_number": 3 + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/sample_notebooks/VikasPrasad/chapter1_6.ipynb b/sample_notebooks/VikasPrasad/chapter1_6.ipynb new file mode 100755 index 00000000..eca44a51 --- /dev/null +++ b/sample_notebooks/VikasPrasad/chapter1_6.ipynb @@ -0,0 +1,165 @@ +{ + "metadata": { + "name": "" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "1: Overview of Data Structures and Algorithms" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1: Page 20" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Thermostat:\n", + "\t\"\"\"A simple thermostat class\"\"\"\n", + "\t#since private instance variables don't exist in Python, hence using a convention: name prefixed with an underscore,\n", + "\t#to treat them as non-public part\n", + "\t_currentTemp = 0.0\n", + "\t_desiredTemp = 0.0\n", + "\n", + "\tdef furnace_on(self):\n", + "\t\t#method body goes here\n", + "\t\tpass\n", + "\n", + "\tdef furnace_off(self):\n", + "\t\t#method body goes here\n", + "\t\tpass\n", + "#end class Thermostat" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2: Page 22" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#demonstrates basic OOP syntax\n", + "\n", + "class BankAccount:\n", + "\t\"\"\"A simple bank account class\"\"\"\n", + "\t\n", + "\tdef __init__(self, openingBalance):\t#special method to cerate objects with instances customized to a specific initial state\n", + "\t\t#since private instance variables don't exist in Python, hence using a convention: name prefixed with an underscore,\n", + "\t\t#to treat them as non-public part\n", + "\t self._balance = openingBalance\t#account balance\n", + "\n", + "\tdef deposit(self, amount):\t#makes deposit\n", + "\t\tself._balance = self._balance + amount\n", + "\n", + "\tdef withdraw(self, amount):\t#makes withdrawl\n", + "\t\tself._balance = self._balance - amount\n", + "\n", + "\tdef display(self):\t#displays balance\n", + "\t\tprint 'Balance=', self._balance\n", + "#end class BankAccount\n", + "\n", + "ba1 = BankAccount(100.00)\t#create account\n", + "\n", + "print 'Before transactions, ',\n", + "ba1.display()\t#display balance\n", + "\n", + "ba1.deposit(74.35)\t#make deposit\n", + "ba1.withdraw(20.00)\t#make withdrawl\n", + "\n", + "print 'After transactions, ',\n", + "ba1.display()\t#display balance\n", + "#end" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Before transactions, Balance= 100.0\n", + "After transactions, Balance= 154.35\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 3: Page 25" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#creating string objects\n", + "str1 = ''\n", + "str2 = 'George'\n", + "\n", + "#verifying results\n", + "print str1\n", + "print str2\n", + "\n", + "#naming string object\n", + "str3 = 'amanuensis'\n", + "\n", + "#verifying results\n", + "print str3\n", + "\n", + "#accessing specific characters from string using [] operator\n", + "ch1 = str2[3]\n", + "\n", + "#verifying results\n", + "print ch1\n", + "\n", + "#finding and printing number of characters in a string\n", + "print len(str1)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "George\n", + "amanuensis\n", + "r\n", + "0\n" + ] + } + ], + "prompt_number": 3 + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/sample_notebooks/VikasPrasad/chapter1_7.ipynb b/sample_notebooks/VikasPrasad/chapter1_7.ipynb new file mode 100755 index 00000000..eca44a51 --- /dev/null +++ b/sample_notebooks/VikasPrasad/chapter1_7.ipynb @@ -0,0 +1,165 @@ +{ + "metadata": { + "name": "" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "1: Overview of Data Structures and Algorithms" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1: Page 20" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Thermostat:\n", + "\t\"\"\"A simple thermostat class\"\"\"\n", + "\t#since private instance variables don't exist in Python, hence using a convention: name prefixed with an underscore,\n", + "\t#to treat them as non-public part\n", + "\t_currentTemp = 0.0\n", + "\t_desiredTemp = 0.0\n", + "\n", + "\tdef furnace_on(self):\n", + "\t\t#method body goes here\n", + "\t\tpass\n", + "\n", + "\tdef furnace_off(self):\n", + "\t\t#method body goes here\n", + "\t\tpass\n", + "#end class Thermostat" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2: Page 22" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#demonstrates basic OOP syntax\n", + "\n", + "class BankAccount:\n", + "\t\"\"\"A simple bank account class\"\"\"\n", + "\t\n", + "\tdef __init__(self, openingBalance):\t#special method to cerate objects with instances customized to a specific initial state\n", + "\t\t#since private instance variables don't exist in Python, hence using a convention: name prefixed with an underscore,\n", + "\t\t#to treat them as non-public part\n", + "\t self._balance = openingBalance\t#account balance\n", + "\n", + "\tdef deposit(self, amount):\t#makes deposit\n", + "\t\tself._balance = self._balance + amount\n", + "\n", + "\tdef withdraw(self, amount):\t#makes withdrawl\n", + "\t\tself._balance = self._balance - amount\n", + "\n", + "\tdef display(self):\t#displays balance\n", + "\t\tprint 'Balance=', self._balance\n", + "#end class BankAccount\n", + "\n", + "ba1 = BankAccount(100.00)\t#create account\n", + "\n", + "print 'Before transactions, ',\n", + "ba1.display()\t#display balance\n", + "\n", + "ba1.deposit(74.35)\t#make deposit\n", + "ba1.withdraw(20.00)\t#make withdrawl\n", + "\n", + "print 'After transactions, ',\n", + "ba1.display()\t#display balance\n", + "#end" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Before transactions, Balance= 100.0\n", + "After transactions, Balance= 154.35\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 3: Page 25" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#creating string objects\n", + "str1 = ''\n", + "str2 = 'George'\n", + "\n", + "#verifying results\n", + "print str1\n", + "print str2\n", + "\n", + "#naming string object\n", + "str3 = 'amanuensis'\n", + "\n", + "#verifying results\n", + "print str3\n", + "\n", + "#accessing specific characters from string using [] operator\n", + "ch1 = str2[3]\n", + "\n", + "#verifying results\n", + "print ch1\n", + "\n", + "#finding and printing number of characters in a string\n", + "print len(str1)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "George\n", + "amanuensis\n", + "r\n", + "0\n" + ] + } + ], + "prompt_number": 3 + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/sample_notebooks/VikasPrasad/chapter1_8.ipynb b/sample_notebooks/VikasPrasad/chapter1_8.ipynb new file mode 100755 index 00000000..b5226e07 --- /dev/null +++ b/sample_notebooks/VikasPrasad/chapter1_8.ipynb @@ -0,0 +1,170 @@ +{ + "metadata": { + "name": "" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "1: Overview of Data Structures and Algorithms" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1: Page 20" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Thermostat:\n", + "\t\"\"\"A simple thermostat class\"\"\"\n", + " \n", + "\t#since private instance variables don't exist in Python,\n", + " #hence using a convention: name prefixed with an underscore, to treat them as non-public part\n", + "\t_currentTemp = 0.0\n", + "\t_desiredTemp = 0.0\n", + "\n", + "\tdef furnace_on(self):\n", + "\t\t#method body goes here\n", + "\t\tpass\n", + "\n", + "\tdef furnace_off(self):\n", + "\t\t#method body goes here\n", + "\t\tpass\n", + " \n", + "#end class Thermostat" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2: Page 22" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#demonstrates basic OOP syntax\n", + "\n", + "class BankAccount:\n", + "\t\"\"\"A simple bank account class\"\"\"\n", + "\t\n", + "\tdef __init__(self, openingBalance):\t#special method to create objects\n", + " #with instances customized to a specific initial state\n", + "\t\t\n", + " #since private instance variables don't exist in Python,\n", + " #hence using a convention: name prefixed with an underscore, to treat them as non-public part\n", + "\t self._balance = openingBalance\t#account balance\n", + "\n", + "\tdef deposit(self, amount):\t#makes deposit\n", + "\t\tself._balance = self._balance + amount\n", + "\n", + "\tdef withdraw(self, amount):\t#makes withdrawl\n", + "\t\tself._balance = self._balance - amount\n", + "\n", + "\tdef display(self):\t#displays balance\n", + "\t\tprint 'Balance=', self._balance\n", + " \n", + "#end class BankAccount\n", + "\n", + "ba1 = BankAccount(100.00)\t#create account\n", + "\n", + "print 'Before transactions, ',\n", + "ba1.display()\t#display balance\n", + "\n", + "ba1.deposit(74.35)\t#make deposit\n", + "ba1.withdraw(20.00)\t#make withdrawl\n", + "\n", + "print 'After transactions, ',\n", + "ba1.display()\t#display balance\n", + "#end" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Before transactions, Balance= 100.0\n", + "After transactions, Balance= 154.35\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 3: Page 25" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#creating string objects\n", + "str1 = ''\n", + "str2 = 'George'\n", + "\n", + "#verifying results\n", + "print str1\n", + "print str2\n", + "\n", + "#naming string object\n", + "str3 = 'amanuensis'\n", + "\n", + "#verifying results\n", + "print str3\n", + "\n", + "#accessing specific characters from string using [] operator\n", + "ch1 = str2[3]\n", + "\n", + "#verifying results\n", + "print ch1\n", + "\n", + "#finding and printing number of characters in a string\n", + "print len(str1)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "George\n", + "amanuensis\n", + "r\n", + "0\n" + ] + } + ], + "prompt_number": 3 + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/sample_notebooks/VikasPrasad/chapter1_9.ipynb b/sample_notebooks/VikasPrasad/chapter1_9.ipynb new file mode 100755 index 00000000..b5226e07 --- /dev/null +++ b/sample_notebooks/VikasPrasad/chapter1_9.ipynb @@ -0,0 +1,170 @@ +{ + "metadata": { + "name": "" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "1: Overview of Data Structures and Algorithms" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1: Page 20" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Thermostat:\n", + "\t\"\"\"A simple thermostat class\"\"\"\n", + " \n", + "\t#since private instance variables don't exist in Python,\n", + " #hence using a convention: name prefixed with an underscore, to treat them as non-public part\n", + "\t_currentTemp = 0.0\n", + "\t_desiredTemp = 0.0\n", + "\n", + "\tdef furnace_on(self):\n", + "\t\t#method body goes here\n", + "\t\tpass\n", + "\n", + "\tdef furnace_off(self):\n", + "\t\t#method body goes here\n", + "\t\tpass\n", + " \n", + "#end class Thermostat" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2: Page 22" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#demonstrates basic OOP syntax\n", + "\n", + "class BankAccount:\n", + "\t\"\"\"A simple bank account class\"\"\"\n", + "\t\n", + "\tdef __init__(self, openingBalance):\t#special method to create objects\n", + " #with instances customized to a specific initial state\n", + "\t\t\n", + " #since private instance variables don't exist in Python,\n", + " #hence using a convention: name prefixed with an underscore, to treat them as non-public part\n", + "\t self._balance = openingBalance\t#account balance\n", + "\n", + "\tdef deposit(self, amount):\t#makes deposit\n", + "\t\tself._balance = self._balance + amount\n", + "\n", + "\tdef withdraw(self, amount):\t#makes withdrawl\n", + "\t\tself._balance = self._balance - amount\n", + "\n", + "\tdef display(self):\t#displays balance\n", + "\t\tprint 'Balance=', self._balance\n", + " \n", + "#end class BankAccount\n", + "\n", + "ba1 = BankAccount(100.00)\t#create account\n", + "\n", + "print 'Before transactions, ',\n", + "ba1.display()\t#display balance\n", + "\n", + "ba1.deposit(74.35)\t#make deposit\n", + "ba1.withdraw(20.00)\t#make withdrawl\n", + "\n", + "print 'After transactions, ',\n", + "ba1.display()\t#display balance\n", + "#end" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Before transactions, Balance= 100.0\n", + "After transactions, Balance= 154.35\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 3: Page 25" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#creating string objects\n", + "str1 = ''\n", + "str2 = 'George'\n", + "\n", + "#verifying results\n", + "print str1\n", + "print str2\n", + "\n", + "#naming string object\n", + "str3 = 'amanuensis'\n", + "\n", + "#verifying results\n", + "print str3\n", + "\n", + "#accessing specific characters from string using [] operator\n", + "ch1 = str2[3]\n", + "\n", + "#verifying results\n", + "print ch1\n", + "\n", + "#finding and printing number of characters in a string\n", + "print len(str1)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "George\n", + "amanuensis\n", + "r\n", + "0\n" + ] + } + ], + "prompt_number": 3 + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/sample_notebooks/Vishwajith VRao/Chapter2.ipynb b/sample_notebooks/Vishwajith VRao/Chapter2.ipynb new file mode 100755 index 00000000..afc828c0 --- /dev/null +++ b/sample_notebooks/Vishwajith VRao/Chapter2.ipynb @@ -0,0 +1,350 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "