
- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions
Find sum of elements in list in Python program
In this article, we will learn about the solution to the problem statement given below.
Problem statement − We are given a list iterable, we need to compute the sum of the list
Here we will be discussing 3 approaches as discussed below
Using for loop
Example
# sum total = 0 # creating a list list1 = [11, 22,33,44,55,66] # iterating over the list 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 the array is 231
Using while loop
Example
# Python program to find sum of elements in list total = 0 ele = 0 # creating a list list1 = [11,22,33,44,55,66] # iterating using loop 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 the array is 231
Using Recursion by creating a function
Example
# list list1 = [11,22,33,44,55,66] # function following recursion def sumOfList(list, size): if (size == 0): return 0 else: return list[size - 1] + sumOfList(list, size - 1) # main total = sumOfList(list1, len(list1)) print("Sum of all elements in given list: ", total)
Output
Sum of the array is 231
Conclusion
In this article, we have learned how to print the sum of elements in a list.
- Related Articles
- Python program to find sum of elements in list
- Program to find sum of odd elements from list in Python
- Program to find sum of non-adjacent elements in a circular list in python
- Program to find largest sum of non-adjacent elements of a list in Python
- Find sum of frequency of given elements in the list in Python
- Program to find sum of unique elements in Python
- Program to find maximum sum of popped k elements from a list of stacks in Python
- Program to find sum of all elements of a tree in Python
- Python program to find Cumulative sum of a list
- Python program to find Tuples with positive elements in List of tuples
- Program to find duplicate item from a list of elements in Python
- Program to find squared elements list in sorted order in Python
- Program to find highest common factor of a list of elements in Python
- Program to find three unique elements from list whose sum is closest to k Python
- Python program to find the sum of Characters ascii values in String List

Advertisements