Why does this simple calculation return 0
>>> 25/100*50
0
while this actually calculates correctly?
>>> .25*50
12.5
>>> 10/2*2
10
What is wrong with the first example?
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,
25/100is zero when performing an integer divison. since the result is less than1.You can “fix” this by adding
from __future__ import divisionto your script. This will always perform a float division when using the/operator and use//for integer division.Another option would be making at least one of the operands a float, e.g.
25.0/100.In Python 3,
25/100is always0.25.