Write a Python Function to Print The Odd Numbers From a List as Argument

Given a list of numbers input to the program your task is to find the   Odd numbers from the list of number and try to print them using if function

The input to the Python function should be a list containing integers.

 

Write a Python Function to Print The Odd Numbers From a List as Argument




Write a Python Function to Print The Odd Numbers From a List as Argument

Let's define a function named print_odd()  and pass the given list of numbers as an input to the Python function.  Print a user friendly message using the print function call.  Use the for loop to iterate all the numbers present in the list and check if the number is completely non divisible by 2.  if the number is not divisible by 2 then print the odd number.

def print_odd(numbers):
   print("Odd Numbers in List are ...")
   for number in numbers:
       if number % 2 != 0:
           print(number, end=" ")





In the main program, take a variable named as size and ask a user to enter the size of the list by converting it to an integer.

Take an empty list of numbers named as data and assign it as an empty list.  using the for loop iterates up to n. Use the input function call to ask the user to enter the list item and then append the item to the list.

size = int(input("Enter List Size : "))
data = []
for _ in range(size):
   temp = int(input("Enter List Item : "))
   data.append(temp)






Once we get the list of numbers, call the print_odd()  function by passing the list of numbers as input.

print_odd(data)






Write a Python Function to Print The Odd Numbers From a List as Argument
========================================

def print_odd(numbers):
   print("Odd Numbers in List are ...")
   for number in numbers:
       if number % 2 != 0:
           print(number, end=" ")


size = int(input("Enter List Size : "))
data = []
for _ in range(size):
   temp = int(input("Enter List Item : "))
   data.append(temp)


print_odd(data)






Output
================

Enter List Size : 5
Enter List Item : 1
Enter List Item : 2
Enter List Item : 3
Enter List Item : 4
Enter List Item : 5
Odd Numbers in List are ...
1 3 5





Output
===============

Enter List Size : 8
Enter List Item : 21
Enter List Item : 57
Enter List Item : 88
Enter List Item : 55
Enter List Item : 43
Enter List Item : 78
Enter List Item : 42
Enter List Item : 90
Odd Numbers in List are ...
21 57 55 43





Conclusion
====================

Try modifying the program to print the Even Numbers from the  given list.

Comment  down  below if you have any suggestions regarding the above program





Post a Comment

0 Comments