{"id":19585,"date":"2021-07-06T18:10:10","date_gmt":"2021-07-06T18:10:10","guid":{"rendered":"https:\/\/www.askpython.com\/?p=19585"},"modified":"2021-07-06T18:10:10","modified_gmt":"2021-07-06T18:10:10","slug":"calculate-square-root","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/examples\/calculate-square-root","title":{"rendered":"4 Methods to Calculate Square Root in Python"},"content":{"rendered":"\n<p>In this tutorial, we are going to discuss the different ways to calculate square root in Python.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">What is a square root?<\/h2>\n\n\n\n<p>In Mathematics, a <strong>square root<\/strong> of a number &#8216;<em>p<\/em>&#8216; is a number &#8216;<em>q<\/em>&#8216; which follows the condition <strong>p = q<sup>2<\/sup><\/strong>. In Python, we have so many methods to calculate the square root of numbers. Let&#8217;s discuss some well-known methods in Python to calculate the square root of numbers.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. Calculate square root using the exponent operator<\/h2>\n\n\n\n<p>In this method, we will define our own function to find the square root of a number. And to calculate the square root of a number we will be using the exponent operator (<code>**<\/code>) in Python. <\/p>\n\n\n\n<p>The defined function will take a number as an argument and return the square root of the number if it&#8217;s positive else it will print a warning. Let&#8217;s implement this in Python code.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n# Define the user defined sqrt() function\n# to calculate the square root of a number\ndef sqrt(N):\n    if N &lt; 0:\n        print(&#039;Square root of negative number does not exist!&#039;)\n        return\n    else:\n        print(f&#039;Square root of number {N}: {N**0.5}&#039;)\n        return\n\n# Call the above defined sqrt() function\n# to calculate the square root of a number\nsqrt(441)\nsqrt(0.81)\nsqrt(6.25)\nsqrt(634)\nsqrt(-121)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nSquare root of number 441: 21.0 \nSquare root of number 0.81: 0.9 \nSquare root of number 6.25: 2.5 \nSquare root of number 634: 25.179356624028344\nSquare root of negative number does not exist!\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">2. Using the sqrt() function<\/h2>\n\n\n\n<p>In Python, the <code>sqrt()<\/code> function is a predefined function that is defined in the <a href=\"https:\/\/www.askpython.com\/python-modules\/python-math-module\" data-type=\"post\" data-id=\"2026\">math module<\/a>. The <code>sqrt()<\/code> function returns the square root of the number passed as an argument. Let&#8217;s see how we can use the built-in <code>sqrt()<\/code> function in a Python program.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n# Import Python math module\nimport math as m\n\n# Call the predefined sqrt() function\n# to calculate the square root of a number\nprint(f&#039;Square root of number 121: {m.sqrt(121)}&#039;)\nprint(f&#039;Square root of number 0.49: {m.sqrt(0.49)}&#039;)\nprint(f&#039;Square root of number 4.41: {m.sqrt(4.41)}&#039;)\nprint(f&#039;Square root of number 265: {m.sqrt(265)}&#039;)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nSquare root of number 121: 11.0 \nSquare root of number 0.49: 0.7 \nSquare root of number 4.41: 2.1\nSquare root of number 265: 16.278820596099706\n<\/pre><\/div>\n\n\n<p><strong>NOTE:<\/strong> If a negative number is passed as an argument to the built-in sqrt() function then it will throw a <strong>math domain error<\/strong>. Let&#8217;s see an example.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n# Import Python math module\nimport math as m\n\n# Call the predefined sqrt() function\n# to calculate the square root of a negative number\nm.sqrt(-125)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<div class=\"wp-block-image is-style-default\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"745\" height=\"158\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/07\/math-domain-error-sqrt.png\" alt=\"Math Domain Error Sqrt\" class=\"wp-image-19851\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/07\/math-domain-error-sqrt.png 745w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/07\/math-domain-error-sqrt-300x64.png 300w\" sizes=\"auto, (max-width: 745px) 100vw, 745px\" \/><figcaption>math domain error: sqrt()<\/figcaption><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">3. Using the pow() function<\/h2>\n\n\n\n<p>In this method to calculate square root, we will be using the built-in <a href=\"https:\/\/www.askpython.com\/python\/built-in-methods\/python-pow\" data-type=\"post\" data-id=\"3276\">pow() function<\/a>. In Python, the <code>pow()<\/code> function is a predefined function that is defined in the <code>math<\/code> module. The <code>pow()<\/code> function takes two arguments one is base and the other is the exponent\/power and returns the square root of the number (<strong>base<\/strong>) passed as the first argument. To calculate the square root the exponent\/power argument is fixed to <strong>0.5<\/strong>. Let&#8217;s see how we can use the built-in <code>pow()<\/code> function in a Python program.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n# Import Python math module\nimport math as m\n\n# Call the predefined pow() function\n# to calculate the square root of a number\nprint(f&#039;Square root of number 625: {m.pow(625, 0.5)}&#039;)\nprint(f&#039;Square root of number 0.64: {m.pow(0.64, 0.5)}&#039;)\nprint(f&#039;Square root of number 1.21: {m.pow(1.21, 0.5)}&#039;)\nprint(f&#039;Square root of number 7: {m.pow(7, 0.5)}&#039;)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nSquare root of number 625: 25.0 \nSquare root of number 0.64: 0.8 \nSquare root of number 1.21: 1.1 \nSquare root of number 7: 2.6457513110645907\n<\/pre><\/div>\n\n\n<p><strong>NOTE: <\/strong>Here also if a negative number is passed as an argument to the built-in <code>pow()<\/code> function then it will throw a <strong>math domain error<\/strong>. Let&#8217;s see an example.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n# Import Python math module\nimport math as m\n\n# Call the predefined pow() function\n# to calculate the square root of a negative number\nm.pow(-121, 0.5)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<div class=\"wp-block-image is-style-default\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"747\" height=\"167\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/07\/math-domain-error-pow.png\" alt=\"Math Domain Error Pow\" class=\"wp-image-19852\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/07\/math-domain-error-pow.png 747w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/07\/math-domain-error-pow-300x67.png 300w\" sizes=\"auto, (max-width: 747px) 100vw, 747px\" \/><figcaption>math domain error: pow()<\/figcaption><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">4. Using built-in np.sqrt() function<\/h2>\n\n\n\n<p>In this method of finding the square root, we will be using the built-in <code>np.sqrt()<\/code> function. In Python, the <code>np.sqrt()<\/code> function is a predefined function that is defined in the <a href=\"https:\/\/www.askpython.com\/python-modules\/numpy\/python-numpy-module\" data-type=\"post\" data-id=\"7694\">numpy module<\/a>. The <code>np.sqrt()<\/code> function returns a <strong>numpy array<\/strong> where each element is the square root of the corresponding element in the numpy array passed as an argument. Let&#8217;s see how we can use the built-in <code>np.sqrt()<\/code> function in a Python program.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n# Import Python numpy module\nimport numpy as np\n\n# Define a numpy array\narr = np.array(&#x5B;0, 225, 0.36, 6.25, 10, -15])\nprint(&#039;NumPy array:&#039;)\nprint(arr)\n\n# Call the predefined np.sqrt() function\n# to calculate the square root of each element\n# in the numpy array\nprint(&#039;Returned NumPy array with Square roots:&#039;)\nprint(np.sqrt(arr))\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nNumPy array: \n&#x5B;  0.   225.     6.25  10.   -15.  ]\nReturned NumPy array with Square roots: \n&#x5B; 0.         15.          2.5         3.16227766         nan] \n&amp;lt;ipython-input-29-541b85f9361a&gt;:13: RuntimeWarning: invalid value encountered in sqrt   print(np.sqrt(arr))\n<\/pre><\/div>\n\n\n<p><strong>NOTE:<\/strong> If there is a negative number in the <strong>numpy array<\/strong> and it is passed to the built-in <code>np.sqrt()<\/code> function then it will throw a <strong>RuntimeWarning<\/strong> saying that an invalid value is encountered in sqrt. And set a <strong>nan<\/strong> value at the place of the square root of the negative element in the returned numpy array.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>In this tutorial, we have learned the different ways to calculate the square root of numbers in Python. We have also learned how to use Python functions like <code>math.sqrt()<\/code>, <code>math.pow()<\/code>, and <code>numpy.sqrt()<\/code>. Hope you have understood the things well and are excited to explore and learn more.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we are going to discuss the different ways to calculate square root in Python. What is a square root? In Mathematics, a square root of a number &#8216;p&#8216; is a number &#8216;q&#8216; which follows the condition p = q2. In Python, we have so many methods to calculate the square root of [&hellip;]<\/p>\n","protected":false},"author":33,"featured_media":19672,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-19585","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-examples"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/19585","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/33"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=19585"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/19585\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/19672"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=19585"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=19585"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=19585"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}