Given a list of integers l is entered by user write code to add the integers and display the sum
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.
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:
Example #1:
# 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:
Example #2 : Using while[] loop
# 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
# 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:
Example #4: Using sum[] method
# 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:
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
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:
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 + startBelow 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 35Error 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:
3Attention 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
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:
- Python Input, Output and Import
- Python Data Types
- Python Operators
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?