CSC-23F-077(CS3B)ASS#01(AI)
CSC-23F-077(CS3B)ASS#01(AI)
List:
• Mutable, meaning you can modify (add, remove, or change) elements.
• Defined with square brackets [].
• Slower compared to tuples in some operations due to mutability.
• Used when the collection of items needs to be changed.
Code:
# Defining a List
my_list = [1, 2, 3, 4]
my_list[2] = 10
my_list.append(5)
Output:
Original List: [1, 2, 3, 4]
Code:
# Defining a Tuple
my_tuple = (1, 2, 3, 4)
print("Error:", e)
# Tuples are immutable, so you cannot add or change items.
Output:
Original Tuple: (1, 2, 3, 4)