- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
- Python Advanced Tutorial
- Python - Classes/Objects
- Python - Reg Expressions
- Python - CGI Programming
- Python - Database Access
- Python - Networking
- Python - Sending Email
- Python - Multithreading
- Python - XML Processing
- Python - GUI Programming
- Python - Further Extensions
Python program to find the sum of all even and odd digits of an integer list
When it is required to find the sum of all even and odd digits of an integer list, a simple iteration and the ‘modulus’ operator are used.
Below is a demonstration of the same −
my_list = [369, 793, 2848, 4314, 57467] print("The list is :") print(my_list) sum_odd = 0 sum_even = 0 for index in my_list: for element in str(index): if int(element) % 2 == 0: sum_even += int(element) else: sum_odd += int(element) print("The result is :") print("The sum of odd digits is :") print(sum_odd) print("The sum of odd digits is :") print(sum_even)
The list is : [369, 793, 2848, 4314, 57467] The result is : The sum of odd digits is : 54 The sum of odd digits is : 46
A list of integers is defined and is displayed on the console.
Two variables ‘sum_odd’ and ‘sum_even’ are declared.
The list is iterated over, and the sum of odd digits and even digits are calculated.
This is done by getting the modulus of the element with 2, and comparing it with 0.
This is the output that is displayed on the console.
- Related Articles
- Count even and odd digits in an Integer in C++
- Python Program for Difference between sums of odd and even digits
- Find the sum of digits of a number at even and odd places in C++
- Program to find sum of odd elements from list in Python
- Program to find sum of all odd length subarrays in Python
- Program to find the sum of all digits of given number in Python
- Find sum of even and odd nodes in a linked list in C++
- Program to find nearest number of n where all digits are odd in python
- C Program for the Difference between sums of odd and even digits?
- C Program for Difference between sums of odd and even digits?
- Python program to Count Even and Odd numbers in a List
- Difference between sums of odd and even digits.
- Program to find minimum digits sum of deleted digits in Python