Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python program to print all positive numbers in a range
When working with ranges that include both positive and negative numbers, you often need to extract only the positive values. Python provides several approaches to filter positive numbers from a range: list comprehension, removal of negatives, filtering with conditions, and using the filter() function.
Method 1: Using List Comprehension
Create a separate list containing only positive numbers from the range ?
low_num = -10
high_num = 15
numbers = list(range(low_num, high_num + 1))
print("Original range:", numbers)
# Extract positive numbers using list comprehension
positive_numbers = [num for num in numbers if num > 0]
print("Positive numbers:", positive_numbers)
Original range: [-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] Positive numbers: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
Method 2: Removing Non-Positive Elements
Start with the full range and remove all non-positive numbers ?
low_num = -8
high_num = 12
numbers = list(range(low_num, high_num + 1))
print("Original range:", numbers)
# Remove non-positive numbers
numbers = [num for num in numbers if num > 0]
print("After removing non-positive:", numbers)
Original range: [-8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] After removing non-positive: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
Method 3: Using filter() with Lambda
Apply the filter() function with a lambda expression to select positive numbers ?
low_num = -6
high_num = 10
numbers = list(range(low_num, high_num + 1))
print("Original range:", numbers)
# Filter positive numbers using filter() and lambda
positive_numbers = list(filter(lambda x: x > 0, numbers))
print("Filtered positive numbers:", positive_numbers)
Original range: [-6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] Filtered positive numbers: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Method 4: Direct Range Generation
Generate only positive numbers by starting the range from 1 ?
low_num = -5
high_num = 8
print(f"Original range would be: {low_num} to {high_num}")
# Generate only positive numbers directly
positive_numbers = list(range(1, high_num + 1))
print("Direct positive range:", positive_numbers)
# Alternative: use max() to ensure we start from 1
positive_numbers_alt = list(range(max(1, low_num), high_num + 1))
print("Using max() approach:", positive_numbers_alt)
Original range would be: -5 to 8 Direct positive range: [1, 2, 3, 4, 5, 6, 7, 8] Using max() approach: [1, 2, 3, 4, 5, 6, 7, 8]
Comparison
| Method | Memory Efficiency | Readability | Best For |
|---|---|---|---|
| List Comprehension | Good | High | Simple filtering with clear logic |
| Remove Non-Positive | Moderate | Medium | When you need to modify existing list |
| filter() + Lambda | Good | Medium | Functional programming approach |
| Direct Range | Excellent | High | When you only need positive numbers |
Conclusion
List comprehension offers the best balance of readability and efficiency for filtering positive numbers. Use direct range generation when you only need positive values, and filter() for functional programming approaches.
