Python program to print all negative numbers in a range
Last Updated :
13 Apr, 2023
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
def negativenumbers(a,b):
out = [i for i in range (a,b + 1 ) if i< 0 ]
print ( * out)
a = - 4
b = 5
negativenumbers(a,b)
|
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
start, end = - 4 , 19
for num in range (start, end + 1 ):
if num < 0 :
print (num, end = " " )
|
Example #2: Taking range limit from user input
Python3
start = int ( input ( "Enter the start of range: " ))
end = int ( input ( "Enter the end of range: " ))
for num in range (start, end + 1 ):
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
a = - 4 ;b = 5
li = []
for i in range (a,b):
li.append(i)
negative_num = list ( filter ( lambda x: (x< 0 ),li))
print (negative_num)
|
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 ])
|
Method: Using list comprehension
Python3
a = - 4 ;b = 5
print ([i for i in range (a,b + 1 ) if i< 0 ])
|
Method: Using pass()
Python3
a = - 4 ;b = 5
for i in range (a,b + 1 ):
if i> = 0 :
pass
else :
print (i,end = " " )
|
Method: Using recursion
Python3
def PrintNegative(itr,end):
if itr = = end:
return
if itr < 0 :
print (itr,end = " " )
PrintNegative(itr + 1 ,end)
return
a = - 5
b = 5
PrintNegative(a,b)
|
Method: Using numpy.arange() and numpy.where():
note: install numpy module using command “pip install numpy”
Here’s the method to print all negative numbers in a range using numpy.arange() and numpy.where():
Algorithm:
Take input values for start and end of the range.
Create an array using numpy.arange() with the start and end values.
Use numpy.where() to filter the negative numbers from the array.
Print the resulting array.
Python code:
Python3
import numpy as np
start = - 4
end = 5
arr = np.arange(start, end + 1 )
neg_arr = arr[np.where(arr< 0 )]
print (neg_arr)
|
Output:
[-4 -3 -2 -1]
Time complexity: O(n) for arange function.
Auxiliary space: O(n), where n is the length of the array generated by numpy.arange().
Please Login to comment...