0

I have a list:

lis = [1, 1, 2, 4, 5, 5, 13, 8, 34, 55, 89, 88, 102]

For this I have the following basics question?

  1. How can I give an expression that sums the last i elements (1<i<5) of the list in the variable lis.

  2. Hot can i give an expression that gives a list with the 3rd - 5th element of this list.

  3. How can I enter an expression that gives a list with the 4th last to 2nd last element: of this list.

It may be that the list can become different Lang.

virat
  • 103
  • 5

1 Answers1

1

Try:

  1. How can I give an expression that sums the last i elements (1<i<5) of the list in the variable list:
>>> sum(l[-5:])
368
  1. How can I give an expression that gives a list with the 3rd - 5th element of this list.
>>> l[2:5]
[2, 4, 5]
  1. How can I enter an expression that gives a list with the 4th last to 2nd last element: of this list.
>>> l[-4:-1]
[55, 89, 88]
Corralien
  • 109,409
  • 8
  • 28
  • 52
  • thx but in the 3 task, shouldn't it be `l[-4:-2]`? because it should give a list with the 4th last to 2nd last element of this list. – virat Dec 09 '21 at 17:23
  • 1
    Sorry I read 4th and 2nd last. So the right answer is `l[-4:-1]` because the right element is not included. – Corralien Dec 09 '21 at 19:05