2.4. Operators

The standard mathematical operators that you are familiar with work the same way in Python as in most other languages.

+          -          *         /         %         **

Addition, subtraction, multiplication, division, and modulus/remainder are all part of the standard set of operators. In addition, Python provides an exponentiation operator, the double star/asterisk ( ** ). Although we are emphasizing the mathematical nature of these operators, please note that some of these operators are overloaded for use with other data types as well, for example, strings and lists.

>>> print -2 * 4 + 3 ** 2
1

As you can see from above, all operator priorities are what you expect: + and - at the bottom, followed by *, /, and %, then comes the unary + and -, and finally,

Get Core Python Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.