Communiquez avec les autres et partagez vos connaissances professionnelles

Inscrivez-vous ou connectez-vous pour rejoindre votre communauté professionnelle.

Suivre

What is negative index in Python?

user-image
Question ajoutée par Muhammad Kaleem akhtar , Freelancer , Erozgaar Program| Punjab Govt Program
Date de publication: 2019/05/19
Abubakar Yazid
par Abubakar Yazid , Project Manager

Negative indexing usually starts where the array ends which is suppoted by python

Negative Index is starting/reading an array inrevrese, from finish to start.

Jamal Farea
par Jamal Farea , Web Developer , freelance

In python the negitive number is used to start slicing from the end for example 

favorite_food = ['rice',chicken','bread']

print(favorite_food[-1])

the result will be : bread .

Rishi Setpal
par Rishi Setpal , Mobile Application Developer , Expenzing

Positive Indexing

...

[0,1,2,3,...,a,b,c]

               ...-3-2-1

Negative Indexing 

It's reading data from last to first.

It is basically accessing index values in reverse order.

for eg: A list stores 5 integers then basically we access it with positive index i.e; list[1] or list[2] etc.

but if we aim to access numbers from back of list then we do it as i.e; list[-1] will give the last element of the list.

Negative indexing works same like general indexing the main difference is it starts from behind/end.
[0,1,2] General
[-2,-1,0] negative index
Thank You

Jolsana Jaimon
par Jolsana Jaimon , Intern , GooseBerry Media

In Python, negative indexing is a feature that allows you to access elements from the end of a sequence, such as a list or a string. The index -1 refers to the last element, -2 refers to the second-to-last element, and soon. For Example:  my_list = [1, 2, 3, 4, 5]

                                                                       last_element = my_list[-1]

                                                                       print(last_element)

                                                                       The Output:5

Asid Mahmood
par Asid Mahmood , Computer Instructor , Prime college of science and commerce

Negative index starting with-1 and positive index start  0.Python supports negative indexing, which typically starts where the array ends.

Shoaib Ahmed
par Shoaib Ahmed , Monitoring & Reporting Officer , Tameer E Khalaq Foundation

Negative indexing in Python refers to accessing elements in a sequence using negative numbers as indices.

Ali Abuhatab
par Ali Abuhatab , Planning Engineer & Site Civil Engineer , Bayt Al Handasah Consultative Co.

n Python, a negative index lets you access elements from the end of a sequence. For example, -1 gives the last element, -2 the second-last, and so on. This is useful for quickly referencing elements from the sequence's end.

TRANSLATE with x English Arabic Hebrew Polish Bulgarian Hindi Portuguese Catalan Hmong Daw Romanian Chinese Simplified Hungarian Russian Chinese Traditional Indonesian Slovak Czech Italian Slovenian Danish Japanese Spanish Dutch Klingon Swedish English Korean Thai Estonian Latvian Turkish Finnish Lithuanian Ukrainian French Malay Urdu German Maltese Vietnamese Greek Norwegian Welsh Haitian Creole Persian     TRANSLATE with COPY THE URL BELOW Back EMBED THE SNIPPET BELOW IN YOUR SITE Enable collaborative features and customize widget: Bing Webmaster Portal Back

More Questions Like This