Table of Contents
Introduction
In this section of python programming, we will print the odd numbers in the range provided by the user. We have already seen in the previous sections to find the odd numbers in the list.
Expression: num % 2 != 0
Program
negative_list = [] n = int(input("Enter the lower bound of the list ")) m = int(input("Enter the upper bound of the list ")) # Using for loop new_list = [] for i in range(n, m+1): if i % 2!= 0: new_list.append(i) print("Odd numbers in the range {0} to {1} is {2}".format(n,m, new_list))
Output
Explanation
In the above code we have used “for loop” to find the odd numbers in the range n to m + 1 (to include upper bound). The “if condition” will check whether the number is odd or not. If the number is odd, it is appended to the list. When all the values get traversed in the range, the odd values are printed on the screen.
0 Comments