In this tutorial, we will learn writing python program to create an array (list in case of python) and calculate the sum of elements stored in the array (list).
Calculating the sum of elements in a list is a basic yet essential task in Python programming. It involves adding up all the numbers in a list to get a total. This operation is commonly used in many applications like statistics, data analysis, and everyday programming tasks. Python provides several straightforward methods to accomplish this, ensuring that even beginners can easily understand and implement them. Below, we’ll explore different ways to find the sum of elements in a list, each method catering to different programming styles or situations.
Problem Statement
Our problem statement is like, our program should take the array size from the user and then the elements of the array (list). After taking the input, It will calculate the sum of elements of the input array (list) and print it as output.
For example:
Case 1: if the user inputs 4 as array (list) size and the array (list) elements as 1,2,3,4.
The output should be 10.
Case 2: if the user inputs 5 as array (list) size and the array (list) elements as 9,8,7,6,5.
The output should be 35.
Our logic to find the sum of Array (list) elements
- Our program will take input to fix the size of the array (list).
- Create a variable named ‘sum’ and assign it with value 0.
- Then our program will run for a ‘for loop’ to take the inputs from the user. The inputs will be the elements (or the content) of the array (list). And also adding the elements into the variable sum.
- Print the variable sum as the output
Python Program to find the sum of array (list)
Program 1: Using + Arithmetic Operation
size=int(input("Enter the number of elements you want in array: "))
arr=[]
sum=0
# adding elements to the array (list)
for i in range(0,size):
elem=int(input("Please give value for index "+str(i)+": "))
arr.append(elem)
sum+=elem
print("Sum of list elements = ",sum)
Output:
Please give value for index 0: 10
Please give value for index 1: 20
Please give value for index 2: 30
Please give value for index 3: 40
Please give value for index 4: 50
Sum of list elements = 150
Explanation:
For the input array (list) [10, 20, 30, 40, 50]
value of sum = 23 + 12 + 2 + 4 + 6 = 47.
Program 2: Using the Built-in sum() Function
The simplest and most direct way to calculate the sum of a list in Python is by using the built-in sum()
function. This function takes the list as an argument and returns the total sum of all elements in the list.
numbers = [1, 2, 3, 4, 5]
result = sum(numbers)
print("Sum of list elements :", result)
Output
Sum of list elements : 15
Program 3: Using functools.reduce()
For those interested in functional programming, Python offers the reduce()
function from the functools
module. This function reduces a list of items to a single value by applying a function (in this case, addition) iteratively to the elements of the list.
from functools import reduce
numbers = [1, 2, 3, 4, 5]
result = reduce(lambda x, y: x + y, numbers)
print("Sum of list elements:", result)
Output
Sum of list elements: 15
Each method above described here provides a clear and straightforward approach to calculating the sum of list elements. For all above method we have added the explanations making them understandable for beginners and versatile enough for more advanced Python users.
Hope this posts help you to understand writing python program to calculate the sum of list.
Similar Reads
-
Find middle element of a linked list in single pass
Linked lists are a fundamental data structure used in computer science for organizing and managing data. A linked list is… -
Python Program to Find Last 3rd element in Singly Linked List
In this tutorial, we are going to learn the writing python program to Find 3rd element of Linked List from… -
Most important JavaScript Interview Questions To Prepare
In this Page we have collected and explained Most important Javascript Interview Questions and Answers for begineers, freshers as well… -
Sum of digits of Given Number in Java
In this tutorial we will learn writing Java program to calculate the sum of its digit. We will also see… -
Hibernate Interview Questions for 2+ years of experience
Certainly! Here's a list of commonly asked interview questions on Hibernate for candidates with 2+ years of experience: Basic Hibernate… -
68 Most Important Microservices Interview Questions
Certainly, here's an extended list of 50 commonly asked interview questions on microservices for candidates with 2+ years of experience:… -
60 Most Important Git Interview Questions
Certainly! Here is a list of commonly asked interview questions on Git for candidates with fresher or having of experience… -
50+ Most important Java Interview Questions for 5+ Years Exp
1. Explain the SOLID principles in Java. Provide examples of how you have applied these principles in your projects. SOLID… -
60+ Spring Boot interview questions for 4+ years Exp.
1. What is Spring Boot and how does it differ from the Spring framework? Spring Boot is a framework designed… -
60+ Mostly Asked Spring Boot Interview Questions for 3+ Yrs
Here is a list of 60+ Spring Boot interview questions for candidates with 3+ years of experience: 1. What is…