If you’ll want to calculate and get the sum of a listing in Python, then you are able to do the next.
Possibility 1 – Utilizing sum()
myList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
listSum = sum(myList)
print(f"Sum of checklist -> {listSum}")
In case you get a TypeError
then you are able to do the next:
myList = ["1", "3", "5", "7", "9"]
myNewList = [int(string) for string in myList]
sum1 = sum(myNewList)
sum2 = sum(quantity for quantity in myNewList)
print(f"Sum of checklist -> {sum1}")
print(f"Sum of checklist -> {sum2}")
Possibility 2 – Utilizing for
myList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
size = len(myList)
listSum = 0
for i in vary(size):
listSum += myList[i]
print(f"Sum of checklist -> {listSum}")