![]() |
VOOZH | about |
Given a list of numbers, the task is to print all positive numbers in the list. A positive number is any number greater than 0.
For example:
a = [-10, 15, 0, 20, -5, 30, -2]
Positive numbers = 15, 20, 30
This method uses NumPy arrays to efficiently extract all positive numbers at once using boolean indexing.
[15 20 30]
This method creates a new list of positive numbers using list comprehension in one line by checking each element of the original list.
[15, 20, 30]
The filter() function can select elements from the list that meet a given condition. We use it with a lambda function to filter positive numbers.
[15, 20, 30]
The most basic method for printing positive numbers is to use a for loop to iterate through the list and check each element.
15 20 30