Python Beginner Concepts Tutorial

# Math Operations

In [1]:
stuff = "Perform basic math operations, store logic and results in Python"

In [2]:
len(stuff)

Out[2]:
64

Being a Mac guy, often times, I'll jump to Spotlight (press Cmd + spacebar) or the Google search bar for math calculations. It feels great to use my keyboard to write out operations fast and have a wide screen to include many numbers.

But, I run into an issue.

Have you ever done one off-the-cuff calculation, erased it, done another calculation, and then realized you wanted to compare your two ansewers? More than just your answers, you should probably compare your logic to see what's wrong.

Please don't make the same mistake I had always done. I recommend to put numbers in a data structure, design a function to save your logic, and store results for easy access. Let me show you how.

### Software Sales Example¶

Python is fantastic because you can easily do commonplace operations such as addition, subtraction, multiplication, division and more! This example will run you through operations in a real-world example.

Perhaps you're in a meeting and somebody asks you to write down the sales amounts for the past 4 months and see your % growth month over month. To solve this fast, you write out the numbers, 24200, 26800, 19500 and then do the operations one by one.

In [3]:
(26800 - 24200)/24200*100

Out[3]:
10.743801652892563
In [4]:
(19500-26800)/26800*100

Out[4]:
-27.238805970149254

However, doing math this way in Python you're prone to errors as you may mispell a large number, you haven't saved your output to compare in calculations and your math is not modular to expand on these calculations for the next few months. Here's what I'd recommend to fix this:

#### Store amounts in a data structure¶

Now, we don't need to re-write these long numbers over and over again. Instead, we can index a number in a list and use auto-correct to write out the list name a second time.

This data structure can be particularly useful because we can easily modify amounts in place and append additional amounts.

In [5]:
sales_by_month = [24200, 26800, 19500]


#### Design a function to calculate month over month growth¶

Our function can store complex logic and allow us to easily perform calculations given any amounts moving forward.

In [6]:
def period_growth(amount_one, amount_two):
return (amount_two - amount_one)/amount_one*100

In [7]:
period_growth(sales_by_month[0], sales_by_month[1])

Out[7]:
10.743801652892563

This growth percentage is the same as our off-the-cuff calculations above. So, our function is the correct logic.

#### Store the results¶

If we do an operation now, we can easily reference it later to share with our boss.

In [8]:
growth_in_february = period_growth(sales_by_month[0], sales_by_month[1])

In [9]:
growth_in_march = period_growth(sales_by_month[1], sales_by_month[2])


If we're asked "What was growth from January to February?", we can just print out our variable.

In [10]:
growth_in_february

Out[10]:
10.743801652892563

### Summary of Operations in Python¶

In [11]:
8 + 2

Out[11]:
10

#### Subtraction¶

In [12]:
6 - 4

Out[12]:
2

#### Multiplication¶

In [13]:
5 * 4

Out[13]:
20

#### Division¶

In [14]:
10 / 5

Out[14]:
2.0

You're probably wondering why the answer is 2.0 and not simply 2. In Python 3, the / operator does floating point division so you're returned a float value of 2.0.

#### Exponentiation¶

The operator ** acts to raise a number to a power.

In [15]:
5 ** 2

Out[15]:
25

#### Floor division¶

The floor division operator, //, divides two numbers and round the result down to an integer.

For example, let's say a movie is 80 minutes. You want to read off the duration of the movie in hours and minutes.

In [16]:
80 // 60

Out[16]:
1

Let's use the modulo operator to find the minutes part of the 80 minute movie.

#### Modulo¶

The modulus operator, %, is used to divide two numbers and return the remainder.

In [17]:
80 % 60

Out[17]:
20

Based on use of these two operations, we know the movie is 1 hour and 20 minutes.

Also, the modulus operator is useful to detect if numbers are even or odd.

In [18]:
8 % 2

Out[18]:
0

8 is evenly divisible by 2 so we're left with a remainder of 0.

In [19]:
7 % 2

Out[19]:
1

7 is not evenly divisible by 2 so we're left with a remainder of 1.