Because you are passing only three arguments, my example below should help you understand:
>>> [1,2,3][-1] # working
3
>>> [1,2,3][-2] # working
2
>>> [1,2,3][-3] # working
1
>>> [1,2,3][-4] # exception as in your code
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list index out of range
Negative index prints a value from the right hand side.
Accessing Lists
For instance our array/list is of size n, then for positive indices 0
is the first index, 1
the second and the last index will be n-1
. For negative indices, -n
is the first index, -(n-1)
second, last negative index will be –1
.
On the basis of your comment, I am adding an example for clarification:
import sys
# main()
if __name__ == "__main__":
print len(sys.argv)
print sys.argv[-1], sys.argv[-2], sys.argv[-3]
print sys.argv[0], sys.argv[1], sys.argv[2]
Please observe the output:
$ python main.py one two
3
two one main.py
main.py one two
The number of arguments passed is three. argv[-1]
is the last argument, which is two