how to add numbers from a list in python code example
Example 1: how to calculate the sum of a list in python
# Python code to demonstrate the working of
# sum()
numbers = [1,2,3,4,5,1,4,5]
# start parameter is not provided
Sum = sum(numbers)
print(Sum) # result is 25
# start = 10
Sum = sum(numbers, 10)
print(Sum) # result is 10 +25 = 35
Example 2: python append n numbers to list
# Basic syntax:
your_list.extend([element]*number)
# Where:
# - element is the element you want to add to your_list
# - number is the number of times you want it repeated
# Note, .extend modifies your_list in place
# Example usage:
your_list = [1, 2, 3]
your_list.extend([0]*5)
print(your_list)
--> [1, 2, 3, 0, 0, 0, 0, 0]
Example 3: how to add list numbers in python
lst = []
num = int(input('How many numbers: '))
for n in range(num):
numbers = int(input('Enter number '))
lst.append(numbers)
print("Sum of elements in given list is :", sum(lst))