
Python List provides different methods to add items to a list. Note: When we slice lists, the start index is inclusive but the end index is exclusive. my_list returns a list with items from index 5 to the end.my_list returns a list with items from index 2 to index 4.

In Python it is possible to access a section of items from the list using the slicing operator :, not just a single item. Note: If the specified index does not exist in the list, Python throws the Inde圎rror exception. Print(languages) # Python Python Negative Indexing The index of -1 refers to the last item, -2 to the second last item and so on. Python allows negative indexing for its sequences. Hence, the first element of a list is present at index 0, not 1. Note: The list index always starts with 0. And, we have used the index number to access the items. Here, we can see each list item is associated with the index number. In the above example, we have created a list named languages. We can access elements of an array using the index number (0, 1, 2 …). In Python, each item in a list is associated with a number. Here, we have created a list named numbers with 3 integer items.Ī list can have any number of items and they may be of different types (integer, float, string, etc.). Instead of creating 5 separate variables, we can simply create a list: Lists ElementsĪ list is created in Python by placing items inside, separated by commas.

Suppose we need to record the ages of 5 students. In Python, lists are used to store multiple data at once.
