Print Squares from 1 to N

Print square of each number from 1 to n.

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

What's going on

Calculate and print square of each number.

Key Concepts:

Loop from 1 to n
Calculate i * i for each
Print formatted output