Menu

Arithmetic Operators

Integer and real results

When you do arithmetic in Python, the answer is not always a whole number. Every result has a type: either an integer (a whole number) or a real (a number with a decimal point, called a float in Python). Which one you get depends on the operator you use and on whether the values you start with are integers or reals.

Division always gives a real

The / operator always returns a real, even when the division works out exactly. 45 / 9 gives 5.0, not 5. Python writes the .0 to show the answer is a real, because a real is allowed to hold a fractional part even when this particular answer has not got one.

Integers in, integers out

If both values are positive integers, then +, -, *, //, % and ** all give an integer result. The table shows some expressions and the type of each result, so you can see how the type changes.

ExpressionResultType of resultWhy
25 + 429integerboth values are integers
25 // 46integerboth values are integers
4 ** 364integerboth values are integers
25 / 46.25real/ always gives a real
45 / 95.0real/ always gives a real, even when it divides exactly
25 + 1.526.5realone of the values is a real
16 * 0.58.0realone of the values is a real

One real is enough

If either value in an arithmetic operation is a real, the result is a real too, whichever operator you use. So 25 + 1.5 gives 26.5, and 16 * 0.5 gives 8.0 โ€” a whole-number value, but still a real because a real value was involved. When both values are positive integers, only / gives a real by itself; with the other operators you need at least one real value in the calculation before the answer becomes a real.

Practise this