Python program to print all positive numbers in a range


Sometimes the task is to select only the positive numbers from a given range. Here, in this Python article, first, the range is taken as input and then the negative as well as positive integers within this range are chosen. In this Python article, from these numbers only the positive numbers are then selected using the different methods in four different examples. In example 1, the positive numbers are picked and separated into another list. In the example 2, all the elements that are not positive are removed. In example 3, the sorted list is split up to zero and only positives are retained. In example 4, the filter is used to select the positive numbers.

Example 1 - Select only the positive numbers in a range and separate these to another list to print

Algorithm

Step 1 − Specify the smallest and highest number in the range as input. The smallest number should be negative and the highest number should be positive. Make a list of integers between the given range

Step 2 − First separate all positive numbers in a separate list. Print these numbers.

Step 3 − Run the program and then check the result.

The Python File Contains this

lowNum=-50
highNum=60
mainlist=[]
listPos=[]

#Making the main list with integers starting from lowNum and upto HighNum
for item in range(lowNum, highNum+1):
   mainlist.append(item)

print("In the given range from ", lowNum, " to", highNum, " :")
print("\nThe Main List :")
print(mainlist)

#dividing the main list into negatives and positives 
for item in mainlist:
   if (item > 0): 
      listPos.append(item)
            
print("\nThe Negative Elements in the Range :")
print(listPos)

Viewing The Result - Example 1

For seeing the result run the Python file in the cmd window.

In the given range from  -50  to 60  :

The Main List :
[-50, -49, -48, -47, -46, -45, -44, -43, -42, -41, -40, -39, -38, -37, -36, -35, -34, -33, -32, -31, -30, -29, -28, -27, -26, -25, -24, -23, -22, -21, -20, -19, -18, -17, -16, -15, -14, -13, -12, -11, -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, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60]

The Positive Elements in the Range :
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60]

Example 2: Remove all the non positive elements and print the remaining positive numbers

Algorithm

Step 1 − First input the smallest and highest number in the range. The smallest number should be negative and the highest number should be positive. Make a list of all integers between the given range

Step 2 − First make a copy of this as a sorted list and then remove all less than and equal to zero numbers from this.

Step 3 − Print all the left-out positive numbers from the main list.

Step 4 − Run the program and then check the result.

The Python File Contains this

lowNum=-40
highNum=70
mainlist=[]
mainlistcopy=[]

#Making the main list with integers starting from lowNum and upto HighNum
for item in range(lowNum, highNum+1):
   mainlist.append(item)


print("In the given range from ", lowNum, " to", highNum, " :")
print("\nThe Main List :")
print(mainlist)

mainlistcopy=sorted(mainlist)

for item in mainlistcopy:
   if (item <= 0): 
      mainlist.remove(item)
            
print("\nThe Positive Elements in the List :")
print(mainlist)

Viewing The Result - Example 2

Open the cmd window and run the python file to see the result.

Enter a negative number for the start of a Range (example -100):  -40
Enter a positive number for the end of a Range (example 100): 70
In the given range from  -40  to 70  :

The Main List :
[-40, -39, -38, -37, -36, -35, -34, -33, -32, -31, -30, -29, -28, -27, -26, -25, -24, -23, -22, -21, -20, -19, -18, -17, -16, -15, -14, -13, -12, -11, -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, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70]

The Positive Elements in the List :
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70]

Example 3 - Split the sorted list upto zero and retain the positives to print

Algorithm

Step 1− Specify as input the smallest and highest number in the range. The smallest number should be negative and the highest number should be positive. Make a list of all the integers between the input range

Step 2− First make a copy of this list and then split this list up to zero.

Step 3− Print all the left-out positive numbers in the list copy.

Step 4− Run the program and then check the result.

The Python File Contains this

lowNum=-20
highNum=40
mainlist=[]
listPos=[]

#Making the main list with integers starting from lowNum and upto HighNum
for item in range(lowNum, highNum+1):
   mainlist.append(item)

print("In the given range from ", lowNum, " to", highNum, " :")
print("\nThe Main List :")
print(mainlist)

listPos=mainlist

listPos[:] = [item for item in listPos if item > 0]
          
print("\nThe Positive Elements in the List :")
print(listPos)

Viewing The Result - Example 3

For seeing the result run the Python file in the cmd window.

Enter a negative number for the start of a Range (example -100):  -20
Enter a positive number for the end of a Range (example 100): 40
In the given range from  -20  to 40  :

The Main List :
[-20, -19, -18, -17, -16, -15, -14, -13, -12, -11, -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, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40]

The Positive Elements in the List :
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40]

Fig 3: Showing the result in the command window.

Example 4: Using filter select the positive numbers and filter out the non positive ones

Algorithm

Step 1 − Provide the input for the smallest and highest number in the range. The smallest number should be negative and the highest number should be positive. Make a list of all integers between this input range

Step 2 − Use a filter with a lambda function.

Step 3 − Filter out the positives in a new list. Print all the filtered positive numbers in the new list.

Step 4 − Run the program and then check the result.

The Python File Contains this

lowNum=-50
highNum=50
mainlist=[]
listPos=[]

#Making the main list with integers starting from lowNum and upto HighNum
for item in range(lowNum, highNum+1):
   mainlist.append(item)

print("In the given range from ", lowNum, " to", highNum, " :")
print("\nThe Main List :")
print(mainlist)

listPos = list(filter(lambda item: item > 0, mainlist))
          
print("\nThe Positive Elements in the Range :")
print(listPos)

Viewing The Result - Example 4

Open the cmd window and run the python file to see the result.

Enter a negative number for the start of a Range (example -100):  -50
Enter a positive number for the end of a Range (example 100): 50
In the given range from  -50  to 50  :

The Main List :
[-50, -49, -48, -47, -46, -45, -44, -43, -42, -41, -40, -39, -38, -37, -36, -35, -34, -33, -32, -31, -30, -29, -28, -27, -26, -25, -24, -23, -22, -21, -20, -19, -18, -17, -16, -15, -14, -13, -12, -11, -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, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50]

The Positive Elements in the Range :
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50]]

In this Python article, using four different examples, the ways to show how to find the positive numbers from a given range and to print these were given. First, in example 1, the positive numbers were separated into another list. In example 2, all the elements that were negatives were removed. In example 3, the list was split and only positives were retained. In example 4, filter and lambda were used to select the positive numbers.

Updated on: 28-Jul-2023

326 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements