Founding Batch Offer ยท HLD ยท LLD ยท System Design at โ‚น799 until 30 Nov

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.

BeginnerPhase 4 - Level 1: Basic ArraysExample 5 of 10
count-positive-negative-zero.py
1# Take array
2n = int(input("Enter array size: "))
3arr = []
4for i in range(n):
5 arr.append(int(input(f"Element {i+1}: ")))
6
7# Count
8positive = 0
9negative = 0
10zero = 0
11
12for element in arr:
13 if element > 0:
14 positive += 1
15 elif element < 0:
16 negative += 1
17 else:
18 zero += 1
19
20print(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. 1

    Collect arr and initialize the sign counters.

  2. 2

    Increment positive, negative, or zero per element.

  3. 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.