Skip to content

Instantly share code, notes, and snippets.

@fabsta
Created August 28, 2016 20:28
Show Gist options
  • Save fabsta/f5136dd32b795c9576c0d74117f147db to your computer and use it in GitHub Desktop.
Save fabsta/f5136dd32b795c9576c0d74117f147db to your computer and use it in GitHub Desktop.

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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment