01 / 06
Dividing is / (a slash)
The basics of syntax
INPUT · Slides
01 / 06
The symbol for dividing is not ÷, it is / (a slash). Same reason times was *.
print(10 / 4)Result
2.5
02 / 06
Here is the surprise. Even when it divides exactly, / gives you a .0 on the end.
10 / 2 shows up as 5.0, not 5. Python has decided that the moment you use / you are in the world of decimals.
print(10 / 2)Result
5.0
03 / 06
Try something like 10 / 3 and the decimals run on and on.
A computer can only hold so many digits of a decimal, so the tail end sometimes stops on an odd-looking digit instead of a neat 3. That is not a bug, that is just how it is.
print(10 / 3)Result
3.3333333333333335
04 / 06
Two slashes together, //, throw away everything after the decimal point.
Use it when what you want is a count — "10 sweets shared between 3 people, how many each?"
print(10 // 3)Result
3
05 / 06
% gives you the remainder. In that same example, it is how many sweets are still in the bag once you have handed them out.
% is the per cent sign, but in Python it means remainder. Completely different job, so watch out for that.
print(10 % 3)Result
1
06 / 06
Same "10 divided by 3", but the symbol changes with what you want to know.
10 / 3 … the exact answer → 3.333333333333333510 // 3 … how many fit → 310 % 3 … what is left → 1% turns up a lot from here on. It is how you check whether a number is even, or a multiple of three.
print(7 // 2)print(7 % 2)Result
3 1