What is sqrt in Python?

Key takeaways:

  • Python provides multiple methods to calculate square roots, including libraries like math and cmath.

  • The math.sqrt() function handles positive numbers but raises an error for negatives.

  • The exponentiation operator ** calculates square roots without requiring imports.

  • The cmath.sqrt() function supports complex numbers, returning results with imaginary components.

  • Each method has specific use cases, from basic calculations to handling complex numbers.

Python is a versatile programming language that offers several ways to compute square roots. In this Answer, we’ll explore these methods and discuss when to use each.

svg viewer

Square roots are a fundamental mathematical operation frequently used in programming tasks such as graphics, scientific computation, and engineering problems. Let’s get right into the details.

What is a square root?

The square root of a number is a value that, when multiplied by itself, gives the original number. For instance, the square root of 1616 is 44 because 4×4=164×4=16. Calculating square roots manually can be tedious, but Python makes it simple. Whether you're working with basic numbers or complex ones, Python has tools to make your work easier.

Square root in Python

Python’s math module provides a built-in sqrt() function specifically for calculating square roots. This method is straightforward and reliable for positive numbers. However, it raises a ValueError if you attempt to compute the square root of a negative number, as it does not handle complex numbers.

The math.sqrt()function

math.sqrt(x)
Syntax of math.sqrt()

Parameters

Parameter

Type

Description

x

float, int

The number for which the square root is to be computed. Must be non-negative.

Return value

  • Returns the square root of x as a floating-point number.

  • If x = 0, it returns 0.0.

  • If x is negative, it raises a ValueError because square roots of negative numbers are not defined in real numbers.

Square root of an integer

Here’s the code to compute the square root of a number:

import math
number = 16
sqrt = math.sqrt(number)
print(f"The square root of {number} is {sqrt}")

Square root using exponentiation

We can also compute the square root using the exponentiation operator (**). This can be done by raising the number to the power of 0.5. This method is simple and doesn’t require importing any modules. Here’s the code implementation:

number = 16
sqrt = number ** 0.5
print(f"The square root of {number} is {sqrt}")

This approach works well for both integers and floats.

Square root of negative numbers

We can also make use of the cmath module if we want to compute the square root of a negative number. It handles complex numbers seamlessly, providing results in the form of a + bj, where j is the imaginary unit.

import cmath
number = -16
sqrt = cmath.sqrt(number)
print(f"The square root of {number} is {sqrt}")

This method is ideal when working with mathematical problems involving complex numbers.

Square root using numpy.sqrt

The numpy.sqrt() function computes the square root of each element in an array or a single number. It is optimized for performance and works efficiently with NumPy arrays.

import numpy as np
# Single number
num = 16
sqrt_num = np.sqrt(num)
print(f"Square root of {num} is {sqrt_num}")
# Array of numbers
arr = np.array([4, 9, 16, 25, 36])
sqrt_arr = np.sqrt(arr)
print(f"Square roots of {arr} are {sqrt_arr}")
  1. Single number:

    1. np.sqrt(16) returns 4.0, as √16 = 4.

  2. Array of numbers:

    1. np.sqrt() applies the square root function element-wise to each value in the array.

This function handles floating-point and integer inputs and returns results in floating-point format. If a negative number is passed, it returns nan (Not a Number) as square roots of negative numbers are not defined in the real number system.

Learn the basics with our engaging “Learn Python” course!

Start your coding journey with Learn Python, the perfect course for beginners! Whether exploring coding as a hobby or building a foundation for a tech career, this course is your gateway to mastering Python—the most beginner-friendly and in-demand programming language. With simple explanations, interactive exercises, and real-world examples, you’ll confidently write your first programs and understand Python essentials. Our step-by-step approach ensures you grasp core concepts while having fun along the way. Join now and start your Python journey today—no prior experience is required!

Conclusion

Python offers multiple ways to compute square roots, each with its strengths and limitations. Whether you’re working on simple calculations, handling complex numbers, or learning numerical methods, you have the tools you need. Try these approaches in your projects and deepen your understanding of Python’s capabilities.

Frequently asked questions

Haven’t found what you were looking for? Contact Us


What is the data type of sqrt in Python?

The data type of the result from sqrt() in Python is typically float for math.sqrt() and complex for cmath.sqrt() when handling negative numbers.


Is sqrt() a library function?

Yes, sqrt() is a library function provided by Python’s math and cmath modules.


What is the sqrt() function in Python?

The sqrt() function in Python computes the square root of a number.

Example:

import math
print(math.sqrt(16))  # Output: 4.0

Can a square root be negative?

A square root itself is non-negative, but negative roots can exist when considering both solutions to x2=nx^2 = n. The square root function always gives the non-negative root. However, the equation x2=nx^2 = n has two solutions, both positive and negative roots.


Free Resources

Copyright ©2025 Educative, Inc. All rights reserved