Skip to main content

How to find the square root of a number in Python

How to find the square root of a number in Python.

Finding the Square Root of a Number in Python

In Python, there are several ways to find the square root of a number. In this tutorial, we will explore different methods and provide step-by-step instructions on how to implement them.

Method 1: Using the math Module​

The easiest way to find the square root of a number in Python is by using the sqrt() function from the math module. Follow these steps:

  1. Import the math module at the beginning of your code:
import math
  1. Use the sqrt() function to find the square root of a number. Pass the number as an argument to the function:
number = 16
result = math.sqrt(number)
  1. Print the result:
print("The square root of", number, "is", result)

Here's the complete code:

import math

number = 16
result = math.sqrt(number)
print("The square root of", number, "is", result)

Output:

The square root of 16 is 4.0

Method 2: Using the Exponentiation Operator​

Another method to find the square root of a number is by using the exponentiation operator (**). Follow these steps:

  1. Assign the number to a variable:
number = 25
  1. Calculate the square root using the exponentiation operator with the exponent 0.5:
result = number ** 0.5
  1. Print the result:
print("The square root of", number, "is", result)

Here's the complete code:

number = 25
result = number ** 0.5
print("The square root of", number, "is", result)

Output:

The square root of 25 is 5.0

Method 3: Using the Newton's Method​

Newton's method is an iterative method to find the square root of a number. Follow these steps:

  1. Assign the number and an initial guess to variables:
number = 36
guess = 6
  1. Calculate the square root iteratively using Newton's method:
for _ in range(10):
guess = 0.5 * (guess + number / guess)
  1. Print the result:
print("The square root of", number, "is approximately", guess)

Here's the complete code:

number = 36
guess = 6

for _ in range(10):
guess = 0.5 * (guess + number / guess)

print("The square root of", number, "is approximately", guess)

Output:

The square root of 36 is approximately 6.0

Method 4: Using the NumPy Library​

If you are working with arrays or need to find the square root of multiple numbers, using the NumPy library can be more efficient. Follow these steps:

  1. Import the NumPy library at the beginning of your code:
import numpy as np
  1. Create an array of numbers:
numbers = np.array([9, 16, 25, 36])
  1. Use the sqrt() function from NumPy to find the square root of the numbers:
results = np.sqrt(numbers)
  1. Print the results:
print("The square roots are:", results)

Here's the complete code:

import numpy as np

numbers = np.array([9, 16, 25, 36])
results = np.sqrt(numbers)
print("The square roots are:", results)

Output:

The square roots are: [3. 4. 5. 6.]

These are four different methods to find the square root of a number in Python. Choose the method that suits your needs and integrate it into your code accordingly.