-4

Eg: list = 1, 2, 3… 10

I am trying to solve a question in my course in as simple a way as possible. Sorry if there is already an answer to this question. I couldn't find anything.

Jett
  • 3
  • 1
  • 2

3 Answers3

2

if a is the list the you can do:

print sum(a[:3])

You can go through the sum() function of python.

Moreover, a[:3] is a slicing operation.

Rahil Hastu
  • 558
  • 2
  • 13
2

Just use slicing:

mylist = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
result = sum(mylist[:3])

Here, you can easily change 3 to some other number.

See:

iz_
  • 15,923
  • 3
  • 25
  • 40
0

The sum function sums numbers in an iterable and is built into Python.

Thus for your situation you can just slice the list and use it.

 numbers = [2.5, 3, 4, -5]

    numbersSum = sum(numbers[:3])
    print(numbersSum)

result is 2.5+3+4=9.5

jsky
  • 2,225
  • 5
  • 38
  • 54