Computers Are Good at Math

A laptop wearing a wizard hat, thinking about 2+2

In some ways, computers are just large machines for doing math. Computers are very, very good at doing math. In fact, modern computers can perform billions or even trillions of calculations per second. However, the math they do is extremely simplistic, the kind that you learned as a child. Fortunately, programs can use these simple operations together to build more complicated operations.

Operators

There are seven basic mathematical operators in Python that we will review and that you will use in Python: Addition, Subtraction, Multiplication, Float Division, Integer Division, Exponentiation, and Modulo.

Operators and Operands

The values to the left and right of the operator are called “operands.” We will also sometimes casually call them “arguments” or “inputs” to the operation, but the correct word is “operand.”

Addition and Subtraction

# Addition
print(1 + 1)
print(-10 + 10)
print(5.0 + 5)

# Subtraction
print(5 - 8)
print(10 - 3.4)

The + operator is used for addition, and the - operator is used for subtraction. Note that the print function is not required to do the math calculation; it is only required to see the result.

Multiplication and Exponentiation

# Multiplication
print(4 * 2)
print(-5 * 5)
print(.5 * 10)
print(.5 * 4)

# Exponents
print(2 ** 4)
print(10 ** .5)

One asterisk (*) is used for multiplication. Two asterisks (**) are used for exponents (also known as powers).

Division

# Float division
print(3 / 12)
print(1 / 1)
print(2.5 / 5)

# Integer division
print(2.5 // .5)
print(2.5 // 5)
print(4 // 3)

# Oops, error!
print(1 / 0)

The forward slash (/) is used for float division. When you divide two integers with float division, you will ALWAYS get a floating-point number. I’ll say that again: Whenever you do regular float division with a single slash, the result will always be a float! It does not matter if the result could be expressed as a whole number integer; Python will always make the result a float. Two forward slashes (//) are used for integer division. If both operands are integers, the result will be converted to an integer by truncating the result (removing the decimal). However, if either operand is a float, then the result will still be a float. Keep in mind, you cannot divide by 0! This causes an error if you try running the code. The operation is syntactically valid, but semantically incorrect.

Modulo

print(16 % 12)
print(4 % 2)     # Even numbers produce 0
print(5 % 2)     # Odd numbers produce 1
print(6 % 2.1)
print(12.5 % .5)

You may never have heard of the Modulo operator, but programmers use it occasionally: Modulo calculates the remainder after division. Modulo is sometimes called “clock arithmetic” because it makes it easy to figure out time. If someone said, “It is 16 o’clock”, you could do “16 modulo 12” and find out they meant “4 pm”. However, modulo has many other uses, like figuring out if numbers are even or odd. “(Any number) modulo two” will help you figure out if the number is even or odd; if it is an odd number, the result will be 1; if it is an even number, the result will be 0.

Using the Operators

print(2 + 3 + 4)

When you run a program with operators, Python will do the math and then replace the result. You will not be able to see the computer do the math by looking at the code! The result will seem to appear in the console immediately. However, the computer did not execute the code instantly, but instead had to first break down the operations into smaller steps. First, the computer will add 2 and 3. Then the computer will add the resulting 5 to 4. The final result (9) is what is printed to the user without any of the intermediate results being shown.

Order of Operations

When you use operators, there is an order of operations. Whenever there’s a tie, the left-most operation happens first. Notice that you can always override any ordering by using parentheses. You might already be familiar with this order, if not, here is a useful mnemonic. PEMMDAS: “Please Excuse My Most Dear Aunt Sally.” Each word of the mnemonic references the order of the operation. Please (Parentheses), Excuse (Exponents), My (Multiplication), Most (Modulo), Dear (Division), Aunt (Addition), Sally (Subtraction). The words shown on the same line have the same level of priority.

Floats and Integers

# Integers make integers
print(1 + 1)

# Any floats make floats
print(1 + 1.0)
print(1.0 - 1)

# Always a whole number
print(5.5 // .3)

# Always float
print(6 / 3)

You will recall that floats are decimal numbers and integers are whole numbers. When any operation involves any floats, the final result will always be a float — even if it could be expressed as a whole number. The only exception to this rule is when you do division. Remember, integer division with integers will produce an integer, but integer division with floats will produce floats.

Summary