深度阅读

How to Calculate square Root in Python

作者
作者
2023年08月22日
更新时间
8.09 分钟
阅读时间
0
阅读量

There are multiple ways to calculate the square root of a number in Python. Here are a few examples:

  1. Using the sqrt() function from the math module:
import math

num = 25
sqrt = math.sqrt(num)

print("The square root of", num, "is", sqrt)
  1. Using the exponential operator **:
num = 25
sqrt = num ** 0.5

print("The square root of", num, "is", sqrt)
  1. Using the sqrt() function from the NumPy module:
import numpy as np

num = 25
sqrt = np.sqrt(num)

print("The square root of", num, "is", sqrt)

All three methods will produce the same output, which is the square root of the input number:

The square root of 25 is 5.0

Note that the math and numpy modules support complex numbers as well as positive real numbers for the sqrt() function.

相关标签

博客作者

热爱技术,乐于分享,持续学习。专注于Web开发、系统架构设计和人工智能领域。