Member-only story

Write a Python function that takes a list of integers as input and returns a new list that contains only the even numbers in the original list.

Developers Group
2 min readApr 6, 2023

Question: Write a Python function that takes a list of integers as input and returns a new list that contains only the even numbers in the original list. Explain your approach and the reasoning behind it.

Answer: Here’s a Python function that does exactly that:

def get_even_numbers(input_list):
# Initialize an empty list to store even numbers
even_numbers = []

# Loop through each number in the input list
for number in input_list:
# Check if the number is even
if number % 2 == 0:
# If it is, append it to the even_numbers list
even_numbers.append(number)

# Return the list of even numbers
return even_numbers

This function takes a list of integers as input, and initializes an empty list called even_numbers to store the even numbers. It then loops through each number in the input list, and checks if the number is even by using the modulo operator % to check if the remainder is 0 when divided by 2. If the number is even, it appends it to the even_numbers list.

Finally, the function returns the even_numbers list containing only the even numbers from the original…

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Developers Group
Developers Group

Written by Developers Group

A Developer is responsible for coding, designing, deploying, and debugging development projects, typically on the server-side (or back-end).

No responses yet

Write a response