How to write square root in Python?

How to write square root in Python?

Python has a predefined sqrt() function that returns the square root of a number. It defines the square root of a value that multiplies itself to give a number. The sqrt() function is not used directly to find the square root of a given number, so we need to use a math module to call the sqrt() function in Python.

For example, the square root of 144 is 12.

How to write square root in Python

Using math.sqrt() method

The sqrt() function is an inbuilt function that returns the square root of any number. Following are the steps to find the square root of a number.

  1. Start the program
  2. Define any number whose square root to be found.
  3. Invoke the sqrt() function and pass the value that you have defined in step 2 and store the result in a variable.
  4. Print the square root.
  5. Terminate the program.

Let’s create a Python program to find the square root of a number.

SqrRoot.py

Output:

How to write square root in Python

Let’s create a python program that finds the square root of a decimal numbers.

SqrRoot.py

Output:

How to write square root in Python

In the following program, we have read a number form the user and find the square root.

SqRoot_Usr.py

Output:

How to write square root in Python

Using math.pow() function

The pow() is an inbuilt function that is used in Python to return the power of a number. It has two parameters. The first parameter defines the number and second parameter defines the power raise to that number.

Pow_Sqrt.py

Output:

How to write square root in Python

Using ** Operator

We can also use the exponent operator to find the square root of the number. The operator can be applied between two operands. For example, x**y. It means that left operand raised to the power of right.

Following are the steps to find the square root of a number.

Step 1. Define a function and pass the value as an argument.

Step 2. If the defined number is less than 0 or negative, it returns nothing.

Step 3. Use the exponential ** sign to find the power of a number.

Step 4. Take the numeric value from the user.

Step 5. Call the function and store its output to a variable.

Step 6. Display the Square Root of a number in Python.

Step 7. Exit from the program.

Let’s implement the above steps in a Python program and calculate the square root of a number.

SqrtFun.py

Output:

How to write square root in Python

As we can see in the above example, first we take an input (number) from the user and then use the exponent ** operator to find out the power of a number. Where 0.5 is equal to √ (root symbol) to raise the power of a given number.

Let’s create a Python program that finds the square root of between the specified range. In the following program, we have found the square root of all the number between 0 to 50.

Sqrloop.py

Output:

How to write square root in Python


原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/263578.html

(0)
上一篇 2022年5月30日
下一篇 2022年5月30日

相关推荐

发表回复

登录后才能评论