In this python function program, we will find the even numbers from a given list.
Even number:
Even numbers are those numbers that can be divided into two equal parts.
What is Function?
It is a block of code that executes when it is called.
To create a function use def keyword.
Steps to solve the program
- Create a function even.
- Use the def keyword to define the function.
- Pass a parameter i.e. list to the function.
- Create an empty list.
- Use a for loop to iterate over the numbers in the list.
- Use an if-else statement to check whether the number is a even number.
- If yes then add it to the empty list.
- Print the output.
- Create a list and pass that list to the function while calling the function.
def even(list1):
even_list = []
for val in list1:
if val%2 == 0:
even_list.append(val)
print(even_list)
l = [1, 2, 3, 4, 5, 6, 7, 8, 9]
even(l)
Output :
[2, 4, 6, 8]
Related Articles
Python function program to execute a string containing Python code.
Python function program to access a function inside a function.
Python function program to find the LCM of two numbers.
Python function program to calculate the sum of numbers from 0 to 10.
Python function program to find the HCF of two numbers.