So far, we haven’t really done anything dynamically yet; we declared and initialized variables and output their values. That’s about to change, because now we are going to be working a bit with the basic math operators that Python provides.
Python supports all of the math operations that you would expect. The basic ones are addition, subtraction, multiplication, and division. Other ones include the exponentiation and modulo operators, which you will see in a moment.
Addition & Subtraction
Let’s start out simple and add two numbers together, store the result in a variable and print out the result.
a = 4 b = 2 c = a + b print(c)
Even though we used variables for the addition, we could just as well have entered numbers instead.
As I am sure you can imagine, the above prints out the number 6.
Let’s now try to divide c by two and print out the result.
print(c / 2)
Notice how I printed out the result of the division — being an expression — directly, instead of storing the result within a variable first. When printing out values, we are using a function named print. We haven’t talked about functions yet, so we will get back to that later. The point is that this function takes an argument. For this argument, we can pass in a value directly, such as “Hello”, 22, or a mathematical expression as in this case. We will get back to all of this later, so don’t worry if that went over your head.
Either way, the above line of code prints out the number 3.0. Note that this number is actually of the type float — being numbers with decimals — instead of int. In this case we are dividing two integers (i.e. whole numbers), but that might not necessarily be the case. So the fact that divisions always result in floating point numbers is just convenient for us in case we need to perform some further operations on the number.
There is nothing special about multiplication in Python; we just use an asterisk (*) in the same way we did with addition, subtraction, and division.
print(2 * 2)
I’m sure it comes as no surprise to you that the above outputs 4. Note that if we multiply two integers, the result is of the data type int. If we multiply by at least one decimal number, the result is a float value.
Apart from the most common mathematical operators — being +, –, * and /) — Python also provides a handy operator for working with exponents. Consider the following example.
print(2 ** 10)
This raises 2 to the power of 10, also noted as 210, where 10 is the exponent. Without getting too much into math, this is the equivalent of multiplying 2 by itself 10 times, i.e. 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2. As you can see, using the exponential operator is much more convenient than typing all of that out manually. The result of the above is 1024.
Another math operator, is the modulo operator. What this operator does, is that it performs a division and then returns the remainder of the division. Consider the following examples.
print(4 % 2) print(4 % 3)
The first line outputs 0. What happens is that 4 is divided by 2, leaving a remainder of 0. The second line divides 4 by 3, which leaves a remainder of 1. Likewise, the expression 105 % 10 gives us the integer 5. Hopefully that makes sense.
So what is the point of this operator? Typically you are not interested in the remainder itself, but just determining whether one number is divisible by another number. We will get back to examples of this at a later point, but a simple example could be to highlight every second row of a table by checking if the current row number is divisible by two (i.e. the remainder is zero). Again, more on that later.
The last math operator that we will look at, is the floor division operator. Suppose that we divide 10 by 3, for instance. This division yields a floating point number (i.e. a decimal number) of 3.33 (I reduced the number of decimals for brevity). When using floor division, the result of a division is rounded down to the nearest integer value, i.e. to a whole number. Consider the following example, where the floor division is denoted by two slashes, i.e. //.
print(10 // 3)
The division itself results in the decimal number 3.33 (again, the actual result produces more decimals). This number is then rounded down to the nearest full number, being 3. So the result of the above is the integer 3.
But what if the division results in a negative number? Suppose that we divide -11 by 3, which gives -3.66. What would the result of the following then be?
print(-11 // 3)
The result would actually be -4. That’s because the number is rounded down, and since -3.66 is below zero, rounding down to the nearest integer (or full number) means rounding down to -4. So if the result of the division is less than zero, we round away from zero, so to speak.