Watch: Building a Reverse Polish Notation (RPN) Evaluator in Python

Image credit: Author

Contents

Reverse Polish Notation (RPN)

# Ambiguity in order of operations for infix notation
1 + 2 + 3
(1 + 2) + 3
1 + (2 + 3)
# No ambiguity in prefix and postfix (using postfix as an example)
# The structure tells us that 1 and 2 are added first, then 3
1 2 + 3 +

Example RPN expressions

# Example 11 10 30 + *
1 40 *
40
# Example 21 2 + 3 +
3 3 +
6
# Example 31 2 - 2 +
-1 2 +
1
# Example 41 2 /
0.5

Source code

Python script for evaluating expressions in Reverse Polish Notation

Code walk-through

Videos you should watch next

More Computing resources

Software Engineer — Data & Machine Learning