×

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

How to read data from com port in python

Comport, the I/O interface is known as a COM port that allows the connection for a serial device to a computer. COM ports are sometimes referred to as serial ports....

3 minutes read.

Python md5_file() function

Python md5_file() function The md5_file() function in PHP calculates the md5 hash of a given file. Syntax md5_file ( string $filename [, bool $raw_output ] )  Parameter filename(required)- This parameter signifies the file to be calculated. raw_output(optional)- It takes a boolean value that specifies hex or binary...

1 minute read.

Python frozenset()

Python frozenset() class The frozenset() class in Python returns a new frozenset object, optionally with elements taken from iterable. Syntax class frozenset([iterable]) Parameter iterable: This parameter represents an iterable object, like list, set, tuple etc. Return This class returns an unchangeable...

1 minute read.

Python Linear regression

In this tutorial, we will understand the meaning, usage, and types of Linear Regression. Further, we will comprehend the terms cost function and Optimization. Linear Regression is the widely used Machine...

3 minutes read.

Python and its advantages

What is Python? Python is an object-oriented programming language, a general-purpose programming language with a high level, and also an interpreted language that has an easy syntax and dynamic semantics. Python...

7 minutes read.

How to Install Tweepy in Python

In this article, we will learn or understand how to install Tweepy in Python and what Tweepy is. Firstly, let’s understand What Tweepy is. As we all know, one of the...

3 minutes read.

NOT IN operator in Python

This page contains all the information about “not in” operator in python. You can also know everything about what type of operator is “not in” in python language and where...

7 minutes read.

Dictionary to JSON Python

In python JSON (JavascriptObject Notation). In the programming language, the text file is made using the script file.We can use many built-in packages which arenamedJSON.Before using the packages, we have...

3 minutes read.

Returning Multiple Values in Python

Python is considered a general-purpose programming language; it is a high-level programming language that is not much difficult and easier to learn. It is rich in libraries that can be...

3 minutes read.

Python Set isdisjoint() method

Python Set isdisjoint() method The set.isdisjoint() method in Python returns a boolean value True if two sets are disjoint sets ( i.e. none of the elements are present in both sets), otherwise it returns...

1 minute read.

Python Packages

A package is a collection of Python modules that share a similar namespace and are generated by putting all of the modules in a single directory with certain special files...

6 minutes read.

What is Python 2

Python is a widely used high-level language. The initial work on developing python was begun in the late 1980s. In 1989, Guido Van Rossum started to work on it. Initially,...

3 minutes read.

Iterate a Dictionary in Python – Part 2

In this tutorial, we will learn above various methods used to Iterate a Dictionary in Python. Dictionary: In Python, a dictionary is an unordered collection of data values that is used to...

3 minutes read.

Converting Set to List in Python

Converting ‘set’ datatype to ‘list’ datatype is called typecasting. Typecasting in programming is a method to convert one datatype into another datatype. It may happen implicitly by the defined language, called...

3 minutes read.

Yield vs Return in Python

In this article, you will learn about " Yield " and " Return " in Python. You will also understand the difference between " Yield " and " Return "...

6 minutes read.

Notepad++ For Python

In this article, you are going to learn what notepad++ is and how it is integrated with python to use the python programming language. You can also learn how to...

4 minutes read.

Python Deep Copy and Shallow Copy

Assignment statements in Python create bindings between a target and an object, not copies of them. The = operator only makes a new variable that shares the reference to the...

3 minutes read.

Python for Data Analysis

Data analysis uses various techniques to read, illustrate, manipulate and evaluate a particular data. You can have access to the data and keep the data updated regularly. You can append...

4 minutes read.

Python Project Ideas

One of the most widely used programming languages today is Python. This pattern appears set to continue through 2023 and beyond. Therefore, working on some current Python project ideas is the...

10 minutes read.

Python Read CSV file

The CSV stands for “comma-separated value,” which is defined as a simple file format that is used to store data into a tabular form such as a database or spreadsheet. It is...

7 minutes read.