How do I index lists in Python in pure Python?

In Python, lists are indexed starting from 0. This means that the first element in a list can be accessed using an index of 0, the second element with an index of 1, and so on. You can also use negative indices to access elements from the end of the list, where -1 refers to the last element, -2 to the second last, and so forth.

Example of List Indexing in Python

# Creating a sample list fruits = ["apple", "banana", "cherry", "date"] # Accessing elements using positive indices print(fruits[0]) # Output: apple print(fruits[2]) # Output: cherry # Accessing elements using negative indices print(fruits[-1]) # Output: date print(fruits[-2]) # Output: cherry

Python List Indexing Python Lists Accessing List Elements