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
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
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
To begin with, define a list without including the elements named NumList=[].
then input list elements according to the list length
Next, declare and initialize two variables: odd_Sum=0, even_Sum=0
Then, using the “for loop”, elements are picked individually from the list to determine oddness and evenness.
Next, “if statements” are used to find a number. If it is even, it is added to evenSum.If the number is not even, it is dealt with by an “else statement.”
After that, the “else statement” adds the element to oddSum.
Finally, the sums of odd numbers and even numbers are displayed.
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:
To begin with, define a list without including the elements named um=[].
then input list elements according to the list length
Next, declare and initialize two variables: odd_Sum=0, even_Sum=0
Then, using the “while loop”, elements are picked individually from the list to determine oddness and evenness.
Next, “if statements” are used to find a number. If it is even, it is added to evenSum.If the number is not even, it is dealt with by an “else statement.”
After that, the “else statement” adds the element to oddSum.
Finally, the sums of odd numbers and even numbers are displayed.
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
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…
How to print multiplication table using Array in C++ language In this post, we will…
C Program to multiplication table using Array In this tutorial , we will discuss about…