{"id":25429,"date":"2021-11-13T14:59:39","date_gmt":"2021-11-13T09:29:39","guid":{"rendered":"https:\/\/python-programs.com\/?p=25429"},"modified":"2021-11-14T15:01:59","modified_gmt":"2021-11-14T09:31:59","slug":"python-cmath-sqrt-method-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-cmath-sqrt-method-with-examples\/","title":{"rendered":"Python cmath.sqrt() Method with Examples"},"content":{"rendered":"
cmath.sqrt() Method in Python:<\/strong><\/p>\n The cmath.sqrt() method returns the given complex number’s square root.<\/p>\n Note:<\/strong> It should be noted that the number must be greater than or equal to 0.<\/p>\n Syntax:<\/strong><\/p>\n Parameters<\/strong><\/p>\n x:<\/strong> This is Required. A number whose square root is to be found.<\/p>\n Return Value:<\/strong><\/p>\n Returns a complex value that represents the complex number’s square root.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Note:<\/strong> The above input format is for dynamic input.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Similarly, try for the other examples<\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n cmath.sqrt() Method in Python: The cmath.sqrt() method returns the given complex number’s square root. Note: It should be noted that the number must be greater than or equal to 0. Syntax: cmath.sqrt(x) Parameters x: This is Required. A number whose square root is to be found. If the number is less than zero, a ValueError …<\/p>\ncmath.sqrt(x)<\/pre>\n
\n
Given Complex Number = 3+4j<\/pre>\n
The given complex number's (3+4j) square root value = \r\n(2+1j)<\/pre>\n
Given realpart = 5\r\nGiven imaginary part = 2<\/pre>\n
The given complex number's (5+2j) square root value = \r\n(2.27872385417085+0.4388421169022545j)<\/pre>\n
cmath.sqrt() Method with Examples in Python<\/h2>\n
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
\n
# Import cmath module(for complex number operations) using the import keyword.\r\nimport cmath\r\n# Give the complex number as static input and store it in a variable.\r\ncomplexnumb = 3+4j\r\n# Pass the given complex number as an argument to the cmath.sqrt() method that\r\n# returns the the given complex number's square root value.\r\n# Store it in another variable.\r\nrslt = cmath.sqrt(complexnumb)\r\n# Print the square root value of the given complex number.\r\nprint(\"The given complex number's\", complexnumb,\r\n \"square root value = \")\r\nprint(rslt)\r\n<\/pre>\n
The given complex number's (3+4j) square root value = \r\n(2+1j)<\/pre>\n
import cmath\r\ncomplexnumb = 12\r\nrslt = cmath.sqrt(complexnumb)\r\nprint(\"The given complex number's\", complexnumb,\r\n \"square root value = \")\r\nprint(rslt)\r\n<\/pre>\n
The given complex number's 12 square root value = \r\n(3.4641016151377544+0j)<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
\n
# Import cmath module(for complex number operations) using the import keyword.\r\nimport cmath\r\n# Give the real part and imaginary part of the complex number as user input\r\n# using map(), int(), split().\r\n# Store it in two variables.\r\nrealnumb, imaginarynumb = map(int, input(\r\n 'Enter real part and complex part of the complex number = ').split())\r\n# Using a complex() function convert those two variables into a complex number.\r\ncomplexnumb = complex(realnumb, imaginarynumb)\r\n\r\n# Pass the given complex number as an argument to the cmath.sqrt() method that\r\n# returns the the given complex number's square root value.\r\n# Store it in another variable.\r\nrslt = cmath.sqrt(complexnumb)\r\n# Print the square root value of the given complex number.\r\nprint(\"The given complex number's\", complexnumb,\r\n \"square root value = \")\r\nprint(rslt)\r\n<\/pre>\n
Enter real part and complex part of the complex number = 5 2\r\nThe given complex number's (5+2j) square root value = \r\n(2.27872385417085+0.4388421169022545j)<\/pre>\n","protected":false},"excerpt":{"rendered":"