Last Updated : 11 Jul, 2025
In this article, we will explore various methods to print all positive numbers in a range. The simplest way to do this is by using a loop. Use a simple for loop to iterate through the given range and check if each number is greater than zero before printing it.
Python
start = -5
end = 3
# Loop through range and print positive numbers
for val in range(start, end + 1):
# checks whether current number is positive
if val > 0:
print(val)
Let's explore other different ways to print all positive numbers in a range
Using List comprehensionList comprehension provides a more compact and efficient way to filter numbers. In this method, we will create a list of positive numbers using a list comprehension and then print them.
Python
start = -5
end = 3
# Use list comprehension to filter positive numbers and print them
res = [val for val in range(start, end + 1) if val > 0]
print(res)
Explanation: [val for val range(start, end + 1) if val > 0] iterates through the range and only includes the numbers that are greater than zero.
Using filter() Functionfilter() function allows us to filter elements from an iterable based on a condition. In this case, we will use filter() to get positive numbers in the range and then print them.
Python
start = -5
end = 3
# Use filter to get positive numbers
res = filter(lambda val: val > 0, range(start, end + 1))
# Convert filter object to list and print
print(list(res))
Explanation:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4