In this Python oops program, we will create a Python class example. Python class called Laptop with attributes brand, model, and storage.
Python class example
Steps to solve the program
- The Laptop class is defined with a constructor method __init__ that takes brand, model, and storage as parameters and initializes the instance variables self.brand, self.model, and self.storage with the provided values.
- The class also has three methods: start_up, shut_down, and check_storage_capacity.
The start_up method simply prints a message indicating that the laptop is starting up. - The shut_down method prints a message indicating that the laptop is shutting down.
- The check_storage_capacity method prints the storage capacity of the laptop in gigabytes.
- The code creates an object laptop of the Laptop class by calling its constructor and passing the brand “Dell”, model “XPS 13”, and storage capacity of 1000 as arguments.
- The start_up method is called on the laptop object to simulate starting up the laptop.
- The shut_down method is called on the laptop object to simulate shutting down the laptop.
- The check_storage_capacity method is called on the laptop object to display the storage capacity of the laptop.
- Thus, we have created a Python class example.
class Laptop:
def __init__(self, brand, model, storage):
self.brand = brand
self.model = model
self.storage = storage
def start_up(self):
print("Starting up the laptop")
def shut_down(self):
print("Shutting down the laptop")
def check_storage_capacity(self):
print(f"Storage capacity: {self.storage}GB")
# Create an object of the Laptop class
laptop = Laptop("Dell", "XPS 13", 1000)
laptop.start_up()
laptop.shut_down()
laptop.check_storage_capacity()
Output:
Starting up the laptop
Shutting down the laptop
Storage capacity: 1000GB
Related Articles
Create a Python class called Book with attributes title, author, and pages.
Create a Python class called EBook that inherits from the Book class.
Create a Python class called ShoppingCart with attributes items and total_cost.
Create a Python class called Animal with attributes name and color.
Create a Python class called Dog that inherits from the Animal class.
Python oops program to create a class with the constructor.