askvity

How to do square root in Python without sqrt?

Published in Python Square Root 1 min read

You can calculate the square root of a number in Python without using the sqrt function by employing the exponentiation operator **.

Using the Exponentiation Operator

The exponentiation operator ** raises a number to a specified power. Since the square root of a number x is equivalent to x raised to the power of 0.5, we can use **0.5 to compute the square root.

number = 25
square_root = number ** 0.5
print(square_root)  # Output: 5.0

In this example, number ** 0.5 calculates the square root of 25, which is 5.0. The reference material explicitly states that using the ** operator with 0.5 as the exponent is an easy way to obtain the square root of a number.

Examples

Here are a few more examples demonstrating the use of the exponentiation operator to find square roots:

  • Example 1: Finding the square root of 9

    number = 9
    square_root = number ** 0.5
    print(square_root)  # Output: 3.0
  • Example 2: Finding the square root of 2

    number = 2
    square_root = number ** 0.5
    print(square_root)  # Output: 1.4142135623730951
  • Example 3: Using variables for clarity.

    number = 16
    power = 0.5
    square_root = number ** power
    print(square_root) # Output: 4.0

Related Articles