Python: Calculate Average of odd and even in a list using loops
Python: Calculate Average of odd and even in a list using loops
In this post, we will realize about the title of “Python: Calculate Average of odd and even in a list using loops”.
In this article, we will explore how to calculate the average of odd and even numbers in a list. The list will be generated using numbers, and we will use both for-loops and while-loops to achieve this.

Here’s a Python program that calculates the average of odd and even numbers
Program 1
In this code, we calculate the average of odd and even numbers using for-loop
# Python code to calculate Average of odd and even numbers in a list #input list NumList=[1,2,3,4,5,6,7,8,9,10] #initialize sums even_Sum=0 even_Count=0 odd_Sum=0 odd_Count=0 #Loop through the list for num in NumList: if num%2==0: even_Sum+=num #Add to even sum even_Count+=1 else: odd_Sum+=num #Add to odd sum odd_Count+=1 #calculate average even_Avg=even_Sum/even_Count if even_Count !=0 else 0 odd_Avg=odd_Sum/odd_Count if odd_Count !=0 else 0 #print the result print(f"Average of even numbers:{even_Avg}") print(f"Average of odd numbers:{odd_Avg}")
When the above code is executed, it produces the following result
Average of even numbers:6.0
Average of odd numbers:5.0
Explanation
- Define a list with numbers
- Initialise variables as even_Sum=0, even_Count=0, odd_Sum=0,odd_Count=0
- Separate odd and even numbers and adding one by one to find the odd sum and even sum
- Calculate the average if odd sum and even sum using sum/count
- Print the result average of odd sum and even sum
Program 2
In this code, we calculate the average of odd and even numbers using user input with while-loop
# Python code to calculate Average of odd and even numbers in a list #input list NumList=[11,12,13,14,15,16,17,18,19,20] #initialize variables even_Sum=0 even_Count=0 odd_Sum=0 odd_Count=0 index=0 #Loop through the list while index <len(NumList): if NumList[index]%2==0: even_Sum+=NumList[index] #Add to even sum even_Count+=1 else: odd_Sum+=NumList[index] #Add to odd sum odd_Count+=1 index+=1 #calculate average even_Avg=even_Sum/even_Count if even_Count !=0 else 0 odd_Avg=odd_Sum/odd_Count if odd_Count !=0 else 0 #print the result print(f"Average of even numbers:{even_Avg}") print(f"Average of odd numbers:{odd_Avg}")
When the above code is executed, it produces the following result
Average of even numbers:16.0
Average of odd numbers:15.0
Explanation
- Define a list with numbers
- Initialise variables as even_Sum=0, even_Count=0, odd_Sum=0,odd_Count=0
- Separate odd and even numbers and adding one by one to find the odd sum and even sum
- Calculate the average if odd sum and even sum using sum/count
- Print the result average of odd sum and even sum
Similar post
C program to find a number is even or odd using the function
C program to separate Odd and Even numbers from an array
C program to Electricity bill calculation using the function
C program to display all even and odd numbers from 1 to n
C program display odd and even numbers without if statements
C program to calculate the sum of odd and even numbers
C program to find whether a number is even or odd
C++ program to find a number is even or odd using the function
C++ program to separate Odd and Even numbers from an array
C++ program to display all even and odd numbers from 1 to n
C++ program calculate Average of odd and even numbers in an array
C++ program to calculate the sum of odd and even numbers
C++ program to find whether a number is even or odd
Java program to find a number is even or odd using the method
Java program to separate Odd and Even numbers from an array
Java program to display all even and odd numbers from 1 to n
Java program display odd and even numbers without if statements
Java program to calculate the sum of odd and even numbers
Java program to find whether a number is even or odd
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