In this tutorial, we will discuss the Python program 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 the Python programming language
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
Program 1
In this program, the user prompts the program to add some numbers to the list, and then the program calculates the sum of odd and even numbers from the list using the “for loop.”
# Python code to Calculate sum of odd and even in a list using while loop #input list NumList=list(map(int, input("Enter numbers separated by spaces: ").split())) #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
Enter numbers separated by spaces: 5 6 7 8 9 10 11 12 13 14
Sum of even numbers:50
Sum of even numbers:45
Method
In this program, the user prompts the program to add some numbers to the list, and then the program calculates the sum of odd and even numbers from the list using the “=while loop.”
Program 2
# Python code to Calculate sum of odd and even in a list using while loop #input list num=list(map(int, input("Enter numbers separated by spaces: ").split())) #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
Enter numbers separated by spaces: 3 6 9 12 15 18 21 24 27 30
Sum of even numbers: 90
Sum of odd numbers: 75
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
Explanation of one dimensional array In this post, we will discuss the concept of "Explanation…
Python code to Calculate sum of odd and even in a list In this tutorial,…
How to find reverse number using method In this article, we will discuss the concept…
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…