The list is a data type in Python that can store multiple items of the same or different data types. The list items are written as comma-separated values. And, we can use an index to refer to an item in the list. For example,
numbers = [1, 2, 3, 4, 5]
Here, numbers is a list that stores the numbers 1, 2, 3, 4, and 5. If we try to print numbers[0], it will print 1. numbers[1] refers to 2, and so on.
Similarly, if we write numbers[-1], then the index will refer to the last number of the list which is 5. And, numbers[-2] will refer to the second-last item of the list which is 4 and so on.
numbers = [1, 2, 3, 4, 5] print(numbers[0]) print(numbers[1]) print(numbers[2]) print(numbers[-2]) print(numbers[-1])
The above program will print the following output:
1 2 3 4 5
Similarly, we can write numbers[1:3] to refer to the items [2, 3]. Please note that when we write numbers[1:3], it refers to the list items from positions 1 to 3, excluding position 3.






0 Comments