{"id":119192,"date":"2026-06-28T22:06:24","date_gmt":"2026-06-28T16:36:24","guid":{"rendered":"https:\/\/www.guvi.in\/blog\/?p=119192"},"modified":"2026-06-28T22:07:31","modified_gmt":"2026-06-28T16:37:31","slug":"how-to-find-square-root-in-python","status":"publish","type":"post","link":"https:\/\/www.guvi.in\/blog\/how-to-find-square-root-in-python\/","title":{"rendered":"How to Find the Square Root in Python\u00a0"},"content":{"rendered":"\n<p>Square roots come up constantly in Python, whether you&#8217;re calculating distances, working with statistics, or solving a math problem. Python doesn&#8217;t have a built-in square root keyword, but it gives you several reliable ways to calculate one, each suited to a slightly different situation.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>TL;DR&nbsp;Summary<\/strong><\/h2>\n\n\n\n<ul>\n<li><strong>Python offers multiple ways to calculate square roots<\/strong>, including math.sqrt(), the exponent operator (** 0.5), math.pow(), numpy.sqrt(), and cmath.sqrt().<\/li>\n\n\n\n<li><strong>math.sqrt() is the preferred choice for most use cases<\/strong> because it is simple, readable, and included in Python&#8217;s standard library.<\/li>\n\n\n\n<li><strong>Different methods handle negative numbers differently<\/strong>, making it important to choose the right approach based on whether you&#8217;re working with real numbers, arrays, or complex values.<\/li>\n<\/ul>\n\n\n\n<p><em>Calculate square roots in Python using math.sqrt() and ** 0.5. Master Python basics to advanced with HCL GUVI\u2019s <\/em><strong><em>Python Zero to Hero Course<\/em><\/strong><em>. <\/em><a href=\"https:\/\/www.guvi.in\/courses\/programming\/python-zero-to-hero\/\" target=\"_blank\" rel=\"noreferrer noopener\"><em>Start your Python journey here<\/em><\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>How Do You Find the Square Root in Python?<\/strong><\/h2>\n\n\n\n<p>The most common way to find a square root in Python is using math.sqrt(), part of the built-in math module. For example, math.sqrt(25) returns 5.0. Other options include the ** exponent operator, math.pow(), NumPy&#8217;s np.sqrt() for arrays, and cmath.sqrt() for negative or complex numbers.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Understanding Square Roots Before You Code<\/strong><\/h2>\n\n\n\n<ol>\n<li>A square root is the inverse of squaring. If 3 squared is 9, then the square root of 9 is 3, because 3 \u00d7 3 equals 9. Simple in concept, but there are three categories of numbers worth understanding before you start calculating.<\/li>\n\n\n\n<li><strong>Positive numbers<\/strong> behave exactly as expected. The square root of 25 is 5, the square root of 1 is 1.<\/li>\n\n\n\n<li><strong>Zero<\/strong> has a square root of zero&nbsp; straightforward, no surprises.<\/li>\n\n\n\n<li><strong>Negative numbers<\/strong> are where things get interesting. There&#8217;s no real number that, when multiplied by itself, gives a negative result.<\/li>\n\n\n\n<li>&nbsp;Two negatives multiplied together always produce a positive number. This means math.sqrt(-10) will throw an error&nbsp; Python&#8217;s math module only works with real numbers, and negative square roots require complex numbers instead.<\/li>\n\n\n\n<li>Keep that distinction in mind. It explains why some of the methods below behave differently when negative numbers are involved.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Method 1: math.sqrt()<\/strong><\/h2>\n\n\n\n<ul>\n<li>This is the standard, most commonly used way to calculate a square root in Python. It&#8217;s part of the math module, which ships with the standard Python installation&nbsp; no external library required.<\/li>\n\n\n\n<li>import math<\/li>\n\n\n\n<li>a = 25 result = math.sqrt(a)<\/li>\n\n\n\n<li>print(result) # Output: 5.0<\/li>\n\n\n\n<li>math.sqrt() always returns a float, even when the result is a whole number. It works for any non-negative number, including decimals:<\/li>\n\n\n\n<li>import math<\/li>\n\n\n\n<li>print(math.sqrt(30.82)) # Output: 5.55157&#8230; print(math.sqrt(0)) # Output: 0.0<\/li>\n\n\n\n<li>Try it on a negative number, and Python raises a ValueError:<\/li>\n\n\n\n<li>import math<\/li>\n\n\n\n<li>print(math.sqrt(-10))<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>ValueError: math domain error<\/strong><\/h2>\n\n\n\n<p>For most everyday Python code, math.sqrt() is the method you&#8217;ll reach for. It&#8217;s fast, simple, and clearly communicates intent to anyone reading your code.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Method 2: The Exponent Operator (**)<\/strong><\/h2>\n\n\n\n<ul>\n<li>Since a square root is mathematically the same as raising a number to the power of 0.5, Python&#8217;s exponent operator can do the job without importing anything at all.<\/li>\n\n\n\n<li><strong>a = 25 result = a ** 0.5<\/strong><\/li>\n\n\n\n<li><strong>print(result) # Output: 5.0<\/strong><\/li>\n\n\n\n<li>This works identically to math.sqrt() for positive numbers, but with zero dependencies. It&#8217;s a useful shortcut when you don&#8217;t want to import math just for one calculation or when you&#8217;re working in an environment with restricted imports.<\/li>\n\n\n\n<li>One quirk worth knowing: unlike math.sqrt(), the exponent operator does not raise an error on negative numbers&nbsp; it returns a complex number instead, because Python&#8217;s ** operator handles complex arithmetic automatically when needed.<\/li>\n\n\n\n<li><strong>result = (-25) ** 0.5 print(result) # Output: (3.0616171314562834e-16+5j) a complex number<\/strong><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Method 3: math.pow()<\/strong><\/h2>\n\n\n\n<ul>\n<li>math.pow() is another option from the math module. It takes two arguments&nbsp; the base and the exponent&nbsp; so calculating a square root means raising to the power of 0.5, just like the exponent operator.<\/li>\n\n\n\n<li><strong>import math<\/strong><\/li>\n\n\n\n<li><strong>a = 25 result = math.pow(a, 0.5)<\/strong><\/li>\n\n\n\n<li><strong>print(result) # Output: 5.0<\/strong><\/li>\n\n\n\n<li>The result is identical to math.sqrt() and the ** operator for positive numbers. The main difference is readability and convention \u2014 math.pow() makes the &#8220;raising to a power&#8221; operation explicit, which can be useful in code that already deals with various exponents.<\/li>\n\n\n\n<li>Like math.sqrt(), math.pow() raises an error on negative numbers, since it also only works with real numbers.<\/li>\n<\/ul>\n\n\n\n<p><em>Calculate square roots in Python using math.sqrt() and ** 0.5. Master Python basics to advanced with HCL GUVI\u2019s <\/em><strong><em>Python Zero to Hero Course<\/em><\/strong><em>. <\/em><a href=\"https:\/\/www.guvi.in\/courses\/programming\/python-zero-to-hero\/\" target=\"_blank\" rel=\"noreferrer noopener\"><em>Start your Python journey here<\/em><\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Method 4: NumPy&#8217;s np.sqrt()<\/strong><\/h2>\n\n\n\n<ol>\n<li>If you&#8217;re already working with NumPy, which is extremely common in <a href=\"https:\/\/www.guvi.in\/blog\/what-is-data-science\/\" target=\"_blank\" rel=\"noreferrer noopener\">data science<\/a> or numerical computing contexts, np.sqrt() is the natural choice. Its biggest advantage is that it works on arrays just as easily as it works on single numbers.<\/li>\n\n\n\n<li><strong>import numpy as np<\/strong><\/li>\n\n\n\n<li><strong>a = 25 result = np.sqrt(a) print(result) # Output: 5.0<\/strong><\/li>\n\n\n\n<li><strong>arr = [1, 25, 100] arr_result = np.sqrt(arr) print(arr_result) # Output: [ 1. 5. 10.]<\/strong><\/li>\n\n\n\n<li>This is the only method on this list that handles entire arrays in a single call, applying the square root to every element. That&#8217;s enormously useful when working with <a href=\"https:\/\/www.guvi.in\/blog\/best-datasets-for-data-science-projects\/\" target=\"_blank\" rel=\"noreferrer noopener\">datasets<\/a>, rather than looping through individual values manually.<\/li>\n\n\n\n<li>Keep in mind the return type: np.sqrt() returns a numpy.float64 for a single number and a numpy.ndarray for an <a href=\"https:\/\/www.guvi.in\/blog\/leaders-in-an-array\/\" target=\"_blank\" rel=\"noreferrer noopener\">array<\/a>, not Python&#8217;s native float or list. This matters if you&#8217;re passing results into code that expects native Python types.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Method 5: cmath.sqrt() for Negative and Complex Numbers<\/strong><\/h2>\n\n\n\n<ul>\n<li>When you specifically need to handle negative numbers or complex numbers, cmath&nbsp; Python&#8217;s complex math module&nbsp; is the right tool. Unlike math.sqrt(), it never raises an error for negative input.<\/li>\n\n\n\n<li><strong>import cmath<\/strong><\/li>\n\n\n\n<li><strong>print(cmath.sqrt(25)) # Output: (5+0j) print(cmath.sqrt(-100)) # Output: 10j print(cmath.sqrt(10+10j)) # Output: (3.7..+1.32..j)<\/strong><\/li>\n\n\n\n<li>Every result from cmath.sqrt() is a complex number, even when the input is a simple positive integer.<\/li>\n\n\n\n<li>&nbsp;That&#8217;s a meaningful difference from the other methods&nbsp; if you only need real number results, this extra complexity isn&#8217;t worth it.&nbsp;<\/li>\n\n\n\n<li>But for any code that may legitimately encounter negative values and needs a defined result instead of a crash, cmath.sqrt() is exactly the right tool.<\/li>\n<\/ul>\n\n\n\n<div style=\"background-color: #099f4e; border: 3px solid #110053; border-radius: 12px; padding: 18px 22px; color: #FFFFFF; font-size: 18px; font-family: Montserrat, Helvetica, sans-serif; line-height: 1.6; box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15); max-width: 750px;\">\n\n  <strong style=\"font-size: 22px; color: #FFFFFF;\">\ud83d\udca1 Did You Know?<\/strong>\n  <br \/><br \/>\n\n  The <strong style=\"color: #FFFFFF;\">square root<\/strong> operation is fundamental to fields such as <strong style=\"color: #FFFFFF;\">machine learning<\/strong>, <strong style=\"color: #FFFFFF;\">statistics<\/strong>, <strong style=\"color: #FFFFFF;\">computer graphics<\/strong>, and <strong style=\"color: #FFFFFF;\">physics<\/strong>. One of its most common applications is the <strong style=\"color: #FFFFFF;\">Euclidean distance formula<\/strong>, which measures the straight-line distance between two points. This calculation powers technologies ranging from recommendation systems and clustering algorithms to GPS navigation, image recognition, and robotics.\n\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Comparing All Five Methods<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Method<\/strong><\/td><td><strong>Library Needed<\/strong><\/td><td><strong>Works on Negative Numbers<\/strong><\/td><td><strong>Best For<\/strong><\/td><\/tr><tr><td>math.sqrt()<\/td><td>math (built-in)<\/td><td>No raises ValueError<\/td><td>General-purpose, single values<\/td><\/tr><tr><td>** 0.5<\/td><td>None<\/td><td>Returns complex number<\/td><td>Quick calculations, no imports<\/td><\/tr><tr><td>math.pow()<\/td><td>math (built-in)<\/td><td>No&nbsp; raises ValueError<\/td><td>Explicit power operations<\/td><\/tr><tr><td>np.sqrt()<\/td><td>numpy<\/td><td>No&nbsp; returns nan<\/td><td>Arrays, data science workflows<\/td><\/tr><tr><td>cmath.sqrt()<\/td><td>cmath (built-in)<\/td><td>Yes&nbsp; returns complex<\/td><td>Negative or complex number handling<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Bonus: Cube Roots in Python<\/strong><\/h2>\n\n\n\n<ul>\n<li>If a square root is a number raised to the power of 0.5, a cube root follows the same logic raised to the power of 1\/3:<\/li>\n\n\n\n<li>a = 27 cube_root = a ** (1\/3)<\/li>\n\n\n\n<li>print(cube_root) # Output: 3.0<\/li>\n\n\n\n<li>This pattern generalises to any root: the nth root of a number is that number raised to the power of 1\/n. NumPy also offers np.cbrt() specifically for cube roots, which handles negative numbers correctly, unlike the exponent approach.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Which Method Should You Use?<\/strong><\/h2>\n\n\n\n<ul>\n<li>For most everyday Python scripts, math.sqrt() is the right default&nbsp; it&#8217;s fast, clear, and built into the standard library. Reach for the exponent operator (** 0.5) when you want a zero-dependency one-liner.&nbsp;<\/li>\n\n\n\n<li>Choose np.sqrt() the moment you&#8217;re working with arrays or already have NumPy in your project. And keep cmath.sqrt() in your back pocket for the specific case where negative or complex inputs are a real possibility rather than a sign of a bug.<\/li>\n\n\n\n<li>Most <a href=\"https:\/\/www.guvi.in\/hub\/python\/\" target=\"_blank\" rel=\"noreferrer noopener\">Python <\/a>programs only ever need one of these math.sqrt()&nbsp; but knowing the other four means you&#8217;ll always have the right tool when an edge case shows up.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>Python gives you five solid ways to calculate a square root, each suited to a different situation. math.sqrt() covers the vast majority of everyday use cases. The exponent operator offers a dependency-free shortcut. math.pow() is functionally identical with more explicit syntax. NumPy&#8217;s np.sqrt() extends naturally to arrays.<\/p>\n\n\n\n<p>&nbsp;And cmath.sqrt() is the only method that gracefully handles negative numbers without crashing. Pick the one that fits your data and your dependencies, and you&#8217;ll never be stuck wondering how to calculate a square root in Python again.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>FAQs<\/strong><\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1782446722683\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \"><strong>1. What is the easiest way to find a square root in Python?<\/strong><\/h3>\n<div class=\"rank-math-answer \">\n\n<p>The easiest and most commonly used method is math.sqrt(). After importing Python&#8217;s built-in math module, you can calculate a square root with math.sqrt(number).<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1782446727690\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \"><strong>2. Can I calculate a square root without importing any module?<\/strong><\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Yes. You can use the exponent operator and raise a number to the power of 0.5.<br \/>result = 25 ** 0.5<br \/>print(result)\u00a0 # 5.0<br \/>This approach requires no imports and works well for simple calculations.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1782446742114\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \"><strong>3. What happens if I use math.sqrt() on a negative number?<\/strong><\/h3>\n<div class=\"rank-math-answer \">\n\n<p>math.sqrt() raises a ValueError because the math module only supports real numbers.<br \/>import math<br \/>math.sqrt(-25)<br \/># ValueError: math domain error<br \/>For negative numbers, use cmath.sqrt() instead.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1782446755972\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \"><strong>4. When should I use numpy.sqrt()?<\/strong><\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Use numpy.sqrt() when working with arrays, matrices, or large datasets. It can compute square roots for multiple values simultaneously, making it ideal for data science and scientific computing tasks.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1782446764521\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \"><strong>5. What is the difference between math.sqrt() and math.pow()?<\/strong><\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Both can calculate square roots, but math.sqrt() is specifically designed for square root operations, while math.pow() performs general exponentiation.<br \/>math.sqrt(25)<br \/>math.pow(25, 0.5)<br \/>For readability and intent, math.sqrt() is usually preferred.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1782446778194\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \"><strong>6. How do I calculate the square root of a negative number in Python?<\/strong><\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Use Python&#8217;s built-in cmath module, which supports complex numbers.<br \/>import cmath<br \/>result = cmath.sqrt(-25)<br \/>print(result)\u00a0 # 5j<br \/>This returns a complex number instead of raising an error.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1782446791021\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \"><strong>7. Which square root method should I choose in Python?<\/strong><\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Choose based on your use case:<br \/><strong>math.sqrt()<\/strong> \u2013 Best for everyday calculations with real numbers.<br \/><strong>** 0.5<\/strong> \u2013 Quick calculations without imports.<br \/><strong>math.pow()<\/strong> \u2013 When working with general exponent operations.<br \/><strong>numpy.sqrt()<\/strong> \u2013 For arrays and data science workloads.<br \/><strong>cmath.sqrt()<\/strong> \u2013 For negative or complex numbers.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>Square roots come up constantly in Python, whether you&#8217;re calculating distances, working with statistics, or solving a math problem. Python doesn&#8217;t have a built-in square root keyword, but it gives you several reliable ways to calculate one, each suited to a slightly different situation.&nbsp; TL;DR&nbsp;Summary Calculate square roots in Python using math.sqrt() and ** 0.5. [&hellip;]<\/p>\n","protected":false},"author":63,"featured_media":119407,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[717],"tags":[],"views":"36","authorinfo":{"name":"Vishalini Devarajan","url":"https:\/\/www.guvi.in\/blog\/author\/vishalini\/"},"thumbnailURL":"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/06\/how-to-find-square-root-in-python-300x150.webp","_links":{"self":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/119192"}],"collection":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/users\/63"}],"replies":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/comments?post=119192"}],"version-history":[{"count":3,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/119192\/revisions"}],"predecessor-version":[{"id":119409,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/119192\/revisions\/119409"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/media\/119407"}],"wp:attachment":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/media?parent=119192"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/categories?post=119192"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/tags?post=119192"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}