Python Program to Print Sum of All Array Elements

Sum of all elements in array Python; Through this tutorial, i am going to show you how to print sum of array elements in python program.

Python Program to Print Sum of All Array Elements

Now, i will write a program to print the sum of all the elements of an array in python; as shown below:

  • Python Program to find Sum of Elements in a List using sum function
  • Program to find Sum of Elements in a List without using for loop
  • Program to find Sum of Elements in a List without using while loop
  • Python Program to Calculate Sum of all Elements in a List using Functions

Python Program to find Sum of Elements in a List Using sum function

# Python Program to find Sum of all Elements in a List using sum function
NumList = []
Number = int(input("Please enter the Total Number of List Elements : "))
for i in range(1, Number + 1):
    value = int(input("Please enter the Value of %d Element : " %i))
    NumList.append(value)
total = sum(NumList)
print("\n The Sum of All Element in this List is : ", total)

After executing the program, the output will be:

Please enter the Total Number of List Elements :  5

Please enter the Value of 1 Element :  10
Please enter the Value of 2 Element :  56
Please enter the Value of 3 Element :  5
Please enter the Value of 4 Element :  44
Please enter the Value of 5 Element :  57

 The Sum of All Element in this List is :  172

Program to find Sum of Elements in a List without using for loop

# Python Program to find Sum of all Elements in a List using for loop
NumList = []
total = 0
Number = int(input("Please enter the Total Number of List Elements : "))
for i in range(1, Number + 1):
    value = int(input("Please enter the Value of %d Element : " %i))
    NumList.append(value)
for j in range(Number):
    total = total + NumList[j]
print("\n The Sum of All Element in this List is : ", total)

After executing the program, the output will be:

Please enter the Total Number of List Elements :  4
Please enter the Value of 1 Element :  10
Please enter the Value of 2 Element :  20
Please enter the Value of 3 Element :  30
Please enter the Value of 4 Element :  40

The Sum of All Element in this List is :  100

Python Program to Calculate Sum of Elements in a List using While loop

# Python Program to find Sum of all Elements in a List using while loop
NumList = []
total = 0
j = 0
Number = int(input("Please enter the Total Number of List Elements : "))
for i in range(1, Number + 1):
    value = int(input("Please enter the Value of %d Element : " %i))
    NumList.append(value)
while(j < Number):
    total = total + NumList[j]
    j = j + 1
print("\n The Sum of All Element in this List is : ", total)

After executing the program, the output will be:

Please enter the Total Number of List Elements :  3
Please enter the Value of 1 Element :  1
Please enter the Value of 2 Element :  2
Please enter the Value of 3 Element :  3

The Sum of All Element in this List is :  6

Python Program to Calculate Sum of all Elements in a List using Functions

# Python Program to find Sum of all Elements in a List using function
def sum_of_list(NumList):
    total = 0
    for j in range(Number):
        total = total + NumList[j]
    return total
NumList = []
Number = int(input("Please enter the Total Number of List Elements : "))
for i in range(1, Number + 1):
    value = int(input("Please enter the Value of %d Element : " %i))
    NumList.append(value)
total = sum_of_list(NumList)
print("\n The Sum of All Element in this List is : ", total)

After executing the program, the output will be:

Please enter the Total Number of List Elements :  5
Please enter the Value of 1 Element :  5
Please enter the Value of 2 Element :  10
Please enter the Value of 3 Element :  52
Please enter the Value of 4 Element :  53
Please enter the Value of 5 Element :  88

The Sum of All Element in this List is :  208

Recommended Python Tutorials

Be First to Comment

Leave a Reply

Your email address will not be published.