Software Engineering

The right way to Calculate the Sum of a Checklist in Python

The right way to Calculate the Sum of a Checklist in Python
Written by admin


If it’s essential calculate and get the sum of an inventory in Python, then you are able to do the next.

Choice 1 – Utilizing sum()

myList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
listSum = sum(myList)
print(f"Sum of record -> {listSum}")

If you happen to 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 record -> {sum1}")
print(f"Sum of record -> {sum2}")

Choice 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 record -> {listSum}")

About the author

admin

Leave a Comment