# How to Calculate Square Root in Python

In layman language square root can be defined as A square root of a number is a value that, when multiplied by itself, gives the number. In Python or any other Programming Language to calculate the square root of a number, we have different methods. And In this tutorial, we will try to cover all the methods to calculate the square root of a number.

To calculate the Square Root in Python we have basically 5 methods or ways. The most common or easiest way is by using a math module sqrt function. Python sqrt function is inbuilt in a math module, you have to import the math package (module). The sqrt function in the python programming language that returns the square root of any number (number > 0).

Contents

## Different Ways to Calculate Square Root in Python

Generally, we have ways to calculate the square root in Python which are mentioned below:

1. Using math.sqrt() Method
2. Using ** operator
3. For real or complex numbers using cmath module
4. Using loop
5. Python Square root of a Number using pow()

## Calculating Square Root in Python Using sqrt() Function

Python math module deals with mathematical functions and calculations. Function sqrt() in the math module is used to calculate the square root of a given number.

### Syntax

Following is the syntax for Python sqrt() function.

``````import math
math.sqrt(num)``````

### Parameters

num –  Here num can be any positive number whom square root you want.

### Return Value of sqrt() Function

sqrt() method in Python will return the square root of the given number in floating-point. If the value is less than 0 then it will return Runtime Error.

### Examples Calculating Square Root Using sqrt() Function

Let’s see some examples to calculate the Python Square Root using sqrt() function.

Example 1: Calculating Square Root of a Positive Integer

``````import math
print("The square root of 25 is",math.sqrt(64))``````

Output:

``The square root of 25 is 8.0``

Example 2: Calculating Square Root of a Floating Point Number

``````import math
print("The square root of 9.9 is:", math.sqrt(12.9))``````

Output:

``The square root of 9.9 is: 3.591656999213594``

Example 3: Calculating Square Root of 0

``````import math
print("The square root of 0 is",math.sqrt(0))``````

Output:

``The square root of 0 is 0.0``

Example 4: Calculating Square Root of a Negative Number

``````import math
print("The square root of -16 is",math.sqrt(-16))``````

Output:

``````Traceback (most recent call last):
File "c:/Users/Karan/Desktop/test.py", line 2, in <module>
print("The square root of -16 is",math.sqrt(-16))
ValueError: math domain error``````

So, when x<0 it does not execute instead generates a ValueError.

Example 5: Calculating Square Root of Boltzmann Constant

``````import math
# Find square root of boltzmann constant

boltzmannConstant_SqRoot = 1.38064852*pow(10,-23)

print("Square root of Boltzmann constant:{}".format(math.sqrt(boltzmannConstant_SqRoot)))``````

Output:

``Square root of Boltzmann constant:3.715707900252655e-12``

### Notes:

Subscribe
Notify of 