First steps in Python (Part 2)
Using operations
By trying to write a fraction, we stumbled onto the syntax for division. What about other operations?
We already know that we can add blank spaces before or after a number, but what about before or after a mathematical operation? For example, the following code
print(4 + 2)
print(4+2)
print(4 + 2)
gives the output
6 6 6
All of these options work, but for the sake of readability, we choose to have one blank separating a number and a symbol.
The following code
print(4 - 2)
print(4 * 2)
print(4 / 2)
gives the output
2 8 2.0
In these examples, notice how adding, subtracting, or multiplying integers results in an integer, but that division results in a floating point number, even when the result has no fractional part. Different programming languages handle division differently. In Python, as we'll see shortly, we have another option if we want the result to be an integer.
What happens if one of the two numbers is a floating point number? The following code
print(4.0 + 2)
print(4.0 - 2)
print(4.0 * 2)
gives the output
6.0 2.0 8.0
Then for all operations, the result is a floating point number. But that's not all. The following code
print(4.0000000000000000000001 + 2)
print(4.0000000000000000000001 - 2)
print(4.0000000000000000000001 * 2)
gives the output
6.0 2.0 8.0
Here's where we can see the effect of only an approximate value being stored. These are not the only operations in Python. Negation, which is written as a dash, makes a positive number negative, and a negative number positive. The following code
print(- 1)
print(- -5)
print(+ 1)
print(+ -5)
gives the output
-1 5 1 -5
Notice how we leave a space between the dash and the number on which it operates, in this case, -5. In contrast, putting a plus sign instead of a dash doesn't change the number on which it operates. We're used to put a plus sign between two numbers; we call that “binary plus” since binary means “two”. The plus we have here is applied to only a single number. We call it “unary plus” since unary means “one”.
For the next two operations, //
and %
, think back to when you first learned division. You were probably taught to find the quotient (that is, the number of times you could subtract the second number from the first) and the remainder (that is, whatever was left over after all the subtractions). The following code
print(31 // 7)
print(31 % 7)
gives the output
4 3
The first calculation gives the number of full weeks in a 31-day month, and the second calculation gives the number of extra days in the incomplete week that makes up the rest of the month. Here we use a double division sign.
What happens if we double the other symbols? The following code
print(3 ++ 2)
print(3 -- 2)
print(3 ** 2)
gives the output
5 5 9
The first two are interpreted as 3+(+2) and 3-(-2). The last one is the most interesting: this is exponentiation; in this case, three squared. Suppose we want to make use of our new knowledge to calculate the number of days of holidays we have. There are two full weeks, plus an extra half week. The code print(2 + .5 * 7)
gives the output 5.5.
Why do we have fewer than 7 days? The problem here is that we need to know the precedence of operations. The multiplication was done first, not the addition. We can fix this using parentheses. The corrected code, print((2 + .5) * 7)
gives the output 17.5. Fortunately, the order of precedence is the same as in mathematics.
Using operations: what we learned
Precedence rules
- Brackets
- Exponentiation
- Division
- Multiplication (same precedence as division)
- Addition
- Subtraction (same precedence as addition)
Brackets are parentheses and they are considered first. After that, exponentiation, division and multiplication (with equal precedence) and finally addition and subtraction, also with equal precedence. If saying “BEDMAS” helps, please feel free to do so.
Summary of operations
Here is a summary of all the operations we have discussed.
Operator | Symbol | Notes |
---|---|---|
addition | + | |
substraction | - | |
multiplication | * | |
division | / | Output floating point |
unary plus | + | Used on one value |
negation | - | Used on one value |
quotient | // | Divide first by second |
remainder | % | Divide first by second |
exponentiation | ** | First to the power second |
Keep in mind that division results in a floating point number, even if the numerator is a multiple of the denominator and both are integers. Don't worry, later on we'll learn how to convert floating point numbers back into integers. Notice that the same symbol is used for both subtraction and negation. The difference is whether it is put between two values or to the left of a single value. The same holds for addition and “unary plus”. For quotient and remainder, we divide the first value by the second to obtain our results. Similarly, when we use exponentiation, the first value is the base and the second is the exponent. For all our operations, if you supply at least one floating point number, what you'll get back is a floating point number.
Using blank spaces
We have some freedom with respect to blank spaces, but need to be careful.
Caution
Do not put blank spaces within a number.
Blanks within a number will lead to an error message.
To make your code more readable:
- Do not put extra blank spaces after (or before).
- Separate symbols from values by one blank.
There are other rules to follow to help make your code more readable, such as not putting extra blanks inside parentheses, and to make symbols in operations more visible by giving a space between a symbol and a value.
Glimpse of the future
- Extra blank spaces at the beginning of a line can change how the program runs.
- Avoid using tabs, which look the same as blank spaces.
Here I'm giving you a glimpse of the future: all such glimpses, as well as all other information in gray, is optional when mentioned. This particular glimpse is to let you know that even though blank spaces may not yet seem very important, they will be later. I'll remind you of these points when they become relevant.