Table of Contents
Introduction
In this section of python programming, we will understand the code to print negative numbers between the given range. The condition of a negative number is that the number should be less than 0.
Program
negative_list = [] n = int(input("Enter the lower bound of the list ")) m = int(input("Enter the upper bound of the list ")) # Check range if n>0 and m>0: print("There is no negative numbers in the given range") else: for i in range(n, m+1): if i < 0: negative_list.append(i) print("Negative numbers in the given range {0} and {1} using for loop are {2}".format(n, m, negative_list))
Output
Explanation
In the above python code we have taken the range from the user and checked whether the start and end both are positive integers. If any of the range value is negative then, the negative numbers in the range given are appended in the list and displayed on the screen.
0 Comments