Informatics Practices · Ch 3 — Brief Overview of Python
Arithmetic Operators
Arithmetic Operators
Arithmetic operators are the workhorses of everyday computation. Python supports operators for the four basic arithmetic operations — addition, subtraction, multiplication and division — and adds three more that programmers use constantly: modular division (remainder), floor division and exponentiation. That gives seven arithmetic operators in all.
The seven arithmetic operators
| Operator | Operation | What it does |
|---|---|---|
+ | Addition | Adds the two numeric values on either side of the operator |
- | Subtraction | Subtracts the operand on the right from the operand on the left |
* | Multiplication | Multiplies the two values on both sides of the operator |
/ | Division | Divides the operand on the left by the operand on the right and returns the quotient |
% | Modulus | Divides the operand on the left by the operand on the right and returns the remainder |
// | Floor division | Divides the operand on the left by the operand on the right and returns the quotient after removing the decimal part; also called integer division |
** | Exponent | Raises the base to the power of the exponent — the base is multiplied by itself as many times as the exponent says |
A few of these deserve a closer look, because students often confuse them:
/(division) gives the full quotient, decimal part included. Dividing 5 by 2 with/gives 2.5.//(floor division) performs the same division but throws away the decimal part, keeping only the whole-number quotient — 5 divided by 2 with//gives 2. This is why it is sometimes called integer division. The order of operands matters: 2 floor-divided by 5 gives 0.%(modulus) is the companion of floor division: instead of the quotient, it hands back the remainder. 13 divided by 5 leaves a remainder of 3, so13 % 5is 3.**(exponent) computes powers:3 ** 4means multiply 3 by itself 4 times, which is 81.
The + and * operators work on strings too
Two arithmetic operators have a second life with strings.
The + operator, when placed between two strings, does not add — it concatenates, joining the string on the right onto the end of the string on the left:
>>> str1 = "Hello"
>>> str2 = "India"
>>> str1 + str2
'HelloIndia'
The * operator repeats the item on its left when the first operand is a string and the second operand is an integer. The string is written out that many times, end to end:
>>> str1 = 'India'
>>> str1 * 2
'IndiaIndia'
This dual behaviour is not limited to strings. The + (concatenation) and * (repetition) operators work in the same manner for the other sequence data types as well, such as lists and tuples. …
| Operator | Operation | Description | Example (Try in Lab) |
|---|---|---|---|
| + | Addition | Adds two numeric values on either side of the operator | >>> num1 = 5>>> num2 = 6>>> num1 + num211 |
| - | Subtraction | Subtracts the operand on the right from the operand on the left | >>> num1 = 5>>> num2 = 6>>> num1 - num2-1 |
| * | Multiplication | Multiplies the two values on both sides of the operator | >>> num1 = 5>>> num2 = 6>>> num1 * num230 |
| / | Division | Divides the operand on the left by the operand on the right of the operator and returns the quotient | >>> num1 = 5>>> num2 = 2>>> num1 / num22.5 |
| % | Modulus | Divides the operand on the left by the operand on the right and returns the remainder | >>> num1 = 13>>> num2 = 5>>> num1 % num23 |