Math Operations¶
Date published: 2018-02-08
Category: Python
Subcategory: Beginner Concepts
Tags: math, functions
stuff = "Perform basic math operations, store logic and results in Python"
len(stuff)
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.
(26800 - 24200)/24200*100
10.743801652892563
(19500-26800)/26800*100
-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.
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.
def period_growth(amount_one, amount_two):
return (amount_two - amount_one)/amount_one*100
period_growth(sales_by_month[0], sales_by_month[1])
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.
growth_in_february = period_growth(sales_by_month[0], sales_by_month[1])
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.
growth_in_february
10.743801652892563
Summary of Operations in Python¶
Addition¶
8 + 2
10
Subtraction¶
6 - 4
2
Multiplication¶
5 * 4
20
Division¶
10 / 5
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.
5 ** 2
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.
80 // 60
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.
80 % 60
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.
8 % 2
0
8 is evenly divisible by 2 so we're left with a remainder of 0
.
7 % 2
1
7 is not evenly divisible by 2 so we're left with a remainder of 1
.