How do you print the sum of odd and even numbers in python?

The following article shows how given an integer list, we can produce the sum of all its odd and even digits.

Input : test_list = [345, 893, 1948, 34, 2346] 
Output : 
Odd digit sum : 36 
Even digit sum : 40 
Explanation : 3 + 5 + 9 + 3 + 1 + 9 + 3 + 3 = 36, odd summation.
Input : test_list = [345, 893] 
Output : 
Odd digit sum : 20 
Even digit sum : 12 
Explanation : 4 + 8 = 12, even summation. 

Method 1 : Using loop, str[] and int[]

In this, we first convert each element to string and then iterate for each of its element, and add to respective summation by conversion to integer.

Python3

test_list = [345, 893, 1948, 34, 2346]

print["The original list is : " + str[test_list]]

odd_sum = 0

even_sum = 0

for sub in test_list:

    for ele in str[sub]:

        if int[ele] % 2 == 0:

            even_sum += int[ele]

        else:

            odd_sum += int[ele]

print["Odd digit sum : " + str[odd_sum]]

print["Even digit sum : " + str[even_sum]]

Output

The original list is : [345, 893, 1948, 34, 2346]
Odd digit sum : 36
Even digit sum : 40

Method 2: Using loop and sum[]

In this, we perform task of getting summation using sum[], and loop is used to perform the task of iterating through each element.

Python3

test_list = [345, 893, 1948, 34, 2346]

print["The original list is : " + str[test_list]]

odd_sum = 0

even_sum = 0

for sub in test_list:

    odd_sum += sum[[int[ele] for ele in str[sub] if int[ele] % 2 == 1]]

    even_sum += sum[[int[ele] for ele in str[sub] if int[ele] % 2 == 0]]

print["Odd digit sum : " + str[odd_sum]]

print["Even digit sum : " + str[even_sum]]

Output

The original list is : [345, 893, 1948, 34, 2346]
Odd digit sum : 36
Even digit sum : 40

Method 3: Using list comprehension 

Python3

test_list = [345, 893, 1948, 34, 2346]

odd_sum = 0

even_sum = 0

odd_sum += sum[[int[ele]

                for sub in test_list for ele in str[sub] if int[ele] % 2 == 1]]

even_sum += sum[[int[ele]

                 for sub in test_list for ele in str[sub] if int[ele] % 2 == 0]]

print["Odd digit sum : " + str[odd_sum]]

print["Even digit sum : " + str[even_sum]]

Output

Odd digit sum : 36
Even digit sum : 40

Method 4: Using the enumerate function

Python3

test_list = [345, 893, 1948, 34, 2346]

odd_sum = 0

even_sum = 0

odd_sum += sum[[int[ele] for i, sub in enumerate[test_list]

                for ele in str[sub] if int[ele] % 2 == 1]]

even_sum += sum[[int[ele] for i, sub in enumerate[test_list]

                 for ele in str[sub] if int[ele] % 2 == 0]]

print["Odd digit sum : " + str[odd_sum]]

print["Even digit sum : " + str[even_sum]]

Output

Odd digit sum : 36
Even digit sum : 40


How do you print the sum of even and odd numbers in python?

Input : test_list = [345, 893, 1948, 34, 2346] Output : Odd digit sum : 36 Even digit sum : 40 Explanation : 3 + 5 + 9 + 3 + 1 + 9 + 3 + 3 = 36, odd summation. Input : test_list = [345, 893] Output : Odd digit sum : 20 Even digit sum : 12 Explanation : 4 + 8 = 12, even summation.

How do you print the sum of even numbers in python?

Example #1: Print all even numbers from given list using for loop Define start and end limit of range. Iterate from start till the range in the list using for loop and check if num % 2 == 0.

How do you find the sum of odd digits in python?

Working :.
Step 1: Read the integer input..
Step 2: initialize s and set it to zero..
Step 3: using modulo with 10 logic we will get last digit of the number..
Step 4: Do this operation by using while loop..
Step 5: Check whether the number is odd or not , if number is odd add to the result..
Step 6: Print the resultant sum..

What is the sum of odd and even numbers?

Sum of an even number and an odd number is always an odd number.

Chủ Đề