Add a number in the Python list

Asked 2 years ago, Updated 2 years ago, 14 views

a = [1, 3, 5, 10, 12]
a = [31]

Can you add numbers in the same list like that?

python

2022-09-20 14:24

1 Answers

I think you want this.

a = [1,2,3,4,52,345,3215,12345,1]
b = sum(a)
print(b)
>> 15968

I think it would have been easier to understand if it was "getting the sum of the elements in the list" than "add the numbers in the list." From now on, we recommend adding explanations so that third parties can clearly see exactly what they want from the desired outcome.

# example list
a = [1, 3, 5, 10, 12]
# desired outcome
31
# Description
31 is the sum of the list elements.


2022-09-20 14:24

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.