Generate a set of tuples containing even and odd numbers from 1 to 10


This Python code uses a set comprehension to create a new set named even_odd_pairs. It contains pairs of numbers where the first number (x) is odd and in the range from 1 to 10, and the second number (y) is even and in the range from 2 to 12. Here's how the code works:

  • {(x, y) for x in range(1, 11, 2) for y in range(2, 12, 2)}: This is a set comprehension that creates pairs (x, y) for x in the range of odd numbers from 1 to 10 (1, 3, 5, 7, 9) and y in the range of even numbers from 2 to 12 (2, 4, 6, 8, 10, 12).
    • for x in range(1, 11, 2): This part of the code sets up the outer loop, iterating through odd numbers from 1 to 10 with a step of 2.
    • for y in range(2, 12, 2): This part of the code sets up the inner loop, iterating through even numbers from 2 to 12 with a step of 2.
    • (x, y): For each combination of x and y, it creates a tuple containing the values of x and y.
  • print(even_odd_pairs): This line of code prints the even_odd_pairs set to the console.

Source Code

even_odd_pairs = {(x, y) for x in range(1, 11, 2) for y in range(2, 12, 2)}
print(even_odd_pairs)

Output

{(3, 4), (5, 4), (3, 10), (9, 2), (5, 10), (9, 8), (1, 6), (7, 4), (7, 10), (5, 6), (3, 6), (9, 4), (9, 10), (1, 2), (1, 8), (7, 6), (3, 2), (5, 2), (3, 8), (5, 8), (9, 6), (1, 4), (1, 10), (7, 2), (7, 8)}

Example Programs