Skip to content

Latest commit

 

History

History
249 lines (187 loc) · 5.53 KB

File metadata and controls

249 lines (187 loc) · 5.53 KB

Arithmetic Operators

Python can do math. And many early programming problems you will come across deal with doing fairly easy math. There are ways to do lots of useful things with numbers.

Basics

Operator Name Description

+

Addition

Add two values

-

Subtraction

Subtract one from another

*

Multiplication

Multiply 2 values

/

Division

Divide 2 numbers

%

Modulus

returns the remainder

Say we needed to multiply two numbers. Maybe 2 times 3. Now we could easily write a program that printed that result.

print(2 * 3)

And that will print 6 on the console. But maybe we’d like to make it a little more complete.

a = 2
b = 3
# Multiply a times b
answer = a * b
print(answer) # -> 6

Using this as a model, how would you write programs to solve these problems?

Tip
  • Lab 1: Subtract A from B and print the result

  • Lab 2: Divide A by B and print the result

  • Lab 3: Use an operator to increase A by 1 and print result

a = 9
b = 3

L1 = b - a
L2 = a / b
L3 = a + 1
print(L1) # -> -6
print(L2) # -> 3
print(L3) # -> 10

Division and Remainder

We know that we can do regular division. If have a simple program like this, we know what to expect:

a = 6 / 3 # -> 2
a = 12 / 3 # -> 4
a = 15 / 3 # -> 5
a = 10 / 4 # -> 2.5

But sometimes, we have a need to know what the remainder of a division is. The remainder operator %, despite its appearance, is not related to percents.

The result of a % b is the remainder of the integer division of a by b.

print( 5 % 2 ) # 1, a remainder of 5 divided by 2
print( 8 % 3 ) # 2, a remainder of 8 divided by 3

Now what’s this about '%' (the remainder) operator?

a = 3
b = 2
# Modulus (Remainder)
answer = a % b
print(answer) # -> 1
a = 19
b = 4
# Remainder
answer = a % b
print(answer) # -> 3

Order is Important

A strange thing about these operators is the order in which they are evaluated. Let’s take a look at this expression.

6 × 2 + 30

We can do this one of two ways:

  • Say we like to do multiplication (I know, who is that?)

    • we would then do the "6 times 2" part first, giving us 12.

    • then we’d add the 30 to 12 giving us 42 [1]

  • But say we don’t like multiplication, and want to save it for later…​

    • we would add 2+30 first, giving us 32

    • and then we multiply it by 6, and, whoa, we get 192!

Wait! Which is right? How can the answers be so different, depending on the order we do the math in? Well, this shows us that the Order of Operations is important. And people have decided upon that order so that this kind of confusion goes away.

Basically, multiply and divide are higher priority than add and subtract. And exponents (powers) are highest priority of all.

There is a simple way to remember this.

P.E.M.D.A.S

Use this phrase to memorize the default order of operations in Python.

Please Excuse My Dear Aunt Sally

  • Parenthesis ( )

  • Exponents 23

  • Multiplication * and Division /

  • Addition + and Subtraction -

Warning

Divide and Multiply rank equally (and go left to right) So, if we have "6 * 3 / 2", we would multiply first and then divide. "6 * 3 / 2" is 9

Add and Subtract rank equally (and go left to right) So if we have "9 - 6 + 5", we subtract first and then add. "9 - 6 + 5" is 8

Tip

30 + 6 × 2 How should this be solved?

Right way to solve 30 + 6 × 2 is first multiply, 6 × 2 = 12, then add 30 + 12 = 42

This is because the multiplication is higher priority than the addition, even though the addition is before the multiplication in the expression. Let’s check it in Python:

result = 30 + 6 * 2
print(result)

This gives us 42.

Now there is another way to force Python to do things "out of order" with parenthesis.

Tip

(30 + 6) × 2

What happens now?

result = (30 + 6) * 2
print(result)

What’s going to happen? Will the answer be 42 or 72?

Python math Object

There is a useful thing in Python called the math object which allows you to perform mathematical tasks on numbers.

To make these work, you need to import the math module.

import math

math.pi # returns 3.141592653589793
math.ceil(4.7)    # returns 5
math.floor(4.4)    # returns 4
x = 5
y = 3
math.pow(x, y) # the value of x to the power of y - x^y^
math.pow(8, 2)      # returns 64.0
math.sqrt(x) # returns the square root of x - 2.2360...
math.sqrt(64)      # returns 8
Important

What does "returns" mean?

When we ask a 'function' like sqrt to do some work for us, we have to code something like:

squareRootTwo = math.sqrt(2.0)
print(squareRootTwo)

We will get "1.4142135623730951" twice in the output. That number (squareRootTwo) is the square root of 2, and it is the result of the function and what the function sqrt "returns"'.

math.pow() Example

Say we need to compute "62 + 5"

result = math.pow(6,2) + 5
print(result)

What will the answer be? 279936 or 41?

How did Python solve it?

Well, 62 is the same as 6 * 6. And 6 * 6 = 36, then add 36 + 5 = 41.

squareRootTwo = math.pow(2, 0.5)
print(squareRootTwo)

And notice, when we raise a number to the 0.5 power, it’s the same as taking its square root!. And that can be handy sometimes.

You’ll learn a lot more about working with numbers in your career as a coder. This is really just the very basics of the very beginnings.


1. The answer to life, the universe and Everything.