Pour calculer le reste de la division euclidienne (ou modulo) avec python, il existe l'opérateur %, illustration
>>> 10 % 20
car 10 = 2 * 5 + 0
>>> 10 % 31
car 10 = 3 * 3 + 1
Remarque: avec des nombres décimaux il est préférable d'utiliser math.fmod():
>>> import math>>> math.fmod(10.0,2.0)0.0>>> math.fmod(10.0,3.0)1.0>>> math.fmod(11.0,3.2)1.3999999999999995
car 11 = 3 * 3.2 + 1.3999999999999995
Références
| Liens | Site |
|---|---|
| Modulo (opération) | wikipedia |
| math — Mathematical functions | python doc |
| How to calculate a mod b in python? | stackoverflow |
| How does % work in Python? | stackoverflow |
