Python Lesson 4: Math & Logic

Advertisements

Hello everybody,

It’s Michael, and today’s lesson will be on Python math and logic, with an emphasis on mathematical and logical operators in Python.

First, we’ll start off with mathematical operators. Python has seven mathematical operators, which include +, -, *, /, %, ** and //. If you know Java (or basic arithmetic for that matter), then the first four operators would look very familiar to you, since they are the addition, subtraction, multiplication, and division operators, respectively.

However, let’s discuss the other three operators-%, **, and //. % is the modulo operator, which is what we would use for modular division. For those unfamiliar with the concept, modular division is similar to regular division, except you are trying to find the remainder of two numbers instead of the quotient. For instance, 11%4 (read as 11 mod 4) is 3, since 11 divided by 4 is 2 with a remainder of 3.

** is the exponentiation operator. This one is pretty simple to explain, since this would be the operator you would use if you want to raise a number to a certain power. That’s all. For example, 8 cubed in Python would be 8**3. This is much simpler than using Java’s Math.power() function (which requires importing a class-not the case with Python).

// would probably look the most unfamiliar to you (I had to look it up the first time I saw it). This is the floor division operator, and I’ll admit that, even though I do lots of coding, I’ve never heard of the concept of floor division. Apparently, floor division is a very simple concept, as it is regular division with any decimal points removed from the quotient. However, keep in mind that floor division rounds DOWN to the nearest integer, so in a division problem like 20/7 with a simple division quotient of 2.86 (rounded to two decimal places), the floor division quotient would be 2, not 3.

Another thing to keep in mind with floor division is that, if either the dividend or divisor is negative, the quotient will also be rounded down, but it will be rounded away from 0 NOT towards 0. For example, -20/7 has a simple division quotient of -2.86 but a floor division quotient of -3, not -2. So when I say a negative floor division quotient would be rounded away from 0, I mean that the quotient will be rounded to the LOWER negative number, not the higher negative number.

Now, let’s see the %, **, and // operators in action:

a = 30 % 8
b = 6**3
c = 177 // 33
print(a)
print(b)
print(c)

And here’s the output:

6
216
5

The answer for a-6-is displayed first. 30 mod 8 is 6, meaning that 30 divided by 8 will give you a remainder of 6. The answer for b-216-is displayed next. 6 cubed is 216. The answer for c-5-is displayed last. The simple division quotient (rounded to two decimal places) is 5.36, so the floor division quotient is rounded down to 5.

Now let’s demonstrate some PEMDAS (remember this?) in Python:

a = 4 – (2 + 5) * 11 / 2
print(a)

And here’s the output:

-34.5

OK, so this is a simple enough problem. Parentheses come first, so (2+5) would be solved first; you would get a result of 7. 7 would then be multiplied by 11 to get 77, which would then be divided by 2 to get 38.5, which would be subtracted from 4 to get the final result of -34.5.

Now let’s try something a little more complex:

a = 100 // (16 + 2) – 31 + 3**4 / (6 * 12) * 15 % 6
print(a)

And here’s the output:

-21.125

Here’s a breakdown showing how to get to the final answer:

  • 100 // (16 + 2) - 31 + 3**4 / (6 * 12) * 15 % 6
  • 100 // 18 - 31 + 3**4 / 72 * 15 % 6
  • 100 // 18 - 31 + 81 / 72 * 15 % 6
  • 5 - 31 + 1.125 * 15 % 6
  • 5 - 31 + 16.875 % 6
  • 5 - 31 + 4.875
  • -26 + 4.875
  • -21.125

Keep in mind that with order of operations in Python, the % and // operators are treated with the same precedence as the simple division operator (/).

Next, I’ll cover comparison operators, which are used to compare two values. There are 6 comparison operators in total-==, !=, >, <, >=, <=, which respectively represent:

  • Equal to
  • Not equal to
  • Greater than
  • Less than
  • Greater than or equal to
  • Less than or equal to

Statements with these logical operators will either return true or false. Let’s demonstrate the comparison operators:

a = (13 + 11) == 24
b = (41 + 16) != 66
c = (15 > 29)
d = (41 < 23)
e = (13 >= 9)
f = (55 <= 22)
print(a)
print(b)
print(c)
print(d)
print(e)
print(f)

And here’s the output:

True
True
False
False
True
False

So, in order, here’s the result for each statement:

  • a-TRUE
  • b-TRUE
  • c-FALSE
  • d-FALSE
  • e-TRUE
  • f-FALSE

The next thing I wanted to discuss are the three logical operators-and,or, and not. The logical operators work just as they do in Java, except in Python, you would words instead of the symbols &&, ||, and !-which are and, or, and not, respectively. However, the logic behind these operators is the same as in Java. By this I mean:

  • For two statements combined with and, both of the individual statements must be true for the statement pair to be true. If one of the statements is false, then both are false.
  • For two statements combined with or, only one of the statements must be true for the statement pair to be true. However, if both statements are false, then the whole statement pair is false.
  • Statement pairs with not often contain an and or or as well (e.g. not(statement pair a or statement pair b)). The statement pair in parentheses would first be evaluated, and the result would be whatever the opposite of the result from the statement pair. For instance, if the statement pair was true, then (assuming there is a not), the result would be false, and vice versa.

Let’s start with a simple demonstration of logical operators:

m = (14 > 19) and (15 + 12 != 33)
o = (51 >= 16) or (9 – 2 > 6)
f = not(14**2 < 28 and 13 % 9 <= 1)
print(m)
print(o)
print(f)

And here’s the output:

False
True
True

The results to each statement are:

  • m-FALSE
  • o-TRUE
  • f-TRUE

Now, let’s try something slightly more complex with logical operators:

m = (14 < 19) and (23 >= 11) or (100 % 16 > 7)
print(m)

And here’s the output:

True

How did we get this result? First of all, keep in mind that the statement pairs will be evaluated from left to right (just as they would in order of operations problems). The result of the first statement pair-(14 < 19) and (23 >= 11)-is true, so that result would be evaluated with the second statement-(100 % 16 > 7)-which is also true. Since we are dealing with two trues and an or, the entire statement group is true.

Next, we’ll discuss the two identity operators-is and is not-which also function as comparison operators. However, these operators don’t evaluate whether or not two objects are equal or not; rather, these operators test whether or not two values are the same object with the same memory location. I know that sounds like a lot of coding jargon, so here’s a program to demonstrate identity operators:

a = (“ant”,”bear”,”cat”,”dog”,”eel”)
b = (“ant”,”bear”,”cougar”, “dog”, “eel”)
print(a is b)
print(a is not b)

And here’s the output:

False
True

A simple way to explain this program is that I’m trying to test whether a and b have the exact same values. The first statement-a in b-is false because a and b do not have the exact same values. However, the second statement-a not in b-is true because a and b do not have the exact same values.

An easy way to remember identity operators is that, when comparing two lists, is means the two lists are exactly alike while is not means that the two lists aren’t exactly alike.

Finally, let’s cover the concept of membership operators. Like identity operators, there are two membership operators-in and not in-and both identity and membership operators are used with lists. Here’s a program demonstrating identity operators:

a = (“carrot”, “celery”, “squash”, “cucumber”, “eggplant”)
print(“squash” in a)
print(“squash” not in a)

And here’s the output:

True
False

In this program, I am testing whether or not squash is in my list (a). Just as with the previous program, I used both possible operators-in and not in-to see which one would give me true and which would give me false. The first statement-squash in a-is true, meaning that squash is in list a. Thus, the second statement-squash not in a-is false.

Thanks for reading,

Michael

Leave a ReplyCancel reply