Python Program To Print Odd Numbers In List Example
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....
- Get Last 2 Digits of Number in Python Tutorial Example
- How to Sum of First and Last Digit of Number in Python?
- How to Get First and Last Digit of Number in Python?
- Get the Last Digit of Number in Python Tutorial Example
- Get the First Digit of Number in Python Tutorial Example
- How to Remove All Decimals from Number in Python?
- How to Convert String to Float with 2 Decimal Places in Python?
- How to Format Number to 2 Decimal Places in Python?