find the sum of a 5 number list in python

To find the sum of a 5 number list in Python, you can use the sum() function. Here's an example code block that demonstrates this:

main.py
my_list = [2, 4, 6, 8, 10]     # Define the list with 5 numbers
total = sum(my_list)            # Use the sum() function to calculate the total
print("The sum of the list is:", total)   # Print the total
204 chars
4 lines

Output:

main.py
The sum of the list is: 30
27 chars
2 lines

Alternatively, you can use a loop to iterate over the list and add up the values:

main.py
my_list = [2, 4, 6, 8, 10]     # Define the list with 5 numbers
total = 0                       # Initialize a total variable to 0
for num in my_list:             # Loop through the list
    total += num                # Add each number to the total
print("The sum of the list is:", total)   # Print the total
310 chars
6 lines

Output:

main.py
The sum of the list is: 30
27 chars
2 lines

related categories

gistlibby LogSnag