• Save
  • Download
  • Clear Output
  • Runtime
  • Run All Cells

Loading Runtime

Math with Python

Now that we know how to represent numbers in Python, let's do something with them. Let's do some math.

Most applications of programming don't involve very much math. In fact, it's a common misconception that math know-how is a prerequisite to learning to code. It's not. I've worked as a professional web developer and there's shockingly little to no math involved.

However, comfort with math is very important if you specifically want to do Data Science and Machine Learning with the code that you write. So if you absolutely hate math, this field probably isn't for you (I know that sounds harsh, but it's the reality).

We're going to start off simple. I'm going to assume you're comfortable with basic algebra, but not much more beyond that. I hope that's ok.

Arithmetic Operators

Python is the language of choice for doing cool stuff with math. There are a lot of helpful tools in the Python ecosystem, but the most fundamental tools are baked into the language itself. We'll start with the so-called "arithmetic operators." The arithmetic operators are a set of symbols that are used for doing basic math calculations. You could probably guess what symbols Python uses for most of these operations:

  • Addition: +
  • Subtraction: -
  • Multiplication: *
  • Division: /
  • Exponentiation: **

Feel free to try out some calculations in one of the code cells found in the video transcript notebook below this video.

By the way, there are a couple of other arithmetic operators in Python, but we're going to skip them for now and come back to them later because each of them kind of deserves its own video. We'll deal with the most important and most basic ones for now.

Addition

8

Subtraction

2

Multiplication

15

Division

1.6666666666666667

Exponentiation

125

What are "operands"?

The symbols that we use to do stuff in Python are called "operators" because they do operations AKA "stuff", they do stuff. But the numbers on either side of the center symbol have a name too.

The values that go on either side of the arithmetic operators are called the "operands". In the case of doing math with Python, these are the numbers that go on either side of the math symbol. -operands.

Just like in math, Python will prioritize some calculations before others. Python will process your math in the following order of precedence unless you use parentheses to indicate otherwise.

Order of Precedence

  • Expressions surrounded in parentheses (, )
  • Exponentiation: **
  • Multiplicative operations: *, /
  • Additive operations: +, -

Notice how the presence or lack of parentheses changes the final result of the following two expressions.

-6.3
6.857142857142857