How do you print only odd numbers in a list Python?

Python program to print odd numbers in a List

Given a list of numbers, write a Python program to print all odd numbers in given list.

Example:

Input: list1 = [2, 7, 5, 64, 14] Output: [7, 5] Input: list2 = [12, 14, 95, 3, 73] Output: [95, 3, 73]
  1. Using for loop : Iterate each element in the list using for loop and check if num % 2 != 0. If the condition satisfies, then only print the number.




    # Python program to print odd Numbers in a List

    # list of numbers

    list1 = [10, 21, 4, 45, 66, 93]

    # iterating each number in list

    for num in list1:

    # checking condition

    if num % 2 != 0:

    print(num, end = " ")

    Output:

    21 45 93
  2. Using while loop :




    # Python program to print odd Numbers in a List

    # list of numbers

    list1 = [10, 21, 4, 45, 66, 93]

    i = 0

    # using while loop

    while(i < len(list1)):

    # checking condition

    if list1[i] % 2 != 0:

    print(list1[i], end = " ")

    # increment i

    i += 1

    Output:

    21 45 93
  3. Using list comprehension :




    # Python program to print odd Numbers in a List

    # list of numbers

    list1 = [10, 21, 4, 45, 66, 93]

    only_odd = [num for num in list1 if num % 2 == 1]

    print(only_odd)

    Output:

    21 45 93
  4. Using lambda expressions :




    # Python program to print odd numbers in a List

    # list of numbers

    list1 = [10, 21, 4, 45, 66, 93, 11]

    # we can also print odd no's using lambda exp.

    odd_nos = list(filter(lambda x: (x % 2 != 0), list1))

    print("Odd numbers in the list: ", odd_nos)

    Output:

    Odd numbers in the list: [21, 45, 93, 11]

How do you print only odd numbers in a list Python?




Article Tags :

Python

Python Programs

School Programming

Python list-programs

python-list

Practice Tags :

python-list

Python program to print all odd numbers in a range

Given starting and end points, write a Python program to print all odd numbers in that given range.

Example:

Input: start = 4, end = 15 Output: 5, 7, 9, 11, 13, 15 Input: start = 3, end = 11 Output: 3, 5, 7, 9, 11

Example #1: Print all odd numbers from given list using for loop

Define start and end limit of range. Iterate from start till the range in the list using for loop and check if num % 2 != 0. If the condition satisfies, then only print the number.




# Python program to print odd Numbers in given range

start, end = 4, 19

# iterating each number in list

for num in range(start, end + 1):

# checking condition

if num % 2 != 0:

print(num, end = " ")

Output:



5 7 9 11 13 15 17 19


Example #2: Taking range limit from user input




# Python program to print Even Numbers in given range

start = int(input("Enter the start of range: "))

end = int(input("Enter the end of range: "))

# iterating each number in list

for num in range(start, end + 1):

# checking condition

if num % 2 != 0:

print(num, end = " ")

Output:

Enter the start of range: 3 Enter the end of range: 11 3 5 7 9 11


Example #3: Taking range limit from user input




# Python program to print Even Numbers in given range

start = int(input("Enter the start of range: "))

end = int(input("Enter the end of range: "))

#create a list that contains only Even numbers in given range

even_list = range(start, end + 1)[start%2::2]

for num in even_list:

print(num, end = " ")

Output:

Enter the start of range: 3 Enter the end of range: 11 3 5 7 9 11

How do you print only odd numbers in a list Python?




Article Tags :

Python

Python Programs

School Programming

Python list-programs

python-list

Practice Tags :

python-list

Python program to print odd numbers in a list

PythonServer Side ProgrammingProgramming



In this article, we will learn about the solution and approach to solve the given problem statement.

Python program to print odd numbers in a List

In this tutorial, you will learn to write a program that will print all the odd numbers in a list. Odd numbers are the numbers that are not divisible by 2. We will use this property of odd numbers in our program. The concept of loops in Python and conditional statements in Python will be used in our program.

For a given list of numbers, the task is to find and print all the odd numbers in the list.

Input: [2, 7, 4, 10, 8, 6, 9]

Output: [7, 9]

Input: [11, 6, 2, 9, 10, 4, 26, 25]

Output: [11, 9, 25]

Python Program to Print Odd Numbers in a List using For Loop

In thispython program, we are usingFor Loopto iterate each element in thislist. Inside thePythonfor loop, we are using the If statement to check and print odd numbers.

# Python Program to Print Odd Numbers in a List NumList = [] Number = int(input("Please enter the Total Number of List Elements: ")) for i in range(1, Number + 1): value = int(input("Please enter the Value of %d Element : " %i)) NumList.append(value) print("\nOdd Numbers in this List are : ") for j in range(Number): if(NumList[j] % 2 != 0): print(NumList[j], end = ' ')Python Program to Print Odd Numbers in a List 1

User entered list elements = [3, 4, 5, 9]. Within this python program, the For Loopiteration is

For Loop – First Iteration:for 0 in range(0, 4)
The condition is True. So, it enters into the If Statement

if(NumList[0] % 2 != 0) => if(3 % 2 != 0) – Condition is True
This Number printed.

Second Iteration: for 1 in range(0, 4) –Condition is True
if(NumList[1] % 2 != 0) => if(4 % 2 != 0) – Condition is False
This Number Skipped.

Third Iteration: for 2 in range(0, 4) –Condition is True
if(NumList[2] % 2 != 0) => if(5 % 2 != 0) – Condition is True
This Number printed.

Fourth Iteration: for 3 in range(0, 4) –Condition is True
if(9 % 2 != 0) – Condition is True
This Number also printed.

Fifth Iteration: for 4 in range(0, 4) –Condition is False
So, it exits from PythonFor Loop