2.6.3.3 Indexing and Slicing with Negative Indices
Python Programming
Negative indices count from the end of the sequence. For example, -1 refers to the last element, -2 refers to the second last element, and so on.
Example: Indexing and slicing with both positive and negative indices on a list.
python
# Create a list of fruits
fruits = ["Apple", "Banana", "Cherry", "Date", "Elderberry " ]
# Indexing
print("First fruit:", fruits [0]) # Positive index
print("Last fruit:", fruits [-1]) # Negative index
# Slicing with positive indices
print("Fruits from index 1 to 3:",