Write a Python Program to Count Positive and Negative Numbers in a List using For Loop, While Loop, and Functions with a practical example.

## Python Program to Count Positive and Negative Numbers in a List using For Loop

In this python program, we are using For Loop to iterate every element in a given List. Inside the Python for loop, we are using the If statement to check and count Positive and Negative numbers.

# Python Program to Count Positive and Negative Numbers in a List NumList = [] Positive_count = 0 Negative_count = 0 Number = int(input("Please enter the Total Number of List Elements: ")) for i in range(1, Number + 1): value = int(input("Please enter the Value of %d Element : " %i)) NumList.append(value) for j in range(Number): if(NumList[j] >= 0): Positive_count = Positive_count + 1 else: Negative_count = Negative_count + 1 print("\nTotal Number of Positive Numbers in this List = ", Positive_count) print("Total Number of Negative Numbers in this List = ", Negative_count)

**OUTPUT**

**ANALYSIS**

User entered elements = [12, -22, 3, 5], Positive_count = 0, Negative_count = 0

**For Loop – First Iteration: **for 0 in range(0, 4)

The condition is True. So, it enters into the If Statement

if(NumList[0] >= 0) => if(12 >= 0) – Condition is True

Positive_count = Positive_count + 1 => 0 + 1 = 1

**Second Iteration:** for 1 in range(0, 4) – Condition is True

if(NumList[1] >= 0) => if(-22 >= 0) – Condition is False, so it enters the Else block.

Negative_count = Negative_count + 1 => 0 + 1 = 1

**Third Iteration:** for 2 in range(0, 4) – Condition is True

if(NumList[2] >= 0) => if(3 >= 0) – Condition is True

Positive_count = 1 + 1 => 2

**Fourth Iteration:** for 3 in range(0, 4) – Condition is True

if(5 >= 0) – Condition is True. So, it enters into the Else block.

Positive_count = 2 + 1 => 3

**Fifth Iteration:** for 4 in range(4) – Condition is False. So, it exits from For Loop

## Python Program to Count Positive and Negative Numbers in a List using While loop

This Python program for counting positive and negative numbers is the same as the above. We just replaced the For Loop with While loop.

# Python Program to Count Positive and Negative Numbers in a List NumList = [] Positive_count = 0 Negative_count = 0 j = 0 Number = int(input("Please enter the Total Number of List Elements: ")) for i in range(1, Number + 1): value = int(input("Please enter the Value of %d Element : " %i)) NumList.append(value) while(j < Number): if(NumList[j] >= 0): Positive_count = Positive_count + 1 else: Negative_count = Negative_count + 1 j = j + 1 print("\nTotal Number of Positive Numbers in this List = ", Positive_count) print("Total Number of Negative Numbers in this List = ", Negative_count)

**OUTPUT**

## Python Program to Count Positive and Negative items in a List using Functions

This Python counting positive and negative list items program is the same as the first example. However, we separated the logic using Functions

# Python Program to Count Positive and Negative Numbers in a List def count_Positive(NumList): Positive_count = 0 for j in range(Number): if(NumList[j] >= 0): Positive_count = Positive_count + 1 return Positive_count def count_Negative(NumList): Negative_count = 0 for j in range(Number): if(NumList[j] % 2 != 0): Negative_count = Negative_count + 1 return Negative_count NumList = [] Number = int(input("Please enter the Total Number of List Elements: ")) for i in range(1, Number + 1): value = int(input("Please enter the Value of %d Element : " %i)) NumList.append(value) Positive_cnt = count_Positive(NumList) Negative_cnt = count_Negative(NumList) print("\nTotal Number of Positive Numbers in this List = ", Positive_cnt) print("Total Number of Negative Numbers in this List = ", Negative_cnt)

**OUTPUT**