Given a list of numbers input to the program your task is to find the even 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 Program to Print Even Numbers in The List Using Function
Let's define a function named print_even() and pass the given list of numbers as a input to the Python function. print a user friendly message using the print function call. all use the for loop to iterate all the numbers present in the list and check if the number is completely divisible by 2. if the number is completely divisible by 2 then print the number.
def print_even(numbers):
print("Even 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_even() function by passing the list of numbers as input.
print_even(data)
Write a Python Function to Print The Even Numbers From a Given List
========================================
def print_even(numbers):
print("Even 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_even(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
Even Numbers in List are ...
2 4
Output
===============
Enter List Size : 8
Enter List Item : 67
Enter List Item : 78
Enter List Item : 45
Enter List Item : 51
Enter List Item : 92
Enter List Item : 66
Enter List Item : 64
Enter List Item : 97
Even Numbers in List are ...
78 92 66 64
Conclusion
====================
Try modifying the program to print the odd numbers from the given list.
0 Comments