SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Tony Gaddis
Chapter:
Lists And Tuples
Exercise:
Checkpoint
Question:5 | ISBN:9780132576376 | Edition: 2

Question

What will the following code display?
numbers = [1, 2, 3, 4, 5]
print(numbers[-2])

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

 

numbers = [1, 2, 3, 4, 5]
print(numbers[-2])

output:
4

  1. As we know as about index,

                         the index of 1 is 0,the index of 2 is 1 and so on....

        2.But in the question, negative slicing had implemented,,that means, indexing is considered from the right to left 

                    -1 means the last element, -2 means the second last element, and so on.

so here,the given index is -2,therefore the negative index of 4 in the list wll be -2

 

1 0

Discussions

Post the discussion to improve the above solution.