Square A Number In Python

In this short guide, I’ll show you different ways to square a number in python. So let’s start by discussing its math definition.

In mathematics, the square of any integer number is the product of that number with itself.

For instance, 3×3 = 9 .Which implies square of 3 is 9.

1. By Using Exponential Operator

The builtin exponential operator represented by ** takes two real numbers as an argument and performs the arithmetic exponential operation on them. For example 8**3 = 8^3 = 8x8x8 = 512

In the following code, we have used an exponential operator to square a number in python.

# program to square a number in python
number = 25

square = number**2 # calculate the square

print(square)

Here is the output of above code.

Output

Program to square a number in python

2. By Multiplying Number Two Times

In this code example, we multiplied the input number with itself to obtain its square.

number = 25

square = number*number # calculate the square

print(square)

3. By Using math.pow() Function

math.pow(x,y) function from the builtin maths library takes x and y as an argument and returns x to the power y.

# how to find the square of a number in python
import math

number = 25

square = math.pow(number) # calculate the square

print(square)

Also read about how to find square root in python

Leave a Reply