VOOZH about

URL: https://www.geeksforgeeks.org/python/python-program-to-print-all-positive-numbers-in-a-range/

⇱ Python Program to Print all Positive Numbers in a Range - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

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.


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.


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.


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.


Output
1 2 3 

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

Related Articles:

Comment
Article Tags: