Home »
Python »
Python programs
Python program to print all positive or negative numbers in a range
Here, we are going to learn how to print all positive or negative numbers between a given range in Python?
Submitted by Shivang Yadav, on April 09, 2021
Python programming language is a high-level and object-oriented programming language. Python is an easy to learn, powerful high-level programming language. It has a simple but effective approach to object-oriented programming.
Printing all positive numbers in a range
We will take two variables as the lower and upper limit of a range. And print all the positive numbers in a range.
Example:
Input:
-3 5
Output:
0, 1, 2, 3, 4, 5
To find all positive numbers in the range, we will take input from the user and then loop over the range. And print all the numbers which are greater than or equal to 0.
Program to print all positive numbers in a range
# Python program to print all
# positive numbers in a range
# Getting list from user
myList = []
lLimit = int(input("Enter Lower limit of the range : "))
uLimit = int(input("Enter Upper limit of the range : "))
# printing all positive values in a range
print("All positive numbers of the range : ")
for i in range(lLimit, uLimit):
if i >= 0:
print(i, end = " ")
Output:
Enter Lower limit of the range : -5
Enter Upper limit of the range : 10
All positive numbers of the range :
0 1 2 3 4 5 6 7 8 9
* Here, the upper value is not included.
Printing all negative numbers in a range
We will take two variables as the lower and upper limit of a range. And print all the negative numbers in a range.
Example:
Input:
-3 5
Output:
-3 -2 -1
To find all negative numbers in the range, we will take input from the user and then loop over the range. And print all the numbers which are less than 0.
Program to print all positive numbers in a range
# python program to print all negative numbers in a range
# Getting list from user
myList = []
lLimit = int(input("Enter Lower limit of the range : "))
uLimit = int(input("Enter Upper limit of the range : "))
# printing all positive values in a range
print("All negative numbers of the range : ")
for i in range(lLimit, uLimit):
if i < 0:
print(i, end = " ")
Output:
Enter Lower limit of the range : -4
Enter Upper limit of the range : 3
All negative numbers of the range :
-4 -3 -2 -1
Python Basic Programs »