Select Page

How to Calculate the Square of a Number in Python

by | Programming, Python, Tips

blog banner for post titled How to Calculate the Square of a Number in Python

The square of a number results from multiplying a number by itself. The square of the number is the same as raising the number to the power of two. For example 8 \times 8 = 64, which is equal to 8^{2} as well. The square of real numbers is always positive so (-8)^{2} =64. There are several ways we can calculate the square of a number in Python:

  • Multiplying the number by itself: number * number
  • Using the exponentiation operator: number ** 2
  • Using math.pow() method: math.pow(number, 2)

Option #1: Square the Number by Multiplying the Number by Itself

square the number by multiplying by itself

To find the square of a number in Python, you can multiply the number itself. Let’s look at how to do this:

# Input a number

number = int(input("Enter an integer:  "))

# Calculate the Square

square = number * number

# Print value

print(f'Square of {number} is {square}')
Enter an integer:  6

Square of 6 is 36

The above code uses the input() function to get the integer value from the user, then calculates the square by multiplying the integer by itself, then prints the result to the console.

Option #2: Square the Number using the Exponentiation Operator

square a number using exponentiation operator

We can calculate the square of a number using the exponentiation operator **. The function returns the exponential power. To calculate the square, we use the number ** 2, which, when said out loud, is “number to the power of 2”. Let’s look at an example with the exponentiation operator.

# Input a number

number = int(input("Enter an integer:  "))

# Calculate the Square

square = number ** 2

# Print value

print(f'Square of {number} is {square}')
Enter an integer:  6

Square of 6 is 36

The above code uses the input() function to get the integer value from the user, then calculates the square by using the exponentiation operator and then prints the result to the console. To calculate the square root of a number, you can use the number ** 0.5.

Option #3: Square the Number Using the math.pow() Function

square a number using math.pow() function

You can use the built-in function math.pow(x, y), which returns the value x to the power of y. You have to import the pow() function from the math module then call it in the code. Let’s look at an example with math.pow():

# Input a number

number = int(input("Enter an integer:  "))

# Calculate the Square

square = int(pow(number, 2))

# Print value

print(f'Square of {number} is {square}')
Enter an integer:  6

Square of 6 is 36

We always set the second argument to square a number using the pow() function to 2.

Find the Square of a Python List

In the above options, you calculated the square of a single number. You may want to calculate the square of several numbers. We can calculate the squares of a list of numbers using list comprehension. Let’s look at an example list of five numbers:

numbers = [7, 21, 16, 8, 3]

squares = [number ** 2 for number in numbers]

print(f'Original numbers are {numbers}')

print(f'Squared numbers are {squares}')
Original numbers are [7, 21, 16, 8, 3]

Squared numbers are [49, 441, 256, 64, 9]

The code defines a list of numbers then generates a list with a list comprehension, consisting of the square numbers using the exponentiation operator.

Using numpy.square() To Square an Array

NumPy is a Python library for manipulating large multi-dimensional arrays and matrices along with mathematical functions to operate on these arrays. We can use numpy.square() to calculate the square value of each element in an array.

To use NumPy, you have to take import the module. Let’s look at an example of using the numpy.square() method on an array of integers.

import numpy as np

numbers = [7, 21, 16, 8, 3]

arr = np.array(numbers)

squares = np.square(arr)

print(f'Squares of array {arr}: {squares}')
Squares of array [ 7 21 16  8  3]: [ 49 441 256  64   9]

In the above code, you import numpy as np and create a numpy array with the np.array() method. The next step involves using the np.square() method to get the square value of every element in the array.

Summary

Congratulations on reading to the end of this tutorial! The square of a number is an essential operation in mathematics. In Python, there are several ways to calculate the square of a number: multiplying the number by itself, using the exponentiation operator **, and using the math.pow() function. To square multiple numbers, you can use list comprehension or the np.square() method on a numpy array containing the numbers you want to square.

For further reading on doing mathematical operations in Python, go to the article: What is 12 Degrees Celsius in Fahrenheit?

Go to the online courses page for Python to learn more about Python for data science and machine learning.

Have fun and happy researching!