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) print("The second fruit in the list is: ", fruits) # Modifying elements in a list fruits = "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.
- Dot Net Framework
- Power Bi
- Scratch 3.0