FactorPad
Build a Better Process

Python Math Operators and PEMDAS Order of Operations

The fastest way to learn rules for math is to punch the examples into your own Python Interpreter.
  1. Basic operations - Review the outline notes.
  2. PEMDAS - Walk through the six basic math operators and introduce the Python Interpreter.
  3. Integers & floats - See the difference between integers and floating point numbers.
  4. Practice - Put it all together with a homework problem.
  5. Next: operators - Introduce relational operators.
face pic by Paul Alan Davis, CFA
Updated: February 21, 2021
Learn by doing. It's time to get out your Python Interpreter and practice.

Outline Back Tip Next

/ factorpad.com / tech / full-stack / python-math-operators.html


An ad-free and cookie-free website.


Learn the rules for Math in Python including PEMDAS

Beginner

Video Tutorial

Videos can also be accessed from our Full Stack Playlist 3 on YouTube.

Python math operators and PEMDAS order of operations | Python for Beginners (4:50)

Code Examples and Video Script

Welcome. Today's question: What are the rules for Math in Python?

I'm Paul, and many of us learn the rules of Math, first on paper, then in a calculator, followed by a spreadsheet, and all of these were much easier than in a programming language. At least, that's what I found.

So here I hope to make your journey less difficult than mine, by highlighting Python's seven, out of the box, basic operations.

At high school in California, we use PEMDAS to memorize the order of operations for math, and we'll see if Python conforms.

We will talk about two numerical data types: integers and floats, and see how we keep them straight with practice in Python.

(Commands in Linux)

(Lessons in Python)

Next we will cover another subject in Math: relational operators.

Step 1 - Basic Math Using Python Operators

In Project 3 (Python for Beginners) so far we installed python3 and now we'll dive into, what to me, is the most exciting part, hands on learning in Python.

paul@fullstack:~$ less notes/video0023.txt

Heading to the Terminal, let's review PEMDAS, which stands for parentheses, exponent, multiplication, division, addition and subtraction.

Rules for math in Python 3 (Video 23) Order of operation - PEMDAS 1. Parentheses ( ) 2. Exponent ** 3. Multiplication * 4. Division / // % 5. Addition + 6. Subtraction - Notes: - after PEMDAS order goes left to right - use parentheses to override order The basic numerical data types - integer - float (floating point, decimal) Notes: - any input of float results in a float - in Python 3 division results in a float - integers only result in integers (except for division) notes/video0023.txt (END)

It details the order of operations, and also note, in Python we have three types of division, regular division, floor division and finding the remainder, using what's called modulo.

Step 2 - Python PEMDAS Order of Operation

Let's head to the the python3 Interpreter and cover the rest of this.

The first way to interact with Python, is like this, one line at a time. Second is a script, or text file, which is the focus of a future Project.

So the three greater-than symbols >>> are a Python signature, like the command line in Linux, meaning it's waiting for us.

paul@fullstack:~$ python3 Python 3.4.2 (default, Oct 8 2014, 10:45:20) [GCC 4.9.1] on linux Type "help", "copyright", "credits" or "license" for more information. >>> #PEMDAS backwards ...

The hash symbol #, or comment, too is similar, meaning the rest of the line is ignored.

Three dots ... is a continuation prompt, meaning Python doesn't have enough to act on.

Python Subtraction

So let's start with the last letter of PEMDAS, subtraction, give it a simple 1 - 2.

... 1 - 2 -1 >>>

And voilà -1.

Python Addition

Next, A for addition. We could enter 1+1 like this, and it works.

>>> 1+1 2

Or 1 +1 like this, and it too works.

>>> 1 +1 2

Or 1 + four spaces 1.

>>> 1 + 1 2

And it also works, but the preferred form is to put a space between each.

>>> 1 + 1 2

Step 3 - Integers & floats

Python division

Next, D, division, and here I should mention the data types: integers and floats. We've been playing with integers so far. Let's try a floating point number like 6.0 divided by the integer 2.

>>> 6.0 / 2 3.0

And we get 3.0, a float.

Python will automatically Interpret the format for the resulting number based on how you input numbers. So input of any float results in a float.

>>> 6 / 2 3.0

Also, with division, in Python 3, at least, something like integer 6 divided by integer 2, equals float 3.0.

Floor division rounds down, so 7 floor, divided by 3 is two and a third (2.3333333333333335).

>>> 7 / 3 2.3333333333333335 >>> 7 // 3 2
Python floor division

And 7, floor divided by 3 is 2, which ignores the remainder.

Python modulo division

To see the remainder only, we use the modulo, or percent sign %.

>>> 7 % 3 1

So 7 % 3 is 1 because 3 goes into 7 two times with one left over.

Modulo can help us determine if a number is positive or negative.

Python multiplication

Next, M, multiplication is pretty straightforward, so two integer 5s equals 25.

>>> 5 * 5 25

And one integer 5 and one float 5.0 equals float 25.0.

>>> 5 * 5.0 25.0
Python exponent or powers

Next, E, for exponent, is entered with two stars **, so 2 to the second power is 4.

>>> 2 ** 2 4

And to the third power is 8.

>>> 2 ** 3 8

What would be 9 to the power of .5, or one half power?

>>> 9 ** .5 3.0

Remember it (a number to the 1/2 power) is the square root.

Python parentheses

Next, P, for parentheses. So after following the rules of operations Python will work from left to right.

So we know 1 + 2 * 4 is going to give us a different answer than (1 + 2) * 4. Right?

>>> 1 + 2 * 4 9 >>> (1 + 2) * 4 12

Step 4 - Practice with a Python Homework Problem

I'll ask you to do the last one for homework.

>>> 5 + (4 - 2) * 2 + 4 % 2 - 4 // 3 - (5 - 3) / 1

Do it by hand on paper before using Python, and there are a couple little tricks in there that will reinforce your understanding. Feel free to pause now, or rewind it.

And to leave the Python Interpreter type exit() followed by Enter.

>>> exit() paul@fullstack:~$

Step 5 - Next: Python Relational Operators

You are welcome to join our journey to Data Science as we take one step at a time and build out our Full Stack.

And our next step is to talk about Relational Operators.

Have a nice day.


What's Next?

See what's going on at our YouTube Channel. For updates follow @factorpad on Twitter. Our no-spam mailing list is for periodic high level directions.

Outline Back Tip Next

/ factorpad.com / tech / full-stack / python-math-operators.html


python math
python math operators
python math symbols
math order of operations
python subtraction
python addition
python division
python floor division
python modulo
python mutiplication
python exponent
pemdas in python
python parentheses
python integer
python float
python 3 math
python tutorial

A newly-updated free resource. Connect and refer a friend today.