Python Square

By | October 31, 2020

Often when we write code in Python, we need to find out the square of a number, and in Python, there are various techniques you can use to do that. In this Python tutorial, you will learn how to square a number in Python.

How to find the square of a Number in Python?

A square of a number can be found out by multiplying that number by itself, and in mathematic it is represented as n2, where n could be any number.

Vamware

For instance, the 9 square will be represented as 92 and calculated as 9×9 =81.

In Python, there are 4 techniques we can use to calculate the square of a number.

  • Using Python exponent or power operator **
  • Using Python pow() method
  • Using Python math.pow() method.
  • And by multiplying the number by itself.

Using exponent operator (**)

In Python, we use the double multiplication symbol **  to represent an exponent or power operator. The ** operator needs two numerical values to perform the exponent operation. And to find the square of a number the operand or numerical value on the right side of ** operator must be 2.

Example calculate the square of a number using Python ** operator

num = int(input("Enter the number: "))
square = num ** 2  #num * num

print("The square of ", num ,"is:", square)

Output

Enter the number: 10
The square of  10 is: 100

Using the Python pow() method

Python has an inbuilt method pow(a, b), which accepts two parameters a and b, and return a result as a raise to the power b. The pow() method stands for power and using this method we can calculate the power or exponent value of a number.

To find out a square of a number using pow(a,b) method we need to specify the b value to 2 and a could be any number which power we need to find.

Example: Using Python pow() method

num = int(input("Enter the number: "))
square = pow(num, 2)  #num * num
print("The square of ", num ,"is:", square)

Output

Enter the number: 20
The square of  20 is: 400

Using Python math.pow() method

Python contains an inbuilt module math for math-related operations, and in the Python math module, we get math.pow() method which can also find the power or exponent value of a specified number.

The working of math.pow() method is similar to the Python pow() method, the only difference is math.pow() method returns a floating-point value whereas pow() can return both integers as well as a float value.

<Note>click here to know the difference between math.pow() and python pow() method 

Example calculate the square of a number using Python math.pow() method

import math
num = int(input("Enter the number: "))
square = math.pow(num, 2)  #num * num
print("The square of ", num ,"is:", square)

Output

Enter the number: 7
The square of  7 is: 49.0

Using the Multiplication operator

We can find the square of a number by multiplying the number by itself. So another approach is, use the Python multiplication operator and multiply the number by itself and you will get the square of the number.

Example: Using Python multiplication operator

num = int(input("Enter the number: "))
square = num * num 
print("The square of ", num ,"is:", square)

Output

Enter the number: 39
The square of  39 is: 1521

Find the square of all values in a Python list/array

By far we have learned how can we calculate the square of an individual number using different Python techniques. But many times, when we do Python coding we came across problems where we have to find the square value for every number present in the Python list or Python array. In that case, we can either use a Python for loop or map() function to perform the square operation on an individual element of the array.

Example: Using list comprehension

List comprehension provides a single line statement as an alternative for using multiline for loop and append method.

my_list = [10,23, 22, 21, 45, 67]
sq_list  =  [pow(i,2) for i in my_list]
print("The square list: ", sq_list)

Output

The square list:  [100, 529, 484, 441, 2025, 4489]

Example : Using Python map()

Python map() function is used when we want to apply a single function on the individual list element.

import math
my_list = [10,23, 22, 21, 45, 67]
sq_list  =  list(map(lambda num : math.pow(num,2), my_list))
print("The square list: ", sq_list)

Output

The square list:  [100.0, 529.0, 484.0, 441.0, 2025.0, 4489.0]

Conclusion

In this Python tutorial, you learned how to square a number in Python using different techniques. Developers mostly use the exponent operator and pow() method to calculate the square or any other power of a number.

Using the pow(), exponent operator and math.pow() method you can not only find the square but also any power of the number.

Leave a Reply

Your email address will not be published. Required fields are marked *