Python – Taking a look at Python 3 Integer Operators, and PEMDAS refresher for Order of Operation in math equations!

wp-15802614007792621229628062731354.jpg

I had to pause my CBT Nuggets studies as it was 2.7 being taught, and once I hit lists and loops the syntax was just too different to keep going, so I will be kind of restarting with a few different materials including the book shown here – Automating the boring stuff!

I highly encourage newcomers to Python to order this book and give it a read!

This is a really great, really intro level resource, and I will be essentially starting over my own studies for Python 3 specifically using this book, a video course of Python 3 and eventually Kirk Byers 8-week free email Python course.

I will be in Kirks March 2020 class, I’d also recommend signing up for that as well if you are brand new to Python 3 (specifically 3.x version) on his website.

So I may end up reviewing some old material I covered while going through the 2.7 course, but I’ve decided I want to spend my time studying Py 3.x so I am kind of doing a reset on the studying (and I’ve been slacking off for a week and need to get back to it).

Without further ado!

A quick review of mathematical operations, and executing them in Python 3.x

You might not have heard this math acronym before PEMDAS, it saved my butt through college level math classes, it describes the order of operations that a mathematical equation will go through to produce a final result.

P – Parenthesis (Everything inside them)
E – Exponent (To the power of…)
M – Multiplication
D – Division
A – Addition
S – Subtraction

A couple of rules with this order of operations for math, is that PEMDAS also applies to everything inside every Parenthesis (all Parenthesis are solved first), and exponents are simply expressed the integer doubled x amount of times.

Lets review PEMDAS Math Order of Operation in Python!

First just a quick example of how math will always, always follow that order:

pemdas1

The first example is 4 * 3 whereas the second line is 2*3 +2, because Multiplication is performed before Addition in the math order of operations.

I am going to run through all the math enumerators here quick, and demo in Python

We’re going to do this if you can dodge a ball you can dodge a wrench style, as this is important, but not a difficult subject to learn beyond maybe memorizing them.

** = Exponent and * = Multiplication

pemdas2

The top is “the power of” 3 where it will times the integer on the left by # of times defined on the right side of the ** operator, and below it demonstrates the same thing only done with Multiplication 3 * 3 * 3.

// = Integer Division (Floored quotient) and / = Division

pemdas3

Integer Division // is exactly that, division that will only return an Integer with no decimal output (Floating Integer), which is referred to as a “Floored Quotient” as well.

Division / of a # will return a Floating Integer, or an integer that includes a decimal.

My curiosity killed the cat question of the day – Is there such thing as a negative Integer?

I tested this just below reversing my two different Divisions to see the output:

pemdas4

Of course division of something will not lead to a negative answer (that I am aware of), but will divide it down to a decimal, or a “Floating Integer” which means that “Integer Division” or a “Floored Quotient” (suppressing decimal output) will result in 0.

However a quick 16 – 8 demonstrates Python absolutely can do Negative Integer, but what about Negative Floating Integers:

pemdas5

It surely does, so that was something I just wanted to play with while doing math operators and Integers, and if there is any oddities I should about up front.

And then there is Multiplication *, Addition +, and Subtraction – as math operators.

However there is of course the “Assignment Operator” for Variables of =, which is used to assign value to a variable which has been previously covered so I won’t go into detail in this article however those concepts are the same with variables and strings.

For example:

pemdas6

This is the “Print function” output for the math of Variables Monkey Pie to equal 10, then showing how we can also see the equation which would go in the order of:

Pie to the power of Monkey
2 * 4
8 / 62
Result of Monkey Pie Exponent + 2(8/62)

So Monkey Pie can get confusing fast, but be sure not to make the mistake of thinking you can assign an Integer as a Variable as seen above, because its not possible:

pemdas7

No Cow Pie in this mathematical operation, however this also underscores a point I have made previously but is a perfect refresher here, Integers cannot be added to an Assignment Operator of = because they are considered “Literal” or not open to interpretation.

5 is not a Cow, 5 represents the Integer value of 5 within Python, and that is it!

Speaking of that is that, that is the end of this particular article on Integers / Math!

I may be recovering a lot of already covered material first as a refresher to myself, but also to make sure I have it documented correctly from multiple sources, just like I have 2-3 videos of CEF / MultiLayer Switching all with probably completely different explanations because of the resource I was using to study it at the time.

So that is how you make Monkey Pie = 10, hope this has been informative for you! 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s