How to Square a Number in Python (6 ways)

Using Python’s in-built libraries or functions, users can find the Square of a number. Multiplying the same number twice gives us the square of the number. This Python article provides diverse ways to arrive at the Square of the number.

Method 1: Use of Pow() Function in Python

Pow function is an in-built function available under the Math module of python. This function helps in the determination of the power of a number.

To determine the Square of the number, it uses two as the power.

Following is the syntax for the pow function as shown below:

Syntax:

Pow (base, exponent)

Explanation: –

The function takes two arguments, namely the base and exponent.

  • Base: the number whose power or Square needs to be calculated.
  • Exponent: is a number used as the superscript to the base number.

Example:

Let us take an example of how to determine the Square of a number using python code. This Python program would calculate a number’s square using base and exponent integers as inputs.

Python Code:

Base=input("Provide base integer")
if Base.isdigit()==True:
    Base=int(Base)
    result=pow(Base,2)
    print("The square result is ", result)
else:
    print("please provide an integer for base")

Output:

Provide base integer2
The square result is 4

Explanation:

Before determining the Square of a number, the above code takes an input from the user. The code checks the input to be a digit or not. If it is true, it determines the Square of number.

Method 2: Use of Power Operator

A power operator in python is represented as ‘**’. It is utilized in python to determine the power of a number. With an exponent of two as input, this operator gives the user the square of a number in Python. A power operator is also referred to as an exponent operator.

Power operator has the following python syntax:

Syntax: –

(Base**Exponent)

Let us take an example of how to determine the Square of a number using the exponent operator in python. This program would take base and exponent integers as input to determine the Square of a number.

Python Code:

Base=input("Provide base integer")
Exponent=input("Provide exponent integer")
if Base.isdigit() & Exponent.isdigit()==True:
    Base=int(Base)
    Exponent=int(Exponent)
    result=Base**Exponent
    print("The square result is ",result)
else:
    print("please provide an integer for base")

Output:

Provide base integer2
Provide Exponent integer2
The square result is 4

Explanation:

Before determining the Square of a number, the above code takes an input from the user. The code checks the input to be a digit or not. If it is true, it determines the Square of number.

Method 3: Use of Multiplication to Determine Square of a Number

The Creation of a python script to determine the Square of a number using multiplication is easy. The following Python code would take a number from the user and multiply it couple of times. It also checks whether the given input is a digit or not.

The python code would follow the following syntax:

Syntax:

(Base* Base)

The above syntax is similar to the basic mathematical representation. This shows that the Square of a number can be determined by multiplying the base by itself a couple of times.

Example:

Let us take an example of how to determine the Square of a number using multiplication in python code. This program would take base as input to determine the Square of a number.

Python Code:

Base=input("Provide base integer")
if Base.isdigit()==True:
    Base=int(Base)
    result=Base*Base
    print("The square result is ",result)
else:
    print("please provide an integer for base")

Output:

Provide base integer2
The square result is 4

Explanation:

Before determining the Square of a number, the above code takes an input from the user. The code checks the input to be a digit or not. If it is true, it determines the Square of number.

Method 4: Use of a List to Determine the Square of a Number

Python provides the functionality of determining the Square of a number for more than one base, and they can be grouped together to be formed as a python list. It is a type of data structure that enables the programmer to store multiple elements or values under one single variable. It would then square each number present in the list.

Let us take an example of how to determine the Square of a number using a list data structure. This program takes a list as an input to determine the Square of a number.

Python Code:

sqr_list = [2,4,6,8]
for Base in sqr_list:
    result=Base**2
    print("The square result is ",result)

Output:

The square result is 4
The square result is 16
The square result is 36
The square result is 64

Explanation:

In the above Python code, a for loop is executed that traverses through each element present in the list and for each element. It determines the Square of a number. Using the above approach, the list helps us in the determination of the Square of various integer values.

Method 5: Use of While Loop in Python

A while loop can also be used to determine the Square of a number in python. It can be termed as the repetition of a specific instruction till the time a specific condition is met. It helps in computing the Square of a number by repeating instructions until the provided condition becomes false.

The below program iterates using a while loop to determine the Square of a number until the counter used as input is either equal to or less than 5.

Python Code:

n_start = 1
while n_start <=5:
    result= n_start **2
    print("The square result is ",result)
    n_start=n_start+1

Output:

The square result is 1
The square result is 4
The square result is 9
The square result is 16
The square result is 25

Method 6: Use of arrays to Determine Square of a Number

Another method that could be utilized to determine the Square of a number is by utilizing the combination of arrays and an in-built function available within python.

One can use the Python array and square method present within the NumPy module to determine the Square of a number. Let us take an example to determine the Square of a number using the above two methods as shown below: –

Python Code:

import numpy as np
NumpyArray = np.array([2,4,6,8])
print("Square of the elements present in array are : \n", np.square(NumpyArray))

Output:

Square of the elements present in the array are:
[ 4 16 36 64]

Summary

  • Python provides six broad ways to determine the Square of a number.
  • A square of a number can be determined using Pow method. It can be found under the Math module and can be defined under def number definition.
  • Square of a number can be determined using arrays. They are data structures that can be accessed using the NumPy module.
  • List can also be used to calculate Square of the number. To each element present in a list, a number multiplied to itself can provide us with the Square of a number.
  • Alternatively, one can also use a simple multiplication operation.
  • Exponent operator also helps in the calculation of the Square of a number. It calculates and outputs the square using ** operator.
  • Squares of a number and square root of a number are two different aspects, and they should not be confused with one another.