42. Problem to create a Python class example

In this Python oops program, we will create a Python class example. Python class called Phone with attributes brand, model, and storage. 

Python class example

Steps to solve the program
  1. The Phone 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.
  2. The class also has three methods: make_call, send_text_message, and check_storage_capacity.
  3. The make_call method takes a number as a parameter and prints a message indicating that a call is being made to that number.
  4. The send_text_message method takes a number and a message as parameters and prints a message indicating that a text message is being sent to the specified number with the provided message.
  5. The check_storage_capacity method prints the storage capacity of the phone in gigabytes.
  6. The code creates an object phone of the Phone class by calling its constructor and passing the brand “Apple”, model “iPhone 14”, and storage capacity of 256 as arguments.
  7. The make_call method is called on the phone object to simulate making a call to the number “1234567890”.
  8. The send_text_message method is called on the phone object to simulate sending a text message to the number “1234567890” with the message “Hello!”.
  9. The check_storage_capacity method is called on the phone object to display the storage capacity of the phone.
  10. Thus, we have created a Python class example.
				
					class Phone:
    def __init__(self, brand, model, storage):
        self.brand = brand
        self.model = model
        self.storage = storage
    
    def make_call(self, number):
        print(f"Making a call to {number}")
    
    def send_text_message(self, number, message):
        print(f"Sending a text message to {number}: {message}")
    
    def check_storage_capacity(self):
        print(f"Storage capacity: {self.storage}GB")

# Create an object of the Phone class
phone = Phone("Apple", "iPhone 14", 256)
phone.make_call("1234567890")
phone.send_text_message("1234567890", "Hello!")
phone.check_storage_capacity()
				
			

Output:

				
					Making a call to 1234567890
Sending a text message to 1234567890: Hello!
Storage capacity: 256GB
				
			

Related Articles

Create a Python class called VIPCustomer that inherits from the Customer class.

Create a Python class called Laptop with attributes brand, model, and storage.

Leave a Comment