Find square root of negative number using python

featured Image

In this article, we will learn how to find the square root of a negative number using python.

To find the square root of a negative number we have to use complex numbers. In python, we have the cmath that provides us with various mathematical functions to work with complex numbers.

Square root of a negative number in Python

To work with complex numbers we can use the cmath library of python. And to get the square of a negative number we can use the cmath.sqrt() method from cmath library.

Example:

import cmath

number = -5

negative_sq_val = cmath.sqrt(number)

print(negative_sq_val)

Output:

2.23606797749979j

Here, in this example, we have imported the cmath module and then passed the negative number to the sqrt() method.

The cmath.sqrt() method performs the mathematical operations to find the square root and returns the number.


Other Articles You’ll Also Like:

Square a number in Python (3 ways)

Related Posts

merge json files in python

How to merge JSON files in python

JSON (JavaScript Object Notation) is a data format used for storing, transferring, and visualizing data. Its simple structure of key:value pairs and array data types makes JSON an easy and…

Read more
Install python pip in ubuntu

How to Install Pip(pip3) in Ubuntu Linux 22.04

Pip is a package manager for Python that allows you to install and manage additional Python packages that are not part of the Python standard library. Pip makes it easy…

Read more
featured Image

How to Write If-Else Statements in One Line in Python

If-else statements are a fundamental concept in Python and other programming languages. They allow you to execute different blocks of code based on whether a condition is true or false….

Read more

Leave a Reply

Your email address will not be published. Required fields are marked *