×

Python Program to Print Sum of all Elements in an Array

Python program to print sum of all elements in an array

A set of objects stored in contiguous memory locations is referred to as an array. The concept is to keep objects of the same type together. In this example, we'll look at a Python program that prints the sum of all the elements in an array. We can get the sum of all array elements in several ways, and some of them are given as follows:

  1. Traverse/access each element, add the elements in a variable sum and then print the sum.
  2. Using the sum() function to get the sum of array elements.
Example:  
Given array = [4, 8, 2]  
Output: 4+8+2 = 14

Source Code

The following source code uses the "for loop" that traverses/access each element and adds them in a variable sum and then prints the sum.

 #Python program to find the sum of all elements in an array   
 #Initialize an array   
 arr1 = [2, 7, 8, 9, 10, 22, 25]  
 #Initialize the variable to store the sum      
 sum = 0 
 #Iterates through an array to add each element of an array   
 for i in range(0, len(arr1)):   
       sum = sum + arr1[i]    
 print ("Sum of the array is ", sum) 

Output

Python program to print sum of all elements in an array

Explanation: In this program, we have first initialized and declare an array. Also, we have declared a temporary variable and initialized its value to 0. Next, we will use the FOR loop to iterate the array elements from 0 to the array length. After each iteration, the sum's value will be increased by the ith element of an array, and when the iterations are completed, the final result will be printed.

The following source code uses the inbuilt function sum() to find the sum of all the elements of an array and their results on the screen:

 #Python program to find the sum of all elements in an array  
 #Initialize an array  
 arr1 = [2, 7, 8, 9, 10, 22, 25]  
 #sum() function will return the value after adding all the elements     
 result = sum(arr1)    
 #Print the result    
 print("The sum of an array is ", result)    

Output

Python program to print sum of all elements in an array

Related Topics

Excel Automation with Python

Data analysis is the upcoming technology in the IT sector; this data analysis can be performed easily with the help of data frames or by using excel sheets. The data...

4 minutes read.

Multiprocessing in python

The word multiprocessing is used to compute the operation in which more than two processors run simultaneously with more than one central processor. The computer's performance increases gradually. Multiprocessing is...

6 minutes read.

Raise Exception in Python

Most of the programmers/developers create large programs to solve complex tasks in Python. The code can be of 1000 lines and even more based on the need of the problem....

5 minutes read.

Python Dictionary Methods

Python Dictionary Methods Python has a set of built-in methods that dictionary objects can call. All the python dictionary methods are as follow: Methods Description clear() The dictionary.clear() method removes all the elements...

3 minutes read.

Python Knapsack problem

Python Knapsack problem Before we dig down about Knapsack problems in Python, first let's have a look at what is actually a knapsack problem. What is a knapsack problem? A problem from the...

5 minutes read.

Python program to find the area of the triangle

Python program to find the area of the triangle This article will discuss how to find the area of a triangle in Python with all three given sides. The area of...

2 minutes read.

How to Convert A List into String In Python?

How to Convert A List into String In Python? List is a data structure in Python that can hold values of different data types. The values are enclosed in square brackets...

3 minutes read.

Pillow Python introduction and setup

Introduction Advanced image processing implies handling the picture carefully with the assistance of a PC. Utilizing picture handling we can perform activities like upgrading the picture, obscuring the picture, separating text...

4 minutes read.

Python Word Tokenizer

Python Overview Python is an Object-Oriented high-level language. Python is designed to be highly beginner-friendly. Python has an English-like syntax, which is very easy to read. Python is an interpreted language...

4 minutes read.

Python Infinity

Introduction We will discover how to define an infinite number in Python in this tutorial. Infinity, as we all know, is a value that cannot be defined and can either be...

5 minutes read.

Alexa Python

The cloud-based voice assistant renders text into speech in a female voice. Alexa is a virtual assistant released by Amazon. Using itself as a system for home automation, Alexa can...

4 minutes read.

JWT Decode Python

Python's PyJWT package makes it possible to encrypt and decrypt JSON Web Tokens (JWT). JWT is a public, recognized global benchmark for safely expressing demands between two parties (RFC 7519). JSON...

7 minutes read.

Python reversed() Function

Python reversed() Function The reversed() in Python returns a reverse iterator. Syntax reversed(seq) Parameter seq: This parameter represents any iterable object. Return This function returns a reversed iterator object. Example 1 # Python Program describing # the reversed() function ...

1 minute read.

Python PyMOTW

The cmd module comprises one class of the general public, Cmd, meant for command processors such interactive shells and other command interpreters as a foundation class. It utilises readline as...

3 minutes read.

Python Logical Operators

In python, there are many operators which we use in our program. Operators are used in manipulating a particular value or operand. Logical operators are used mainly to evaluate an...

7 minutes read.

Python sorted() function

Python sorted() function The sorted() function returns a sorted list of the specified iterable object. Syntax sorted(iterable, *, key=None, reverse=False) Parameter iterable: It is a required parameter that represents the sequence to sort, list, dictionary, tuple etc. key:...

1 minute read.

How to make a firewall in Python?

Firewall: The firewall is a network which controls the incoming and outgoing network traffics of a monitor. It blocks the dataset based on the set of rules written in the security...

3 minutes read.

Python print() function

Python print() function The print() function prints the specified message to the screen or other standard output devices. Syntax print(*objects,sep=' ',end='\n',file=sys.stdout,flush=False) Parameter objects: This function represents an object, which will be converted to a string...

1 minute read.

Python Assert

Python Assert Python provides an assert statement which is used to check the logical expression. If the given logical expression is true, then it precedes for the next line; otherwise, it raises an...

2 minutes read.

How to Create an Array in Python?

How to Create an Array in Python In the previous article, we have discussed how to declare an array in Python. So, let's have a quick revision of that, and then we...

5 minutes read.