Question: Consider the following code fragment: square_numbers = Squares(5, 50) for num in square_numbers: print(num) This prints all of the square numbers between 5 and 50

Consider the following code fragment:

square_numbers = Squares(5, 50) for num in square_numbers: print(num)

This prints all of the square numbers between 5 and 50 inclusive (note: a square number is one which has a whole square root, such as 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, etc.). It works because the Squares class is iterable, i.e., it has an __iter__() method which creates an Iterator object (which has an implementation of the __next__() method). Define the Squares class and SquaresIterator class so that the square numbers in the range specified can be printed using a for loop as in the example above. HINT: you can assume that the math module has been imported - you may find math.sqrt() and math.ceil() helpful functions.

For example:

Test Result
for i in Squares(5, 50): print(i)
9 16 25 36 49
for i in Squares(4, 16): print(i)
4 9 16

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!