Python Program to Print all Positive Numbers in a Range
Last Updated :
11 Nov, 2025
Given two integers, start and end, the task is to print all positive numbers between the given range, including both endpoints. For Examples:
Input: start = -5, end = 3
Output: [1, 2, 3]
Below are the different methods to solve this problem:
Using List comprehension
List comprehension iterates over the range, selects numbers greater than zero, and stores them in a list.
Python
a = -5
b = 3
res = [i for i in range(a, b + 1) if i > 0]
print(res)
Explanation:
- range(a, b + 1): generates numbers from start to end (inclusive).
- if i > 0: keeps only positive numbers.
- The result is a list of all positive values in the given range.
Using filter() Function
The filter() function applies a condition to each element of the range. It returns only elements that satisfy the condition.
Python
a = -5
b = 3
res = filter(lambda val: val > 0, range(a, b + 1))
print(list(res))
Explanation:
- lambda val: val > 0: defines a function that returns True for positive numbers.
- filter(): selects only those numbers.
- list(): converts the filter object into a list for display.
Using for Loop
This approach uses a simple loop to check each number in the given range and print it if it’s positive.
Python
a = -5
b = 3
for i in range(a, b + 1):
if i > 0:
print(i, end=" ")
Explanation: Iterates from start to end, prints the number only if it’s greater than zero.
Using while Loop
A while loop can also be used to iterate manually from start to end and print positive numbers.
Python
a = -5
b = 3
while a <= b:
if a > 0:
print(a, end=" ")
a += 1
Explanation: Increments start in each step, printing only positive numbers until the range ends.
Related Articles:
Explore
Python Fundamentals
Python Data Structures
Advanced Python
Data Science with Python
Web Development with Python
Python Practice