The following program demonstrates how to Create and Use a List in Python.
Code Example on How to Create and Use a List in Python
# Create a list
fruits = ["apple", "banana", "cherry", "date"]
# Accessing elements in a list
print("The first fruit in the list is: ", fruits[0])
print("The second fruit in the list is: ", fruits[1])
# Modifying elements in a list
fruits[0] = "mango"
print("The modified list: ", fruits)
# Adding elements to a list
fruits.append("grapes")
print("The modified list after adding an element: ", fruits)
# Removing elements from a list
fruits.remove("banana")
print("The modified list after removing an element: ", fruits)
# Sorting a list
fruits.sort()
print("The sorted list: ", fruits)
# Reversing a list
fruits.reverse()
print("The reversed list: ", fruits)
# Finding the length of a list
print("The length of the list: ", len(fruits))
In this program, we first create a list of fruits and then demonstrate various operations that can be performed on a list, such as accessing elements, modifying elements, adding elements, removing elements, sorting a list, reversing a list, and finding the length of a list.
Further Reading
Examples of OpenCV Library in Python
A Brief Introduction of Pandas Library in Python