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. Show Example: Input: list1 = [2, 7, 5, 64, 14] Output: [7, 5] Input: list2 = [12, 14, 95, 3, 73] Output: [95, 3, 73]
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, 11Example #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.
Output: 5 7 9 11 13 15 17 19
Output: Enter the start of range: 3 Enter the end of range: 11 3 5 7 9 11
Output: Enter the start of range: 3 Enter the end of range: 11 3 5 7 9 11Article Tags : Python Python Programs School Programming Python list-programs python-list Practice Tags : python-list Python program to print odd numbers in a listPythonServer 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 ListIn 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 LoopIn 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 = ' ')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) if(NumList[0] % 2 != 0) => if(3 % 2 != 0) – Condition is True Second Iteration: for 1 in range(0, 4) –Condition is True Third Iteration: for 2 in range(0, 4) –Condition is True Fourth Iteration: for 3 in range(0, 4) –Condition is True Fifth Iteration: for 4 in range(0, 4) –Condition is False |