Python Exercise: Count the number of even and odd numbers from a series of numbers
Python Conditional: Exercise-6 with Solution
Write a Python program to count the number of even and odd numbers in a series of numbers
Pictorial Presentation of Even Numbers:
Pictorial Presentation of Odd Numbers:
Sample Solution:
Python Code:
# Create a tuple named 'numbers' containing integer values from 1 to 9
numbers = (1, 2, 3, 4, 5, 6, 7, 8, 9)
# Initialize counters for counting odd and even numbers
count_odd = 0
count_even = 0
# Iterate through each element 'x' in the tuple 'numbers'
for x in numbers:
# Check if the current number 'x' is even by evaluating 'not x % 2'
if not x % 2: # If 'x' modulo 2 equals 0, it's even
# Increment the count of even numbers
count_even += 1
else:
# If 'x' modulo 2 doesn't equal 0, it's odd; increment the count of odd numbers
count_odd += 1
# Print the total count of even and odd numbers
print("Number of even numbers:", count_even)
print("Number of odd numbers:", count_odd)
Sample Output:
Number of even numbers : 4 Number of odd numbers : 5
Flowchart:
Even Numbers between 1 to 100:
Odd Numbers between 1 to 100:
Python Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a Python program that accepts a word from the user and reverse it.
Next: Write a Python program that prints each item and its corresponding type from the following list.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://198.211.115.131/python-exercises/python-conditional-exercise-6.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics