Program to find the odd numbers in the list using Python

Introduction

Previously we have seen how to find the even numbers in the list. Now, in this section of python programming we will understand the code to find the odd numbers in the given list.

Expression: num % 2 != 0

Program to find the odd numbers in the list

Program

list_input = [2, 4, 11, 17, 19]
#Using lambda expression
output1 = list(filter(lambda x: (x % 2 != 0), list_input))
print("Odd numbers using lambda expression: ", output1)
# Using for loop
new_list = []
for i in list_input:
    if i % 2!= 0:
        new_list.append(i)
print("Odd numbers using for loop:", new_list)
# Uisng while loop
new_list1 = []
num = 0
while num < len(list_input):
    if list_input[num] % 2 != 0:
        new_list1.append(list_input[num])
    num += 1
print("Odd numbers using while loop:", new_list1)

Output

Program to find the odd numbers in the list Output

Explanation

In the above python code we have used three methods (same as what we used to find the even numbers) to find the odd numbers in the given list.

As compared to finding the even numbers, finding the odd numbers differs in the expression. In “for and while loop”, we have checked for each value in the list whether it leaves remainder 0 on dividing by 2. If the remainder is not equalled to 0, then the number is odd and the value is printed on the screen.

Similarly, when using lambda expression, we have given the property (NUM% 2 != 0) as an expression to find whether the numbers are odd numbers or not.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.