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

Print Cubes from 1 to N

Display cubes from one through n with a for loop. Beginners trace three-factor multiplication and distinguish a printed power label from executable arithmetic.

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

Output

Enter n: 5
1³ = 1
2³ = 8
3³ = 27
4³ = 64
5³ = 125

In this one

  • Build a cube with multiplication
  • Include the endpoint
  • Separate notation from arithmetic

How it works

  1. 1

    Visit one through n.

  2. 2

    Multiply i by itself three times.

  3. 3

    Print the original value beside its cube.

Edge cases

Is n included?

Is n included?

Yes, when n is positive.

What if n is negative?

What if n is negative?

The range is empty; this example does not report invalid input.

What's going on

A cube multiplies a number by itself three times. i * i * i calculates it for each visited integer.

The f-string pairs the original value with its cube. For three, that result is twenty-seven. range excludes its stopping bound, so adding one to n includes the requested endpoint without an extra iteration.

Is n included? Yes, when n is positive.

What if n is negative? The range is empty; this example does not report invalid input.

Quiz

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

Question 1

What is printed for i = 2?

Pick an answer to check it.

Question 2

Is n included?

Pick an answer to check it.

Questions people ask

Is n included?

Yes, when n is positive.

What if n is negative?

The range is empty; this example does not report invalid input.

What is printed for i = 2?

8.