Pocket calculator

Python understands the standard arithmetic operators, including +, -, / (division), and * (multiplication). The Python interpreter makes a handy calculator:

Note that the second example evaluates 29 (and not 54); the interpreter multiplies 4 by 6 before adding 5. Python uses operator precedence rules to decide what to do first. You can control order explicitly by using parentheses:

In practice, it's often easiest to use parentheses (even when they aren't required) to make code more readable.

Was this article helpful?

0 0

Post a comment