Open In App

Python program to print all positive numbers in a range

Last Updated : 13 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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. 

Python3




# 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 

Python3




# 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

Python3




# 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 

Python3




# 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 

Python3




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() 

Python3




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

Python3




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:

  • The range() function produces a range of integers from a to b (inclusive).
  • The filter() function is used with a lambda function as its first argument and the range as its second argument. The lambda function returns True for any integer that is greater than or equal to 0.
  • The filter() function returns an iterator, which is converted to a list using the list() function.
  • The resulting list contains only the positive integers within the range [a, b], which are printed to the console using the print() function.
    So, the overall approach is to use the range() function to generate a range of integers, filter out the negative integers using the filter() function with a lambda function, and convert the resulting iterator to a list using the list() function.

Python3




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.



Similar Reads

Python Program to find Sum of Negative, Positive Even and Positive Odd numbers in a List
Given a list. The task is to find the sum of Negative, Positive Even, and Positive Odd numbers present in the List. Examples: Input: -7 5 60 -34 1 Output: Sum of negative numbers is -41 Sum of even positive numbers is 60 Sum of odd positive numbers is 6 Input: 1 -1 50 -2 0 -3 Output: Sum of negative numbers is -6 Sum of even positive numbers is 50
7 min read
Find longest range from numbers in range [1, N] having positive bitwise AND
Given a number N, the task is to find the longest range of integers [L, R] such that 1 ≤ L ≤ R ≤ N and the bitwise AND of all the numbers in that range is positive. Examples: Input: N = 7Output: 4 7Explanation: Check and from 1 to 7Bitwise AND operations:from 1 to 7 is 0 from 2 to 7 is 0from 3 to 7 is 0from 4 to 7 is 4Therefore, maximum range comes
6 min read
Generate an array with K positive numbers such that arr[i] is either -1 or 1 and sum of the array is positive
Given two positive integers, N and K ( 1 ≤ K ≤ N ), the task is to construct an array arr[](1-based indexing) such that each array element arr[i] is either i or -i and the array contains exactly K positive values such that sum of the array elements is positive. If more than one such sequence can be generated, print any of them. Otherwise, print "-1
7 min read
Find the last positive element remaining after repeated subtractions of smallest positive element from all Array elements
Given an array arr[] consisting of N positive integers, the task is to find the last positive array element remaining after repeated subtractions of the smallest positive array element from all array elements. Examples: Input: arr[] = {3, 5, 4, 7}Output: 2Explanation: Subtract the smallest positive element from the array, i.e. 3, the new array is a
6 min read
Python program to print positive numbers in a list
Given a list of numbers, write a Python program to print all positive numbers in given list. Example: Input: list1 = [12, -7, 5, 64, -14] Output: 12, 5, 64 Input: list2 = [12, 14, -95, 3] Output: [12, 14, 3] Example #1: Print all positive numbers from given list using for loop Iterate each element in the list using for loop and check if number is g
4 min read
Python program to print all negative numbers in a range
Given the start and end of a range, write a Python program to print all negative numbers in a given range. Examples: Input: a = -4, b = 5 Output: -4, -3, -2, -1 Input: a = -3, b= 4 Output: -3, -2, -1 Method: Print all the negative numbers using a single-line solution. Print all negative numbers using for loop. Define the start and end limits of the
4 min read
Python program to print all even numbers in a range
Given starting and end points, write a Python program to print all even numbers in that given range. Example: Input: start = 4, end = 15 Output: 4, 6, 8, 10, 12, 14 Input: start = 8, end = 11 Output: 8, 10 Example #1: Print all even numbers from the given list using for loop Define start and end limit of range. Iterate from start till the range in
5 min read
Python program to print all odd numbers in a range
Given starting and endpoints, write a Python program to print all odd numbers in that given range. Example: Input: start = 4, end = 15 Output: 5, 7, 9, 11, 13, 15 Input: start = 3, end = 11 Output: 3, 5, 7, 9, 11 Example #1: Print all odd numbers from the given list using for loop Define the start and end limit of the range.Iterate from start till
7 min read
Only integer with positive value in positive negative value in array
Given an array of N integers. In the given, for each positive element 'x' there exist a negative value '-x', except one integer whose negative value is not present. That integer may occur multiple number of time. The task is find that integer. Examples: Input : arr[] = { 1, 8, -6, -1, 6, 8, 8 } Output : 8 All positive elements have an equal negativ
8 min read
Number of ways to obtain each numbers in range [1, b+c] by adding any two numbers in range [a, b] and [b, c]
Given three integers a, b and c. You need to select one integer from the range [a, b] and one integer from the range [b, c] and add them. The task to calculate the number of ways to obtain the sum for all the numbers in the range [1, b+c]. Examples: Input: a = 1, b = 2, c = 2 Output: 0, 0, 1, 1 Explanation: The numbers to be obtained are [1, b+c] =
10 min read
three90RightbarBannerImg