Codeforcoding

Python code to Calculate sum of odd and even in a list

Python code to Calculate sum of odd and even in a list

In this tutorial, we will discuss The Python code to calculate the sum of odd and even numbers in a list

In this article, we are going to learn the concept of how to  calculate the sum of odd and even numbers in a list using  the Python programming language

Python code to Calculate sum of odd and even in a list
Python list – sum of odd and even

What is even or odd numbers?

When any integer value which ends in 0,2,4,6,8 is divided by two it is called as an even number

Example for even numbers – 34,-64,78,788

When any integer value which ends in 0,1,3,5,7,9 is not divided by two it is called as an odd number

Example for odd numbers – 33,-69,75,785

Here, we can use a modular operator to find odd or even number in the list

if n%2==0 n is an even number

if n%2==1 n is an odd number

 

Python code to Calculate sum of odd and even in a list

Calculating the sum of odd and even numbers using for loop

Program 1

The program will calculate the sum of odd and even numbers from the list using “for loop”.

# Python code to calculate sum of odd and even numbers in a list using for loop
#input list
NumList=[1,2,3,4,5,6,7,8,9,10]
#initialize sums
even_Sum=0
odd_Sum=0

#Loop through the list
for num in NumList:
    if num%2==0:
         even_Sum+=num  #Add to even sum
    else:
          odd_Sum+=num  #Add to odd sum
    
    #print the result
    
print(f"Sum of even numbers:{even_Sum}")
print(f"Sum of even numbers:{odd_Sum}")

When the above code is executed, it produces the following results

Sum of even numbers:30
Sum of even numbers:25

Method

 

 

Calculating the sum of odd and even numbers using “while loop”

Program 2

The program will calculate the sum of odd and even numbers from the list using “while loop”.

# Python code to Calculate sum of odd and even in a list using while loop
#input list
num=[1,2,3,4,5,6,7,8,9,10]
#initialized variable
even_Sum=0
odd_Sum=0

index=0

#Loop through the list using while
while index<len(num):
    if num[index]%2==0:
        even_Sum+=num[index]
      
    else:
        odd_Sum+=num[index]
      
    index+=1
        
print(f"Sum of even numbers: {even_Sum}")
print(f"Sum of odd numbers: {odd_Sum}")

 

When the above code is executed, it produces the following results

Sum of even numbers: 30
Sum of odd numbers: 25

 

Method:

 

Similar post

Python program check whether a number is odd or even

Python program to check a number is even or odd using the function

Python program to display even and odd numbers without if

Python program to display even and odd number in the given range

Separate odd and even numbers in a list to different two list

Python Program to find odd and even numbers from a list

 

Suggested for you

for loop in Python

while loop in Python

If statements in Python

5 methods to add two numbers in Java
Python program to calculate the sum of odd and even numbers in a list
Exit mobile version