ADVERTISEMENT
ADVERTISEMENT

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 »



ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.