In this tutorial, you’ll learn how to calculate use Python to calculate the square root of a number, using the Show The Quick Answer: Use math.sqrt() to Calculate a Python Square Root Use the math library to calculate a square root with Python!Let’s see what you’ll learn in this post!
What is a Square Root?In math, you often hear of the square of a number,
which is often represented by a superscript 2. So, a square of a number Because of this, the square of a number will always have some properties:
But why are we learning about squares, if
this article is about the What I mean by this, is that the square root of Now that you have a solid understanding of what the square root is, let’s dive into how to calculate the square root using Python! Want to learn something else? Want to learn how to calculate the standard deviation in Python? Check out my in-depth tutorial here! Python Square Root Using math.sqrtTo calculate the square root in Python, you can use the built-in The # Using the sqrt() function to calculate a square root from math import sqrt number = 25 square_root = sqrt(number) print(square_root) # Returns: 5.0 Let’s explore what we’ve done here:
Something important to note here is that the square root returned is a floating point value. This is because for most numbers (other than those called perfect squares), the value returned is not a neat integer value. Later on in this tutorial, you’ll learn how to create an integer square root. If you want to skip ahead, click here. Check out some other Python tutorials on datagy, including our complete guide to styling Pandas and our comprehensive overview of Pivot Tables in Pandas! Python Square Root Using ExponentiationIn an earlier section of this tutorial, you learned that the square
root is the base of a square number. Similarly, we can write the square root of a number In Python, we can raise any number to a particular power using the exponent operator Let’s see how we can get the Python square root without using the # Use exponents to calculate a square root number = 25 square_root = number**(1/2) print(square_root) # Returns: 5.0 You can see here that this returns the same value as if we had used the Similarly, we could also have written Important tip:
If you’re using Python 2, you will need to ensure you’re using float division and write Square Root Limitations – Negative Numbers and ZeroIn the two sections below, you’ll learn about two special cases of square roots. In particular, you’ll learn how to calculate the square root of zero, as well as what happens when you try to calculate the square root of a negative number! Calculate the Square Root of Zero Using PythonNow, let’s see how we can use Python to calculate the value of the square root of zero. We can do this, again, using the # Calculating the square root of zero from math import sqrt number = 0 square_root = sqrt(number) print(square_root) # Returns: 0.0 We can see from the example above that calculating the square root of zero does not cause an error. Calculating the Square Root of Negative Numbers with PythonFinally, let’s take a look at what happens when we try to calculate the square root of negative numbers with Python. You’ll remember, from the earlier section, that squares are always positive numbers. So, what happens when we try to take the square root of a negative number? Let’ give this a shot! # Calculating the square root of a negative number from math import sqrt number = -25 square_root = sqrt(number) print(square_root) # Returns: ValueError: math domain error We can see that trying to calculate the square root of a negative number returns a In the next section, you’ll learn how to calculate the integer square root. Python Integer Square RootThere may be times when you want to return an integer value when you are calculating the square root of a value. Keep in mind, that unless you’re working with specific numbers (“perfect squares”), this won’t be the true square root of that number. Python’s Let’s see how this is done: # Calculating the integer square root with Python from math import isqrt number = 27 square_root = isqrt(number) print(square_root) # Returns: 5 Similar to our example of using the The reason for this is that the ConclusionIn this post, you learned how to use Python to calculate a square root, both with and without the To learn more about the |