To add all the elements in a list together in Python, you can use the sum()
function.
Here's an example:
main.py74 chars4 lines
Alternatively, you can use a loop to add up all the elements in the list manually:
main.py100 chars6 lines
Note that sum()
can be used with other iterable types as well, not just with lists. Additionally, it's worth mentioning that the +
operator can be used to concatenate two lists together, but it doesn't work as a summation operator for lists. To perform arithmetic operations on lists (like summation), you need to use operator overloading.
gistlibby LogSnag