From the course: Programming Foundations: Numerical Mathematics and Calculations
Mathematical operators and order of operations - Python Tutorial
From the course: Programming Foundations: Numerical Mathematics and Calculations
Mathematical operators and order of operations
- [Instructor] When someone mentions math, the first thing that probably comes to your mind are mathematical operations. Addition, subtraction, multiplication, and division. Although computing them is pretty straightforward and simple, combining them into expressions can soon become more complex to handle. We can add and subtract numbers using the addition and subtraction operators. For example, eight plus seven or 135 minus 97. For multiplication, we'll use the asterisk operator. For example, by typing six multiplied by eight. And for division, we are going to use a forward slash. For example, if a type 24 divided by 15, we'll get 1.6. In a case where you would want the remainder, then you will use the modulo operator, by typing 24 modulo 15. Except for these four mathematical operators, there is a fifth one called exponential operator that we use to calculate the power of numbers. It is written using the double asterisk operator. Let's see, for example, how we would calculate three to the power three. We would type three double asterisk three. Let's try it out by opening our Jupiter notebook. Let's add two integers by using the plus operator and run our code by typing shift enter. For subtracting, we use the subtraction operator, minus. Cool. To multiply six by eight, we'll use the asterisk sign. And to divide 24 by 15, we'll use slash. As you can see, when we divide two numbers in Python, we get as a result a floating point number. Let's see what is the remainder of dividing 24 by 15 by typing 24 modulo 15. Lastly, to calculate the power of the number, for example, three to the power of three, we will use double asterisk and when we run our code, we get 27. Now, when we play with all mathematical operators, the cool thing is that we can combine them together into more complicated expressions and even use parenthesis. There is an unforgettable acronym in math called PEMDAS that stands for parenthesis, exponents, multiplication, division, addition, subtraction. We'll use it to remember the order of calculations. Let's try it out on a simple example, seven plus seven multiplied by three. What do you think the result would be? As we have both addition and multiplication, because of the PEMDAS, Python will first calculate the multiplication, seven multiplied by three. It's 21 and plus seven, we get 28. What if we change our expression by adding parenthesis around addition? In this case, the expression inside parenthesis is evaluated first, and we get seven plus seven is 14 times three and the final result is 42. Let's see our third and last example that we'll use. 30 minus two, double asterisk, three plus three, multiplied by four. In our expression, we'll first calculate two to the power of three. After that, multiplication, three by four. And lastly, subtraction and addition. When we run our code, we get 34. Great job. We'll continue our journey by exploring common mathematical functions in the next lesson.
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
Mathematical operators and order of operations4m 4s
-
(Locked)
Common mathematical functions4m 15s
-
(Locked)
Using built-in math functions4m 39s
-
(Locked)
Writing a program to calculate factors of an integer3m 42s
-
(Locked)
Challenge: Converting units of measurement1m 30s
-
(Locked)
Solution: Converting units of measurement2m 49s
-
-
-
-
-
-
-