How to Square a Number in Python

What you’ll build or solve

You’ll learn the standard ways to square a number in Python and choose the right one for ints, floats, and user input.

When this approach works best

Squaring a number is a good fit when you:

  • Need a quick calculation, like squaring a distance, price, or score inside a script.
  • Work with formulas in a small program, like area calculations or simple physics.
  • Validate or transform data, like squaring measurements before summing them.

Avoid this approach when you actually need a square root, or when you need to square every item in a collection and you try to do it as a single-number calculation. In those cases, use math.sqrt() for roots, or a loop or list comprehension for multiple values.

Prerequisites

  • Python installed

Step-by-step instructions

1) Pick the squaring method

In Python, squaring usually means raising to the power of 2.

Option A (most common): exponent operator **

n = 7
squared = n ** 2
print(squared)  # 49

Option B: built-in pow()

n = 7
squared = pow(n, 2)
print(squared)  # 49

Use ** if you want the clearest, most common style. Use pow() if you prefer a function call or already use it for other powers.


2) Square a value you already have

Start with a plain variable. This keeps your logic easy to test.

base = 12
result = base ** 2
print(f"{base} squared is {result}")

This works for whole numbers and decimals:

whole = 5
decimal = 1.5

print(whole ** 2)    # 25
print(decimal ** 2)  # 2.25

If your number might be negative, keep parentheses around the base:

base = -4
print((base) ** 2)  # 16

What to look for: -4 ** 2 is not the same as (-4) ** 2. Without parentheses, Python reads it as -(4 ** 2).


3) Square user input from the keyboard

input() returns text, so convert it to a number first.

raw = input("Enter a number: ")
n = float(raw)
print(n ** 2)

If you want whole numbers only:

raw = input("Enter a whole number: ")
n = int(raw)
print(n ** 2)

What to look for: If the user types 3.5 and you call int("3.5"), you’ll get a ValueError. Use float() for decimals.


4) Square a number and format the result

Sometimes you want clean output, especially with floats.

n = 2.5
squared = n ** 2
print(f"Squared: {squared:.2f}")

If you see a value like 2.2500000000000004, that’s normal float precision. Rounding for display is usually enough.


Examples you can copy

Example 1: Square a single measurement

You receive a measurement and need its square for a simple formula.

distance = 9
score = distance ** 2
print(score)

Example 2: Compute the area of a square from user input

This treats the input as the side length and computes area.

side = float(input("Side length: "))
area = side ** 2
print(f"Area: {area}")

Example 3: Square a list of numbers

This example uses a list comprehension.

values = [2, -3, 0, 4.5]
squared_values = [v ** 2 for v in values]
print(squared_values)  # [4, 9, 0, 20.25]

Example 4: Square a command-line argument

import sys

if len(sys.argv) < 2:
    raise SystemExit("Usage: python square.py <number>")

n = float(sys.argv[1])
print(n ** 2)

Example 5: Reusable square function

def square(n):
    return n ** 2

print(square(10))
print(square(2.5))

Example 6: Squaring money-like decimals

If you need exact decimal math, use Decimal instead of float.

from decimal import Decimal

price = Decimal("19.99")
print(price ** 2)  # 399.6001

Common mistakes and how to fix them

Mistake 1: Using ^ for powers

What you might do:

print(5 ^ 2)

Why it breaks: ^ is bitwise XOR in Python, not exponentiation.

Correct approach:

print(5 ** 2)      # 25
print(pow(5, 2))   # 25

Mistake 2: Forgetting parentheses with negative numbers

What you might do:

print(-4 ** 2)

Why it breaks: Python applies ** before the unary minus, so this becomes -(4 ** 2).

Correct approach:

print((-4) ** 2)  # 16

Mistake 3: Squaring text from input() without converting it

What you might do:

n = input("Enter a number: ")
print(n ** 2)

Why it breaks: input() returns a string, and strings can’t be raised to a power.

Correct approach:

n = float(input("Enter a number: "))
print(n ** 2)

Troubleshooting

If you see TypeError: unsupported operand type(s) for **, convert your value first with n = float(n) or n = int(n).

If you see ValueError: invalid literal for int(), the input has decimals or extra characters. Use float() or clean the input.

If you see ValueError from float(), the input is not a number. Try 3.14 instead of 3,14.

If you get a result like 2.2500000000000004, round for display with round(n ** 2, 2) or format with :.2f.

If you accidentally wrote -4 ** 2 and got -16, add parentheses: (-4) ** 2.

If your command line example exits immediately, you may have run it without an argument. Try python square.py 8.


Quick recap

  • Square a number with n ** 2 or pow(n, 2).
  • Use parentheses for negative bases: (-4) ** 2.
  • Convert user input with int() or float() before squaring.
  • Round or format floats when you print them.
  • For collections, square each item with a loop or a list comprehension.