Open In App

Python program to print all negative numbers in a range

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

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 

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



Similar Reads

Python program to print negative numbers in a list
Given a list of numbers, write a Python program to print all negative numbers in the given list. Example: Input: list1 = [12, -7, 5, 64, -14] Output: -7, -14 Input: list2 = [12, 14, -95, 3] Output: -95 Example #1: Print all negative numbers from the given list using for loop Iterate each element in the list using for loop and check if the number is
5 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
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 rang
4 min read
Print all the pairs that contains the positive and negative values of an element
Given an array of distinct integers, print all the pairs having a positive value and negative value of a number that exists in the array. Note: Order of the pairs doesn't matter. Examples: Input: arr[] = { 1, -3, 2, 3, 6, -1 }Output: -1 1 -3 3Input: arr[] = { 4, 8, 9, -4, 1, -1, -8, -9 }Output: -1 1 -4 4 -8 8 -9 9 Naive approach(using two loops) Al
13 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
Python program to count positive and negative numbers in a list
Given a list of numbers, write a Python program to count positive and negative numbers in a List. Example: Input: list1 = [2, -7, 5, -64, -14] Output: pos = 2, neg = 3 Input: list2 = [-12, 14, 95, 3] Output: pos = 3, neg = 1 Example #1: Count positive and negative numbers from the given list using for loop Iterate each element in the list using for
6 min read
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
Python Program to Rearrange positive and negative numbers in O(n) time and O(1) extra space
An array contains both positive and negative numbers in random order. Rearrange the array elements so that positive and negative numbers are placed alternatively. Number of positive and negative numbers need not be equal. If there are more positive numbers they appear at the end of the array. If there are more negative numbers, they too appear in t
3 min read
Find ratio of zeroes, positive numbers and negative numbers in the Array
Given an array a of integers of size N integers, the task is to find the ratio of positive numbers, negative numbers and zeros in the array up to four decimal places. Examples: Input: a[] = {2, -1, 5, 6, 0, -3} Output: 0.5000 0.3333 0.1667 There are 3 positive, 2 negative, and 1 zero. Their ratio would be positive: 3/6 = 0.5000, negative: 2/6 = 0.3
7 min read
three90RightbarBannerImg