Generate a set of numbers that are perfect squares from 1 to 100


This Python code uses a set comprehension to create a set named perfect_squares that contains perfect square numbers in the range from 1 to 100. Here's how the code works:

  • {x for x in range(1, 101) if int(x**0.5)**2 == x}: This set comprehension creates a set by iterating through the values of x from 1 to 100 (inclusive). For each value of x, it checks if the integer value of the square root of x (int(x**0.5)) squared (**2) is equal to x. If this condition is true, it includes x in the perfect_squares set.
  • print(perfect_squares) : This line of code prints the perfect_squares set to the console.

Source Code

perfect_squares = {x for x in range(1, 101) if int(x**0.5)**2 == x}
print(perfect_squares)

Output

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

Example Programs