Sum of Array Elements in Python: Program to Add Array Elements | FACE Prep

Sum of Array Elements in Python: Program to Add Array Elements | FACE Prep

Sum of Array Elements in Python | Python Program to Add the Elements of an Array

Calculating the sum of elements in an array is a common task in programming. This article explains how to write a Python program to compute the sum of all elements in an array efficiently.

What is an Array in Python?

In Python, an array is a collection of elements of the same type. Arrays are particularly useful when you want to store multiple values and perform operations like summing up the elements. Although Python lists can function like arrays, you can also use the array module for stricter type enforcement.

Problem Statement

Write a Python program to find the sum of elements in a given array. The program should:

  1. Take the size of the array as input.
  2. Accept the array elements from the user.
  3. Compute and display the sum of the array elements.

Input and Output Format

  • Input:
    • The first input is the size of the array.
    • The subsequent inputs are the elements of the array.
  • Output:
    • The sum of all the elements in the array.

Example:

Input:

Enter the size of the array: 3

Enter array elements:

1

2

3

Output:

Sum: 6

 

Algorithm to Calculate the Sum of Array Elements

Sum of Array in Python

Get the size of the array from the user.

  • Initialize an empty list, lst = [].
  • Use a for loop to collect array elements from the user.
  • Append each element to the list.
  • Use Python’s built-in sum() function to calculate the sum of all elements in the list.
  • Print the result.

Python Code to Compute Array Sum

# Python program to calculate the sum of array elements

 

# Step 1: Initialize an empty list

lst = []

 

# Step 2: Input the size of the array

num = int(input(“Enter the size of the array: “))

 

# Step 3: Collect array elements

print(“Enter array elements: “)

for _ in range(num):

    element = int(input())

    lst.append(element)

 

# Step 4: Calculate the sum using the built-in sum() function

array_sum = sum(lst)

 

# Step 5: Output the sum

print(“Sum:”, array_sum)

 

Explanation of the Code

  1. Input Handling:
    • The input() function is used to take inputs from the user. The size of the array determines how many elements need to be entered.
  2. List Construction:
    • An empty list is initialized, and the elements are appended one by one using a loop.
  3. Sum Calculation:
    • The built-in sum() function is an efficient way to calculate the sum of all elements in the list.
  4. Output:
    • The calculated sum is printed.

Advantages of Using the Built-in sum() Function

Sum of Array in Python

 

  • Efficiency: It’s optimized for speed and works directly with iterable objects.
  • Readability: The code is concise and easy to understand.

Frequently Asked Questions (FAQs)

Q1. Can I use arrays from the array module instead of lists?

  • Yes, you can import the array module and use it to ensure all elements are of the same type.

Example:

from array import array

arr = array(‘i’, [1, 2, 3])

print(“Sum:”, sum(arr))

 

Q2. How do I handle invalid inputs?

  • Use tryexcept blocks to catch exceptions like entering non-integer values.

Q3. What if the array is empty?

  • If the array is empty, sum() will return 0.

Conclusion

This simple Python program demonstrates how to calculate the sum of array elements using both user input and built-in functions. You can extend this logic to handle more complex scenarios or integrate it into larger programs. 

 

Sum of Array in Python
c