AppDividend
Latest Code Tutorials

Python square: How to Square a Number

In Mathematics, a square is a result of multiplying a number by itself. That multiplication is done just once, like so: n x n. That makes squaring the same as raising the number to the power of 2.

For example, 8×8 is 64, just as 8 squared (82) is 64. One feature of squares is that they are always positive. This is because of the negative times. Another negative always gives the positive. For instance, -8 squared, or (-8)2, is also 64.

The square of a number is that number multiplied by itself.

How to Square a Number in Python

To calculate the square of a number in Python, we have three different ways.

  1. By multiplying numbers two times: (number*number)
  2. By using Exponent Operator (**): (number**2)
  3. Using math.pow() method: (math.pow(number, 2))

Python square: Using multiplying two times

To find a square of the number, multiply the number by itself. This method is the easiest way to calculate squares in Python.

# input a number
digit = int(input("Enter an integer number: "))

# calculate square
square = digit*digit

# print
print(f"Square of {digit} is {square}")

Output

Enter an integer number: 10
Square of 10 is 100

In this example, we have used the python input method to get the input from the user and then calculate the square based on multiplying by itself.

Square a number using an Exponent operator.

We can find the square of a given number is to use Exponent Operator (**); it returns the exponential power. So this operator is represented by **.

# input a number
digit = int(input("Enter an integer number: "))

# calculate square using exponent operator
square = digit**2

# print
print(f"Square of {digit} is {square}")

Output

Enter an integer number: 11
Square of 11 is 121

We have calculated a square using an exponent operator(**) in this example.

Also, Statement x**y will be calculated as “x to the power of y”.

Using math.pow() method to calculate square

Math.pow(x, y) is a built-in method of math library, and it returns the value of “x to the power y“.

To use the pow() method, we need to import the math library in the program and then import the pow function from the math module.

from math import pow

# input a number
digit = int(input("Enter an integer number: "))

# calculate square
square = int(pow(digit, 2))

# print
print(f"Square of {digit} is {square}")

Output

Enter an integer number: 19
Square of 19 is 361

Math.pow() function raises some value to absolute power. Its first argument is a number we want to raise; the second argument is an exponent.

When we square with the pow() method, our second argument is always 2. The input() function will return the string, so we have converted the string to int. Then we have used math.pow() method to get the square of the number.

Find the Square of Python list.

We can get the squares of list values using list comprehension.

numbers = [11, 21, 19, 30, 46]

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

# print
print('Original Values: ', numbers)
print('Squared Values: ', squaredValues)

Output

Original Values:  [11, 21, 19, 30, 46]
Squared Values:  [121, 441, 361, 900, 2116]

This example first makes a list of named numbers. Its contents are various integer values.

Then we generate a new list with a list comprehension. The code between square brackets ([ and ]) squares each number value with the exponent (**) operator.

Those number values are generated by an in-line for loop expression: for the number in numbers. This goes through our original numbers list and makes each element available as the number variable, one at a time.

After that list comprehension, the squared list has each value squared. We then output the original and squared values with the Python print() function.

We could also get the square values differently. For instance, with simple multiplication, you can achieve your result.

See the following code.

numbers = [11, 21, 19, 30, 46]

squaredValues = [number * number for number in numbers]

# print
print('Original Values: ', numbers)
print('Squared Values: ', squaredValues)

Output

Original Values:  [11, 21, 19, 30, 46]
Squared Values:  [121, 441, 361, 900, 2116]

In this example, we have defined a Python list and then used list comprehension to create the square of list items.

Python square of the array

To find the square of an array, we need to use the Numpy library.

The Numpy square() method in Python is a mathematical function to calculate the square value of each element in the array.

import numpy as np

arr = np.array([11, 19, 21, 29, 46])
print("Square Value of arr1 : \n", np.square(arr))
print("\n")
arr2 = [-19, -21]
print("Square Value of arr2 : \n", np.square(arr2))

Output

Square Value of arr1 :
 [ 121  361  441  841 2116]


Square Value of arr2 :
 [361 441]

In this example, we have imported numpy as np and then created an array using the np.array() method.

Then used, the np.square() method to get the square value of every array element.

Conclusion

In this tutorial, we have seen how to calculate the square of a number using the exponent operator, multiply by itself, and math.pow() function. Then we have seen how to find the square of each element of the list using list comprehension.

That’s it for this tutorial.

See also

Python sqrt()

Python sum()

Python Modulo operator

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.