Code
result = (4 + 3) * 2
print(result) # Output: 14 (since 4 + 3 = 7, and 7 * 2 = 14)14
The majority of the text and code has been generated by AI.
Editor: Witek ten Hove
See book section
Mathematical Order of Operations
Mathematical order of operations refers to the rules that dictate the sequence in which mathematical operations should be performed in an expression. These rules ensure that every mathematical expression is evaluated in a consistent and unambiguous manner. The order of operations is also known as “PEMDAS” which is an acronym representing the sequence of operations: Parentheses, Exponents, Multiplication and Division (from left to right), and Addition and Subtraction (from left to right).
Python, along with many other programming languages, follows the standard order of operations used in mathematics. The order of operations is essential to obtain accurate and meaningful results. Let’s break down each component of the order of operations and illustrate them with Python examples, including real-world scenarios.
1. Parentheses
Parentheses are used to indicate which parts of an expression should be evaluated first. Expressions within parentheses are calculated before anything else.
Example 1: Simple Parentheses
2. Exponents
Exponents are mathematical operations that raise a base to a certain power.
Example 2: Exponents
3. Multiplication and Division
These operations are performed from left to right, whichever comes first in the expression.
Example 3: Multiplication and Division
4. Addition and Subtraction
Similarly, addition and subtraction are performed from left to right, whichever comes first in the expression.
Example 4: Addition and Subtraction
Real-World Examples
Let’s consider some real-world examples where understanding the order of operations is crucial:
Example 5: Shopping Calculations
16.2
Example 6: Engineering Calculation
10.463746938836012
![]()
In the second example, we use the Pythagorean theorem to find the length of the hypotenuse in a right-angled triangle. The order of operations ensures that the squares and addition are performed before the square root operation.
Basic: Make a short video explaining how to calculate distances between two coordinates. Do proper research and use code demos.
Stretch: Expand your video with an exploration of how to calculate the distance between geographical locations using their coordinates taking into account the curvature of the earth. Discuss and illustrate with code examples and maps. Provide a list of interesting resources on the matter.
Challenge: Share your content online (e.g. Linkedin or Medium), gather feedback and write a reflection on it.