KEMBAR78
Lecture 6 python oop (ewurc) | PPTX
Python
Lecture 6
Problem Set 6: OOP
Topic
• Object Oriented Programming
Class
class Point:
def __init__(self, x, y):
self.x = int(x)
self.y = int(y)
p = Point(4,4)
print(type(p))
print(f'({p.x}, {p.y})')
p = Point()
print(type(p))
print(f'({p.x}, {p.y})')
Class 2
class Point:
def __init__(self, x, y):
self.x = int(x)
self.y = int(y)
def __init__(self, x=0, y=0):
self.x = int(x)
self.y = int(y)
p = Point()
print(type(p))
print(f'({p.x}, {p.y})')
Class: 3
class Point:
def __init__(self, x=0, y=0):
self.x = int(x)
self.y = int(y)
p = Point()
print(type(p))
print(f'({p.x}, {p.y})')
Class: User Input
class Point:
def __init__(self, x=0, y=0):
self.x = int(x)
self.y = int(y)
x,y = input().split(',')
p = Point(x,y)
print(type(p))
print(f'({p.x}, {p.y})')
Class: print
class Point:
def __init__(self, x=0, y=0):
self.x = int(x)
self.y = int(y)
p = Point(4,2)
print(p)
Class: Overload print
class Point:
def __init__(self, x=0, y=0):
self.x = int(x)
self.y = int(y)
def __str__(self):
return f"({self.x}, {self.y})"
p = Point(4,2)
print(p)
Class: Destructor
class Point:
def __init__(self, x=0, y=0):
self.x = int(x)
self.y = int(y)
def __del__(self):
print("Object Destoryed")
def __str__(self):
return f"({self.x}, {self.y})"
p = Point(4,2)
print(p)
Class: Operator Overloading
class Point:
def __add__(self,other):
sum = Point()
sum.x = self.x + other.x
sum.y = self.y + other.y
return sum
p = Point(4,4)
q = Point(2,2)
r = p + q
print(p)
print(q)
print(r)
Magic Methods: Binary Operators
• https://www.python-course.eu/python3_magic_methods.php
• + __add__(self, other)
• - __sub__(self, other)
• * __mul__(self, other)
• // __floordiv__(self, other)
• / __truediv__(self, other)
• % __mod__(self, other)
• ** __pow__(self, other[, modulo])
• << __lshift__(self, other)
• >> __rshift__(self, other)
• & __and__(self, other)
• ^ __xor__(self, other)
• | __or__(self, other)
Magic Methods: Comparison Operators
• < object.__lt__(self, other)
• <= object.__le__(self, other)
• == object.__eq__(self, other)
• != object.__ne__(self, other)
• >= object.__ge__(self, other)
• > object.__gt__(self, other)
Task
• Distance of two Point
• Is two Point Equal
• Find Slope of two Point
• print(p.slope(q))
Class: Distance
def __sub__(self, other):
r = Point()
r.x = self.x - other.x
r.y = self.y - other.y
return math.sqrt(r.x**2 + r.y**2)
p = Point(4,4)
q = Point(2,2)
dis = p - q
print(dis)
Class: Equal
def __eq__(self, other):
if self.x == other.x:
if self.y == other.y:
return True
return False
p = Point(4,4)
q = Point(3,4)
print(p==q)
p = Point(4,2)
q = Point(4,2)
print(p==q)
Class: Slope 1
def slope(self, other):
r = Point()
r.x = self.x - other.x
r.y = self.y - other.y
m = r.y/r.x
return m
p = Point(4,4)
q = Point(4,2)
print(p.slope(q))
Class: Slope 2
def slope(self, other):
r = Point()
r.x = self.x - other.x
r.y = self.y - other.y
try:
m = r.y/r.x
except:
print("Error")
return None
return m
p = Point(4,4)
q = Point(4,2)
print(p.slope(q))
Class: Slope 3
def slope(self, other):
r = Point()
r.x = self.x - other.x
r.y = self.y - other.y
try:
m = r.y/r.x
except Exception as e:
print("Error: ",e)
return None
return m
p = Point(4,4)
q = Point(4,2)
print(p.slope(q))
Class: Division
def __truediv__(self,divisor):
r = Point()
try:
r.x = self.x /divisor
r.y = self.y /divisor
except Exception as e:
print("Error: ",e)
return None
return r
p = Point(4,4)
q = Point(2,2)
r = p + q
print(r/0)
print(r/3)
Class: Inheritance
#from fileName import Point
from point import Point
class Point3d(Point):
def __init__(self, x=0, y=0, z = 0):
super().__init__(x,y)
self.z = int(z)
p = Point3d(4,4,1)
print(type(p))
Task
• Write a print overload method
• Distance of two Point in 3D
• Is two Point Equal in 3D
Class: Inheritance
def __str__(self):
return f"({self.x}, {self.y}, {self.x})“
p = Point3d(4,4,1)
print(type(p))
print(p)
Class: Inheritance
def __str__(self):
st = super().__str__()
st = st[:-1]
st += f", {self.z})"
return st
p = Point3d(4,4,1)
print(type(p))
print(p)
Any Question?
Prepared by
Mun Al Mamun
President
East West University Robotics Club
munewu@gmail.com
T h i s s l i d e i s p r o v i d e a s a c o u r s e
m a t e r i a l i n t h e w o r k s h o p
“ W o r k s h o p O n P y t h o n P r o g r a m m i n g ”
O r g a n i z e d b y
E a s t W e s t U n i v e r s i t y R o b o t i c s C l u b

Lecture 6 python oop (ewurc)

  • 2.
  • 3.
  • 4.
    Class class Point: def __init__(self,x, y): self.x = int(x) self.y = int(y) p = Point(4,4) print(type(p)) print(f'({p.x}, {p.y})') p = Point() print(type(p)) print(f'({p.x}, {p.y})')
  • 5.
    Class 2 class Point: def__init__(self, x, y): self.x = int(x) self.y = int(y) def __init__(self, x=0, y=0): self.x = int(x) self.y = int(y) p = Point() print(type(p)) print(f'({p.x}, {p.y})')
  • 6.
    Class: 3 class Point: def__init__(self, x=0, y=0): self.x = int(x) self.y = int(y) p = Point() print(type(p)) print(f'({p.x}, {p.y})')
  • 7.
    Class: User Input classPoint: def __init__(self, x=0, y=0): self.x = int(x) self.y = int(y) x,y = input().split(',') p = Point(x,y) print(type(p)) print(f'({p.x}, {p.y})')
  • 8.
    Class: print class Point: def__init__(self, x=0, y=0): self.x = int(x) self.y = int(y) p = Point(4,2) print(p)
  • 9.
    Class: Overload print classPoint: def __init__(self, x=0, y=0): self.x = int(x) self.y = int(y) def __str__(self): return f"({self.x}, {self.y})" p = Point(4,2) print(p)
  • 10.
    Class: Destructor class Point: def__init__(self, x=0, y=0): self.x = int(x) self.y = int(y) def __del__(self): print("Object Destoryed") def __str__(self): return f"({self.x}, {self.y})" p = Point(4,2) print(p)
  • 11.
    Class: Operator Overloading classPoint: def __add__(self,other): sum = Point() sum.x = self.x + other.x sum.y = self.y + other.y return sum p = Point(4,4) q = Point(2,2) r = p + q print(p) print(q) print(r)
  • 12.
    Magic Methods: BinaryOperators • https://www.python-course.eu/python3_magic_methods.php • + __add__(self, other) • - __sub__(self, other) • * __mul__(self, other) • // __floordiv__(self, other) • / __truediv__(self, other) • % __mod__(self, other) • ** __pow__(self, other[, modulo]) • << __lshift__(self, other) • >> __rshift__(self, other) • & __and__(self, other) • ^ __xor__(self, other) • | __or__(self, other)
  • 13.
    Magic Methods: ComparisonOperators • < object.__lt__(self, other) • <= object.__le__(self, other) • == object.__eq__(self, other) • != object.__ne__(self, other) • >= object.__ge__(self, other) • > object.__gt__(self, other)
  • 14.
    Task • Distance oftwo Point • Is two Point Equal • Find Slope of two Point • print(p.slope(q))
  • 15.
    Class: Distance def __sub__(self,other): r = Point() r.x = self.x - other.x r.y = self.y - other.y return math.sqrt(r.x**2 + r.y**2) p = Point(4,4) q = Point(2,2) dis = p - q print(dis)
  • 16.
    Class: Equal def __eq__(self,other): if self.x == other.x: if self.y == other.y: return True return False p = Point(4,4) q = Point(3,4) print(p==q) p = Point(4,2) q = Point(4,2) print(p==q)
  • 17.
    Class: Slope 1 defslope(self, other): r = Point() r.x = self.x - other.x r.y = self.y - other.y m = r.y/r.x return m p = Point(4,4) q = Point(4,2) print(p.slope(q))
  • 18.
    Class: Slope 2 defslope(self, other): r = Point() r.x = self.x - other.x r.y = self.y - other.y try: m = r.y/r.x except: print("Error") return None return m p = Point(4,4) q = Point(4,2) print(p.slope(q))
  • 19.
    Class: Slope 3 defslope(self, other): r = Point() r.x = self.x - other.x r.y = self.y - other.y try: m = r.y/r.x except Exception as e: print("Error: ",e) return None return m p = Point(4,4) q = Point(4,2) print(p.slope(q))
  • 20.
    Class: Division def __truediv__(self,divisor): r= Point() try: r.x = self.x /divisor r.y = self.y /divisor except Exception as e: print("Error: ",e) return None return r p = Point(4,4) q = Point(2,2) r = p + q print(r/0) print(r/3)
  • 21.
    Class: Inheritance #from fileNameimport Point from point import Point class Point3d(Point): def __init__(self, x=0, y=0, z = 0): super().__init__(x,y) self.z = int(z) p = Point3d(4,4,1) print(type(p))
  • 22.
    Task • Write aprint overload method • Distance of two Point in 3D • Is two Point Equal in 3D
  • 23.
    Class: Inheritance def __str__(self): returnf"({self.x}, {self.y}, {self.x})“ p = Point3d(4,4,1) print(type(p)) print(p)
  • 24.
    Class: Inheritance def __str__(self): st= super().__str__() st = st[:-1] st += f", {self.z})" return st p = Point3d(4,4,1) print(type(p)) print(p)
  • 25.
  • 26.
    Prepared by Mun AlMamun President East West University Robotics Club munewu@gmail.com T h i s s l i d e i s p r o v i d e a s a c o u r s e m a t e r i a l i n t h e w o r k s h o p “ W o r k s h o p O n P y t h o n P r o g r a m m i n g ” O r g a n i z e d b y E a s t W e s t U n i v e r s i t y R o b o t i c s C l u b