Computer Science, asked by piyushh1051976, 2 months ago

if L=[21,32,43,45,34,34,45] then
print (L[-4:-2]) will print:​

Answers

Answered by indiantechsmith
1

Output will be :

[45,34]

Answered by anindyaadhikari13
1

Answer:

Given list,

>> L = [21, 32, 43, 45, 34, 34, 45]

>> print(L{-4:-2])

>  [45, 34]

Explanation:

Python supports negative indexing. Let us write the elements of the list with their index (negative index).

\dagger \:\boxed{\begin{array}{c|c|c|c|c|c|c|c}\sf Item: &\sf21& \sf 32 & \sf43 & \sf45 & \sf34 & \sf34 &\sf 45\\\sf Index: & \sf-7 & \sf-6 & \sf-5 &\sf-4 &\sf-3 & \sf-2 &-1\\\end{array}}

From the table,

>> L[-1] = 45, L[-2] = 34 and so on.

So, L[-4 : -2] returns the elements of the list in the index range -4 to -3  (-2 is excluded). Step value is not mentioned. So, by default, step value = 1

Elements of the list in this range are - 45 and 34. So,

>> L[-4 : -2] = [45, 34]

Refer to the attachment.

•••♪

Attachments:
Similar questions