Square a number in Python (3 ways)

featured Image

In this article, we will learn different ways to find the square of a number using different python methods and libraries.

A square of a number is a number multiplied by itself. If n is a number then the square of the number would be n x n.

In Python, we get a number of in-built libraries and methods to perform the multiplications of numbers easily and effectively to get the square.

Find the square of a number

In python, we can find the square of a number by :

  1. Using simple multiplication
  2. Using exponent operator
  3. Using math.pow() method

Let’s check some examples to understand it better.

Find Square of number by Multiplication

The easiest way to get the square of a number is by multiplying the number itself. So we just have to write a function that takes in the number as an argument and returns the square of it.

def getSquare(n):
    return n*n

square_val = getSquare(5)

print(square_val)

Output:

25

Here, the function multiplied the number by itself and returns the square of the given number.

Find square of a number using exponent operator

In Python, one of the most important used arithmetic operators is the exponent operator.

The ** is the exponent operator used to perform exponent arithmetic. It takes in two real numbers and returns a single number.

For example, to find the square of a number the mathematical expression is a2 which can be written as a**2 using python exponent operator. It means a to the power of 2.

num = 5

square_val = num ** 2

print(square_val)

Output:

25

Here, we have used the exponent operator and to calculate the square of a number using the exponent operator.

Using math.pow() method to calculate square of a number

The math.pow() is used to returns the value of a base raised to a power in python. It is used to find the square of any number in python.

The math.pow() method is provided by the math python library. It provides utility functions to perform different mathematical operations in python.

Syntax:

math.pow(num,exp)

To find the square of a number using pow() method:

  1. Import pow from the math library
  2. Pass the two numbers as parameters. The first parameter is the number and the second is the exponential power to the number. Here it will be 2 as we are finding the square of the number.

Example:

from math import pow

num = 5

square_val = pow(num, 2)

print(square_val)

Output:

25.0

Find the square of array and list in python

Now if we want to find the square of the numbers in an array or a Python list then we can use:

  1. List comprehension
  2. Numpy square() method

Find the square of Python List

If we have a list of numbers, then we can find the square of each number using list comprehension.

The list comprehension is a short-hand syntax that creates a list based on an existing python list.

numbers = [2, 3, 4, 5]

square_val = [num ** 2 for num in numbers]

print(square_val)

Output:

[4, 9, 16, 25]

Here, we have used the for loop to iterate through each number in the list and used the exponent operator (**) to find the square root of each number in the list.

Square of an Array in python

In Python, we can find the square of items (numbers) in an array using the Numpy library.

The numpy library provides the numpy.square() method that performs mathematical operations on the numbers and returns us the square of a given number.

To use square() method we first need to import numpy library and use numpy.array() method to create the arraay in python.

Example:

import numpy as np

numbers_arr = np.array([2, 3, 4, 5])

square_val = np.square(numbers_arr)

print(square_val)

Output:

[4, 9, 16, 25]

Here, we have imported numpy as np and created the array of numbers using np.array(). Next, we pass the array as an argument to the np.square() method, which returns the square value of the numbers.


Exercises:

Q1. Write a program to find out square of the numbers from 1 to 5 in Python

numbers_arr = list(range(1,6))

def findSquare(numbers):
    return [num**2 for num in numbers]

square_val = findSquare(numbers_arr)

print('List numbers: ',numbers_arr)
print('Square values: ',square_val)

Output:

List numbers:  [1, 2, 3, 4, 5]
Square values:  [1, 4, 9, 16, 25]

Here, we have used range to create a list from 1 to 5. The range(1,6) means start from 1 upto the number 6 (not including 6).

Then we used a function that uses list comprehension and the exponent operator(**) to calculate and return the list of square numbers.


Other Articles You’ll Also Like:

Check for prime number using for and while loop in Python

Related Posts

merge json files in python

How to merge JSON files in python

JSON (JavaScript Object Notation) is a data format used for storing, transferring, and visualizing data. Its simple structure of key:value pairs and array data types makes JSON an easy and…

Read more
Install python pip in ubuntu

How to Install Pip(pip3) in Ubuntu Linux 22.04

Pip is a package manager for Python that allows you to install and manage additional Python packages that are not part of the Python standard library. Pip makes it easy…

Read more
featured Image

How to Write If-Else Statements in One Line in Python

If-else statements are a fundamental concept in Python and other programming languages. They allow you to execute different blocks of code based on whether a condition is true or false….

Read more

Leave a Reply

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