Open In App

Python Program to Print all Positive Numbers in a Range

Last Updated : 11 Nov, 2025
Comments
Improve
Suggest changes
16 Likes
Like
Report

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)

Output
[1, 2, 3]

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))

Output
[1, 2, 3]

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=" ")

Output
1 2 3 

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

Output
1 2 3 

Explanation: Increments start in each step, printing only positive numbers until the range ends.


Explore