Class Object Program using multiple methods in Python

class Laptop():
    def __init__(self, brand_name, model_name, price):
        #initializing instance varaibles
        self.bn = brand_name
        self.mn = model_name
        self.p = price

    def apply_disc(self, disc):
        after_disc = self.p - (self.p * (disc/100))
        return after_disc

L1 = Laptop('Lenovo', 'ABCD', 40000)
L2 = Laptop('Dell', 'EFGH', 50000 )
L3 = Laptop('HP', 'IJKL', 60000)

print(L1.apply_disc(20))

# print(L1.bn, L1.mn, L1.p)
# print(L2.bn, L2.mn, L2.p)
# print(L3.bn, L3.mn, L3.p)

Basic Class and Object implementation using Python

  1. The init() method gets called depends upon how many objects we create of that class.
  2. init() method always starts with self keyword according to naming convention. It is not always necessary to write self.
  3. init() is a method as well as a constructor in Python.
class Person:
    def __init__(self, first_name, last_name, age):
        print("Init method / constructor gets called here.")
        self.fn = first_name
        self.ln = last_name
        self.a = age

p1 = Person('Sayan', 'Dey', 21)
p2 = Person('Dey', 'Sayan', 21)
print(p1.fn)
print(p2.ln)

Hotel Booking System using Python

def hotel_booking(arr, dept, room):
    event = [(t, 'RED') for t in arr] + [(t, "BLUE") for t in dept]
    event = sorted(event)
    
    guest = 0
    for e in event:
        if e[1] == 'RED':
            guest+=1
        else:
            guest-=1
        
        if guest > room:
            return 'Sorry the rooms are full'
    return 'Rooms available'

arr = [1, 3, 5]
dept = [2, 6, 8]
k = 1

print(hotel_booking(arr, dept, k))