Skip to content
Related Articles
Open in App
Not now

Related Articles

Python program to print all negative numbers in a range

Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 13 Mar, 2023
Improve Article
Save Article
Like Article

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 range. Iterate from start range to end range using for loop and check if num is less than 0. If the condition satisfies, then only print the number. 

Python3




# Python code
# To print all negative numbers in a given range 
  
  
def negativenumbers(a,b): 
  # Checking condition for negative numbers 
  # single line solution 
  out=[i for i in range(a,b+1) if i<0
  # print the all negative numbers
  print(*out) 
  
# driver code 
# a -> start range 
a=-4 
# b -> end range 
b=5 
negativenumbers(a,b)

Output

-4 -3 -2 -1

Time Complexity: O(n)
Auxiliary Space: O(1)

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

Python3




# Python program to print negative 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

-4 -3 -2 -1 

  Example #2: Taking range limit from user input 

Python3




# Python program to print negative 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: -15
Enter the end of range: 5
-15 -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 

Method: Using lambda function 

Python3




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

Output

[-4, -3, -2, -1]

Time Complexity: O(n)
Auxiliary Space: O(n), where n is length of list

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

[-4, -3, -2, -1]

Method: Using list comprehension

Python3




a=-4;b=5
print([i for i in range(a,b+1) if i<0])

Output

[-4, -3, -2, -1]

Method: Using pass()  

Python3




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

Output

-4 -3 -2 -1 

Method: Using recursion

Python3




#Recursive function to print Negative numbers
def PrintNegative(itr,end):
  if itr == end: #Base Condition
    return
  if itr < 0#checking Negative or not
    print(itr,end=" ")
  PrintNegative(itr+1,end)  #Recursive function call
  return
a = -5
b = 5
PrintNegative(a,b) 

Output

-5 -4 -3 -2 -1 

My Personal Notes arrow_drop_up
Like Article
Save Article
Related Articles

Start Your Coding Journey Now!