# 6 Ways to Square a Number in Python

Python is the most preferred programming language when it comes to dealing with a large amount of data. Python possesses numerable in-built libraries and various default methods which helps you to perform multiple operations on the data effectively and quickly. Squaring a number is one such operation where you multiple the number by itself. In this article, we will study various methods to square a number in python along with the example and outputs. So, let's get started!

## How to Square a Number in Python?

Squaring a number means multiplying the number by itself. Below are six methods by which you can find the square of a number in python.

### 1) Multiplication

The first approach that comes to your mind for finding the square of a number is to multiply the number by itself. This is the easiest and simplest method to find the square of the number in python. Check out the example below for understanding the working of this method.

For example:

```def number(a):
return a*a
print(number(5))
```

Output:

```25
```

### 2) Using an exponent operator

You can also find the square of a given number using the exponent operator in python. It is represented by "**". While applying this method, the exponent operator returns the exponential power resulting in the square of the number. Note that the statement “a**b” will be defined as “a to the power of b”.

For example:

```n = 5
result = n ** 2
print(result)
```

Output

```25
```

### 3) Using the pow() method

Python possesses an in-built library named “math” which helps you to perform all types of math operators on given data. pow() is one of the methods from the math library which can help you to find the square of a number in python. You can also use the pow() method to find other exponential power of a given number.

To use this method, we have to first import the math library using the “import” keyword. Later, the pow() method intakes a two-parameter, where the first parameter is the number and the second parameter suggests the exponential power to the number. In our case, the second parameter will be “2” as we want to find the square of the number. Take a look at the below example for a better understanding of the pow() method:

For example:

```n = 5
result = pow(n, 2)
print(result)
```

Output

```25
```

### 4) Square a list of the number

The list is one of the data structures in python which helps to store multiple elements under a single variable. When the list possesses an integer value, you can find the square of each number inside the list by multiplying by itself using the for loop as shown below:

For example:

```sample_list = [2,4,6,8]
result = [number ** 2 for number in sample_list]
print(result)
```

Output

```[4, 16, 36, 64]
```

### 5) Using while loop

One of the least used methods to find the square of a number in python is by making use of a while loop. While loop repeats the block of code until the given condition gets false. Therefore, we will find the square of the number using the while loop till the condition becomes false as shown in the below example:

For example:

```n = 1
while n <= 5:
print (n, '\t', n ** 2)
n += 1
```

Output:

```1        1
2        4
3        9
4        16
5        25
```

### 6) Square of array

To find the square of the array containing the integer values, the easiest way is to make use of the NumPy library. Numpy is an in-built python library that helps to perform all kinds of numerical operations on data with simple and efficient steps.

The NumPy square method will help you to calculate the square of each element in the array and provide you with the final output. To make use of the NumPy library, you have to import it using the “import” keyword as shown in the below example:

For example:

```import numpy as np

arr = np.array([2,4,6,8])
print("Square Value of arr : \n", np.square(arr))
```

Output

``` Square Value of arr :
[4 16 36 64]
```

## Conclusion

As python is used in many data-driven domains like machine learning and artificial intelligence, performing mathematical operations in python is not an uncommon task. Therefore, we have presented various methods which will help you to find the square of the given number explicitly with the least possible steps. It is highly recommended to learn and make use of these steps and your programming efficient and faster. For more such programming knowledge, visit our blogs at Favtutor.

### FavTutor - 24x7 Live Coding Help from Expert Tutors! 