Python program to print odd numbers within a given range
In this post, you will learn how to print the odd numbers within a given range using the Python programming language.
Python print odd numbers in range using a forloop
In the given Python program, we loop from the lower range to the upper range limit and use an if statement to check whether the number is odd or not, and print the numbers.
# Print odd numbers in range
start, end = 3, 30
print("\nOdd numbers from %d to %d are: " % (start, end))
for num in range(start, end + 1):
# checking condition
if num % 2 != 0:
print(num, end = " ")
Output of the above code-
Odd numbers from 3 to 30 are:
3 5 7 9 11 13 15 17 19 21 23 25 27 29
Python print odd numbers in range using user inputs
In the given Python program, we first read two numbers from the user using the built-in function input() and store them in separate variables x and y, respectively. Next, we loop from the lower range to the upper range limit and use an if statement to check whether the number is odd or not, and print the number.
# Print odd numbers in range
x = int(input("Enter the lower limit of range: "))
y = int(input("Enter the upper limit of range: "))
print("\nOdd numbers from %d to %d are: " % (x, y))
for i in range(x, y+1):
if(i%2==1):
print(i, end=" ")
Output of the above code-
Enter the lower limit of range: 4
Enter the upper limit of range: 20
Odd numbers from 4 to 20 are:
5 7 9 11 13 15 17 19
Python print odd numbers in range using while loop
Here, we have used the while loop to iterate from the lower range to the upper range limit and print odd numbers within this range.
# Print odd numbers in range
x = int(input("Enter the lower limit of range: "))
y = int(input("Enter the upper limit of range: "))
print("\nOdd numbers from %d to %d are: " % (x, y))
while(x < y + 1):
if(x % 2 != 0):
print(x)
x += 1
Output of the above code-
Enter the lower limit of range: 4
Enter the upper limit of range: 20
Odd numbers from 4 to 20 are:
5
7
9
11
13
15
17
19
Python print odd numbers in range using lambda function
Lambda is an anonymous function that contains any number of arguments, but only one expression. This function is defined without a name. Here, we have used the lambda function to print all odd numbers in a given range.
# Python program to print odd numbers
# within a given range
# using the lambda function
a=5;b=19
list_x=[]
for i in range(a,b+1):
list_x.append(i)
odd_num = list(filter(lambda x: (x%2!=0),list_x))
print(odd_num)
Output of the above code:
[5, 7, 9, 11, 13, 15, 17, 19]
Python print odd numbers in range using recursion
A recursion function is a function that is called by itself. In the given Python program, we use the recursion function to print odd numbers in a given range.
# Python program to print odd numbers
# within a given range
# using the recursion function
def odd_nums(start,end):
if start>end:
return
print(start+1,end=" ")
return odd_nums(start+2,end)
start=2;end=22
odd_nums(start,end)
Output of the above code:
3 5 7 9 11 13 15 17 19 21 23
Python print odd numbers in range using list comprehension
A list comprehension is a syntactic construct available in some programming languages for creating a list based on existing lists. Here, we use the list comprehension to print the odd numbers in a given range.
# Python program to print odd numbers
# within a given range
# using the list comprehension
start, end = 7, 20
x = [i for i in range(start,end+1) if i%2!=0]
print(*x)
Output of the above code:
7 9 11 13 15 17 19
Related Articles
Reverse pyramid pattern in Python
Fizzbuzz program in Python
Python split string by comma
Python alive progress bar
Python progress bar tqdm
Python language translator code
Python take screenshot of specific window
Web scraping Python BeautifulSoup
Reverse a number in Javascript
HTML open link in new tab
Python print without newline
Python iterate list with index
Python add list to list
Python random choice
Python dict inside list
Remove character from string Python
Find average of n numbers in Python
Python program to multiply two numbers
Swapping of two numbers in Python