How do you square a number in Python, and how do you check if a number is a square using Python?
To square a number in Python use the power operator
**
followed by the number 2. For example, to square the number 9 simply write
9 ** 2
.
Here’s an example demonstrating the use of this operator when squaring a number:
>>> 5 ** 2
25.0
>>> 12 ** 2
144.0
>>> -9 ** 2
-81.0
Wait! I thought squaring a negative number produces a positive number? Doesn’t
-9 x -9 = +81
? Why then does Python give a negative answer?
Python has interpreted the last example above as follows:
9 ** 2
and then
-1 * 81
.
To ensure a negative number is squared properly be sure to wrap the number in parentheses, like so:
>>> (-9) ** 2
81
This follows the BODMAS mathematical principle where numbers in brackets are evaluated first, followed by order (power) operations.
You can apply the same technique if trying to cube a number, simply change the exponent (the number after the power operator) to
3
as seen below:
>>> 9 ** 3
729
Can You Use
math.pow()
To Square A Number?
If you still want to use the
math
library you can use the
math.pow(n, e)
method which takes two parameters the first being the number and the second being the exponent raised.
An example of this is demonstrated below:
>>> import math
>>> math.pow(9, 2)
81.0
>>> math.pow(-9, 2)
81.0
As can be seen from the above example, squaring a number using the
math
library is achieved by using the
math.pow()
function with the second parameter, the exponent, being the number
2
.
Is A Number A Square Number?
Is there a way to determine if a number is a square number in Python?
What is a square number? A square number is a number that can be multiplied by the same integer twice. For example,
81
is a square number because the integer
9
when squared is equal to it. However,
82
is not a square number because neither
9
nor
10
when squared is equal to it.
One approach to finding if a number is a square number in Python is to square root the number , remove any remainders, then square that rounded number and see if it matches the original number.
Here’s how this would look in the Python REPL as a one-liner:
>>> ((81 ** (1/2)) // 1) ** 2 == 81
True
>>> ((82 ** (1/2)) // 1) ** 2 == 82
False
In the first example above I used a square number, the number
81
and applied the
square root one-liner technique
then I applied the
//
operator which obtains the integer portion of a division operation (in essence truncating or removing any remainders) and then I applied the power operator to square it all.
An alternative could have been to use the built-in function
round()
like so:
>>> round(81 ** (1/2)) ** 2 == 81
True
>>> round(82 ** (1/2)) ** 2 == 82
False
Finally, an equality expression is assigned to the original number to see if it matches. In the first example, it does, and confirms the expression. The second example fails and once again confirms the expression.
The same technique could be applied to other problems such as determining whether a number is a cube number, here you’ll see why the
round()
is preferred to the
//
truncating operator:
>>> 5 ** 3
125
>>> ((125 ** (1/3)) // 1) ** 3 == 125
False
As shown above the cube of 5 is 125, therefore, if using the same approach to determine if a number is a cube number we get a
False
value – which is incorrect. This has to do with
floating-point precision
. Therefore, to get around this issue using this approach apply the
round()
function as follows:
>>> round(125 ** (1/3)) ** 3 == 125
True
Finally, the expression used above could be modified to use the
math
library, as demonstrated below:
>>> import math
>>> math.pow(round(math.sqrt(81)), 2) == 81
True
>>> math.pow(round(math.sqrt(82)), 2) == 82
False
Summary
Squaring a number in Python can be done using the power operator followed by the number 2 like
9 ** 2
would represent the squaring of the number 9. This can be done without needing to import the
math
library.
If, however, you would like to use the
math
library there is a power function
math.pow(n, e)
where the exponent
e
would be the value 2 and the first parameter
n
would represent the number you’re trying to square. For example,
math.pow(9, 2)
producing the equivalent result of
9 ** 2
.