Founding Batch Offer · HLD · LLD · System Design at ₹799 until 30 Nov

Print Squares from 1 to N

Print each integer and its square through an entered limit. Students practice a range loop, multiplication, and f-string labels rather than a cumulative sum.

IntermediatePhase 2 - Level 3: Mathematical & Logical PatternsExample 1 of 13
print-squares-from-1-to-n.py
1# Take n
2n = int(input("Enter n: "))
3
4# Print squares
5for i in range(1, n + 1):
6 print(f"{i}² = {i * i}")

Output

Enter n: 5
1² = 1
2² = 4
3² = 9
4² = 16
5² = 25

In this one

  • Compute squares per term
  • Label formatted values
  • Distinguish rows from totals

How it works

  1. 1

    Visit integers from one through n.

  2. 2

    Compute i * i for the current value.

  3. 3

    Print its label and square on one line.

Edge cases

What happens for n = 0?

What happens for n = 0?

The loop prints no rows.

Does the superscript perform exponentiation?

Does the superscript perform exponentiation?

No. i * i computes the value.

What's going on

Each i is squared independently with i * i. The loop visits one through n because its stop is n + 1.

For n equal to three, the results are one, four, and nine on separate labelled lines. No accumulator combines them. The superscript in the label is display text; the multiplication expression performs the calculation.

What happens for n = 0? The loop prints no rows.

Does the superscript perform exponentiation? No. i * i computes the value.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

What is the square of four?

Pick an answer to check it.

Question 2

What happens for n = 0?

Pick an answer to check it.

Questions people ask

What happens for n = 0?

The loop prints no rows.

Does the superscript perform exponentiation?

No. i * i computes the value.

What is the square of four?

16.