AppDividend
Latest Code Tutorials

Python Sqrt Example | Python Math Square Root Tutorial

0

Python Sqrt Example | Python Math Square Root Tutorial is today’s topic. We can import the math module and then call the sqrt function to get the output. The sqrt() method returns square root of x for x > 0. Let’s deep dive in Python Math Sqrt() Example.

Python Sqrt Example

The syntax for Python sqrt() function is following.

math.sqrt(number)

See the following example.

# app.py

import math

print(math.sqrt(81))

The output is following.

 

Python Math Square Root Tutorial

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

Now, there is one another way which 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)

The output of the above code is following.

 

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))

The output is following.

 

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))

The output is following.

 

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.