Given a list of integers l is entered by user write code to add the integers and display the sum

Python program to calculate the sum of elements in a list

Hello everyone, am back to discuss about a new python program. Here we learn how to sum all the elements in a list quite easily. We use a predefined function called sum[] and apply it to the list, the functions returns the sum of all the elements in a list.

Sum of elements in a list — programminginpython.com

Python program to find sum of elements in list

Given a list of numbers, write a Python program to find the sum of all the elements in the list.
Example:

Input: [12, 15, 3, 10] Output: 40 Input: [17, 5, 3, 5] Output: 30

Example #1:

Python3




# Python program to find sum of elements in list
total = 0
# creating a list
list1 = [11, 5, 17, 18, 23]
# Iterate each element in list
# and add them in variable total
for ele in range[0, len[list1]]:
total = total + list1[ele]
# printing total value
print["Sum of all elements in given list: ", total]

Output:

Sum of all elements in given list: 74


Example #2 : Using while[] loop

Python3




# Python program to find sum of elements in list
total = 0
ele = 0
# creating a list
list1 = [11, 5, 17, 18, 23]
# Iterate each element in list
# and add them in variable total
while[ele < len[list1]]:
total = total + list1[ele]
ele += 1
# printing total value
print["Sum of all elements in given list: ", total]

Output:



Sum of all elements in given list: 74


Example #3: Recursive way

Python3




# Python program to find sum of all
# elements in list using recursion
# creating a list
list1 = [11, 5, 17, 18, 23]
# creating sum_list function
def sumOfList[list, size]:
if [size == 0]:
return 0
else:
return list[size - 1] + sumOfList[list, size - 1]
# Driver code
total = sumOfList[list1, len[list1]]
print["Sum of all elements in given list: ", total]

Output:

Sum of all elements in given list: 74


Example #4: Using sum[] method

Python3




# Python program to find sum of elements in list
# creating a list
list1 = [11, 5, 17, 18, 23]
# using sum[] function
total = sum[list1]
# printing total value
print["Sum of all elements in given list: ", total]

Output:

Sum of all elements in given list: 74

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course




Article Tags :
Python
Python Programs
School Programming
Python list-programs
python-list
Practice Tags :
python-list
Read Full Article

sum[] function in Python

Sum of numbers in the list is required everywhere. Python provide an inbuilt function sum[] which sums up the numbers in the list.

Syntax:

sum[iterable, start] iterable : iterable can be anything list , tuples or dictionaries , but most importantly it should be numbers. start : this start is added to the sum of numbers in the iterable. If start is not given in the syntax , it is assumed to be 0.

Possible two syntaxes:

sum[a] a is the list , it adds up all the numbers in the list a and takes start to be 0, so returning only the sum of the numbers in the list. sum[a, start] this returns the sum of the list + start

Below is the Python implementation of the sum[]




# Python code to demonstrate the working of
# sum[]
numbers = [1,2,3,4,5,1,4,5]
# start parameter is not provided
Sum = sum[numbers]
print[Sum]
# start = 10
Sum = sum[numbers, 10]
print[Sum]

Output:

25 35

Error and Exceptions



TypeError : This error is raised in the case when there is anything other then numbers in the list.




# Python code to demonstrate the exception of
# sum[]
arr = ["a"]
# start parameter is not provided
Sum = sum[arr]
print[Sum]
# start = 10
Sum = sum[arr, 10]
print[Sum]

Runtime Error :

Traceback [most recent call last]: File "/home/23f0f6c9e022aa96d6c560a7eb4cf387.py", line 6, in Sum = sum[arr] TypeError: unsupported operand type[s] for +: 'int' and 'str'

So the list should contain numbers

Practical Application: Problems where we require sum to be calculated to do further operations such as finding out the average of numbers.




# Python code to demonstrate the practical application
# of sum[]
numbers = [1,2,3,4,5,1,4,5]
# start = 10
Sum = sum[numbers]
average= Sum/len[numbers]
print [average]

Output:

3

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course




Article Tags :
Python
Python-Built-in-functions
Read Full Article

Python Program to Add Two Numbers

In this program, you will learn to add two numbers and display it using print[] function.

To understand this example, you should have the knowledge of the following Python programming topics:

In the program below, we've used the + operator to add two numbers.

Python Exercise: Find the sum of all the numbers in a list

Last update on September 30 2021 07:12:26 [UTC/GMT +8 hours]

How to compute the sum of a list in python

When given a list of integers, how do you find the sum of all the elements in the list?

Video liên quan

Bài mới nhất

Chủ Đề