Here’s how to square a number in Python: use the exponent operator, n ** 2, or multiply, n * n. There’s no built-in square() function, so wrap either one in def square(n) when you want a name for it:
side = 12
area = side ** 2 # 144
Squaring looks too simple to test, yet Python 3.12.5 and NumPy 2.5.3 turned up two results worth knowing, and they’re both in the screenshots below.
You’ll see the built-in ways to square a number, a reusable square() function, the negative-number trap, why math.pow loses digits, lists and NumPy arrays, perfect squares and a speed test.
Square a number with **, multiplication and pow()
All four give 144 for a side of 12. The only visible difference is the type of the result:
import math
side = 12
print(side ** 2) # exponent operator
print(side * side) # multiply by itself
print(pow(side, 2)) # built-in pow
print(math.pow(side, 2)) # always returns a float
price = 19.99
print(price ** 2, round(price ** 2, 2))
Output:
144
144
144
144.0
399.60009999999994 399.6

Floats carry tiny binary errors, so round for display. Geometry is the classic use, as in finding the area of a square in Python.
Write a square() function in Python
If you call squaring in many places, a small named function documents the intent. Type hints make it clear it takes ints and floats:
def square(n: int | float) -> int | float:
"""Return n squared."""
return n * n
print(square(12), square(-4), square(2.5))
square_lambda = lambda n: n ** 2 # fine for one-off use, e.g. as a key
sides = [3, 12, 7]
print(list(map(square, sides)), max(sides, key=square_lambda))
try:
square("12")
except TypeError as err:
print("TypeError:", err)
Output:
144 16 6.25
[9, 144, 49] 12
TypeError: can't multiply sequence by non-int of type 'str'

A string like "12" fails, so convert input first with int(); see converting a string to an integer in Python.
Why does -3 ** 2 give -9 in Python?
** binds more tightly than a leading minus sign, so Python reads -3 ** 2 as -(3 ** 2). Parentheses fix it:
x = -3
print(-3 ** 2) # ** binds tighter than the minus sign -> -(3 ** 2)
print((-3) ** 2)
print(x ** 2) # a variable holding -3 is fine
print(2 ** 3 ** 2) # ** groups right to left: 2 ** 9
Output:
-9
9
9
512

A variable is safe because its value is already negative. Note that ** also groups right to left. For cubes and other powers, see exponents in Python.
Why is math.pow wrong for large numbers?
Python integers have unlimited precision, but math.pow() converts to a 64-bit float, which keeps only about 15 to 17 significant digits:
import math
n = 123_456_789_123
exact = n ** 2
approx = math.pow(n, 2)
print("n ** 2: ", exact)
print("math.pow: ", approx)
print("as int: ", int(approx))
print("off by: ", int(approx) - exact)
Output:
n ** 2: 15241578780560891109129
math.pow: 1.5241578780560892e+22
as int: 15241578780560891838464
off by: 729335
n ** 2 is exact; the float version is off in the last digits. For money and measurements, use integers or Decimal, and round numbers to 2 decimal places only for display.
Square every number in a list or NumPy array
For a list, use a comprehension. For NumPy arrays, np.square() and ** work element by element, but watch the dtype:
import numpy as np
sales = [3, 7, 12, 25]
print([x ** 2 for x in sales])
print(list(map(lambda x: x * x, sales)))
arr = np.array(sales)
print(np.square(arr), arr ** 2)
small = np.array([100, 120], dtype=np.int8)
print("int8 squared:", np.square(small), "<- overflow, wrapped around")
print("int64 squared:", np.square(small.astype(np.int64)))
Output:
[9, 49, 144, 625]
[9, 49, 144, 625]
[ 9 49 144 625] [ 9 49 144 625]
int8 squared: [16 64] <- overflow, wrapped around
int64 squared: [10000 14400]

NumPy fixed-size integers wrap around without an error. Making lists of numbers to square is covered in creating an array from 1 to N in Python.
Check whether a number is a perfect square
The reverse question comes up often. math.isqrt() returns the exact integer square root, so squaring it back tells you the answer:
import math
def is_perfect_square(n: int) -> bool:
if n < 0:
return False
root = math.isqrt(n) # exact integer square root
return root * root == n
for n in (144, 150, 0, 10**30, 10**30 + 1):
print(f"{n:>32,} {is_perfect_square(n)}")
print(math.sqrt(10**30 + 1).is_integer(), "<- float sqrt gets this one wrong")
Output:
144 True
150 False
0 True
1,000,000,000,000,000,000,000,000,000,000 True
1,000,000,000,000,000,000,000,000,000,001 False
True <- float sqrt gets this one wrong

math.sqrt() rounds very large values, so is_integer() can lie. For roots in general, see square roots in Python.
Which way to square a number is fastest?
Here are two million squarings of 1234 with each method, best of three runs:
import math
import timeit
n = 1234
for label, stmt in [("n * n", "n * n"), ("n ** 2", "n ** 2"), ("pow(n, 2)", "pow(n, 2)"), ("math.pow(n, 2)", "math.pow(n, 2)")]:
t = min(timeit.repeat(stmt, globals={"n": n, "math": math}, number=2_000_000, repeat=3))
print(f"{label:<16}{t * 1000:8.1f} ms per 2,000,000")
Output:
n * n 38.1 ms per 2,000,000
n ** 2 54.3 ms per 2,000,000
pow(n, 2) 67.7 ms per 2,000,000
math.pow(n, 2) 123.9 ms per 2,000,000
On my machine n * n is the quickest and math.pow isn’t faster despite being in the math module. The gaps are tiny, so I use n ** 2 because it reads clearly. Digit-level math is in summing the squares of digits.
Squaring methods compared
| Code | Result for 12 | Notes |
|---|---|---|
n ** 2 | 144 | Exact for ints; mind -3 ** 2 |
n * n | 144 | Exact, quickest |
pow(n, 2) | 144 | Same as ** |
math.pow(n, 2) | 144.0 | Float; loses precision on big ints |
np.square(arr) | Array | Watch small integer dtypes |
If you’re practicing Python math, these come next:
- Exponential functions with SciPy
- Use the ceil() function in Python
- Draw a square with Python turtle
- Find the maximum value with max()
Frequently asked questions
How do you square a number in Python?
Use n ** 2. n * n and pow(n, 2) give the same result.
Why does -3 ** 2 return -9?
The exponent is applied before the minus sign. Write (-3) ** 2 to get 9.
Should I use math.pow to square numbers?
Not for integers. It returns a float and can lose precision for large values.
How do I square every item in a list?
Use a list comprehension: [x ** 2 for x in nums], or np.square() for NumPy arrays.
Is there a square function in Python?
No built-in one. Use n ** 2, or write def square(n): return n * n.
Where is the ** operator documented?
See the power operator in the Python reference.
