ابدأ بالتواصل مع الأشخاص وتبادل معارفك المهنية

أنشئ حسابًا أو سجّل الدخول للانضمام إلى مجتمعك المهني.

متابعة

What is negative index in Python?

user-image
تم إضافة السؤال من قبل Muhammad Kaleem akhtar , Freelancer , Erozgaar Program| Punjab Govt Program
تاريخ النشر: 2019/05/19
Abubakar Yazid
من قبل 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
من قبل 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 .

Harsh Chauhan
من قبل Harsh Chauhan

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

Rishi Setpal
من قبل Rishi Setpal , Mobile Application Developer , Expenzing

Positive Indexing

...

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

               ...-3-2-1

Negative Indexing 

sheik junaid
من قبل sheik junaid

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.

Amena Al Lajami
من قبل Amena Al Lajami , Quality Assurance Engineer , Maids.cc

It's reading data from last to first.

komal Ghauri
من قبل komal Ghauri , Senior Technology Project Manager , House Building Finance Limited

nagative index commanly begin where the arry end

Md Haroon
من قبل Md Haroon , Operational Analyst , SoftwareOne

In Python, negative indexing is a feature that allows you to access elements in a sequence, such as a string, list, or tuple, from the end instead of the beginning. It provides a way to conveniently access elements relative to the end of the sequence without having to know its length.

Negative indexing is achieved by using negative numbers as indices. In Python, the index -1 refers to the last element, -2 refers to the second-to-last element, and so on. Here's an example to illustrate negative indexing:

Example:

  my_list = [1, 2, 3, 4, 5] print(my_list[-1]) # Output: 5 print(my_list[-2]) # Output: 4

In the example above, my_list[-1] returns the last element of the list (5), and my_list[-2] returns the second-to-last element (4).

Negative indexing can be particularly useful when you need to access elements in reverse order or when you don't know the length of the sequence in advance. It provides a convenient way to access elements from the end without explicitly calculating the index based on the length of the sequence.

Asid Mahmood
من قبل 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.

Jolsana Jaimon
من قبل 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

المزيد من الأسئلة المماثلة

هل تحتاج لمساعدة في كتابة سيرة ذاتية تحتوي على الكلمات الدلالية التي يبحث عنها أصحاب العمل؟