DevNet Associate DEVASC 200-901 Official Certification Guide by Adrian Iliesiu (z-lib.org)

(andrew) #1
% Performs modulus division (printing the
remainder only)

30
% 7

2

*
*

Indicates an exponent 2 **
8

2
5
6

When working with numbers in Python, a defined order
of precedence must be observed in calculations. Python
uses the following order (also known as PEMDAS):


1. Parentheses: Parentheses are always evaluated first.
2. Power: The exponent is evaluated.
3. Multiplication: Any multiplication is performed.
4. Division: Division is evaluated.
5. Addition: Addition is performed.
6. Subtraction: Subtraction is performed.
7. Left to right: After PEMDAS, anything else (such as sqrt() or
other math functions) is evaluated from left to right.

In most languages, the parentheses are preferred over
anything else. Most Python programmers use them
liberally in their math formulas to make them simpler to
construct without being so strict with the rules. Take the
following example:


>>> 5 * 6 - 1
29

Python evaluates the multiplication first and then
subtracts 1 from the result. If you wanted the subtraction
to happen first, you could simply add parentheses
around the parts you want evaluated:


>>> 5 * (6 - 1)
25
Free download pdf