Arithmetic operators allow us to perform arithmetic operations on values. There are nine arithmatical operators in python:
Operator | Name |
---|---|
+ | addition |
- | subtraction |
* | multiplication |
/ | division |
+x, -x | unary positive and negation |
** | exponent |
// | floor division |
% | modulo operation |
We have already seen the use of some of them in the earlier pages.
The + operator is used to add two or multiple numbers. The operands are generally of 'int' or 'float' type.
In python, arithmetic calculation is possible between 'int' and 'float' type values; the resultant value wil be of 'float' type.
String literals can also be added, or better we say, joined with the + operator. This is called string concatenation.
In the above, the + is also known as concatenation operator.
The - operator is used to perform subtraction on numeric values.
In the following, the evaluated output of m - n is 1 which is a whole number, still the variable k is of 'float' type.
The * is used to perform multiplication operations.
The * operator can also join string literals in a different way. print("A" * 7) will produce a string of series of "A".
In the above, the *
is also known as repetition operator.
Note that the difference between
9 and "9" has
produced different outputs.
So, python + and
* operators can be applied to join strings
in addition to addition and multiplication of numbers.
The / operator is used to divide a number by another number.
The division operator / always results in a float number(having a decimal point) even if both the numerator and the denominator are integers. Another note on division, if the denominator is zero, an ZeroDivisionError will be raised by the interpreter.
So far we have seen that operators act on double operands, such as:
b + c, 456.7 - 89, length * breadth, side / 2
They are called binary operators because they operate on two operands.
The + and - operators can also operate on a single operand.
They are called unary operators because
they operate on a single operand (x here).
The +x unary positive operator
produces the same value as the operand. It is almost never used.
The - unary negation operator
produces the same value as the operand, but opposite in sign. In another word,
the operand is multiplied by -1 and the result is taken as the output.
In the above, the + is not an addition operator, rather it is the unary positive operator, and the - is the unary negation operator. They both act on a single operand number.
It is due to the unary positive and unary negation operators the following statements are completely valid in python:
2++1 = 2 + 1 = 3,
4 + +1 = 4 + 1 = 5,
9--3 = 9 - (-3) = 12,
7- -8 = 7 - (-8) = 15
But we should avoid such obscure entries, and write cleaner codes for clarity.
For example, instead of writing 9--3,
we should put parentheses in proper locations, like 9 - (-3).
The ** operator raises the left operand to the power of the right operand. So, the expression 9 ** 2 becomes 81 (9 raised to the power 2).
Alternately, for the above calculation, we can use python pow() to get the same output. The pow() is a python function which carries out the exponent operation.
When there are more than one ** operators in an expression, the right most sub-expression is evaluated first.
For 4 ** 3 ** 2, first 3 ** 2 is evaluated to 9, then 4 ** 9 is evaluated to 262144.
4 ** 3 ** 2 = 4 ** 9 = 262144 ✔
4 ** 3 ** 2 = 64 ** 2 = 4096 ❌
This happens because of the right to left associativity of ** where the right most ** gets the highest preference.
The // operator performs floor division between two numbers.
The result is always an integer. So, it is also known as integer division.
To understand floor division, we have to dig a little
about floor and ceil of number.
You may skip the following, or read it later.
floor and ceil of a number ...more... ▶
As found in Wikipedia,
The floor function is the function that takes as input a real number x,
and gives as output the greatest integer less than or equal to x,
denoted ⌊x⌋ or floor(x).Similarly, the ceiling function maps x to
the smallest integer greater than or equal to x, denoted ⌈x⌉ or ceil(x).
In mathematics and computer programming, floor
and ceil are the
two functions which receive a real number as input, and produces
an integer based on the input. They do it in their own way.
floor of r = largest integer <= r.
ceil of r = smallest integer >= r.
Ceil rounds down:
floor(4.5) = 4, floor(1.8) = 1,
floor(-2.1) = -3 (not -2),
floor(-3.9) = -4 (not -3)
The nearest left side integer is the output.
Ceil rounds up:
ceil(4.5) = 5, ceil(1.8) = 2
ceil(-2.1) = -2, ceil(-3.9) = -3
The nearest right side integer is the output.
Actually floor rounds down, or to the left of the number line, as seen above.
For both positive and negative numbers, same rule is applied;
though it appears to be different for negative numbers.
On the other hand ceil rounds up, or to the right of the number line.
The ceil is not required in any of the operations in this page,
but discussed here because it is complementary to floor.
Now we understand what is floor of a number.
floor(r) = n, where n is an integer, n <= r
Now, in floor division, n // d = floor of (n / d)
where n is the numerator, and d is the denominator
For positive inputs, floor division is quite simple; the fractional part is removed from the quotient, and the integer part is the output.
But for negative number inputs, pay attention:
23 // 5 = floor(23 / 5) = floor(4.6) = 4
10 // 4 = floor(10 / 4) = floor(2.5) = 2
-23 // 5 = floor(-23 / 5) = floor(-4.6) = -5
10 // -4 = floor(10 / -4) = floor(-2.5) = -3
The modulo operator % calculates the remainder of a division.
We know, when 13 is divided by 5,
the quotient is 2, and the remainder is 3.
13 = 5 × 2 + 3
When 21 is divided by 7, the quotient is
3, and the remainder is 0.
21 = 7 × 3 + 0
So, in python 13 % 5 produces a result of 3.
21 % 7 produces a result of 0.
Pretty simple! But when negative numbers are involved, the evaluation is not so simple.
It seems that guessing the remainder is not so simple
when negative numbers are involved in the input.
For a modulo operation, the following equation is
always satisfied, both for positive and negative numbers:
r = n - (d * (n // d))
where r is the remainder, n is the numerator, and d is the denominator. This formula is applied by the interpreter to determine the remainder of a division.
--- x ---
Want to leave a message for me?