;

Python Program to Count the Positive and Negative Numbers in a List


Tutorialsrack 30/04/2020 Python

In this python program, we will learn how to count the positive and negative numbers in a given list. In this program, we will identify the positive and negative numbers and display the total number of positive and negative numbers in a given list.

Here is the source code of the program to count the positive and negative numbers in a list.

Program 1: Python Program to Count the Positive and Negative Numbers in a List Using For Loop

Python Program to Count the Positive and Negative Numbers in a List Using For Loop
# 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)
Output

Enter the Total Number of List Elements: 6

Enter the Value of 1 Element: -8

Enter the Value of 2 Element: -3

Enter the Value of 3 Element: 5

Enter the Value of 4 Element: 4

Enter the Value of 5 Element: -6

Enter the Value of 6 Element: 2

 

List Entered By the User:  [-8, -3, 5, 4, -6, 2]

 

Total Number of Positive Numbers in this List =   3

Total Number of Negative Numbers in this List =  3

Program 2: Python Program to Count the Positive and Negative Numbers in a List Using While Loop

Python Program to Count the Positive and Negative Numbers in a List Using While Loop
# 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)
Output

Enter the Total Number of List Elements: 6

Enter the Value of 1 Element: -8

Enter the Value of 2 Element: 6

Enter the Value of 3 Element: 4

Enter the Value of 4 Element: 6

Enter the Value of 5 Element: 3

Enter the Value of 6 Element: 2

 

List Entered By the User:  [-8, 6, 4, 6, 3, 2]

 

Total Number of Positive Numbers in this List =  5

Total Number of Negative Numbers in this List =  1

Program 3: Python Program to Count the Positive and Negative Numbers in a List By Defining Function

Python Program to Count the Positive and Negative Numbers in a List By Defining Function
# 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] % 2 != 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)
Output

Enter the Total Number of List Elements: 6

Enter the Value of 1 Element: -5

Enter the Value of 2 Element: -5

Enter the Value of 3 Element: -6

Enter the Value of 4 Element: -4

Enter the Value of 5 Element: 5

Enter the Value of 6 Element: 3

 

List Entered By the User:  [-5, -5, -6, -4, 5, 3]

 

Total Number of Positive Numbers in this List =   2

Total Number of Negative Numbers in this List =  4


Related Posts



Comments

Recent Posts
Tags