You may have heard this before, but there are a number of common concepts between math and programming. Just like in your math class, you can do math with your variables. These are some operations you can use:
- + : addition
- – : subtraction
- * : multiplication
- / : division
- %: modulo (this is a special one, it does NOT mean percent)
- **: exponent
Let’s open a new Python file to practice.
print(4+3) # should return 7
print(2*5) # should return 10
print(10/2) # should return 5.0
print(2**3) # should return 2 to the 3rd power or 8
So we know now that Python console can do some simple math calculations for us. Now we will try a little more complex one. Before you type this in and hit enter, make a guess based on your knowledge of math. What do you think the answer will be?
print(10*3+4/2-8)
What value did you guess? Was it the value you received from Python console? The value you should have received was 24.0. We know that in math when we are simplifying an equation like this one we use PEMDAS.
P: Parenthesis
E: Exponents
M: Multiplication
D: Division
A: Addition
S: Subtraction
The Python programming language also follows this rule, with a few minor modifications. One such modification is the Modulo (%). What we would typically think of as the percent sign in normal math equations, this symbol in Python programming means to return the remainder from division. So the modulus of 5%2 is 1 (2 goes into 5 twice with a remainder of 1). It is often used to find out whether a number is even or not. For example, I can pick any number and two. If the modulus of that number and two is 0, the number is even. Otherwise, the number is odd. Let’s try some examples in a Python script.
print(10%3) # should return 1
print(9%3) # should return 0
print(11%7) # should return 4
print(345829%2) #should return 1, so it is an odd number
The modulo operator is the result of division, so it has the same precedence as multiplication and division in the PEMDAS order of operations. Now that we know the modulo operator, let’s try one more example. Try to guess what the value will be before hitting enter:
print((10%3*2)+4-5*2+2**4)
The answer you should receive is 12. If you don’t understand, try writing out the steps of the calculation on paper until you are able to get to the correct answer.
So we know python can do order of operations, but can it also do math on variable values? Let’s try!
x=4
y=3
print(x*y)
The value that is printed should be 12 (3 multiplied by 4). When performing math on variables, Python substitutes the current value of the variable and completes the calculation just as we saw earlier using the order of operations. We can even use the same variables multiple times. Let’s try this example (make sure x and y are still in the variables pane and are set to 4 and 3, respectively):
z=x**y+x-y
print(z)
The printed value should be 65. So we can use variables multiple times and use their values to be stored in other variable values. This is just another reason variables are so valuable!