50 XP

21. Squares of the evens

Intermediate comprehensions

Write a function even_squares(numbers) that takes a list of whole numbers and returns a new list with the square of each even number, in the order they appear. Leave out the odd numbers. The square of a number is the number times itself: the square of 4 is 16.

A number is even when it divides by 2 with nothing left over. That includes 0 and negative numbers such as -4. If there are no even numbers, or the list is empty, return an empty list [].

Try to write it in one line, with a list comprehension.

Example 1

Inputnumbers = [1, 2, 3, 4]
Output[4, 16]
Explanation2 and 4 are even: 2 * 2 = 4, 4 * 4 = 16.

Example 2

Inputnumbers = [5, 7]
Output[]
ExplanationThere are no even numbers.

Example 3

Inputnumbers = [-4, 0, 9]
Output[16, 0]
Explanation-4 and 0 are even: -4 * -4 = 16, 0 * 0 = 0.

Constraints

  • numbers has 0 to 100 items.
  • Each item is a whole number from -1000 to 1000.

Submit also runs 3 more tests you can't see.

Practises List Comprehensions (lesson 15).

Code
def even_squares(numbers):
    """Square the even numbers of a list.

    numbers: list of int; may be empty
    Returns: list of int, the squares of the even
        numbers, in order
    """
    # Write your code here
    pass