2.2 Variables, Objects and Expressions
45
2.2.7 Division—Quotient and Remainder
It is sometimes desirable to pick out the quotient and remainder of a division
a
b ,
for integer or floating point numbers a and b. In Python, we can do this with the
operators // (integer division) 6 and % (modulo), respectively. They have the same
precedence as *, and their use may be exemplified by
In [1]: 11//2
Out[1]: 5
In [2]: 11%2
Out[2]: 1
Note that the sign of the remainder follows the sign of the denominator, i.e.,
In [1]: -11%2
Out[1]: 1
In [2]: 11%-2
Out[2]: -1
2.2.8 Using Parentheses
Note that parentheses are often very important to group parts of expressions together
in the intended way. Let us consider some variable x with the value 4 and assume
that you want to divide 1.0 by x + 1. We know the answer is 0.2, but the way we
present the task to Python is critical, as shown by the following example.
In [1]: x = 4
In [2]: 1.0/x+1
Out[2]: 1.25
In [3]: 1.0/(x+1)
Out[3]: 0.2
In the first try, we see that 1.0 is divided by x (i.e., 4), giving 0.25, which is then
added to 1. Python did not understand that our complete denominator was x+1. In
our second try, we used parentheses to “group” the denominator, and we got what
we wanted.
2.2.9 Round-Off Errors
Since most numbers can be represented only approximately on the computer, this
gives rise to what is called rounding errors. We may illustrate this if we take a look
6 Note that in Python 2.x, the operator / gives integer division, unless either the numerator and/or
the denominator is a float.
45
2.2.7 Division—Quotient and Remainder
It is sometimes desirable to pick out the quotient and remainder of a division
a
b ,
for integer or floating point numbers a and b. In Python, we can do this with the
operators // (integer division) 6 and % (modulo), respectively. They have the same
precedence as *, and their use may be exemplified by
In [1]: 11//2
Out[1]: 5
In [2]: 11%2
Out[2]: 1
Note that the sign of the remainder follows the sign of the denominator, i.e.,
In [1]: -11%2
Out[1]: 1
In [2]: 11%-2
Out[2]: -1
2.2.8 Using Parentheses
Note that parentheses are often very important to group parts of expressions together
in the intended way. Let us consider some variable x with the value 4 and assume
that you want to divide 1.0 by x + 1. We know the answer is 0.2, but the way we
present the task to Python is critical, as shown by the following example.
In [1]: x = 4
In [2]: 1.0/x+1
Out[2]: 1.25
In [3]: 1.0/(x+1)
Out[3]: 0.2
In the first try, we see that 1.0 is divided by x (i.e., 4), giving 0.25, which is then
added to 1. Python did not understand that our complete denominator was x+1. In
our second try, we used parentheses to “group” the denominator, and we got what
we wanted.
2.2.9 Round-Off Errors
Since most numbers can be represented only approximately on the computer, this
gives rise to what is called rounding errors. We may illustrate this if we take a look
6 Note that in Python 2.x, the operator / gives integer division, unless either the numerator and/or
the denominator is a float.
