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.

41. Problem to create an inheritance example using Python

In this Python oops program, we will create an inheritance example using Python. Python class called VIPCustomer that inherits from the Customer class.

Inheritance example using Python

Steps to solve the program
  1. The Customer class is defined with a constructor method __init__ that takes name and balance as parameters and initializes the instance variables self.name and self.balance with the provided values.
  2. The class also has two methods, deposit and withdraw, to perform deposit and withdrawal operations on the customer’s account.
  3. The deposit method takes an amount as a parameter and adds it to the balance of the customer.
  4. The withdraw method takes an amount as a parameter and checks if the requested amount is less than or equal to the current balance. If so, it subtracts the amount from the balance. Otherwise, it prints “Insufficient balance”.
  5. The VIPCustomer class is defined as a subclass of Customer. It extends the functionality of Customer and adds additional attributes and methods specific to VIP customers.
  6. The __init__ method of VIPCustomer takes name, balance, credit_limit, and discount_rate as parameters. It calls the superclass Customer’s __init__ method using super() to initialize the name and balance attributes.
  7. Additionally, the VIPCustomer class introduces two new attributes, credit_limit and discount_rate, specific to VIP customers.
  8. The calculate_available_credit method calculates the available credit for the VIP customer by subtracting the current balance from the credit limit.
  9. The code creates an object vip_customer of the VIPCustomer class by calling its constructor and passing the name “John Doe”, an initial balance of 5000, a credit limit of 10000, and a discount rate of 0.1 as arguments.
  10. The calculate_available_credit method is called on the vip_customer object to determine the available credit.
  11. Finally, the available credit is printed using the print statement.
  12. Thus, we have created an inheritance example using Python.
				
					class Customer:
    def __init__(self, name, balance):
        self.name = name
        self.balance = balance
    
    def deposit(self, amount):
        self.balance += amount
    
    def withdraw(self, amount):
        if amount <= self.balance:
            self.balance -= amount
        else:
            print("Insufficient balance")



class VIPCustomer(Customer):
    def __init__(self, name, balance, credit_limit, discount_rate):
        super().__init__(name, balance)
        self.credit_limit = credit_limit
        self.discount_rate = discount_rate
    
    def calculate_available_credit(self):
        available_credit = self.credit_limit - self.balance
        return available_credit

# Create an object of the VIPCustomer class
vip_customer = VIPCustomer("John Doe", 5000, 10000, 0.1)
available_credit = vip_customer.calculate_available_credit()
print("Available Credit:", available_credit)

				
			

Output:

				
					Available Credit: 5000
				
			

Related Articles

Create a Python class called Customer with attributes name and balance.

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

40. Problem to create a customer class in Python

In this Python oops program, we will create a customer class in Python. Class called Customer with attributes name and balance.

Customer class in Python

Steps to solve the program
  1. The Customer class is defined with a constructor method __init__ that takes name and balance as parameters and initializes the instance variables self.name and self.balance with the provided values.
  2. The class also has two methods, deposit and withdraw, to perform deposit and withdrawal operations on the customer’s account.
  3. The deposit method takes an amount as a parameter and adds it to the balance of the customer.
  4. The withdraw method takes an amount as a parameter and checks if the requested amount is less than or equal to the current balance. If so, it subtracts the amount from the balance. Otherwise, it prints “Insufficient balance”.
  5. The code creates an object customer of the Customer class by calling its constructor and passing the name “Jason Roy” and an initial balance of 1000 as arguments.
  6. The deposit method is called on the customer object, which adds 500 to the current balance.
  7. The withdraw method is then called on the customer object, which subtracts 200 from the current balance.
  8. Finally, the remaining balance is printed using the print statement.
  9. Thus, we have create a customer class in Python.
				
					class Customer:
    def __init__(self, name, balance):
        self.name = name
        self.balance = balance
    
    def deposit(self, amount):
        self.balance += amount
    
    def withdraw(self, amount):
        if amount <= self.balance:
            self.balance -= amount
        else:
            print("Insufficient balance")

# Create an object of the Customer class
customer = Customer("Jason Roy", 1000)
customer.deposit(500)
customer.withdraw(200)
print("Balance:", customer.balance)

				
			

Output:

				
					Balance: 1300
				
			

Related Articles

Create a Python class called Manager that inherits from the Employee class.

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

39. Problem to create a single inheritance example in Python

In this Python oops program, we will create an single inheritance example in Python. Class called Manager that inherits from the Employee class.

Single inheritance example in Python

Steps to solve the program
  1. The Employee class is defined with a constructor method __init__ that takes name and salary as parameters and initializes the instance variables self.name and self.salary with the provided values.
  2. The class also has a method print_details that prints the employee’s name and salary.
  3. The Manager class is defined as a subclass of Employee class, inheriting its attributes and methods.
  4. The Manager class has an additional constructor method __init__ that takes name, salary, department, and bonus as parameters. It calls the superclass’s __init__ method using super() to initialize the name and salary attributes inherited from the Employee class, and initializes the department and bonus attributes with the provided values.
  5. The Manager class also has a method calculate_total_compensation that calculates the total compensation of the manager by adding their salary and bonus.
  6. The code creates an object manager of the Manager class by calling its constructor and passing the name “John Walker”, salary 60000, department “Sales”, and bonus 5000 as arguments.
  7. The calculate_total_compensation method is called on the manager object, which calculates the total compensation by adding the salary and bonus.
  8. The total compensation is then printed using the print statement.
  9. Thus, we have created a single inheritance example in Python.
				
					class Employee:
    def __init__(self, name, salary):
        self.name = name
        self.salary = salary
    
    def print_details(self):
        print("Name:", self.name)
        print("Salary:", self.salary)

class Manager(Employee):
    def __init__(self, name, salary, department, bonus):
        super().__init__(name, salary)
        self.department = department
        self.bonus = bonus
    
    def calculate_total_compensation(self):
        total_compensation = self.salary + self.bonus
        return total_compensation

# Create an object of the Manager class
manager = Manager("John Walker", 60000, "Sales", 5000)
total_compensation = manager.calculate_total_compensation()
print("Total Compensation:", total_compensation)
				
			

Output:

				
					Total Compensation: 65000
				
			

Related Articles

Create a Python class called Employee with attributes name and salary.

Create a Python class called Customer with attributes name and balance.

38. Problem to create employee class in Python

In this Python oops program, we will create employee class in Python. Include a method to print the employee’s name and salary.

Create Employee class in Python

Steps to solve the program
  1. The Employee class is defined with a constructor method __init__ that takes name and salary as parameters and initializes the instance variables self.name and self.salary with the provided values.
  2. The class also has a method print_details that prints the employee’s name and salary.
  3. The code creates an object employee of the Employee class by calling its constructor and passing the name “John Walker” and salary 50000 as arguments.
  4. The print_details method is called on the employee object, which prints the employee’s name and salary.
				
					class Employee:
    def __init__(self, name, salary):
        self.name = name
        self.salary = salary
    
    def print_details(self):
        print("Name:", self.name)
        print("Salary:", self.salary)

# Create an object of the Employee class
employee = Employee("John Walker", 50000)
employee.print_details()
				
			

Output:

				
					Name: John Walker
Salary: 50000
				
			

Related Articles

Create a Python class called Shape with a method to calculate the area of the shape.

Create a Python class called Manager that inherits from the Employee class.

37. Problem to create a shape class in Python

In this Python oops program, we will create a shape class in Python. Python class called Shape with a method to calculate the area of the shape.

Create a shape class in Python

Steps to solve the program
  1. The Shape class in Python is defined as a base class. It has a method calculate_area, which is left undefined (implemented as pass). This method will be overridden in the derived classes.
  2. The Square class is derived from the Shape class. It has a constructor method __init__ that takes the side_length as a parameter and initializes the instance variable self.side_length.
  3. The class overrides the calculate_area method inherited from the Shape class. It calculates the area of a square by multiplying the side_length by itself (side_length ** 2).
  4. The Triangle class is derived from the Shape class. It has a constructor method __init__ that takes the base and height as parameters and initializes the instance variables self.base and self.height.
  5. The class overrides the calculate_area method inherited from the Shape class. It calculates the area of a triangle using the formula 0.5 * base * height.
  6. The code creates objects square and triangle of the Square and Triangle classes, respectively, by passing the required parameters to their constructors.
  7. The calculate_area method is called on each object, and the calculated areas are printed.
				
					class Shape:
    def calculate_area(self):
        pass

class Square(Shape):
    def __init__(self, side_length):
        self.side_length = side_length
    
    def calculate_area(self):
        return self.side_length ** 2

class Triangle(Shape):
    def __init__(self, base, height):
        self.base = base
        self.height = height
    
    def calculate_area(self):
        return 0.5 * self.base * self.height

# Create objects of the Square and Triangle classes
square = Square(5)
triangle = Triangle(4, 6)
print("Square Area:", square.calculate_area())
print("Triangle Area:", triangle.calculate_area())
				
			

Output:

				
					Square Area: 25
Triangle Area: 12.0
				
			

Related Articles

Create a Python class called Course with attributes name, teacher, and students.

Create a Python class called Employee with attributes name and salary.

36. Problem to create a Python class real world example

In this Python oops program, we will create a Python class real world example. Python class called Course with attributes name, teacher, and students.

Python class real world example

Steps to solve the program
  1. The Course class has a constructor method __init__ that takes two parameters: name and teacher. It initializes the instance variables self.name, self.teacher, and self.students. The self.students variable is initialized as an empty list.
  2. The class also has three methods:
    The add_student method appends a student to the self.students list.
    The remove_student method removes a student from the self.students list if the student is present.
    The print_details method prints the course name, teacher’s name, and the list of students enrolled in the course.
  3. The code creates an object course of the Course class with the name “Python” and the teacher’s name “Mr. Dipesh Yadav”.
  4. Two students, “John Walker” and “Jade Smith”, are added to the course using the add_student method.
  5. The print_details method is called on the course object, which prints the course details, including the course name, teacher’s name, and the list of enrolled students.
  6. The remove_student method is called to remove the student “Jade Smith” from the course.
  7. The print_details method is called again to print the updated course details.
  8. Thus, we have created a Python class real world example.
				
					class Course:
    def __init__(self, name, teacher):
        self.name = name
        self.teacher = teacher
        self.students = []
    
    def add_student(self, student):
        self.students.append(student)
    
    def remove_student(self, student):
        if student in self.students:
            self.students.remove(student)
    
    def print_details(self):
        print("Course Name:", self.name)
        print("Teacher:", self.teacher)
        print("Students:")
        for student in self.students:
            print("- ", student)

# Create an object of the Course class
course = Course("Python", "Mr. Dipesh Yadav")
course.add_student("John walker")
course.add_student("Jade Smith")
course.print_details()
course.remove_student("Jade Smith")
course.print_details()

				
			

Output:

				
					Course Name: Python
Teacher: Mr. Dipesh Yadav
Students:
-  John walker
-  Jade Smith
Course Name: Python
Teacher: Mr. Dipesh Yadav
Students:
-  John walker
				
			

Related Articles

Create a Python class called StudentRecord with attributes name, age, and grades.

Create a Python class called Shape with a method to calculate the area of the shape.

35. Problem to create a Python class example

In this Python oops program, we will create a Python class example. Python class called StudentRecord with attributes name, age, and grades.

Python class example

Steps to solve the program
  1. The StudentRecord class has a constructor method __init__ that takes three parameters: name, age, and grades. It initializes the instance variables self.name, self.age, and self.grades with the provided values.
  2. The class also has a method called calculate_average_grade which calculates the average grade of the student. It sums up all the grades in the self.grades list, divides it by the number of grades, and returns the average grade.
  3. The print_details method prints the student’s name, age, and average grade by calling the calculate_average_grade method.
  4. The code creates an object student of the StudentRecord class with the name “John Snow”, age 20, and a list of grades [80, 90, 85, 95].
  5. The print_details method is called on the student object, which prints the student’s details, including their name, age, and average grade.
  6. Thus, we have created a Python class example.
				
					class StudentRecord:
    def __init__(self, name, age, grades):
        self.name = name
        self.age = age
        self.grades = grades
    
    def calculate_average_grade(self):
        total_grades = sum(self.grades)
        average_grade = total_grades / len(self.grades)
        return average_grade
    
    def print_details(self):
        print("Name:", self.name)
        print("Age:", self.age)
        print("Average Grade:", self.calculate_average_grade())

# Create an object of the StudentRecord class
student = StudentRecord("John show", 20, [80, 90, 85, 95])
student.print_details()
				
			

Output:

				
					Name: John show
Age: 20
Average Grade: 87.5
				
			

Related Articles

Create a Python class called ElectricCar that inherits from the Car class.

Create a Python class called Course with attributes name, teacher, and students.

34. Problem to create an inheritance real life example in Python

In this Python oops program, we will create an inheritance real life example in Python. Python class called ElectricCar that inherits from the Car class.

Inheritance real life example 

Steps to solve the program
  1. The Car class represents a general car and has a constructor method __init__ that takes three parameters: make, model, and year. It initializes the instance variables self.make, self.model, and self.year with the provided values.
  2. The class also has a print_details method that prints the make, model, and year of the car.
  3. The ElectricCar class is a subclass of Car and represents an electric car. It extends the Car class by adding two additional attributes: battery_size and range_per_charge.
  4. The __init__ method of the ElectricCar class takes the same parameters as the Car class, as well as the new attributes specific to electric cars. It calls the superclass’s (Car) __init__ method using super() to initialize the inherited attributes.
  5. The calculate_range method calculates the range of the electric car based on the battery size and range per charge.
  6. The code creates an object electric_car of the ElectricCar class with the make “Tesla”, model “Model S”, year 2022, battery size 75, and range per charge 5.
  7. The calculate_range method is called on the electric_car object, which calculates the range of the electric car.
  8. The calculated range is stored in the variable range and printed.
  9. Thus, we have created an inheritance real life example.
				
					class Car:
    def __init__(self, make, model, year):
        self.make = make
        self.model = model
        self.year = year
    
    def print_details(self):
        print("Make:", self.make)
        print("Model:", self.model)
        print("Year:", self.year)
        

class ElectricCar(Car):
    def __init__(self, make, model, year, battery_size, range_per_charge):
        super().__init__(make, model, year)
        self.battery_size = battery_size
        self.range_per_charge = range_per_charge
    
    def calculate_range(self):
        return self.battery_size * self.range_per_charge

# Create an object of the ElectricCar class
electric_car = ElectricCar("Tesla", "Model S", 2022, 75, 5)
range = electric_car.calculate_range()
print("Range:", range)

				
			

Output:

				
					Range: 375
				
			

Related Articles

Create a Python class called Car with attributes make, model, and year.

Create a Python class called StudentRecord with attributes name, age, and grades.

33. Problem to create an OOPS example in Python

In this Python oops program, we will create an OOPS example in Python. Python class called Car with attributes make, model, and year.

OOPS example in Python

Steps to solve the program
  1. The Car class has a constructor method __init__ that takes three parameters: make, model, and year. It initializes the instance variables self.make, self.model, and self.year with the provided values.
  2. The print_details method prints the details of the car, including the make, model, and year. It retrieves the values of the instance variables and prints them with descriptive labels.
  3. The code creates an object car of the Car class with the make “Mahindra”, model “Thar”, and year 2021.
    The print_details method is called on the car object, which prints the details of the car.

				
					class Car:
    def __init__(self, make, model, year):
        self.make = make
        self.model = model
        self.year = year
    
    def print_details(self):
        print("Make:", self.make)
        print("Model:", self.model)
        print("Year:", self.year)

# Create an object of the Car class
car = Car("Mahindra", "Thar", 2021)
car.print_details()

				
			

Output:

				
					Make: Mahindra
Model: Thar
Year: 2021
				
			

Related Articles

Create a Python class called CheckingAccount that inherits from the BankAccount class.

Create a Python class called ElectricCar that inherits from the Car class.