After completing this section, you will be able to:
We have already used some basic arithmetic operators in the previous exercises. Let's have a systematic look at all the available operators.
The following table contains the most common arithmetic operators, along with examples:
| Operator | Purpose | Example | Result |
|---|---|---|---|
+ |
Addition | 2 + 4 |
6 |
- |
Subtraction | 10 - 2.5 |
7.5 |
* |
Multiplication | -2 * 123 |
-246 |
/ |
Division (float) | 9 / 2 |
4.5 |
// |
Division (integer) | 9 // 2 |
4 |
% |
Modulo | 9 % 2 |
1 |
** |
Exponentiation | 2 ** 3 |
8 |
A calculation usually consists of operands and operators:
The data type of an operand usually determines the data type of the result: if two integers are added together, the result will also be an integer. If a floating point number is subtracted from another floating point number, the result will also be a floating point number.
In fact, if a single one of the operands in an expression is a floating point number, the result will also be a floating point number, regardless of the other operands.
Notice Python also has an integer division operator //. If the operands are integers, it will produce an integer result. The result is rounded down to the nearest integer:
The order of operations is familiar from mathematics: first calculate the exponents, then multiplication and division, and finally addition and subtraction. The order can be changed with parentheses.
With operators on the same level of precedence, the parentheses determine which operation is performed first:
Strings and integers are processed quite differently in Python programs. The following two programs should clarify this difference:
So, when two numbers are added together, we get the sum. When two strings are "added" together, we get a new string with the two original strings concatenated together.
Strings and integers are not directly compatible with each other. For example, trying to execute the following would cause an error:
If you try to make the following calculation:
Python would interpret it as string concatenation, and the result would thus be 12. If you actually want to perform addition on two numbers, and the operands are strings, they will have to be converted to integers with the int function:
Similarly, strings can be converted into floating point numbers with the float function:
Let's have a look at a program which calculates the sum of three numbers given by the user:
The program uses four different variables, but two would easily suffice in this case:
All inputs from the user are read into the one and the same variable number. The value of the variable sum is increased by the value of the variable number each time the user inputs a new number.
Let's take a closer look at this command:
Here, the value of the variable sum and the value of the variable number are added together, and the result is stored back in the variable sum. For example, if before the command the value of sum is 3 and the value of number is 2, after the command is executed, the value of sum is 5.
Increasing the value of a variable is a very common operation. As such, there is a commonly used shorthand notation which achieves the same result as the explicit summing up above:
This allows us to write the above program a little more concisely:
In fact, we don't necessarily need the variable number at all. The inputs from the user can also be processed like this:
Of course, it will depend on the context how many variables are needed. If it is required to remember each value the user inputs, it will not be possible to "reuse" the same variable to read different values from the user. Consider the following:
On the other hand, the above program does not have a named variable for storing the sum of the two values.
"Reusing" a variable only makes sense when there is a need to temporarily store things of a similar type and purpose, for example when summing numbers.
In the following example the variable data is used to first store the name of the user, and then their age. This is not at all sensible.
A better idea is to use separate variables, with descriptive names:
Complete the following exercises to practice arithmetic operations in Python!
Please write a program which asks the user for a number. The program then prints out the number multiplied by five.
Please write a program which asks for the user's name and year of birth, and prints out a message as follows:
Please write a program which asks for two integer numbers. The program should print which one is greater. If they are equal, it should print a different message.
Please write a program which asks for the user's name and address. The program should also print out the given information, as follows:
The following program should work, but it contains errors. Please fix the program so that it works as intended.
Please write a program which prints out a box:
Please write a program which asks the user for a number and calculates its square root. You will need the function sqrt from the math module.
Please write a program which asks the user for four numbers. The program should then print out the sum and mean of the numbers.
You've completed Arithmetic Operations! You now know how to:
Total points: 8/8