In this python program, we will learn how to count the positive and negative numbers on a given list.
Here is the source code of the program to count the positive and negative numbers in a given list.
# Python Program to Count the Positive and Negative Numbers in a List Using For Loop
NumList = []
Positive_count = 0
Negative_count = 0
# Take the Input From the User
Number = int(input("Enter the Total Number of List Elements: "))
for i in range(1, Number + 1):
value = int(input("Enter the Value of %d Element: " %i))
NumList.append(value)
# Print the List Entered by the User
print("\nList Entered By the User: ",NumList)
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)
Enter the Total Number of List Elements: 6
Enter the Value of 1 Element: 12
Enter the Value of 2 Element: 63
Enter the Value of 3 Element: -65
Enter the Value of 4 Element: -56
Enter the Value of 5 Element: 32
Enter the Value of 6 Element: -54
List Entered By the User: [12, 63, -65, -56, 32, -54]
Total Number of Positive Numbers in this List = 3
Total Number of Negative Numbers in this List = 3
# Python Program to Count the Positive and Negative Numbers in a List Using While Loop
NumList = []
Positive_count = 0
Negative_count = 0
j = 0
# Take the Input From the User
Number = int(input("Enter the Total Number of List Elements: "))
for i in range(1, Number + 1):
value = int(input("Enter the Value of %d Element: " %i))
NumList.append(value)
# Print the List Entered by the User
print("\nList Entered By the User: ",NumList)
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)
Enter the Total Number of List Elements: 8
Enter the Value of 1 Element: 785
Enter the Value of 2 Element: -78
Enter the Value of 3 Element: -654
Enter the Value of 4 Element: -5236
Enter the Value of 5 Element: 65
Enter the Value of 6 Element: 56
Enter the Value of 7 Element: -85
Enter the Value of 8 Element: 65
List Entered By the User: [785, -78, -654, -5236, 65, 56, -85, 65]
Total Number of Positive Numbers in this List = 4
Total Number of Negative Numbers in this List = 4
# Python Program to Count the Positive and Negative Numbers in a List By Defining Function
# Define a Function
def count_Positive(NumList):
Positive_count = 0
for j in range(Number):
if(NumList[j] >= 0):
Positive_count = Positive_count + 1
return Positive_count
# Define a Function
def count_Negative(NumList):
Negative_count = 0
for j in range(Number):
if(NumList[j] <= 0):
Negative_count = Negative_count + 1
return Negative_count
NumList = []
Number = int(input("Enter the Total Number of List Elements: "))
for i in range(1, Number + 1):
value = int(input("Enter the Value of %d Element: " %i))
NumList.append(value)
# Print the List Entered by the User
print("\nList Entered By the User: ",NumList)
Positive_count = count_Positive(NumList)
Negative_count = count_Negative(NumList)
print("\nTotal Number of Positive Numbers in this List = ", Positive_count)
print("Total Number of Negative Numbers in this List = ", Negative_count)
Enter the Total Number of List Elements: 7
Enter the Value of 1 Element: -56
Enter the Value of 2 Element: 65
Enter the Value of 3 Element: 36
Enter the Value of 4 Element: -52
Enter the Value of 5 Element: -63
Enter the Value of 6 Element: 65
Enter the Value of 7 Element: 65
List Entered By the User: [-56, 65, 36, -52, -63, 65, 65]
Total Number of Positive Numbers in this List = 4
Total Number of Negative Numbers in this List = 3
Comments