Program to print the sum of all elements in an Array in Python. The program contains array initialisation, variable assignment, print statement and for loop iterative statements. This program is for Beginners, class 11,12 and B Tech computer science students.
Program to print the sum of all elements in an Array
What is an Array ?
A Python Array is a collection of common type of data structures having elements with same data type. It is used to store collections of data.
In this Program :
First the array is initialised. A variable stores the value of sum equal to zero. The loop transverse the value from 0 to length of Array. It calculates the sum of array elements.
Source code :
a=[1,25,56,25,32,10,0,1,25]
s=0
for i in range (0,len(a)):
s=s+a[i]
print(s)
Source code Explanation :
step 1: Intialise the array
a=[1,25,56,25,32,10,0,1,25]
step 2: A variable is created to store the value of sum
s=0
step 3: This loop will transverse the array
for i in range (0,len(a)):
step 4:This will sum the numbers
s=s+a[i]
step 5:printing the value of sum of elements
print(s)
Output:
RESTART: C:\csstudy\source code file 2\Python program to print the sum of all elements in an array.py 175 >>>
Output Explanation:
RESTART: C:\csstudy\source code file 2\Python program to print the sum of all elements in an array.py
175
The loop transverse the value from 0 to length of Array. It calculates the sum of array elements.
Conclusion:
The program is running fine.
Thank you for learning, reading and downloading the source code for Program to print the sum of all elements in an Array in Python. The program contains array initialisation, variable assignment, print statement and for loop iterative statements. This program is for Beginners, class 11,12 and B Tech computer science students. For more programs, practical file of class 11,12 and Python projects click the links below:
/category/python-programs/
Computer Science with Python Practical Book Solutions Class 11 Term -2 – CS Study
Computer Science Practical file for class 12 Term-2 – CS Study
Python Projects for Class 12 with Source Code (Mysql Connectivity) – CS Study