Python Practice › OOP Programs › Program 43

Laptop class

EasyOOP Programs · 43 of 48

Create Laptop with brand, model and storage, with methods to start up, shut down and check storage.

Advertisement
Output
Inspiron 15 started True
512 GB
Inspiron 15 shut down False

Approach

  1. Keep an is_on flag.

Solution

Python
class Laptop:    def __init__(self, brand, model, storage_gb):        self.brand, self.model, self.storage_gb = brand, model, storage_gb        self.is_on = False     def start(self):        self.is_on = True        return f"{self.model} started"     def shutdown(self):        self.is_on = False        return f"{self.model} shut down"     def storage(self):        return f"{self.storage_gb} GB" lp = Laptop("Dell", "Inspiron 15", 512)print(lp.start(), lp.is_on)print(lp.storage())print(lp.shutdown(), lp.is_on)
Output
Inspiron 15 started True
512 GB
Inspiron 15 shut down False

Learn the concept

New to this topic? Read the lesson, then come back.

More oop programs

← All 48 OOP Programs

Leave a Comment