Skip to main content

How to Find Square Root in Python

Hello everyone, in this tutorial, I’ll show 3 ways to find square root in Python.

Let’s see how to do it.

1. Using Exponent

number = int(input("enter a number: "))
sqrt = number ** 0.5
print("square root:", sqrt)

Output:

enter a number: 64
square root: 8.0

In above program, first we’re taking a number from user as input and the entered value will be converted to int from string and will store into variable called number. Then we are using  exponent (**) sign, which is used to find out the power of a number. But we know that if a number have power ½ or 0.5 then it will represent to the square root of that number. That’s why we are using 0.5 as power here. So the number**0.5 will give us square root of that number and will be stored in variable named as sqrt. In last line we’re just printing the square root of the number.

2. Using math.sqrt() Method

import math
number = int(input("enter a number:"))
sqrt = math.sqrt(number)
print("square root:" , sqrt)

sqrt() is the predefined method used to find square root in python. But we have to import math module to use the sqrt() method. In first line, we’re importing math module, then in next line, taking input from user. After that we’re finding the square root of the number using sqrt() method and result will be stored into sqrt variable. In last line, just printing the square root as in first program.

3. Using math.pow() Method

import math
number = int(input("enter a number:"))
sqrt = math.pow(number, 0.5)
print("square root: ", sqrt)

pow() is also a predefined method to find out power of a number, it takes two arguments as input, first is the number itself and second one is power of that number. The program is same as first program where we’re using (**) sign to find out the square root but only different is this that here we’re using a predefined method pow() instead of  (**) sign to get the power of that number.

If you’ve any problem related with article or have any other possible way to find square root in python then please let us know in comments.

The post How to Find Square Root in Python appeared first on The Crazy Programmer.



from The Crazy Programmer https://www.thecrazyprogrammer.com/2018/05/how-to-find-square-root-in-python.html

Comments

Popular posts from this blog

dotnet sdk list and dotnet sdk latest

Can someone make .NET Core better with a simple global command? Fanie Reynders did and he did it in a simple and elegant way. I'm envious, in fact, because I spec'ed this exact thing out in a meeting a few months ago but I could have just done it like he did and I would have used fewer keystrokes! Last year when .NET Core was just getting started, there was a "DNVM" helper command that you could use to simplify dealing with multiple versions of the .NET SDK on one machine. Later, rather than 'switching global SDK versions,' switching was simplified to be handled on a folder by folder basis. That meant that if you had a project in a folder with no global.json that pinned the SDK version, your project would use the latest installed version. If you liked, you could create a global.json file and pin your project's folder to a specific version. Great, but I would constantly have to google to remember the format for the global.json file, and I'd constan

R vs Python for Machine Learning

There are so many things to learn before to choose which language is good for Machine Learning. We will discuss each and everything about R as well as Python and the situation or problem in which situation we have to use which language. Let’s start Python and R are the two most Commonly used Programming Languages for Machine Learning and because of the popularity of both the languages Novice or you can say fresher are getting confused, whether they should choose R or Python language to commence their career in the Machine learning domain. Don’t worry guys through this article we will discuss R vs Python for Machine Learning. So, without exaggerating this article let’s get started. We will start it from the very Basics things or definitions. R vs Python for Machine Learning Introduction R is a programming language made by statisticians and data miners for statistical analysis and graphics supported by R foundation for statistical computing. R also provides high-quality graphics and

Top Tips For PCB Design Layout

Are you thinking about designing a printed circuit board? PCBs are quite complicated, and you need to make sure that the layout that you choose is going to operate as well as you want it to. For this reason, we have put together some top tips for PCB design layout. Keep reading if you would like to find out more about this. Leave Enough Space One of the most important design tips for PCB layout is that you need to make sure that you are leaving enough space between the components. While many people might think that packing components closely is the best route to take, this can cause problems further down the line. This is why we suggest leaving extra space for the wires that will spread. This way, you’ll have the perfect PCB design layout. Print Out Your Layout Struggling to find out if your components sizes match? Our next tip is to print out your layout and compare the printed version to your actual components. Datasheets can sometimes come with errors, so it doesn’t hurt to do