PROGRAMMING

 
REMEMBERS




Positive

p103 ! In Python integer division always return floor division.
 
# Modulus operation: 
# 
# dividend % divisor = reminder
#
# Python always does floor division

assert 0 % 7 == 0
assert 1 % 7 == 1
assert 2 % 7 == 2
assert 3 % 7 == 3  # 3 // 7 = 0 reminder 3
assert 7 % 7 == 0

assert 8 % 7 == 1
assert 9 % 7 == 2

assert 25 % 7 == 4
assert 7 % 25 == 7
... 9 lines
 

Negative

! Python modulus always return a number with the same sign as the denominator.
 
"""Modulus with negative numbers:

Python modulus (%) always return a number with ...
the same sign as the denominator. 

Python applies the distribute law of Modulo operator:

  (a + b) mod n = ((a mod n) + (b mod n)) mod n

Python always does floor division."""


# 23 % 5 = ?
assert 23 // 5  ==  4
assert 23 == 4*5 + 3  # reminder 3
assert 23 % 5 ==  3

# 23 % -5 = ?
assert 23 // -5 == -5
assert 23 == -5 * -5 - 2  # reminder -2
assert 23 % -5 == -2 

# -3 % 7 = ?
assert -3 // 7 == -1
assert -3 == -1*7 + 4  # remainder 4
assert -3 % 7 == 4

# Distributive law
assert (2 + 2) % 3 == 1
assert (2 + 2) % 3 == (2 % 3 + 2 % 3) % 3
assert (2 + 2) % 3 != 2 % 3 + 2 % 3
... 16 lines
 

Questions     github Functions
Reassignment

        A B C D E F
🔔
1/3