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
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:
** * / % // + –
Now, lets discuss each of these operators with relevant examples having operands 9 & 5.
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 -
It would get us the output (81+1-45)
on the other hand, check for following -
As already explained, parenthesis would command the highest order. So,
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
* operator repeats a string n number of times. Again -
>>> a='Tech' >>> 3*a