Python Program To Print Odd Numbers In List Example

Python

Nicesnippets

1100

28-07-2021


Hello Friends,

In this blog, I will learn you how to print odd numbers in list using python. We will talk about python program to print odd numbers in list example. This article will give you simple example of how to display odd number in list using python.

In this example I am using for loop and check if num % 2 != 0. If the condition satisfies, then only print the number.

Here i will give you three example for python program to print odd numbers in list. So let's see below example:

Example 1


# Python program to print odd numbers

# declare list

my_list = [1,2,3,4,5,6,7,8,9]

# for loop

for num in my_list:

# checking condition

if num % 2 != 0:

print(num)

Output:

1

3

5

7

9

Example 2

# Python program to print odd numbers

# declare list

my_list = [1,2,3,4,5,6,7,8,9]

# list of numbers

num = 0

# using while loop

while(num < len(my_list)):

# checking condition

if my_list[num] % 2 != 0:

print(my_list[num])

# increment num

num += 1

Output:

1

3

5

7

9

Example 3

# Python program to print odd numbers

# declare list

my_list = [1,2,3,4,5,6,7,8,9]

# using list comprehension

odd_nos = [num for num in my_list if num % 2 != 0]

print("odd numbers is : ", odd_nos)

Output:

('odd numbers is : ', [1,3,5,7,9])

It will help you....


Recommended Posts