Python Program to Find Sum of All Array Elements

Sum of array elements in python; Through this tutorial, you will learn how to sum of all array elements in a python program.

Python Program to Find Sum of All Array Elements

  • 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 Array Programs

  1. Python Program to Find the GCD of Two Numbers or Array
  2. Python Program to Find Sum of All Array Elements
  3. Python Count the Occurrences in an Array
  4. Python: Find the Union and Intersection of Two Arrays

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *