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 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
What's going on
Classify each element and count.
Key Concepts:
Check if > 0, < 0, or == 0
Increment respective counter
Print all counts
🔗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