Arithmetic Operators

Table of contents:

Python arithmetic Operators

Arithmetic operators allow us to perform arithmetic operations on values. There are nine arithmatical operators in python:

+ 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 + for addition

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 Concatenation

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 - for subtraction

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 Operator * for multiplication

The * is used to perform multiplication operations.

String repetition

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 / for division

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.

Operators Unary Positive and Unary Negation

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 for exponentiation

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 for floor division

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... ▶

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 %

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?

Warning: include(/home/u886389547/domains/learnasyoulike.com/public_html/arrows.php): failed to open stream: No such file or directory in /home/u886389547/domains/learnasyoulike.com/public_html/coding/python-arithmetic-operators.php on line 754

Warning: include(): Failed opening '/home/u886389547/domains/learnasyoulike.com/public_html/arrows.php' for inclusion (include_path='.:/opt/alt/php74/usr/share/pear') in /home/u886389547/domains/learnasyoulike.com/public_html/coding/python-arithmetic-operators.php on line 754