Count Even and Odd Elements
Count even and odd list elements with Python remainder tests. Students classify every integer once and distinguish the count of matches from the sum of values.
1# Take array2n = int(input("Enter array size: "))3arr = []4for i in range(n):5 arr.append(int(input(f"Element {i+1}: ")))67# Count8even = 09odd = 01011for element in arr:12 if element % 2 == 0:13 even += 114 else:15 odd += 11617print(f"Even: {even}, Odd: {odd}")
Output
Enter array size: 5 Element 1: 2 Element 2: 3 Element 3: 4 Element 4: 5 Element 5: 6 Even: 3, Odd: 2
In this one
- Count values by parity
- Include zero
- Distinguish frequency from total
How it works
- 1
Collect integers and initialize even and odd.
- 2
Test each value modulo two.
- 3
Increment one counter and print both totals.
Edge cases
Is zero even here?
Is zero even here?
Yes. Its remainder modulo two is zero.
What about an empty list?
What about an empty list?
Both counters stay zero.
What's going on
Each collected element is tested with element % 2 == 0. A match increments even; the other branch increments odd.
Zero belongs to the even count, and negative integers are classified correctly by their remainder. Repeated values each contribute a count because this measures list entries. The two counters together account for every element without changing the list.
Is zero even here? Yes. Its remainder modulo two is zero.
What about an empty list? Both counters stay zero.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
What is incremented for a match?
Pick an answer to check it.
Question 2
Is zero even here?
Pick an answer to check it.
Questions people ask
Is zero even here?
Yes. Its remainder modulo two is zero.
What about an empty list?
Both counters stay zero.
What is incremented for a match?
A counter by one.
๐Related Content
- ๐
Phase 4 - Learn Concepts
Review Phase 4 concepts and explanations
- ๐
Phase 4 - Quiz
Test your Phase 4 understanding with quiz questions
- ๐ป
Phase 4 - All Practice Problems
Explore all practice problems for Phase 4
- ๐
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- ๐ง
Logic Building Overview
Learn about the complete logic building curriculum