Count Positive, Negative, Zero

Count positive, negative, and zero elements.

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

What's going on

Classify each element and count.

Key Concepts:

Check if > 0, < 0, or == 0
Increment respective counter
Print all counts