Calculations
doing math with Python
Arithmetical operations
We can do arithmetic with Python:
Python (version 2.7+) converts all integers to floats!
Also, division by zero can result in a ZeroDivisionError!
Calculations with variables
Predictably, we can also do mathematics with variables in Python:
Other operators
Exponents
To denote "to the power of", we use the notation **
Modulo
To denote "the remainder of", we use the notation %
(as seen as in JavaScript)
Updates (plus-equals)
To write x = x + 1
to mean "add one to the variable x
", we can use the shorthand +=
:
Predictably, we can combine -
, *
, /
, and %
with =
as well:
Last updated