Python math operators and PEMDAS order of operations | Python for Beginners (4:50)
Videos can also be accessed from our Full Stack Playlist 3 on YouTube.
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.
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.
Heading to the Terminal, let's review PEMDAS, which stands for parentheses, exponent, multiplication, division, addition and subtraction.
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.
Let's head to the the
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.
The hash symbol
#, or comment, too
is similar, meaning the rest of the line is ignored.
... is a continuation
prompt, meaning Python doesn't have enough to act on.
So let's start with the last letter of PEMDAS, subtraction, give it
1 - 2.
Next, A for addition. We could enter
1+1 like this, and it works.
1 +1 like this, and it too works.
1 + four spaces
And it also works, but the preferred form is to put a space between each.
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
divided by the integer
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.
Also, with division, in Python 3, at least, something like integer
6 divided by integer
2, equals float
Floor division rounds down, so
7 floor, divided by
3 is two and a third
7, floor divided by
2, which ignores the remainder.
To see the remainder only, we use the modulo, or percent sign
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.
Next, M, multiplication is pretty straightforward, so two integer
And one integer
5 and one float
5.0 equals float
Next, E, for exponent, is entered with two stars
2 to the second power is
And to the third power is
What would be
9 to the power of
.5, or one half power?
Remember it (a number to the 1/2 power) is the square root.
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?
I'll ask you to do the last one for homework.
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
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.
See what's going on at our YouTube Channel. Subscribe here.