To square a number, you multiply that number by itself. And there are multiple ways to do this in Python.
You can directly multiple a number by itself (number * number) but in this article, I'll show you three ways you can do this without hardcoding both numbers.
The three ways are:
- **, the power operator
- the in-built
math.pow()function from the
How to Use the Power Operator (**) in Python
** is called the power operator. You use it to raise a number to a specified power. Here is the syntax:
number ** exponent
The expression above is evaluated as number * number... (for as many times as the value of the exponent). You can also read the expression as 52.
Using this operator, you can find the square of a number using 2 as the exponent. For example, to find the square of 5, you can do this:
square = 5 ** 2 print(square) # 25
The power operator evaluates the expression as 5 * 5, which results in 25.
How to Use the
pow() Function in Python
Python has an inbuilt
pow() function, which evaluates a number to the power of another number. Here's the syntax:
pow(base, exponent) // interpreted as ^3
The code above is interpreted as baseexponent.
The function accepts two arguments: the number to be raised (known as the base) and the power the number should be raised to (the exponent).
To find the square of a number using this function, the number will be the base, and the exponent will be 2, which means number2.
To find the square of 5, for example, you can use this function like this:
square = pow(5, 2) print(square) # 25
pow() function also receives a third argument: the modulo. The sign for modulo is %. This argument evaluates the remainder when a value is divided by another.
For example, 5 % 2 gives 1 because 5 divided by 2 is 2, remainder 1.
Applying the modulo the
pow() function looks like this:
mod = pow(5, 2, 3) print(mod) ## 1 ## 5 * 5 is 25 ## 25 % 3 is 1
According to the python documentation on pow, this approach computes more efficiently than
pow(5,2) % 3
How to USe the math.pow() Function in Python
math.pow() comes from Python's
math module. This function is similar to the in-built
pow() function in usage and syntax, except that it has two differences:
- it only accepts two arguments: the base and the exponent
- it always returns a float number even when the raised number is a whole number.
math.pow(5, 2) returns 25.0.
pow() will only return a float number when the number is a float. It will return an integer if the number is whole. But
math.pow() always returns a float number.
Now you know how to square numbers in Python! Thank you for reading.