Write a Python function that takes a list of integers and returns a new list with only the even numbers, in order.
Computer Science
ScanSolve AI Answer
``python
def get_even_numbers(numbers):
"""
Takes a list of integers and returns a new list containing only the even numbers,
preserving their original order.
Args:
numbers: A list of integers.
Returns:
A new list containing only the even numbers from the input list.
"""
even_numbers = []
for number in numbers:
if number % 2 == 0:
even_numbers.append(number)
return even_numbers
Example usage:
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
evens = get_even_numbers(my_list)
print(evens) # Output: [2, 4, 6, 8, 10]
another_list = [11, 22, 33, 44, 55]
evens_again = get_even_numbers(another_list)
print(evens_again) # Output: [22, 44]
empty_list = []
print(get_even_numbers(empty_list)) # Output: []
no_evens = [1, 3, 5, 7]
print(get_even_numbers(no_evens)) # Output: []
``
Was this helpful?
Still stuck on this one?
Ask a follow-up, and the answer starts from this question.
Got a different question?Ask your own question
More Computer Science Questions
QCreate a Python function that takes a list of numbers and returns the sum of all even numbers in the…QWrite Python programs to print even numbers from 1 to 100 and a number guessing game.QClassify the given numbers as even or odd. Also, tick the even/odd number in the given sets.QIdentify the even numbers from the following list: 127, 3860, 4249, 58242, 714931, 96, 3245, 5174.QIdentify the sequence of even numbers from the given arithmetic progression.
Browse by topic:Python