SlideShare a Scribd company logo
Play with Python
            Lab2
Object Oriented Programming
About Lecture 2 and Lab 2
Lecture 2 and its lab aims at covering Basic
  Object Oriented Programming concepts
Classes, Constructor (__init__) and Objects
Example 1
Create a new Project in
                                  shp1 = IrregularShape()
  Aptana "Lab2", then             shp1.AddShape(Square(3))

  add a new PyDev                 shp1.AddShape(Triangle(4, 5))
                                  shp1.AddShape(Square(10))
  module called main              print shp1.Area()

Write the following code          ========
  in your main file:              Output:
                                  119.0
class Square:
         def __init__(self, l):
         self.length = l
         def Area(self):
         return self.length**2
class Triangle:
     def __init__(self, b, h):
Example 1
class Square:
     def __init__(self, l):
       self.length = l
     def Area(self):
        return self.length**2
class Triangle:
   def __init__(self, b, h):
      self.base = b
      self.height = h
   def Area(self):
      return 0.5*self.base*self.height
Example 1
class IrregularShape:
   def __init__(self):
      self.shapes = []
   def AddShape(self, shape):
   self.shapes.append(shape)
   def Area(self):
   area = 0
   for shape in self.shapes:
      area += shape.Area()
   return area
Example 1

shp1 = IrregularShape()
shp1.AddShape(Square(3))
shp1.AddShape(Triangle(4, 5))
shp1.AddShape(Square(10))
print shp1.Area()

========
Output:
119.0
Exercise 1 (5 Minutes)
Add another IrregularShape that has double
 the area of shp1 in the previous example,
 ONLY by using shp1 object that you have
 just created, not using any other shapes
Exercise 1 (Solution)
shp2 = IrregularShape()
shp2.AddShape(shp1)
shp2.AddShape(shp1)
print shp2.Area()



=====
Output:
238.0
Example 2
Type this SquareMatrix                 def addNumber(self, number):
                                           resultMatrix = SquareMatrix()
   class in your main file:
                                           matrixDimension = len(self.matrix)


                                           for rowIndex in range(0,matrixDimension):
#square matrix only
                                                                 newRow = []
class SquareMatrix:
                                                                 for columnIndex in range(0,matrixDimension):
    def __init__(self):
             self.matrix = []                        newRow.append(self.matrix[rowIndex][columnIndex]    +
                                           number)
    def appendRow(self, row):                                    resultMatrix.appendRow(newRow)
             self.matrix.append(row)
                                           return resultMatrix
    def printMatrix(self):
             for row in self.matrix:
                                                                                 Output:
                                       ================================          [-1, 1]
             print row                 mat = SquareMatrix()                      [8, 4]
                                       mat.appendRow([0, 2])
                                       mat.appendRow([9, 5])
                                       mat.addNumber(-1)
Example 2
#square matrix only
class SquareMatrix:
   def __init__(self):
         self.matrix = []


   def appendRow(self, row):
         self.matrix.append(row)


   def printMatrix(self):
         for row in self.matrix:
                            print row
Example 2
def addNumber(self, number):
   resultMatrix = SquareMatrix()
   matrixDimension = len(self.matrix)


   for rowIndex in range(0,matrixDimension):
                    newRow = []
                    for columnIndex in range(0,matrixDimension):
                    newRow.append(self.matrix[rowIndex][columnIndex]         + number)
                    resultMatrix.appendRow(newRow)


   return resultMatrix
================================
mat = SquareMatrix()                                               Output:
mat.appendRow([0, 2])                                              [-1, 1]
                                                                   [8, 4]
mat.appendRow([9, 5])
mat.addNumber(-1)
mat.printMatrix()
Example 2
This class represents a square matrix (equal row, column dimensions),
   the add number function


matrix data is filled by row, using the appendRow() function


addNumber() function adds a number to every element in the matrix
Exercise 2 (10 minutes)
Add a new fucntion add() that returns a new
 matrix which is the result of adding this matrix
 with another matrix:

mat1 = SquareMatrix()
mat1.appendRow([1, 2])    1   2   1 -2     2 0
mat1.appendRow([4, 5])
                          4   5   -5 1     -1 6
mat2 = SquareMatrix()
mat2.appendRow([1, -2])
mat2.appendRow([-5, 1])



mat3 = mat1.add(mat2)
mat3.printMatrix()
Exercise 2 (Solution)
def add(self, otherMatrix):
          resultMatrix = SquareMatrix()
          matrixDimension = len(self.matrix)
     for rowIndex in range(0,matrixDimension):
           newRow = []
           for columnIndex in range(0,matrixDimension):
                  newRow.append(self.matrix[rowIndex][columnIndex] + otherMatrix.matrix[rowIndex][columnIndex])
           resultMatrix.appendRow(newRow)
          return resultMatrix
=======================
mat1 = SquareMatrix()                         Fun: add 3 matrices in one line like this:
mat1.appendRow([1, 2])                        mat1.add(mat2).add(mat3).printMatrix()
mat1.appendRow([4, 5])



mat2 = SquareMatrix()
mat2.appendRow([1, -2])
mat2.appendRow([-5, 1])
Exercise 3 (10 minutes)
Add a new function mutliplyNumber(t), which multiplies a positive
integer t to the matrix, ONLY using the add(othermatrix) method


         1     -1                            5 -5
         -1     1            5               -5 5
Exercise 3 (Solution)
def multiplyNumber(self, times):
     result = self
     for i in range(0, times-1):
      result = result.add(self)
     return result
==========================

mat = SquareMatrix()
mat.appendRow([1, -1])
mat.appendRow([-1, 1])


mat.multiplyNumber(5)

More Related Content

What's hot (19)

Chapter 7.3
Chapter 7.3Chapter 7.3
Chapter 7.3
sotlsoc
 
Chapter2
Chapter2Chapter2
Chapter2
Krishna Kumar
 
Matlab Graphics Tutorial
Matlab Graphics TutorialMatlab Graphics Tutorial
Matlab Graphics Tutorial
Cheng-An Yang
 
What is TensorFlow and why do we use it
What is TensorFlow and why do we use itWhat is TensorFlow and why do we use it
What is TensorFlow and why do we use it
Robert John
 
Gentlest Introduction to Tensorflow
Gentlest Introduction to TensorflowGentlest Introduction to Tensorflow
Gentlest Introduction to Tensorflow
Khor SoonHin
 
TensorFlow in Practice
TensorFlow in PracticeTensorFlow in Practice
TensorFlow in Practice
indico data
 
(2015 06-16) Three Approaches to Monads
(2015 06-16) Three Approaches to Monads(2015 06-16) Three Approaches to Monads
(2015 06-16) Three Approaches to Monads
Lawrence Evans
 
Matlab 1
Matlab 1Matlab 1
Matlab 1
asguna
 
02 arrays
02 arrays02 arrays
02 arrays
Rajan Gautam
 
Gentlest Introduction to Tensorflow - Part 2
Gentlest Introduction to Tensorflow - Part 2Gentlest Introduction to Tensorflow - Part 2
Gentlest Introduction to Tensorflow - Part 2
Khor SoonHin
 
Gentlest Introduction to Tensorflow - Part 3
Gentlest Introduction to Tensorflow - Part 3Gentlest Introduction to Tensorflow - Part 3
Gentlest Introduction to Tensorflow - Part 3
Khor SoonHin
 
Chapter2
Chapter2Chapter2
Chapter2
Nashra Akhter
 
Scilab - Piecewise Functions
Scilab - Piecewise FunctionsScilab - Piecewise Functions
Scilab - Piecewise Functions
Jorge Jasso
 
Introduction to Functions
Introduction to FunctionsIntroduction to Functions
Introduction to Functions
Melanie Loslo
 
Multi dimensional arrays
Multi dimensional arraysMulti dimensional arrays
Multi dimensional arrays
Aseelhalees
 
Array
ArrayArray
Array
Malainine Zaid
 
Chapter 7.2
Chapter 7.2Chapter 7.2
Chapter 7.2
sotlsoc
 
5. R basics
5. R basics5. R basics
5. R basics
FAO
 
Scala Functional Patterns
Scala Functional PatternsScala Functional Patterns
Scala Functional Patterns
league
 
Chapter 7.3
Chapter 7.3Chapter 7.3
Chapter 7.3
sotlsoc
 
Matlab Graphics Tutorial
Matlab Graphics TutorialMatlab Graphics Tutorial
Matlab Graphics Tutorial
Cheng-An Yang
 
What is TensorFlow and why do we use it
What is TensorFlow and why do we use itWhat is TensorFlow and why do we use it
What is TensorFlow and why do we use it
Robert John
 
Gentlest Introduction to Tensorflow
Gentlest Introduction to TensorflowGentlest Introduction to Tensorflow
Gentlest Introduction to Tensorflow
Khor SoonHin
 
TensorFlow in Practice
TensorFlow in PracticeTensorFlow in Practice
TensorFlow in Practice
indico data
 
(2015 06-16) Three Approaches to Monads
(2015 06-16) Three Approaches to Monads(2015 06-16) Three Approaches to Monads
(2015 06-16) Three Approaches to Monads
Lawrence Evans
 
Matlab 1
Matlab 1Matlab 1
Matlab 1
asguna
 
Gentlest Introduction to Tensorflow - Part 2
Gentlest Introduction to Tensorflow - Part 2Gentlest Introduction to Tensorflow - Part 2
Gentlest Introduction to Tensorflow - Part 2
Khor SoonHin
 
Gentlest Introduction to Tensorflow - Part 3
Gentlest Introduction to Tensorflow - Part 3Gentlest Introduction to Tensorflow - Part 3
Gentlest Introduction to Tensorflow - Part 3
Khor SoonHin
 
Scilab - Piecewise Functions
Scilab - Piecewise FunctionsScilab - Piecewise Functions
Scilab - Piecewise Functions
Jorge Jasso
 
Introduction to Functions
Introduction to FunctionsIntroduction to Functions
Introduction to Functions
Melanie Loslo
 
Multi dimensional arrays
Multi dimensional arraysMulti dimensional arrays
Multi dimensional arrays
Aseelhalees
 
Chapter 7.2
Chapter 7.2Chapter 7.2
Chapter 7.2
sotlsoc
 
5. R basics
5. R basics5. R basics
5. R basics
FAO
 
Scala Functional Patterns
Scala Functional PatternsScala Functional Patterns
Scala Functional Patterns
league
 

Viewers also liked (7)

Object oriented programming with python
Object oriented programming with pythonObject oriented programming with python
Object oriented programming with python
Arslan Arshad
 
Python object oriented programming (lab2) (2)
Python object oriented programming (lab2) (2)Python object oriented programming (lab2) (2)
Python object oriented programming (lab2) (2)
iloveallahsomuch
 
Python an-intro v2
Python an-intro v2Python an-intro v2
Python an-intro v2
Arulalan T
 
Advance OOP concepts in Python
Advance OOP concepts in PythonAdvance OOP concepts in Python
Advance OOP concepts in Python
Sujith Kumar
 
Python: Basic Inheritance
Python: Basic InheritancePython: Basic Inheritance
Python: Basic Inheritance
Damian T. Gordon
 
Python: Multiple Inheritance
Python: Multiple InheritancePython: Multiple Inheritance
Python: Multiple Inheritance
Damian T. Gordon
 
Basics of Object Oriented Programming in Python
Basics of Object Oriented Programming in PythonBasics of Object Oriented Programming in Python
Basics of Object Oriented Programming in Python
Sujith Kumar
 
Object oriented programming with python
Object oriented programming with pythonObject oriented programming with python
Object oriented programming with python
Arslan Arshad
 
Python object oriented programming (lab2) (2)
Python object oriented programming (lab2) (2)Python object oriented programming (lab2) (2)
Python object oriented programming (lab2) (2)
iloveallahsomuch
 
Python an-intro v2
Python an-intro v2Python an-intro v2
Python an-intro v2
Arulalan T
 
Advance OOP concepts in Python
Advance OOP concepts in PythonAdvance OOP concepts in Python
Advance OOP concepts in Python
Sujith Kumar
 
Python: Multiple Inheritance
Python: Multiple InheritancePython: Multiple Inheritance
Python: Multiple Inheritance
Damian T. Gordon
 
Basics of Object Oriented Programming in Python
Basics of Object Oriented Programming in PythonBasics of Object Oriented Programming in Python
Basics of Object Oriented Programming in Python
Sujith Kumar
 
Ad

Similar to Python object oriented programming (lab2) (2) (20)

Python Numpy Source Codes
Python Numpy Source CodesPython Numpy Source Codes
Python Numpy Source Codes
Amarjeetsingh Thakur
 
Computer Science CS Project Matrix CBSE Class 12th XII .pdf
Computer Science CS Project Matrix CBSE Class 12th XII .pdfComputer Science CS Project Matrix CBSE Class 12th XII .pdf
Computer Science CS Project Matrix CBSE Class 12th XII .pdf
PranavAnil9
 
Concept of Data science and Numpy concept
Concept of Data science and Numpy conceptConcept of Data science and Numpy concept
Concept of Data science and Numpy concept
Deena38
 
Array presentation
Array presentationArray presentation
Array presentation
Learnbay Datascience
 
CE344L-200365-Lab2.pdf
CE344L-200365-Lab2.pdfCE344L-200365-Lab2.pdf
CE344L-200365-Lab2.pdf
UmarMustafa13
 
python.pdf
python.pdfpython.pdf
python.pdf
SwapnilGujar10
 
Introduction to MatLab programming
Introduction to MatLab programmingIntroduction to MatLab programming
Introduction to MatLab programming
Damian T. Gordon
 
lec08-numpy.pptx
lec08-numpy.pptxlec08-numpy.pptx
lec08-numpy.pptx
lekha572836
 
Pythonic Math
Pythonic MathPythonic Math
Pythonic Math
Kirby Urner
 
NumPy-python-27-9-24-we.pptxNumPy-python-27-9-24-we.pptx
NumPy-python-27-9-24-we.pptxNumPy-python-27-9-24-we.pptxNumPy-python-27-9-24-we.pptxNumPy-python-27-9-24-we.pptx
NumPy-python-27-9-24-we.pptxNumPy-python-27-9-24-we.pptx
tahirnaquash2
 
Object Orientation vs Functional Programming in Python
Object Orientation vs Functional Programming in PythonObject Orientation vs Functional Programming in Python
Object Orientation vs Functional Programming in Python
Tendayi Mawushe
 
Numpy intro presentation for college.pdf
Numpy intro presentation for college.pdfNumpy intro presentation for college.pdf
Numpy intro presentation for college.pdf
kakkarskrishna22
 
Introduction to MATLAB
Introduction to MATLABIntroduction to MATLAB
Introduction to MATLAB
Damian T. Gordon
 
Chapter 8_Array.pdf
Chapter 8_Array.pdfChapter 8_Array.pdf
Chapter 8_Array.pdf
marifmohmd
 
GE3151_PSPP_UNIT_4_Notes
GE3151_PSPP_UNIT_4_NotesGE3151_PSPP_UNIT_4_Notes
GE3151_PSPP_UNIT_4_Notes
Guru Nanak Technical Institutions
 
Numpy.pdf
Numpy.pdfNumpy.pdf
Numpy.pdf
Arvind Pathak
 
Arrays in python
Arrays in pythonArrays in python
Arrays in python
Lifna C.S
 
Matlab
MatlabMatlab
Matlab
Sri Chakra Kumar
 
III MCS python lab (1).pdf
III MCS python lab (1).pdfIII MCS python lab (1).pdf
III MCS python lab (1).pdf
srxerox
 
Arrays with Numpy, Computer Graphics
Arrays with Numpy, Computer GraphicsArrays with Numpy, Computer Graphics
Arrays with Numpy, Computer Graphics
Prabu U
 
Computer Science CS Project Matrix CBSE Class 12th XII .pdf
Computer Science CS Project Matrix CBSE Class 12th XII .pdfComputer Science CS Project Matrix CBSE Class 12th XII .pdf
Computer Science CS Project Matrix CBSE Class 12th XII .pdf
PranavAnil9
 
Concept of Data science and Numpy concept
Concept of Data science and Numpy conceptConcept of Data science and Numpy concept
Concept of Data science and Numpy concept
Deena38
 
CE344L-200365-Lab2.pdf
CE344L-200365-Lab2.pdfCE344L-200365-Lab2.pdf
CE344L-200365-Lab2.pdf
UmarMustafa13
 
Introduction to MatLab programming
Introduction to MatLab programmingIntroduction to MatLab programming
Introduction to MatLab programming
Damian T. Gordon
 
lec08-numpy.pptx
lec08-numpy.pptxlec08-numpy.pptx
lec08-numpy.pptx
lekha572836
 
NumPy-python-27-9-24-we.pptxNumPy-python-27-9-24-we.pptx
NumPy-python-27-9-24-we.pptxNumPy-python-27-9-24-we.pptxNumPy-python-27-9-24-we.pptxNumPy-python-27-9-24-we.pptx
NumPy-python-27-9-24-we.pptxNumPy-python-27-9-24-we.pptx
tahirnaquash2
 
Object Orientation vs Functional Programming in Python
Object Orientation vs Functional Programming in PythonObject Orientation vs Functional Programming in Python
Object Orientation vs Functional Programming in Python
Tendayi Mawushe
 
Numpy intro presentation for college.pdf
Numpy intro presentation for college.pdfNumpy intro presentation for college.pdf
Numpy intro presentation for college.pdf
kakkarskrishna22
 
Chapter 8_Array.pdf
Chapter 8_Array.pdfChapter 8_Array.pdf
Chapter 8_Array.pdf
marifmohmd
 
Arrays in python
Arrays in pythonArrays in python
Arrays in python
Lifna C.S
 
III MCS python lab (1).pdf
III MCS python lab (1).pdfIII MCS python lab (1).pdf
III MCS python lab (1).pdf
srxerox
 
Arrays with Numpy, Computer Graphics
Arrays with Numpy, Computer GraphicsArrays with Numpy, Computer Graphics
Arrays with Numpy, Computer Graphics
Prabu U
 
Ad

Recently uploaded (20)

Oracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI ProfessionalOracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI Professional
VICTOR MAESTRE RAMIREZ
 
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdfcnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
AmirStern2
 
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Agentic AI: Beyond the Buzz- LangGraph Studio V2Agentic AI: Beyond the Buzz- LangGraph Studio V2
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Shashikant Jagtap
 
Soulmaite review - Find Real AI soulmate review
Soulmaite review - Find Real AI soulmate reviewSoulmaite review - Find Real AI soulmate review
Soulmaite review - Find Real AI soulmate review
Soulmaite
 
Azure vs AWS Which Cloud Platform Is Best for Your Business in 2025
Azure vs AWS  Which Cloud Platform Is Best for Your Business in 2025Azure vs AWS  Which Cloud Platform Is Best for Your Business in 2025
Azure vs AWS Which Cloud Platform Is Best for Your Business in 2025
Infrassist Technologies Pvt. Ltd.
 
Your startup on AWS - How to architect and maintain a Lean and Mean account J...
Your startup on AWS - How to architect and maintain a Lean and Mean account J...Your startup on AWS - How to architect and maintain a Lean and Mean account J...
Your startup on AWS - How to architect and maintain a Lean and Mean account J...
angelo60207
 
Trends Artificial Intelligence - Mary Meeker
Trends Artificial Intelligence - Mary MeekerTrends Artificial Intelligence - Mary Meeker
Trends Artificial Intelligence - Mary Meeker
Clive Dickens
 
Mastering AI Workflows with FME - Peak of Data & AI 2025
Mastering AI Workflows with FME - Peak of Data & AI 2025Mastering AI Workflows with FME - Peak of Data & AI 2025
Mastering AI Workflows with FME - Peak of Data & AI 2025
Safe Software
 
6th Power Grid Model Meetup - 21 May 2025
6th Power Grid Model Meetup - 21 May 20256th Power Grid Model Meetup - 21 May 2025
6th Power Grid Model Meetup - 21 May 2025
DanBrown980551
 
Jeremy Millul - A Talented Software Developer
Jeremy Millul - A Talented Software DeveloperJeremy Millul - A Talented Software Developer
Jeremy Millul - A Talented Software Developer
Jeremy Millul
 
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
Edge AI and Vision Alliance
 
End-to-end Assurance for SD-WAN & SASE with ThousandEyes
End-to-end Assurance for SD-WAN & SASE with ThousandEyesEnd-to-end Assurance for SD-WAN & SASE with ThousandEyes
End-to-end Assurance for SD-WAN & SASE with ThousandEyes
ThousandEyes
 
Down the Rabbit Hole – Solving 5 Training Roadblocks
Down the Rabbit Hole – Solving 5 Training RoadblocksDown the Rabbit Hole – Solving 5 Training Roadblocks
Down the Rabbit Hole – Solving 5 Training Roadblocks
Rustici Software
 
LSNIF: Locally-Subdivided Neural Intersection Function
LSNIF: Locally-Subdivided Neural Intersection FunctionLSNIF: Locally-Subdivided Neural Intersection Function
LSNIF: Locally-Subdivided Neural Intersection Function
Takahiro Harada
 
Introduction to Internet of things .ppt.
Introduction to Internet of things .ppt.Introduction to Internet of things .ppt.
Introduction to Internet of things .ppt.
hok12341073
 
vertical-cnc-processing-centers-drillteq-v-200-en.pdf
vertical-cnc-processing-centers-drillteq-v-200-en.pdfvertical-cnc-processing-centers-drillteq-v-200-en.pdf
vertical-cnc-processing-centers-drillteq-v-200-en.pdf
AmirStern2
 
Domino IQ – What to Expect, First Steps and Use Cases
Domino IQ – What to Expect, First Steps and Use CasesDomino IQ – What to Expect, First Steps and Use Cases
Domino IQ – What to Expect, First Steps and Use Cases
panagenda
 
What is Oracle EPM A Guide to Oracle EPM Cloud Everything You Need to Know
What is Oracle EPM A Guide to Oracle EPM Cloud Everything You Need to KnowWhat is Oracle EPM A Guide to Oracle EPM Cloud Everything You Need to Know
What is Oracle EPM A Guide to Oracle EPM Cloud Everything You Need to Know
SMACT Works
 
ELNL2025 - Unlocking the Power of Sensitivity Labels - A Comprehensive Guide....
ELNL2025 - Unlocking the Power of Sensitivity Labels - A Comprehensive Guide....ELNL2025 - Unlocking the Power of Sensitivity Labels - A Comprehensive Guide....
ELNL2025 - Unlocking the Power of Sensitivity Labels - A Comprehensive Guide....
Jasper Oosterveld
 
Oracle Cloud Infrastructure AI Foundations
Oracle Cloud Infrastructure AI FoundationsOracle Cloud Infrastructure AI Foundations
Oracle Cloud Infrastructure AI Foundations
VICTOR MAESTRE RAMIREZ
 
Oracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI ProfessionalOracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI Professional
VICTOR MAESTRE RAMIREZ
 
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdfcnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
AmirStern2
 
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Agentic AI: Beyond the Buzz- LangGraph Studio V2Agentic AI: Beyond the Buzz- LangGraph Studio V2
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Shashikant Jagtap
 
Soulmaite review - Find Real AI soulmate review
Soulmaite review - Find Real AI soulmate reviewSoulmaite review - Find Real AI soulmate review
Soulmaite review - Find Real AI soulmate review
Soulmaite
 
Azure vs AWS Which Cloud Platform Is Best for Your Business in 2025
Azure vs AWS  Which Cloud Platform Is Best for Your Business in 2025Azure vs AWS  Which Cloud Platform Is Best for Your Business in 2025
Azure vs AWS Which Cloud Platform Is Best for Your Business in 2025
Infrassist Technologies Pvt. Ltd.
 
Your startup on AWS - How to architect and maintain a Lean and Mean account J...
Your startup on AWS - How to architect and maintain a Lean and Mean account J...Your startup on AWS - How to architect and maintain a Lean and Mean account J...
Your startup on AWS - How to architect and maintain a Lean and Mean account J...
angelo60207
 
Trends Artificial Intelligence - Mary Meeker
Trends Artificial Intelligence - Mary MeekerTrends Artificial Intelligence - Mary Meeker
Trends Artificial Intelligence - Mary Meeker
Clive Dickens
 
Mastering AI Workflows with FME - Peak of Data & AI 2025
Mastering AI Workflows with FME - Peak of Data & AI 2025Mastering AI Workflows with FME - Peak of Data & AI 2025
Mastering AI Workflows with FME - Peak of Data & AI 2025
Safe Software
 
6th Power Grid Model Meetup - 21 May 2025
6th Power Grid Model Meetup - 21 May 20256th Power Grid Model Meetup - 21 May 2025
6th Power Grid Model Meetup - 21 May 2025
DanBrown980551
 
Jeremy Millul - A Talented Software Developer
Jeremy Millul - A Talented Software DeveloperJeremy Millul - A Talented Software Developer
Jeremy Millul - A Talented Software Developer
Jeremy Millul
 
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
Edge AI and Vision Alliance
 
End-to-end Assurance for SD-WAN & SASE with ThousandEyes
End-to-end Assurance for SD-WAN & SASE with ThousandEyesEnd-to-end Assurance for SD-WAN & SASE with ThousandEyes
End-to-end Assurance for SD-WAN & SASE with ThousandEyes
ThousandEyes
 
Down the Rabbit Hole – Solving 5 Training Roadblocks
Down the Rabbit Hole – Solving 5 Training RoadblocksDown the Rabbit Hole – Solving 5 Training Roadblocks
Down the Rabbit Hole – Solving 5 Training Roadblocks
Rustici Software
 
LSNIF: Locally-Subdivided Neural Intersection Function
LSNIF: Locally-Subdivided Neural Intersection FunctionLSNIF: Locally-Subdivided Neural Intersection Function
LSNIF: Locally-Subdivided Neural Intersection Function
Takahiro Harada
 
Introduction to Internet of things .ppt.
Introduction to Internet of things .ppt.Introduction to Internet of things .ppt.
Introduction to Internet of things .ppt.
hok12341073
 
vertical-cnc-processing-centers-drillteq-v-200-en.pdf
vertical-cnc-processing-centers-drillteq-v-200-en.pdfvertical-cnc-processing-centers-drillteq-v-200-en.pdf
vertical-cnc-processing-centers-drillteq-v-200-en.pdf
AmirStern2
 
Domino IQ – What to Expect, First Steps and Use Cases
Domino IQ – What to Expect, First Steps and Use CasesDomino IQ – What to Expect, First Steps and Use Cases
Domino IQ – What to Expect, First Steps and Use Cases
panagenda
 
What is Oracle EPM A Guide to Oracle EPM Cloud Everything You Need to Know
What is Oracle EPM A Guide to Oracle EPM Cloud Everything You Need to KnowWhat is Oracle EPM A Guide to Oracle EPM Cloud Everything You Need to Know
What is Oracle EPM A Guide to Oracle EPM Cloud Everything You Need to Know
SMACT Works
 
ELNL2025 - Unlocking the Power of Sensitivity Labels - A Comprehensive Guide....
ELNL2025 - Unlocking the Power of Sensitivity Labels - A Comprehensive Guide....ELNL2025 - Unlocking the Power of Sensitivity Labels - A Comprehensive Guide....
ELNL2025 - Unlocking the Power of Sensitivity Labels - A Comprehensive Guide....
Jasper Oosterveld
 
Oracle Cloud Infrastructure AI Foundations
Oracle Cloud Infrastructure AI FoundationsOracle Cloud Infrastructure AI Foundations
Oracle Cloud Infrastructure AI Foundations
VICTOR MAESTRE RAMIREZ
 

Python object oriented programming (lab2) (2)

  • 1. Play with Python Lab2 Object Oriented Programming
  • 2. About Lecture 2 and Lab 2 Lecture 2 and its lab aims at covering Basic Object Oriented Programming concepts Classes, Constructor (__init__) and Objects
  • 3. Example 1 Create a new Project in shp1 = IrregularShape() Aptana "Lab2", then shp1.AddShape(Square(3)) add a new PyDev shp1.AddShape(Triangle(4, 5)) shp1.AddShape(Square(10)) module called main print shp1.Area() Write the following code ======== in your main file: Output: 119.0 class Square: def __init__(self, l): self.length = l def Area(self): return self.length**2 class Triangle: def __init__(self, b, h):
  • 4. Example 1 class Square: def __init__(self, l): self.length = l def Area(self): return self.length**2 class Triangle: def __init__(self, b, h): self.base = b self.height = h def Area(self): return 0.5*self.base*self.height
  • 5. Example 1 class IrregularShape: def __init__(self): self.shapes = [] def AddShape(self, shape): self.shapes.append(shape) def Area(self): area = 0 for shape in self.shapes: area += shape.Area() return area
  • 6. Example 1 shp1 = IrregularShape() shp1.AddShape(Square(3)) shp1.AddShape(Triangle(4, 5)) shp1.AddShape(Square(10)) print shp1.Area() ======== Output: 119.0
  • 7. Exercise 1 (5 Minutes) Add another IrregularShape that has double the area of shp1 in the previous example, ONLY by using shp1 object that you have just created, not using any other shapes
  • 8. Exercise 1 (Solution) shp2 = IrregularShape() shp2.AddShape(shp1) shp2.AddShape(shp1) print shp2.Area() ===== Output: 238.0
  • 9. Example 2 Type this SquareMatrix def addNumber(self, number): resultMatrix = SquareMatrix() class in your main file: matrixDimension = len(self.matrix) for rowIndex in range(0,matrixDimension): #square matrix only newRow = [] class SquareMatrix: for columnIndex in range(0,matrixDimension): def __init__(self): self.matrix = [] newRow.append(self.matrix[rowIndex][columnIndex] + number) def appendRow(self, row): resultMatrix.appendRow(newRow) self.matrix.append(row) return resultMatrix def printMatrix(self): for row in self.matrix: Output: ================================ [-1, 1] print row mat = SquareMatrix() [8, 4] mat.appendRow([0, 2]) mat.appendRow([9, 5]) mat.addNumber(-1)
  • 10. Example 2 #square matrix only class SquareMatrix: def __init__(self): self.matrix = [] def appendRow(self, row): self.matrix.append(row) def printMatrix(self): for row in self.matrix: print row
  • 11. Example 2 def addNumber(self, number): resultMatrix = SquareMatrix() matrixDimension = len(self.matrix) for rowIndex in range(0,matrixDimension): newRow = [] for columnIndex in range(0,matrixDimension): newRow.append(self.matrix[rowIndex][columnIndex] + number) resultMatrix.appendRow(newRow) return resultMatrix ================================ mat = SquareMatrix() Output: mat.appendRow([0, 2]) [-1, 1] [8, 4] mat.appendRow([9, 5]) mat.addNumber(-1) mat.printMatrix()
  • 12. Example 2 This class represents a square matrix (equal row, column dimensions), the add number function matrix data is filled by row, using the appendRow() function addNumber() function adds a number to every element in the matrix
  • 13. Exercise 2 (10 minutes) Add a new fucntion add() that returns a new matrix which is the result of adding this matrix with another matrix: mat1 = SquareMatrix() mat1.appendRow([1, 2]) 1 2 1 -2 2 0 mat1.appendRow([4, 5]) 4 5 -5 1 -1 6 mat2 = SquareMatrix() mat2.appendRow([1, -2]) mat2.appendRow([-5, 1]) mat3 = mat1.add(mat2) mat3.printMatrix()
  • 14. Exercise 2 (Solution) def add(self, otherMatrix): resultMatrix = SquareMatrix() matrixDimension = len(self.matrix) for rowIndex in range(0,matrixDimension): newRow = [] for columnIndex in range(0,matrixDimension): newRow.append(self.matrix[rowIndex][columnIndex] + otherMatrix.matrix[rowIndex][columnIndex]) resultMatrix.appendRow(newRow) return resultMatrix ======================= mat1 = SquareMatrix() Fun: add 3 matrices in one line like this: mat1.appendRow([1, 2]) mat1.add(mat2).add(mat3).printMatrix() mat1.appendRow([4, 5]) mat2 = SquareMatrix() mat2.appendRow([1, -2]) mat2.appendRow([-5, 1])
  • 15. Exercise 3 (10 minutes) Add a new function mutliplyNumber(t), which multiplies a positive integer t to the matrix, ONLY using the add(othermatrix) method 1 -1 5 -5 -1 1 5 -5 5
  • 16. Exercise 3 (Solution) def multiplyNumber(self, times): result = self for i in range(0, times-1): result = result.add(self) return result ========================== mat = SquareMatrix() mat.appendRow([1, -1]) mat.appendRow([-1, 1]) mat.multiplyNumber(5)