fabsta
8/28/2016 - 8:28 PM

15. math (python).md

MATH

basic operations

10 + 4          # add (returns 14)
10 - 4          # subtract (returns 6)
10 * 4          # multiply (returns 40)
10 ** 4         # exponent (returns 10000)
10 / 4          # divide (returns 2 because both types are 'int')
10 / float(4)   # divide (returns 2.5)
5 % 4           # modulo (returns 1) - also known as the remainder

force '/' in Python 2.x to perform 'true division' (unnecessary in Python 3.x)

from __future__ import division
10 / 4          # true division (returns 2.5)
10 // 4         # floor division (returns 2)