What is a negative index in python?



What is a negative index in python?..

Answer / chaitanya

Python arrays & list items can be accessed with positive or negative numbers (also known as index).

For instance our array/list is of size n, then for positive index 0 is the first index, 1 second, last index will be n-1. For negative index, -n is the first index, -(n-1) second, last negative index will be – 1.

A negative index accesses elements from the end of the list counting backwards.

An example to show negative index in python

>>> import array

>>> a= [1, 2, 3]

>>> print a[-3]

1

>>> print a[-2]

2

>>> print a[-1]

3

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More Python Interview Questions

How to create an empty class in python?

0 Answers  


What is a generator in python?

0 Answers  


What is python easy?

0 Answers  


What are the two ways to use python interpreter?

0 Answers  


Tell me what is dict and list comprehensions are?

0 Answers  






Tell me what is the command to debug a python program?

0 Answers  


What are different ways to create an empty numpy array in python?

0 Answers  


Do while loops python?

0 Answers  


Tell me the use of // operator in python?

0 Answers  


What are the rules for local and global variables in Python?

1 Answers  


How is numpy different from scipy?

0 Answers  


Tell me what are generators in python?

0 Answers  


Categories