~/ home / tech / full stack / python math operators
Beginner
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)
python3
less
(Lessons in Python)
#
( )
**
*
/
//
+

exit()
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 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 greaterthan 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.
Three dots ...
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
a simple 1  2
.
And voilà 1
.
Next, A for addition. We could enter
1+1
like this, and it works.
Or 1 +1
like this, and it too works.
Or 1 +
four spaces
1
.
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 6.0
divided by the integer 2
.
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
3.0
.
Floor division rounds down, so
7
floor, divided by
3
is two and a third
(2.3333333333333335
).
And 7
, floor divided by
3
is
2
, which ignores the remainder.
To see the remainder only, we use the modulo, or percent sign
%
.
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.
Next, M, multiplication is pretty straightforward, so two integer
5
s equals
25
.
And one integer 5
and one float
5.0
equals float
25.0
.
Next, E, for exponent, is entered with two stars
**
, so
2
to the second power is
4
.
And to the third power is 8
.
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
Enter
.
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.
~/ home / tech / full stack / python math operators