The basics of syntax

INPUT · Slides

Dividing, and what is left over

01 / 06

Dividing is / (a slash)

The symbol for dividing is not ÷, it is / (a slash). Same reason times was *.

print(10 / 4)

Result

2.5

02 / 06

The answer to / is always a decimal

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

When it does not divide neatly, it gets long

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

// tells you how many fit

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

% tells you what is left over

% 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

Picking between the three

Same "10 divided by 3", but the symbol changes with what you want to know.

  • 10 / 3 … the exact answer → 3.3333333333333335
  • 10 // 3 … how many fit → 3
  • 10 % 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