Why are negative indexes used?

They are used to access locations from last indexes of any array. So lets say you want to print last two items of an array you will write print(A[-2:]) or if you dont want to display last 3 items of the array you would write print(A[:-3]).

SE

Related Python Mentoring answers

All answers ▸

[Student query] Why do we import libraries/modules such as Numpy and Matplotlib into Python?


How does a for loop work in Python?


Print "Hello World!" ten times without typing (or pasting) the print function more than once


Given a list of N numbers, use a single list comprehension to produce a new list that only contains those values that are: (a) even numbers, and (b) from elements in the original list that had even indices .