Python Square: How to Calculate the Square of a Number?

Posted in /  

Python Square: How to Calculate the Square of a Number?
vinaykhatri

Vinay Khatri
Last updated on March 28, 2024

    We often need to find out the square of a number when we write code in Python. In the Python programming language, there are various techniques you can use to do the same. In this Python tutorial, you will learn how to find the square of a number using 4 distinct techniques.

    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. If n is the number, then the square of the same is represented by n 2 . For instance, the square of 9 will be represented as 9 2 and calculated as 9x9 =81. Similarly, the square of 81 will be 81x81 i.e. 6561. The 4 techniques that we are going to use in this Python tutorial for finding out the square of a number are:

    1. Using the Exponent or Power Operator (**)
    2. With the pow() Method
    3. Using the math.pow() Method
    4. Multiplying the number by itself i.e. using the Multiplication Operator (*).

    1. Using the Exponent Operator (**)

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

    Example to calculate the square of a number using the power (**) 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

    2. Using the pow() Method

    Python has an inbuilt method pow(a,b) . It accepts 2 parameters a and b, and returns a result as a raised to the power b . The pow() stands for power method . Using this method we can calculate the power or exponent value of a number. To find out the the square of a number using the pow(a,b) method we need to specify the b value to 2 and a could be any number whose square we need to find.

    Example to calculate the square of a number using the 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

    3. Using the math.pow() Method

    Python contains an inbuilt module math for performing mathematical operations. In the Python math module, we have the math.pow() method that can also find the power or exponent value of a specified number. The working of the math.pow() method is similar to the pow() method. The only difference between the two is that while the math.pow() method returns a floating-point value, the pow() method can return both an integer as well as a float value.

    <Note> C lick here to know the difference between the math.pow() and pow() methods.

    Example to calculate the square of a number using the 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

    4. Using the Multiplication Operator (*)

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

    Example to calculate the square of a number using the 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 the Values in a Python List/Array

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

    Example: Using list comprehension.

    List comprehension provides a single line statement as an alternative for using multiline for loop and the 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 the map() function.

    The map() function is used when we want to apply a single function on all the elements of a list or array.

    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, we learned how to square a number in Python using 4 different techniques. Usually, developers use the exponent operator and the pow() method to calculate the square as well as any other power of a number. Have any queries regarding the article or the Python code? Let us know via the comments section below. You can also share your views about the code there.

    People are also reading:

    Leave a Comment on this Post

    0 Comments