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 ```
``` ```
``` (adsbygoogle = window.adsbygoogle || []).push({}); Shop Best laptop for Excel\$ Best laptop for Solidworks\$399+ Best laptop for Roblox\$399+ Best laptop for development\$499+ Best laptop for Cricut Maker\$299+ Best laptop for hacking\$890 Best laptop for Machine Learning\$699+ Raspberry Pi robot kit\$150 (adsbygoogle = window.adsbygoogle || []).push({}); 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 All questions (adsbygoogle = window.adsbygoogle || []).push({}); ```
``` ```
``` ```
``` News 24/09/2023 Mastering English Language for Software Developers: A Crucial Skill 16/09/2023 Explaining sorting algorithms with examples in Python 16/09/2023 What is the difference between comparison operators in Python? Wiki Python functions Python | How to copy data from one Excel sheet to another __main__ Python module Common xlabel/ylabel for matplotlib subplots ast Python module Check if one list is a subset of another in Python code Python module How to specify multiple return types using type-hints __main__ Python module Printing words vertically in Python code Python module Python Extract words from a given string Python functions Cyclic redundancy check in Python Python functions Finding mean, median, mode in Python without libraries ast Python module Python add suffix / add prefix to strings in a list ast Python module Why do I get "Pickle - EOFError: Ran out of input" reading an empty file? Python functions Python - Move item to the end of the list Python functions Python - Print list vertically © 2017—2023 Python Engineering Hub EN | ES | DE | FR | IT | RU | TR | PL | PT | JP | KR | CN | HI | NL Python.Engineering is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to amazon.com Python Loops Counters NumPy NLP Regular Expressions Wiki Tech news Python Wiki StackOverflow PHP JavaScript Books All books Computations Development Cryptography For dummies Big Data document.addEventListener("DOMContentLoaded", () => { let arrayCode = document.querySelectorAll('pre'); arrayCode.forEach(element => { element.classList.add("prettyprint"); }); }); window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'G-Q022WLXW4X'); ```