Python Practice › OOP Programs › Program 43
Laptop class
Create Laptop with brand, model and storage, with methods to start up, shut down and check storage.
Output
Inspiron 15 started True 512 GB Inspiron 15 shut down False
Approach
- Keep an
is_onflag.
Solution
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.