# How to square a number in Python? All possible ways

We all know, by multiplying a number by itself, we can generate the square of a number. There are different possible ways to get the square of a number in python. some of them are inbuilt and others are user-defined techniques.

Let’s see them one by one,

## Basic method

The basic approach involves just multiplying the number by itself.

## Inbuilt function pow()

In python one of the method to square, a given number is by using the inbuilt function pow(base, exponent). This function gives the result when a number is raised to the power of another number. Hence to do squaring we can do pow(number,2). That implies the number increased to power 2 or in other words square of a number.

base – the number whose power needs to be calculated.

exponent – the number of times the base is going to multiply itself.

For example:- pow(10,2) = 100, pow(8,2) = 64, etc.

## (**)power operator

“******” is a symbol used to generate the power of a number. We can also use it for squaring by doing “number raise to power two” i.e (num**2) will give a square of num.

For example:- to get a square of 2 -> 2**2, will give 4. similarly square of 5 -> 5**2, will give 25.

## Math module

Module refers to files that may contain some statements or definitions in python language. such as, test.py is a python file and hence can be called a module. ‘test’ would be the module name.

modules are small, structured, and manageable codes of a large program. let’s say the test.py module contains some defined functions like add, multiply, etc.

To use the module in python, firstly we need to import that module:- *import test*

moving to use the functions of this module, we need to import them also. like, to use add function of the test module import that in this way,

As mentioned above in python there are different types of modules out of which, the math module contains the defined functions of all the mathematical operations such as add, multiply, power, etc.

So we can use the power function (math. pow()) already defined in the python math module to generate the power of a number.

For example,

When you have to import a module to use its function, you can use any of the above-mentioned methods. the difference is only that in the first case we need to write “module. function()” while calling the function. In all three cases, we will get 25.0 as the answer.

The difference between the inbuilt power function(pow()) and the power function defined in the math module(math. pow()) is only that In the former case if the base value is float it will give the result as float value and if the base is an integer it will give result as integer, but math. pow() will always give a float value as the answer.

## User-defined square function

Other methods for squaring may include a user-defined function. A function defined by the user will not be inbuilt. In this way also we can either build a square function or a power function.

## Conclusion

Now you know different ways to get the square of a number. then go and do a lot of practice, I will be happy if you solve this question differently. Thank you.

Sharing is caring

Did you like what Srishti Kumari wrote? Thank them for their work by sharing it on social media.

## No comments so far

Curious about this topic? Continue your journey with these coding courses:

302 students learning

Haris

Python Crash Course for Beginners

Surendra varma Pericherla

Learn Data Structures Using Python