x = 16
sqrt = x**(.5) #returns 4
sqrt = x**(1/2) #returns 1
I know I can import math and use sqrt, but I’m looking for an answer to the above. What is integer division in Python 2? This behavior is fixed in Python 3.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
In Python 2,
sqrt=x**(1/2)does integer division.1/2 == 0.So x(1/2) equals x(0), which is 1.
It’s not wrong, it’s the right answer to a different question.
If you want to calculate the square root without an import of the math module, you’ll need to use
x**(1.0/2)orx**(1/2.). One of the integers needs to be a floating number.Note: this is not the case in Python 3, where
1/2would be0.5and1//2would instead be integer division.