Creating a list: You can create a list in Python by enclosing a comma-separated sequence of items in square brackets. For example:
python
Copy code
my_list = [1, 2, 3, 4, 5]
Accessing elements in a list: You can access individual elements in a list using their index. List indexes start at 0, so the first element in a list is at index 0, the second element is at index 1, and so on. For example:
python
Copy code
my_list = [1, 2, 3, 4, 5]
print(my_list[0]) # Output: 1
print(my_list[3]) # Output: 4
Slicing a list: You can slice a list to extract a portion of it. Slicing is done by specifying a start index, an end index (exclusive), and an optional step value. For example:
python
Copy code
my_list = [1, 2, 3, 4, 5]
print(my_list[1:4]) # Output: [2, 3, 4]
print(my_list[0:5:2]) # Output: [1, 3, 5]
Modifying a list: You can modify the items in a list by assigning new values to individual elements. For example:
python
Copy code
my_list = [1, 2, 3, 4, 5]
my_list[2] = 10
print(my_list) # Output: [1, 2, 10, 4, 5]
Adding items to a list: You can add new items to a list using the append() method or the extend() method. The append() method adds a single item to the end of the list, while the extend() method adds multiple items to the end of the list. For example:
python
Copy code
my_list = [1, 2, 3, 4, 5]
my_list.append(6)
print(my_list) # Output: [1, 2, 3, 4, 5, 6]
my_list.extend([7, 8, 9])
print(my_list) # Output: [1, 2, 3, 4, 5, 6, 7, 8, 9]
Removing items from a list: You can remove items from a list using the remove() method or the pop() method. The remove() method removes the first occurrence of a specified item from the list, while the pop() method removes the item at a specified index (or the last item in the list if no index is specified) and returns its value. For example:
python
Copy code
my_list = [1, 2, 3, 4, 5]
my_list.remove(3)
print(my_list) # Output: [1, 2, 4, 5]
popped_item = my_list.pop()
print(popped_item) # Output: 5
print(my_list) # Output: [1, 2, 4]