Python is the most preferred programming language when it comes to dealing with a large amount of data. It possesses many in-built libraries which help you to perform multiple operations such as squaring a number. In this article, we will learn about all the methods to Square a Number in Python with various use cases. So, let's get started!
6 Ways to Square a Number in Python
Before moving to the techniques, let's first revise what we are going to do. Squaring a number means multiplying the number by itself. Let's say you have a number x, then its square is x².
Below are 6 methods by which you can find the square of a number:
The simplest approach to finding the square of a number in Python is to multiply the number by itself. This is the easiest way out of all of them where we use the multiplication operator '*'.
def number(a): return a*a print(number(5))
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”.
n = 5 result = n ** 2 print(result)
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. The pow() is one of the methods from the math library which can help you to find the square of a number. 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:
n = 5 result = pow(n, 2) print(result)
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.
sample_list = [2,4,6,8] result = [number ** 2 for number in sample_list] print(result)
[4, 16, 36, 64]
5) Using a 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 the 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.
n = 1 while n <= 5: print (n, '\t', n ** 2) n += 1
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.
import numpy as np arr = np.array([2,4,6,8]) print("Square Value of arr : \n", np.square(arr))
Square Value of arr : [4 16 36 64]
Program to Input a Number from the User
This is one of the beginner-level Python problems that any programmer solves. In the below program, an integer is given by the user as the input, and the square of that integer is printed as the output. Here we have used the multiplication method to find the square. Check the code below:
n = int(input("Enter the integer to square: ")) output =n*n print(output)
Enter the integer to square: 45 2025
Program for Square of N numbers
Sometimes, we need a list of squared numbers. First, the ‘n’ number is defined by the user. The following Python program prints the squares of all the numbers till n. Here we used the exponent operator with the for loop.
n = int(input("Enter the number of integers to square: ")) for i in range(1, n+1): square = i ** 2 print(square)
Enter the number of integers to square: 3 1 4 9
Program to Square Numbers in a Range
After we know how to print squares of 'n' numbers, we can also use this for a range of numbers and print them. In this case, a range of numbers is defined by the user.
n = int(input("Enter the lower range to square: ")) m = int(input("Enter the upper range to square: ")) for i in range(n, m+1): square = i ** 2 print(square)
Enter the lower range to square: 4 Enter the upper range to square: 8 16 25 36 49 64
We have presented various methods which will help you to find the square of the number in Python. It is highly recommended to understand all of them to make your programming efficient. You can now learn how to round down numbers in Python as well. Happy Learning :)