class Bag:
def __init__(self):
self.data = [] # Initialize an empty list to store elements
def add(self, item):
self.data.append(item) # Add item to the list
def get_current_size(self):
return len(self.data) # Return the current number of elements
def is_full(self, max_size):
return len(self.data) >= max_size # Check if the Bag is full
def is_empty(self):
return len(self.data) == 0 # Check if the Bag is empty
def __str__(self):
return "Bag contents: " + ", ".join(map(str, self.data)) # Convert list elements to strings and join them with commas
bag = Bag()
bag.add(5)
bag.add(10)
bag.add(5)
bag.add(20)
print(bag)
# Print the current size, and check if the Bag is full or empty
max_size = 50
print("Current size:", bag.get_current_size())
print("Is full:", "Yes" if bag.is_full(max_size) else "No")
print("Is empty:", "Yes" if bag.is_empty() else "No")
To embed this project on your website, copy the following code and paste it into your website's HTML: