Computer Science, asked by buju63, 3 months ago

Find out the output: sampleList
[10, 20, 30, 40, 50]
print(sampleList[-2]
print(sampleList[-4:-1])​

Answers

Answered by asraful78629
1

print(sampleList[-4:-1]

Answered by dreamrob
2

Given:

sampleList = [10, 20, 30, 40, 50]

print(sampleList[-2])

print(sampleList[-4:-1])

Output:

40

[20, 30, 40]

Explanation:

sampleList:               10     20     30     40     50

Indexing:                   0       1        2        3       4

Negative indexing: -5      -4      -3       -2       -1

At index -2 the element is 40.

Therefore, print(sampleList[-2]) will print 40.

sampleList[starting index : ending index - 1]

sampleList[-4 : -1] means elements will be extracted from index -4 up to index -2.

Therefore, print(sampleList[-4:-1]) will print [20, 30, 40]

Therefore the output is:

40

[20, 30, 40]

Similar questions