Program to get the median of list elements

In this program, we will get the median of all the elements from the list.

Steps to solve the program
  1. Take an input list with some values.
  2. Sort list items with the sorted() function.
  3. Get the length of the sorted list.
  4. If the length of the list is an even number then the median of the list will average two mid-values of the list.
  5. If the length of a list is odd then the mid-value of the list will be
    the median value.
				
					input_lst = [4, 45, 23, 21, 22, 12, 2]

# sort all list values
sorted_lst = sorted(input_lst)

# get length of sorted list
n = len(sorted_lst)

if n%2 == 0:
    # get first mid value
    mid1 = sorted_lst[n//2]
    # get second mid value
    mid2 = sorted_lst[n//2 -1]
    # average of mid1 and mid2 will be median value
    median = (mid1 + mid2)/2
    print("Median of given list :", median)
else:
    median = sorted_lst[n//2]
    print("Median of given list :",  median)
				
			

Output :

				
					# input_lst = [4, 45, 23, 21, 22, 12, 2]

Median of given list : 21.5
				
			

Related Articles

48. Problem to create an inheritance example in Python

In this Python oops program, we will create an inheritance example in Python. Python class called Dog that inherits from the Animal class.

Inheritance example in Python

Steps to solve the program
  1. The Dog class extends the Animal class, which means it inherits the properties and methods of the Animal class.
  2. The Dog class has its own constructor method __init__ that takes four parameters: name, color, breed, and weight. It calls the constructor of the Animal class using super() to initialize the name and color properties inherited from the Animal class. It also initializes two additional properties: self.breed to store the breed of the dog and self.weight to store the weight of the dog.
  3. The Dog class overrides the print_details method inherited from the Animal class. It calls the print_details method of the Animal class using super().print_details() to print the name and color of the dog. Then, it prints the additional details: breed and weight.
  4. The code creates an object dog of the Dog class by calling its constructor with the arguments “Jelly”, “Golden”, “Golden Retriever”, and 25.
  5. The print_details method is called on the dog object to print the details of the dog, including the inherited name and color, as well as the breed and weight specific to the Dog class.
  6. Thus, we have created an inheritance example in Python.
				
					class Animal:
    def __init__(self, name, color):
        self.name = name
        self.color = color
    
    def print_details(self):
        print("Name:", self.name)
        print("Color:", self.color)

class Dog(Animal):
    def __init__(self, name, color, breed, weight):
        super().__init__(name, color)
        self.breed = breed
        self.weight = weight
    
    def print_details(self):
        super().print_details()
        print("Breed:", self.breed)
        print("Weight:", self.weight)

# Create an object of the Dog class
dog = Dog("Jelly", "Golden", "Golden Retriever", 25)
dog.print_details()
				
			

Output:

				
					Name: Jelly
Color: Golden
Breed: Golden Retriever
Weight: 25
				
			

Related Articles

Create a Python class called Animal with attributes name and color.

47. Problem to create an inheritance example in Python

In this Python oops program, we will create an inheritance example in Python. Python class called Animal with attributes name and color.

Inheritance example in Python

Steps to solve the program
  1. The Animal class has a constructor method __init__ that takes two parameters: name and color. It initializes two instance variables: self.name to store the name of the animal and self.color to store the color of the animal.
  2. The class also has a print_details method that prints the name and color of the animal.
  3. The code creates an object animal of the Animal class by calling its constructor with the arguments “Lion” and “Golden”.
  4. The print_details method is called on the animal object to print the details of the animal.
  5. Thus, we have creates an inheritance example in Python.
				
					class Animal:
    def __init__(self, name, color):
        self.name = name
        self.color = color
    
    def print_details(self):
        print("Name:", self.name)
        print("Color:", self.color)

# Create an object of the Animal class
animal = Animal("Lion", "Golden")
animal.print_details()
				
			

Output:

				
					Name: Lion
Color: Golden
				
			

Related Articles

Create a Python class called ShoppingCart with attributes items and total_cost.

Create a Python class called Dog that inherits from the Animal class.

46. Problem to create a Python class example

In this Python oops program, we will create a Python class example. Python class called ShoppingCart with attributes items and total_cost.

Python class example

Steps to solve the program
  1. The ShoppingCart class has a constructor method __init__ that initializes two instance variables: self.items as an empty list to store the items in the cart, and self.total_cost as 0 to track the total cost of the items.
  2. The class also has three methods:
  3. The add_item method takes item and cost as parameters and appends the item to the self.items list and adds the cost to the self.total_cost.
  4. The remove_item method takes item and cost as parameters and removes the item from the self.items list if it exists and subtracts the cost from the self.total_cost.
  5. The calculate_total_cost method returns the self.total_cost, representing the total cost of the items in the cart.
  6. The code creates an object cart of the ShoppingCart class by calling its constructor without any arguments.
  7. The add_item method is called three times on the cart object to add three items: “Shirt” with a cost of 250, “Pants” with a cost of 500, and “Shoes” with a cost of 1000.
  8. The remove_item method is called on the cart object to remove the “Shirt” item with a cost of 250.
  9. The calculate_total_cost method is called on the cart object to calculate the total cost of the items in the cart, and the result is assigned to the total_cost variable.
  10. Finally, the contents of the cart are printed using cart.items, and the total cost is printed using total_cost.
  11. Thus, we have created Python class example.
				
					class ShoppingCart:
    def __init__(self):
        self.items = []
        self.total_cost = 0
    
    def add_item(self, item, cost):
        self.items.append(item)
        self.total_cost += cost
    
    def remove_item(self, item, cost):
        if item in self.items:
            self.items.remove(item)
            self.total_cost -= cost
    
    def calculate_total_cost(self):
        return self.total_cost

# Create an object of the ShoppingCart class
cart = ShoppingCart()
cart.add_item("Shirt", 250)
cart.add_item("Pants", 500)
cart.add_item("Shoes", 1000)
cart.remove_item("Shirt", 250)
total_cost = cart.calculate_total_cost()
print("Items in cart:", cart.items)
print("Total Cost:", total_cost)
				
			

Output:

				
					Items in cart: ['Pants', 'Shoes']
Total Cost: 1500
				
			

Related Articles

Create a Python class called EBook that inherits from the Book class.

Create a Python class called Animal with attributes name and color.

45. Problem to create an inheritance example in Python

In this Python oops program, we will create an inheritance example in Python, Python class called EBook that inherits from the Book class.

Inheritance example in Python

Steps to solve the program
  1. The Book class is defined with a constructor method __init__ that takes title, author, and pages as parameters and initializes the instance variables self.title, self.author, and self.pages with the provided values.
  2. The class also has three methods: get_title, get_author, and get_pages.
  3. The get_title method returns the title of the book.
  4. The get_author method returns the author of the book.
  5. The get_pages method returns the number of pages in the book.
  6. The EBook class is defined as a subclass of the Book class. It inherits the attributes and methods of the Book class.
  7. The __init__ method of the EBook class extends the functionality of the Book class’s __init__ method by adding two additional parameters: file_size and format. It initializes the instance variables self.file_size and self.format with the provided values.
  8. The open_book method prints a message indicating the opening of the e-book.
  9. The close_book method prints a message indicating the closing of the e-book.
  10. The code creates an object ebook of the EBook class by calling its constructor and passing the title “Harry Potter”, author “J.K. Rowling”, number of pages 300, file size “10MB”, and format “PDF” as arguments.
  11. The get_title, get_author, and get_pages methods inherited from the Book class are called on the ebook object to retrieve the respective book details, which are then printed.
  12. The file_size and format attributes specific to the EBook class are accessed directly from the ebook object and printed.
  13. The open_book method is called on the ebook object to simulate opening the e-book, and a corresponding message is printed.
  14. The close_book method is called on the ebook object to simulate closing the e-book, and a corresponding message is printed.
  15. Thus, we have created an inheritance example in Python.
				
					class Book:
    def __init__(self, title, author, pages):
        self.title = title
        self.author = author
        self.pages = pages
    
    def get_title(self):
        return self.title
    
    def get_author(self):
        return self.author
    
    def get_pages(self):
        return self.pages



class EBook(Book):
    def __init__(self, title, author, pages, file_size, format):
        super().__init__(title, author, pages)
        self.file_size = file_size
        self.format = format
    
    def open_book(self):
        print("Opening the e-book")
    
    def close_book(self):
        print("Closing the e-book")

# Create an object of the EBook class
ebook = EBook("Harry Potter", "J.K. Rowling", 300, "10MB", "PDF")
print("Title:", ebook.get_title())
print("Author:", ebook.get_author())
print("Pages:", ebook.get_pages())
print("File Size:", ebook.file_size)
print("Format:", ebook.format)
ebook.open_book()
ebook.close_book()
				
			

Output:

				
					Title: Harry Potter
Author: J.K. Rowling
Pages: 300
File Size: 10MB
Format: PDF
Opening the e-book
Closing the e-book
				
			

Related Articles

Create a Python class called Book with attributes title, author, and pages.

Create a Python class called ShoppingCart with attributes items and total_cost.

44. Problem to create a Python class example

In this Python oops program, we will create a Python class example. Python class called Book with attributes title, author, and pages.

Python class example

Steps to solve the program
  1. The Book class is defined with a constructor method __init__ that takes title, author, and pages as parameters and initializes the instance variables self.title, self.author, and self.pages with the provided values.
  2. The class also has three methods: get_title, get_author, and get_pages.
  3. The get_title method returns the title of the book.
  4. The get_author method returns the author of the book.
  5. The get_pages method returns the number of pages in the book.
  6. The code creates an object book of the Book class by calling its constructor and passing the title “Harry Potter”, author “J.K. Rowling”, and number of pages 300 as arguments.
  7. The get_title method is called on the book object to retrieve the title of the book, which is then printed.
  8. The get_author method is called on the book object to retrieve the author of the book, which is then printed.
  9. The get_pages method is called on the book object to retrieve the number of pages in the book, which is then printed.
  10. Thus, we have created a Python class example.
				
					class Book:
    def __init__(self, title, author, pages):
        self.title = title
        self.author = author
        self.pages = pages
    
    def get_title(self):
        return self.title
    
    def get_author(self):
        return self.author
    
    def get_pages(self):
        return self.pages

# Create an object of the Book class
book = Book("Harry Potter", "J.K. Rowling", 300)
print("Title:", book.get_title())
print("Author:", book.get_author())
print("Pages:", book.get_pages())
				
			

Output:

				
					Title: Harry Potter
Author: J.K. Rowling
Pages: 300
				
			

Related Articles

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

Create a Python class called EBook that inherits from the Book class.

43. Problem to create a Python class example

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
  1. 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.
  2. 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.
  3. The shut_down method prints a message indicating that the laptop is shutting down.
  4. The check_storage_capacity method prints the storage capacity of the laptop in gigabytes.
  5. 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.
  6. The start_up method is called on the laptop object to simulate starting up the laptop.
  7. The shut_down method is called on the laptop object to simulate shutting down the laptop.
  8. The check_storage_capacity method is called on the laptop object to display the storage capacity of the laptop.
  9. 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 Phone with attributes brand, model, and storage. 

Create a Python class called Book with attributes title, author, and pages.

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.