The basic explanation is that in almost all programming languages, dividing 2 variables of numeric type T returns a value of that type T.
Integers division is performed by the processor as an euclidian division, returning the quotient (as an integer).
The print format %f
will not perform the variable type conversion for you.
I strongly suggest you read the proposed duplicate question for further understanding of python behaviour.
Example:
12 = (2 * 5) + 2 => 12 / 5 = 2 12 % 5 = 2
12 = (1 * 7) + 5 => 12 / 7 = 1 12 % 7 = 5
In python :
Python 2.7.15 (v2.7.15:ca079a3ea3, Apr 30 2018, 16:30:26) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> 12/5
2
>>> 12%5
2
>>> 12/7
1
>>> 12%7
5
if you want to obtain a float, do as https://stackoverflow.com/users/8569905/banghua-zhao proposed.
cast in float, and then perform a division. The processor will then using floating point division and return a float.
As pointed out in a comment below, if 2 operands have a different type, the operator computation is performed with the most restrictive type : float will take precedence over integer. In the following examples, one cast to float would be sufficient.
>>> float(12)/float(5)
2.4
Note that the % operator still performs an euclidian divison and gives you a the result as a float
>>> float(12)%float(5)
2.0
>>> float(12)%float(7)
5.0