Arithmetic Operators
Python does all its maths with seven operators. You already know four of them from your maths class. The other three — **, % and // — are new. Two of them give answers that surprise most students at first, so read those parts carefully.
1The seven operators
7 + 2 → 97 - 2 → 57 * 2 → 147 ** 2 → 4910 / 2 → 5.010 // 4 → 210 % 4 → 2The three blue operators work just as they do in maths. The purple one means "to the power of". The three amber ones are the ones you must study carefully, so most of this lesson is about them.
2Division always gives a float
In Python, the / operator always gives a float — a number with a decimal point. It does this even when the division is exact and the answer looks like a whole number.
10 / 2 is 5.0, and not 5. If a question asks for the output of print(20 / 5), the correct answer is 4.0. Students forget the .0 very often. Do not be one of them.3One float makes the whole answer a float
This is an important rule for all arithmetic in Python. If any operand in the expression is a float, then the final answer is also a float. Just one operand with a decimal point is enough to make the whole answer a float.
/ always gives a float, even when both operands are int.4** — to the power of
You cannot type a small raised number on a keyboard, so Python uses two asterisks instead. 2 ** 3 means 23, which is 2 × 2 × 2.
5% — the remainder
When you divide 10 by 4, the quotient is 2 and the remainder is 2. The % operator gives you that remainder. It does not give the quotient. This operator is also called the modulus operator.
n % 2 == 0 means "n is an even number". You will use this again and again when you learn if statements.6// — the quotient (floor division)
The % operator gives the remainder. The // operator gives the quotient — that is, how many complete times the divisor divides the dividend. With positive numbers, it looks like Python is simply removing the digits after the decimal point:
But "remove the decimal part" is not the correct rule. It gives the wrong answer as soon as a negative number is used. The correct rule is given by the name itself. Floor means Python moves down the number line, to the whole number just below the exact answer.
Move the sliders below and see this for yourself. The purple mark is the exact answer. The green arrow shows // moving down to the whole number below it. Make the dividend negative, and you will see the answer move away from zero, and not towards it.
25 / 4 lies between two whole numbers. // moves down to the whole number below it, which is 6. Now make the dividend negative. That is where most students make a mistake.-16 // 3 is -6, and not -5. Floor division always moves down the number line. For positive numbers, moving down gives the same answer as removing the decimal part. For negative numbers, it does not.7Try them all
Type your own expressions. Try the three that students get wrong most often: 10 / 2, -16 // 3 and 7 % 25.
8Recap
+ - * work as they do in maths. ** means "to the power of". / always gives a float. % gives the remainder. // gives the quotient, by moving down the number line. And if any one operand is a float, the whole answer is a float.What does print(10 / 2) show?
What does print(-16 // 3) show?
What does print(7 % 25) show?
What does print(2 + 3.0) show?