How to write square root in Python?

In Python, you can write the square root of a number using the sqrt() function from the built-in math module. Here’s an example:

import math

x = 16
y = math.sqrt(x)
print(y)

Output:

4.0

In this example, we first import the math module using the import statement. Then we define a variable x and set it equal to the number whose square root we want to find. We use the sqrt() function from the math module to calculate the square root of x, and store the result in the variable y. Finally, we use the print() function to display the value of y.

Using math.pow() function:

Yes, you can also use the math.pow() function to calculate the square root of a number in Python. The square root of a number x can be calculated as math.pow(x, 0.5). Here’s an example:

import math

x = 16
y = math.pow(x, 0.5)
print(y)

Output:

4.0

In this example, we again import the math module using the import statement. Then we define a variable x and set it equal to the number whose square root we want to find. We use the math.pow() function with arguments x and 0.5 to calculate the square root of x, and store the result in the variable y. Finally, we use the print() function to display the value of y.

Using ** Operator:

Yes, you can also use the ** operator to calculate the square root of a number in Python. The square root of a number x can be calculated as x**0.5. Here’s an example:

x = 16
y = x**0.5
print(y)

Output:

4.0

In this example, we define a variable x and set it equal to the number whose square root we want to find. We use the ** operator with arguments x and 0.5 to calculate the square root of x, and store the result in the variable y. Finally, we use the print() function to display the value of y.