Python Program to Print all Positive Numbers in a Range (original) (raw)

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:

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:

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

`