Home ยป How to Count Positive and Negative Numbers in a List with python

How to Count Positive and Negative Numbers in a List with python

1 Year Subscription
Java SE 11 Programmer I [1Z0-815] Practice Tests
Spring Framework Basics Video Course
Java SE 11 Developer (Upgrade) [1Z0-817]
Java SE 11 Programmer II [1Z0-816] Practice Tests
Oracle Java Certification

In this article we show how to count positive and negative elements in a list

We will show 2 metjods of doing this

Table of Contents

Example 1

Method 1 uses a for loop

# Count the Positive and Negative Numbers in a List Using a For Loop

mylist = []
positive_number = 0
negative_number = 0

# Input From the User
Number = int(input("Enter the Number of List Elements: "))
for i in range(1, Number + 1):
    value = int(input("Enter the Value of Element %d : " %i))
    mylist.append(value)

# Print the List Entered by the User
print("Users list is: ",mylist)

#loop through incrementing counts
for j in range(Number):
    if(mylist[j] >= 0):
        positive_number = positive_number + 1
    else:
        negative_number = negative_number + 1

# Print the totals
print("\nTotal Number of Positive Numbers in the List =  ", positive_number)
print("\nTotal Number of Negative Numbers in the List = ", negative_number)

Lets try this example out

>>> %Run listcountposneg1.py
Enter the Number of List Elements: 6
Enter the Value of Element 1 : 1
Enter the Value of Element 2 : -5
Enter the Value of Element 3 : 4
Enter the Value of Element 4 : -6
Enter the Value of Element 5 : 3
Enter the Value of Element 6 : 2
Users list is:  [1, -5, 4, -6, 3, 2]

Total Number of Positive Numbers in the List =   4

Total Number of Negative Numbers in the List =  2

Example 2

Method 1 uses a while loop

# Count the Positive and Negative Numbers in a List Using a While Loop

mylist = []
positive_number = 0
negative_number = 0
j = 0

# Input From the User
Number = int(input("Enter the Number of List Elements: "))
for i in range(1, Number + 1):
    value = int(input("Enter the Value of Element %d : " %i))
    mylist.append(value)

# Print the List Entered by the User
print("Users list is: ",mylist)

#loop through incrementing counts
while(j < Number): if(mylist[j] >= 0):
        positive_number = positive_number + 1
    else:
        negative_number = negative_number + 1
    j = j + 1

# Print the totals
print("\nTotal Number of Positive Numbers in the List =  ", positive_number)
print("\nTotal Number of Negative Numbers in the List = ", negative_number)

Lets see a test run

>>> %Run listcountposneg2.py
Enter the Number of List Elements: 7
Enter the Value of Element 1 : 1
Enter the Value of Element 2 : 2
Enter the Value of Element 3 : 3
Enter the Value of Element 4 : 4
Enter the Value of Element 5 : -3
Enter the Value of Element 6 : -4
Enter the Value of Element 7 : 2
Users list is:  [1, 2, 3, 4, -3, -4, 2]

Total Number of Positive Numbers in the List =   5

Total Number of Negative Numbers in the List =  2

Link

This is in our github repository

https://github.com/programmershelp/maxpython/tree/main/code%20example/List%20examples

You may also like

Leave a Comment

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More