89DEVs

6 ways to square a number in Python

6 ways to square a number in Python

The square of a number is the result of multiplying the number by itself. Calculating the square of a number is the same as raising the number to the power of 2. square = number * number To square a number in Python, choose between one of the following options.
  1. power operator
  2. pow() built-in function
  3. multiplication operator
  4. operator.mul() method
  5. math.pow() method
  6. numpy.square() method
All ways lead to the same result, so it depends on your personal preferences which way to chose. The math.pow() method returns float values instead of integers. Use another way for computing exact integer powers.

power operator

The power operator ** can raise the value of a number to the power of 2. # square a number using the power operator myNumber = 5 mySquare = myNumber ** 2 print(mySquare) The number is raised to the power of 2. 25 It can also raise the value of a number to any other power. For example we can use the power operator to raise our number to the power of 3. # square a number using the power operator myNumber = 5 mySquare = myNumber ** 3 print(mySquare) The result of 5 * 5 * 5 is 125. 125

pow() built-in function

The built-in pow() function is made for squaring numbers. The first argument is our number and the second argument is the power to be raised. # square a number using the pow() function myNumber = 5 mySquare = pow(myNumber, 2) print(mySquare) The square of our number is returned. 25

multiplication operator

Perhaps the easiest way to square a number in Python is to simply multiply it with itself. This can be done by using the multiplication operator or the mul() method of the operator module. # square a number using multiplication myNumber = 5 mySquare = myNumber * myNumber print(mySquare) The result is returned as an integer. 25

operator.mul() method

The next example shows the use of the mul() method, which does exactly the same thing. It accepts two arguments and we pass our number in both. The mul() method is part of the operator module, so we have to import operator first. # square a number using the mul() method import operator myNumber = 5 mySquare = operator.mul(myNumber, myNumber) print(mySquare) The correct result is returned: 5 * 5 is 25. 25

math.pow() method

Another way to square a number is the pow() method of the math module. First import math to load the math module. Then we can use math.pow(x, y) to raise x to the power y. # square a number using the math.pow() method import math myNumber = 5 mySquare = math.pow(myNumber, 2) print(mySquare) The correct result is returned. Unlike the power operator, the math.pow() method converts both arguments to type float and returns a float value. Use the power operator or the pow() built-in function to compute exact integer values. 25.0

numpy.square() method

The numpy.square() method returns the square value of each array element. # square a number with numpy.square() import numpy as np myNumber = 5 mySquare = np.square(myNumber) print(mySquare) The correct result is returned. 25

square each element in array

numpy.square() is especially useful to square multiple numbers at once. We can use the square() method on any numpy array. # square multiple numbers with numpy.square() import numpy as np myNumbers = [5, 2, 10] mySquares = np.square(myNumbers) print(mySquares) A numpy array containing the squared numbers is returned. [ 25 4 100]

square list of numbers

To square each number in a list, use list comprehension or the map() function. # square a list using list comprehension myNumbers = [1, 2, 3, 4, 5] mySquares = [number ** 2 for number in myNumbers] print(mySquares) The list of squared numbers is returned. [1, 4, 9, 16, 25] We can achieve the same result by combing the pow(), map(), and list() functions. # square a list using the map() function myNumbers = [1, 2, 3, 4, 5] # raise all 5 numbers to the power of two powers = [2] * 5 # map pow function to myNumbers and create list mySquares = list(map(pow, myNumbers, powers)) # print list of squares print(mySquares) The same list of squared numbers is returned. [1, 4, 9, 16, 25] Alternatively, we can use a lambda function in combination with map(). # square a list using map() with lambda func myNumbers = [1, 2, 3, 4, 5] mySquares = list(map(lambda number: number ** 2, myNumbers)) print(mySquares) The correct list of squared numbers is returned. [1, 4, 9, 16, 25]

square in loops

To square in a loop, use the power operator as shown below. # square in loops myNumbers = [1, 2, 3, 4, 5] for n in myNumbers: print(n ** 2) The squares for all elements in the list are printed. 1 4 9 16 25

square root

To find the square root of positive numbers we can use the power operator. # square root of positive numbers myNumber = 16 mySqrt = myNumber ** 0.5 print(mySqrt) The square root of 16 is returned. 4.0 Alternatively, we can use the math.sqrt() method. # square root using math.sqrt() method import math myNumber = 16 mySqrt = math.sqrt(myNumber) print(mySqrt) The square root of 16 is returned. 4.0 Another way is using numpy.sqrt() method. # square root using numpy.sqrt() method import numpy as np myNumber = 16 mySqrt = np.sqrt(myNumber) print(mySqrt) The square root of 16 is returned. 4.0

                
        

Summary


Click to jump to section