Change language

Python program for counting positive and negative numbers in a list

Example :

  Input:  list1 = [2, -7, 5, -64, -14]  Output:  pos = 2, neg = 3  Input:  list2 = [-12, 14, 95, 3]  Output:  pos = 3, neg = 1 

Example # 1: Counting positive and negative numbers from a given list using a for loop

Iterate through each item in the list using a for loop and check if num & gt ; = 0, a condition for testing positive numbers. If the condition is met, then increase the pos_count, otherwise, increase the neg_count.

Exit :

 Positive numbers in the list: 4 Negative numbers in the list: 3 

Example # 2. Using a while loop

# Python program for counting positive and negative numbers in a list

 
# list of numbers

list1 = [ 10 , - 21 , 4 , - 45 , 66 , - 93 , 1 ]

  

pos_count, neg_count = 0 , 0

  
# iterate over each number in the list

for num in list1:

 

  # check status

if num" = 0 :

pos_count + = 1

  

  else :

neg_count + = 1

 

print ( "Positive numbers in the list:" , pos_count)

print ( "Negative numbers in the list:" , neg_count)

< p> # Python program for counting positive and negative numbers in a list

 
# list of numbers

list1 = [ - 10 , - 21 , - 4 , - 45 , - 66 , 93 , 11 ]

  

pos_count, neg_count = 0 , 0

num = 0

  
# using a while loop

while (num & lt;  len (list1)):

 

# check status

if list1 [num]" = 0 :

pos_count + = 1

else :

neg_count + = 1

 

# n increment number

num + = 1

 

print ( "Positive numbers in the list:" , pos_count)

print ( "Negative numbers in the list:" , neg_count)

Exit:

 Positive numbers in the list: 2 Negative numbers in the list: 5 

Example # 3: Using Python lambda expressions

# Python program for counting positives
# and negative numbers in the list

 
# list of numbers

list1 = [ 10 , - 21 , - 4 , 45 , 66 , 93 , - 11 ]

 

neg_count = len < / code> ( list ( filter ( lambda x: (x & lt;  0 ), list1)))

  
# we can also do len (list1) - neg_count

pos_count = len ( list ( filter ( lambda x: (x" = 0 ), list1)))

 

print ( " Positive numbers in the list: " , pos_count)

print ( "Negative numbers in the list: " , neg_count)

Output:

 Positive numbers in the list: 4 Negative numbers in the list: 3 

Example # 4: Usage

# Python program for counting positives
# and negative numbers in the list

  
# list of numbers

list1 = [ - 10 , - 21 , - 4 , - 45 , - 66 , - 93 , 11 ]

 

only_pos = [num for num in list1 if num" = 1 ]

pos_count = len (only_pos)

  

print ( "Positive numbers in the list: " , pos_count)

print ( "Negative numbers in the list:" , len (list1) - pos_count)

Exit:

 Positive numbers in the list: 1 Negative numbers in the list: 6 

Shop

Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best laptop for development

$499+
Gifts for programmers

Best laptop for Cricut Maker

$299+
Gifts for programmers

Best laptop for hacking

$890
Gifts for programmers

Best laptop for Machine Learning

$699+
Gifts for programmers

Raspberry Pi robot kit

$150

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically