Tuple of even squares up to 100 in Python


This Python code creates a tuple called even_squares, which contains the squares of even numbers from 0 to 10 (inclusive). Here's how the code works:

  • even_squares = tuple(x**2 for x in range(11) if x**2 % 2 == 0) : This line of code initializes a variable named even_squares and assigns it a tuple created using a generator expression.
    • for x in range(11): This part of the code sets up a loop that iterates through numbers from 0 to 10 using the range(11) iterable.
    • if x**2 % 2 == 0: For each value of x in the range, it calculates the square of x using x**2 and checks if the square is an even number by evaluating x**2 % 2 == 0.
    • x**2: If the square of x is even, it includes the square in the generator expression.
    • tuple(...): This surrounds the generator expression and converts the generated even squares into a tuple.
  • print(even_squares): This line of code prints the even_squares tuple (which contains the even squares of numbers from 0 to 10) to the console.

Source Code

even_squares = tuple(x**2 for x in range(11) if x**2 % 2 == 0)
print(even_squares)

Output

(0, 4, 16, 36, 64, 100)

Example Programs