4Geeks chosen to deliver AI education in the Bahamas alongside Harvard, Oxford, and Columbia.See more

How to Square a Number in Python

On how to square a number in Python we explore different ways that includes pow(), math.pow, the power operator (**) as we create as well our own function to receive exactly this squared value
3 min readLast updated

Short answer: To square a number in Python, use the exponent operator **, as in 5 ** 2, which returns 25. The built-in pow(5, 2) gives the same result, math.pow(5, 2) returns the float 25.0, np.power(arr, 2) squares every item of a NumPy array, and a custom function can simply return num * num.

The most straightforward way to square a number in Python is to use the built-in pow (power) function. It receives two parameters: the number to square and the exponent to raise it to.

python
square = pow(6, 2)
print(square)
#Output-> 36

There are many ways or reasons to square a number in python: Using the function pow(), math.pow() and others, here is a more detailed explanation:

Creating the square function ourselves:

The first method we are presenting is exactly creating a function that will return the squared number of whatever number it receives: if it receives x we will return x * x.

As you should already know, a squared number is the number multiplied by itself.

Let's say the square of 2 would be 4 because 2*2=4. If we were to choose 5, we would receive 25 and so on.

Keeping that in mind, we can create our function:

python
def square(num):
    return num*num
 
print(square(3))
#Output-> 9

Using the power(square) operator:

Python comes with a built-in power operator **. The syntax is quite straight forward as all the other logic or math operators.

python
def squareOperator(base):
    return base ** 2
 
print(squareOperator(4))
#Output-> 16

Using the math.pow():

The only difference between the math.pow() and pow resides in the first needs the math module imported and it always returns a float number, even if the result of the operation has no floating point or decimals (if you want to learn more about Float numbers, check What is a float in Python).

python
import math
 
def squareMathPow(base, power):
    return math.pow(base, power)
 
print(squareMathPow(5,2))
#Output-> 25.0

Using the Numpy np.power() function:

We can also use a built-in function in the numpy library. The syntax is as simple as:

python
import numpy as np
 
print(np.power(5, 2))
#Output-> 25

You can find more of these articles at 4Geeks. Hope you enjoyed the reading and keep on the Geek side!

Which method should you use?

MethodExampleBest for
** operator5 ** 2 returns 25Everyday code, it is the most readable option
math.pow()math.pow(5, 2) returns 25.0When you want the result as a float
np.power()np.power(arr, 2)Squaring every item of a NumPy array at once
Your own functionsquare(5) returns 25Reusing the logic with a descriptive name

Squaring numbers in Python is a simple operation that appears everywhere, from geometry to data science. A programming bootcamp helps you turn fundamentals like this into complete projects, with mentors reviewing your progress.