Numerical operators in Python

In this article, we would discuss various numerical operators in Python v3.9. We use these operators on Python expressions. Python expressions consists of operands and operators. For instance,

a + b

where,

a & b are the operands,
+ is the operator.

There is an order of precedence in Python for operators as well. We will discuss it as well. Besides, in Additional Info section – we will see how numerical operators (* +) are different from string operators.

Numerical operators in Python

The numerical operators are:

** 
* 
/ 
% 
// 
+ 
–
Operator Operation
** Power of
* Multiplication
/ Division
% Remainder
// Integer division
+ Addition
Subtraction

Now, lets discuss each of these operators with relevant examples having operands 9 & 5.

Operator Expression Outcome
** 9**5 59049
* 9*5 45
/ 9/5 1.8
% 9%5 4
// 9//5 1
+ 9+5 14
9-5 4

Furthermore, as already hinted – there is an order of precedence for operators.

Operands within Parenthesis are the first. Thereafter, ** operator. Followed by * , / , // , and % operators. And, finally its the + and operators which are at the last. This is for expressions evaluated left to right.

Lastly, open a Python shell for expressions discussed below. It should help us make things clear –

>>> 9**2+9//5-9*5

It would get us the output (81+1-45)

37

on the other hand, check for following –

>>> 9*5+9**2-9//5

output (45+81-1)

125

As already explained, parenthesis would command the highest order. So,

>>> 9*(5+9)**2-9//5

output (9*196-1)

1763

In conclusion, we have discussed various numerical operators. And, their order of precedence was also there.

Additional Info

String operators are * and +. These two act as numerical operators as well. Since we have already seen their functioning as numerical operators. Therefore, in this section lets see how these two operators can be used on string literals.

+ operator on string literals concatenates the two. We assign values to two operands ‘a‘ and ‘b‘.

>>> a='Tech'
>>> b='Piezo'
>>> a+b

Output –

'TechPiezo'

* operator repeats a string n number of times. Again –

>>> a='Tech'
>>> 3*a

Output –

'TechTechTech'