Count Positive, Negative, Zero
Count positive, negative, and zero values in a Python list. Students maintain three independent counters and use one exclusive branch for each element's sign.
1# Take array2n = int(input("Enter array size: "))3arr = []4for i in range(n):5 arr.append(int(input(f"Element {i+1}: ")))67# Count8positive = 09negative = 010zero = 01112for element in arr:13 if element > 0:14 positive += 115 elif element < 0:16 negative += 117 else:18 zero += 11920print(f"Positive: {positive}, Negative: {negative}, Zero: {zero}")
Output
Enter array size: 5 Element 1: 5 Element 2: -3 Element 3: 0 Element 4: 10 Element 5: -2 Positive: 2, Negative: 2, Zero: 1
In this one
- Keep three counters
- Classify every position once
- Check counter totals
How it works
- 1
Collect arr and initialize the sign counters.
- 2
Increment positive, negative, or zero per element.
- 3
Print all three completed counts.
Edge cases
Where is zero counted?
Where is zero counted?
Only in the final else branch.
What does an empty list produce?
What does an empty list produce?
All three counts remain zero.
What's going on
After collecting the integers, all three counters start at zero. Each element enters exactly one branch of the if-elif-else chain.
Positive values increment positive; negative values increment negative; the remaining value is zero. Counts measure positions, not distinct values. Their final sum equals the list length because every element contributes to one and only one group.
Where is zero counted? Only in the final else branch.
What does an empty list produce? All three counts remain zero.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
What should the three counts add up to?
Pick an answer to check it.
Question 2
Where is zero counted?
Pick an answer to check it.
Questions people ask
Where is zero counted?
Only in the final else branch.
What does an empty list produce?
All three counts remain zero.
What should the three counts add up to?
The number of elements.
๐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