Python program to print all positive numbers in a range

• Difficulty Level : Medium
• Last Updated : 26 Oct, 2018

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 range in the list using for loop and check if num is greater than or equeal to 0. If the condition satisfies, then only print the number.

 # Python program to print positive Numbers in given range  start, end = -4, 19  # iterating each number in listfor num in range(start, end + 1):          # checking condition    if num >= 0:        print(num, end = " ")

Output:

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

Example #2: Taking range limit from user input

 # Python program to print positive Numbers in given range  start = int(input("Enter the start of range: "))end = int(input("Enter the end of range: "))  # iterating each number in listfor num in range(start, end + 1):          # checking condition    if num >= 0:        print(num, end = " ")

Output:

Enter the start of range: -215
Enter the end of range: 5
0 1 2 3 4 5

My Personal Notes arrow_drop_up