GeeksforGeeks App
Open App
Browser
Continue

# 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 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 ```

## 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

## 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]`

## Python3

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

Output

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

## 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 `

## 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 `

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` `# Taking input values for start and end of the range``start ``=` `-``4``end ``=` `5` `# Creating an array using numpy.arange()``arr ``=` `np.arange(start, end``+``1``)` `# Filtering negative numbers using numpy.where()``neg_arr ``=` `arr[np.where(arr<``0``)]` `# Printing the resulting array``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().

My Personal Notes arrow_drop_up