python class gt code example

Example 1: python class

class Person:#set name of class to call it 
  def __init__(self, name, age):#func set ver
    self.name = name#set name
    self.age = age#set age
   

    def myfunc(self):#func inside of class 
      print("Hello my name is " + self.name)# code that the func dose

p1 = Person("barry", 50)# setting a ver fo rthe class 
p1.myfunc() #call the func and whitch ver you want it to be with

Example 2: class python

class Person:
  def __init__(self, name, age):
    self.name = name
    self.age = age

p1 = Person("John", 36)

p1.age = 40

print(p1.age)
---------------------------------------------------------------
40

Example 3: what is __lt__

class Area:        
  def __init__(self, height, width):
    self.height = height
    self.width = width
   def __eq__(self, other):
    if isinstance(other, Area):
      return self.height * self.width == other.height * other.width
    else:
      return False
   def __ne__(self, other):
    return not self == other
  def __lt__(self, other):
    if isinstance(other, Area):
      return self.height * self.width < other.height * other.width
    else:
      return False
   def __gt__(self, other):
    if isinstance(other, Area):
      return self.height * self.width > other.height * other.width
    else:
      return False         
   def __le__(self, other):
    return self == other or self < other        
  def __ge__(self, other):
    return self == other or self > other    
a1 = Area(7, 10)
a2 = Area(35, 2)
a3 = Area(8, 9)
print('Testing ==')
print(a1 == 'hello')
print(a1 == a2)
print(a1 == a3)
print('Testing !=')
print(a1 != 'hello')
print(a1 != a2)
print(a1 != a3)
print('Testing <')
print(a1 < 'hello')
print(a1 < a2)
print(a1 < a3)
print('Testing >')
print(a1 > 'hello')
print(a1 > a2)
print(a1 > a3)
print('Testing <=')
print(a1 <= 'hello')
print(a1 <= a2)
print(a1 <= a3)
print('Testing >=')
print(a1 >= 'hello')
print(a1 >= a2)
print(a1 >= a3)

Example 4: what is __lt__

class Area:        def __init__(self, height, width):        self.height = height        self.width = width        def __eq__(self, other):        if isinstance(other, Area):            return self.height * self.width == other.height *     other.width        else:            return False            def __ne__(self, other):        return not self == other        def __lt__(self, other):        if isinstance(other, Area):            return self.height * self.width < other.height * other.width        else:            return False            def __gt__(self, other):        if isinstance(other, Area):            return self.height * self.width > other.height * other.width        else:            return False         def __le__(self, other):        return self == other or self < other        def __ge__(self, other):        return self == other or self > other    a1 = Area(7, 10)a2 = Area(35, 2)a3 = Area(8, 9)print('Testing ==')print(a1 == 'hello')print(a1 == a2)print(a1 == a3)print('Testing !=')print(a1 != 'hello')print(a1 != a2)print(a1 != a3)print('Testing <')print(a1 < 'hello')print(a1 < a2)print(a1 < a3)print('Testing >')print(a1 > 'hello')print(a1 > a2)print(a1 > a3)print('Testing <=')print(a1 <= 'hello')print(a1 <= a2)print(a1 <= a3)print('Testing >=')print(a1 >= 'hello')print(a1 >= a2)print(a1 >= a3)