6.1. While: List of squares
"For a given integer N, print all the squares of positive integers where the square is less than or equal to N, in ascending order."
1 4 9 16 25 36 49
I think it's better to use a while loop here.
For example, if x is one million, the last number of the list will be one million, but you'll compute the square of each number from one to one million (so the last square will be one trillion).
x = int(input("Enter the number:")) i = 1 while i*i <= x: print(i*i) i += 1
Here is a modification of your own script:
from math import sqrt x = int(input("Enter the number: ")) print("All the squares in the given range are:") print([i**2 for i in range(1, int(sqrt(x))+1) if i**2 <= x])
Try your script with x = 10000000000 and try this new version. You'll see the difference.