# How to find Square Root in Python?

In this article, you will be learning how to find square roots in Python and what are the popular square root functions in Python.

## What is a Square root?

Square root, in mathematics, is a factor of a number that, when multiplied by itself, gives the original number. For example, both 3 and –3 are square roots of 9.

## How to calculate the Square root in Python?

The math module in Python has `sqrt()` and `pow()` functions, using which you can calculate the square root of a given number.

### Using sqrt() function

The `sqrt()` function takes one parameter and returns the square root of the provided number.

Syntax:

sqrt(x)  # x is the number whose square root needs to be calculated.

Example

Let’s take a various and find the square root of a decimal, positive number, zero.

``````# Import math module
import math

# calculate square root of given number
print(math.sqrt(25))

# square root of 10
print(math.sqrt(10))

# square root of 0
print(math.sqrt(0))

# square root of decimal number
print(math.sqrt(4.5))``````

Output

``````5.0
3.1622776601683795
0.0
2.1213203435596424``````

The `sqrt()` method can take only positive numbers in case if you provide the negative number you will get a ValueError as shown below.

``````# Import math module
import math

# calculate square root of negative number
print(math.sqrt(-33))
``````

Output

``````Traceback (most recent call last):
File "c:\Projects\Tryouts\main.py", line 5, in <module>
print(math.sqrt(-33))
ValueError: math domain error``````

### Using pow() function

The` pow()` method can be used to compute the square root of any number. This `pow()` function takes two parameters and multiplies them to compute the results. This is done in order to the mathematical equation where,

x2 = y or y=x**.5

The syntax of this function is as follows:

Syntax

pow(x,y)  # where y is the power of x or x**y

``````# Import math module
import math

# calculate square root of given number
print(math.pow(25,0.5))

# square root of 10
print(math.pow(10,0.5))

# square root of 0
print(math.pow(0,0.5))

# square root of decimal number
print(math.pow(4.5,0.5))``````

Output

``````5.0
3.1622776601683795
0.0
2.1213203435596424``````

Subscribe to get notified of the latest articles. We will never spam you. Be a part of our ever-growing community.

## How to check if a file exists in Python?

Table of Contents Hide Python check if a file exists using OS ModuleUsing os.path.exists()Using os.path.isfile()Using os.path.isdir()Using pathlib.Path.exists() When you perform a file operation such as reading from a file or…

## AttributeError: ‘numpy.ndarray’ object has no attribute ‘index’

Table of Contents Hide What is AttributeError: ‘numpy.ndarray’ object has no attribute ‘index’?How to fix AttributeError: ‘numpy.ndarray’ object has no attribute ‘index’?Example 1: Get the index position of an element…

## Python Compare Strings: A Step-By-Step Guide

In Python the strings are compared with == and != operators. These operators compare if the two given strings are equal or not and return true or false based on…

## Python String ljust()

Table of Contents Hide SyntaxParametersReturn valueExample 1: Python program to left justify a stringExample 2: ljust() Method With * fillcharExample 3: Returns an original string if the width is less…

## Python String center()

Table of Contents Hide SyntaxParameterReturn ValueExample 1: center() Method With default fillcharExample 2: center() Method With * fillchar Example 3: Returns an original string if the width is less than…