30.8k views
3 votes
Print the squares for the numbers from 1 to 21

(use range(..) to generate the range)
output
1 4 9 16 25 36 49 64 81 100 121 144 169 196 225 256 289 324 361 400

User Mbrenon
by
8.0k points

1 Answer

5 votes

Final answer:

To print the squares for the numbers from 1 to 21, you can use the range function in Python and calculate the square of each number using a loop.

Step-by-step explanation:

To print the squares for the numbers from 1 to 21, we can use the range function in Python. The range function generates a sequence of numbers from the starting value to one less than the ending value, with a default step of 1. We can iterate over this sequence and calculate the square of each number.

  1. Create a for loop to iterate over the numbers in the range from 1 to 21.
  2. Within the loop, calculate the square of each number using the exponentiation operator (**).
  3. Print each square number on a new line.

The code snippet below demonstrates how to achieve this:

for num in range(1, 22):
square = num ** 2
print(square)

The output will be: 1 4 9 16 25 36 49 64 81 100 121 144 169 196 225 256 289 324 361 400.

User Shafreeck Sea
by
8.8k points