Loading [MathJax]/jax/output/HTML-CSS/fonts/TeX/fontdata.js

Arithmetic operators: integer division

A // (double slash) sign is an integer divisional operator. It differs from the standard / operator in two details:

  • its result lacks the fractional part- it's absent (for integers), or is always equal to zero (for floats); this means the results are always rounded. 
  • It conforms to the integer vs. float rule.

Run the example below and see the results:

 

print(6 // 3)

print(6 // 3.)

print(6. // 3)

print(6. // 3.)

As you can see, integer by integer division gives an integer result. All other cases produce floats.

Let's do some more advanced tests. 

Looks at the following snippet:

print(6 // 4)

print(6. // 4)

Imagine that we used / instead of // - could you predict the results? 

Yes, it would be 1.5 in both cases. That's clear.

But what results should we expect with // division? 

Run the code and see for yourself.

1

1.0

What we get is two ones - one integer and one float. 

The result of integer division is always rounded to the nearest integer value that is less than the real (not rounded) result.

This is very important: rounding always goes to the lesser integer. 

Look at the code below and try to predict the results once again: 

print(-6 // 4)

print(6. // -4)

-2

-2.0

Note: some of the values are negative. This will obviously affect the result. But how? 

The Result is two negative twos. The real (not rounded) result is -1.5 in both cases. However, the results are the subjects of rounding. The rounding goes toward the lesser of the integer value,  and the lesser integer value is -2, hence: 

-2 and -2.0

NOTE: 

Integer division can also be called floor division. You will defintely come across this term in the future. 

 

 

Operators: remainder (modulo)

The next operator is quote a peculiar one, because it has no equivalent among traditional arithmetic operators. 

Its graphical representation is Python is the % (percent) sign, which may look a bit confusing. 

Try to think of it as a slash (division operator) accompanied by two funny circles. 

The result of the operator is a remainder left after the integer division. 

In other words, it's the value left over after dividing one value by another to produce an integer quotient. 

Note: the operator is sometimes called modulo in other programming languages. 

Take a look at the snippet - try to predict its result and then run it: 

print(14%4)

As you can see, the result is two.

2

This is why:

  • 14//4 gives 3 -> this is the integer quotient
  • 3*4 gives 12 -> as a result of quotient and divisor multiplication
  • 14-12 gives 2 -> this is the remainder

This example is somewhat more complicated: 

print(12 % 4.5)

What is ther result? 

3.0 - not 3 but 3.0 (the rule still works: 12 // 4.5 gives 2.0; 2.0 * 4.5 gives 9.0; 12 - 9.0 gives 3.0)

Operators: how not to divide

As you probably know, division by zero doesn't work. 

Do not try to; 

  • perform a division by zero
  • perform an integer division by zero
  • find a remainder of a division by zero