Open In App

Python program to print all positive numbers in a range

Given start and end of a range, write a Python program to print all positive numbers in given range. 

Example:



Input: start = -4, end = 5
Output: 0, 1, 2, 3, 4, 5 

Input: start = -3, end = 4
Output: 0, 1, 2, 3, 4

Example #1: Print all positive numbers from given list using for loop Define start and end limit of range. Iterate from start till the range in the list using for loop and check if num is greater than or equal to 0. If the condition satisfies, then only print the number. 




# Python program to print positive Numbers in given range
  
start, end = -4, 19
  
# iterating each number in list
for num in range(start, end + 1):
  
    # checking condition
    if num >= 0:
        print(num, end=" ")

Output:



0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 

Time Complexity: O(N)

Here N is value of range, i.e. (end – start).

Auxiliary Space: O(1)

As constant extra space is used.

Example #2: Taking range limit from user input 




# Python program to print positive Numbers in given range
  
start = int(input("Enter the start of range: "))
end = int(input("Enter the end of range: "))
  
# iterating each number in list
for num in range(start, end + 1):
  
    # checking condition
    if num >= 0:
        print(num, end=" ")

Output:

Enter the start of range: -215
Enter the end of range: 5
0 1 2 3 4 5 

Time Complexity: O(N)

Here N is value of range, i.e. (end – start).

Auxiliary Space: O(1)

As constant extra space is used.

Method: Using the lambda function




# Python code To print all positive numbers 
# in a given range using the lambda function
a=-4;b=5 
li=[]
for i in range(a,b+1):
    li.append(i)
# printing positive numbers using the lambda function
positive_num = list(filter(lambda x: (x>=0),li))  
print(positive_num)

Output
[0, 1, 2, 3, 4, 5]

Time Complexity: O(N)

Here N is value of range, i.e. (end – start).

Auxiliary Space: O(1)

As constant extra space is used.

Method: Using the list comprehension 




# Python code
# To print all positive numbers in a given range
a=-4;b=5
out=[i for i in range(a,b+1) if i>0]
  # print the all positive numbers
print(*out)

Output

[0, 1, 2, 3, 4, 5]

Time Complexity: O(N)

Here N is value of range, i.e. (end – start).

Auxiliary Space: O(1)

As constant extra space is used.

Method: Using enumerate function 




a=-4;b=5;l=[]
for i in range(a,b+1):
  l.append(i)
print([a for j,a in enumerate(l) if a>=0])

Output
[0, 1, 2, 3, 4, 5]

Time Complexity: O(N)

Here N is value of range, i.e. (end – start).

Auxiliary Space: O(1)

As constant extra space is used.

Method: Using pass() 




a=-4;b=5 
for i in range(a,b+1):
  if i<0:
    pass 
  else:
    print(i,end=" ")

Output
0 1 2 3 4 5 

Time Complexity: O(N)

Here N is value of range, i.e. (end – start).

Auxiliary Space: O(1)

As constant extra space is used.

Method: Using recursion




def printPositives(start,end):   #defining recursive function to print positives
  if start==end:return  #base condition
  if start>=0:   #check for positive number
    print(start,end=' ')
  printPositives(start+1,end)  # recursive calling
a,b=-5,10
printPositives(a,b) #function calling

Output
0 1 2 3 4 5 6 7 8 9 

Time Complexity: O(N)

Here N is value of range, i.e. (end – start).

Auxiliary Space: O(N)

As we call recursively N extra space is used.

Method : Using filter() function:




a = -4
b = 5
  
positive_nums = list(filter(lambda x: x >= 0, range(a, b+1)))
print(positive_nums)

Output
[0, 1, 2, 3, 4, 5]

Time Complexity: O(N) as we are iterating to the range between a and b.

Space Complexity: O(N) as list() function creates a new list with O(n) space complexity.


Article Tags :