AppDividend
Latest Code Tutorials

Python Sqrt Example | How To Calculate Square Root In Python

0

Python sqrt() is an inbuilt math library function that returns square root of x for x > 0. We can import the math module and then call the sqrt() function to get the square root of any given value.

Python Sqrt Example

If you are looking to find a solution regarding calculate square root in Python then use the sqrt() function.

The syntax for Python sqrt() function is the following.

math.sqrt(number)

See the following example.

# app.py

import math

print(math.sqrt(81))

See the output.

Python Math Square Root Tutorial

It is the best way to find the square root of any number.

Now, there is one another way that will give us the same output, but the number must have positive, not negative and not complex. See the below example.

// app.py

num = 49

sq_root = num ** 0.5
print(sq_root)

See the output.

Python Sqrt Example

Now, if we want to get the square root of a complex number, then we can use the cmath module.

See the following example.

// app.py

import cmath

print(cmath.sqrt(1 + 2j))

See the output.

Python Sqrt Example | Python Math Square Root Tutorial

Let’s find the square root of the float value. See the following example.

// app.py

import math

print(math.sqrt(math.pi))

See the output.

Python Math Square Root

Finally, Python Sqrt Example | Python Math Square Root Tutorial is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.