Generate a set of squares of numbers from 1 to 10


Creates a set named squares that contains the squares of numbers from 1 to 10. Here's how the code works:

  • {x**2 for x in range(1, 11)}: This is a set comprehension. It iterates over the numbers from 1 to 10 (inclusive) using range(1, 11) and calculates the square of each number x using x**2. The set comprehension collects these squared values into a set.
  • print(squares): This line of code prints the set squares to the console.

Source Code

squares = {x**2 for x in range(1, 11)}
print(squares)

Output

{64, 1, 4, 36, 100, 9, 16, 49, 81, 25}

Example Programs