We all know the basic plus sign (+), minus sign (-), and multiplication sign (*). The above mentioned signs that you have studied in basic mathematics are Arithmetic Operators. We use arithmetic operators in an Expression. An expression is a line of code that Python reduces to a single value. For example: 4+2=6
In the above
example, ‘4+2’ is the expression and ‘6’ is the value reduced
to Python.
An expression is made up of operators
and operands.
In the above
example, ‘4’ and ‘2’ are operands and ‘+’ is the operator. So in simple words you can say that operands surround the
operator (there are still exceptions)
OPERATORS:
1. Exponent (**):
Exponent is basically the number of
times a number is multiplied by itself. For example, 23 =
8. But if you want to this same calculation in Python you must use the exponent
(**) operator. So the above expression would look like this in Python, 2**3. This expression will give the
output 8.
2. Division (/): We all know the classic mathematics division. In Python we use ‘/’ operator to perform a division. BUT REMEMBER, THIS OPERATOR RETURNS THE QUOTIENT AND NOT THE REMAINDER. For example, 4/2 will NOT return 0, it will return 2.0.
Hope you have got
this cleared. But if you want to find the remainder, you can use the following
operator.
3. Modulus (%): Now this is the operator you will use to find the remainder
of a division. For
example, 4%2 will return 0 instead of 2.
4. Addition (+): The
classic plus sign (+). The most popular operator of all, a guy with no
knowledge about programming is still familiar with it, because it was in your
maths syllabus when you were in 1st Grade. I hope this doesn’t need
any introduction, but for the sake of it....here it is.... This operator performs addition in Python. For example, 2+2 will return 4.
5. Subtraction (-): The second most popular operator, this was the next chapter after
addition. The ‘-‘ operator performs
subtraction in Python. For example, 10-6
will return 4.
6. Absolute Division (//): You may not have learnt about this in your mathematics class because this is more/less exclusive to programming, but I will try to explain this the best way I can. This operator performs an absolute division. This means that if you want to perform a division and get its value in its absolute form (without the decimal point), you can use this operator. For example, 11/2 will return 5.5 BUT 11//2 will return 5. See the difference?? The second output is in its absolute form i.e. the decimal is removed.
Here are all the operators at a
glance:
Action |
Operator |
Exponent |
** |
Division |
/ |
Modulus |
% |
Addition |
+ |
Subtraction |
- |
Absolute Division |
// |
ORDER OF OPERATORS:
Order of operators is a set of rules
used in mathematical calculations to evaluate an expression. Python follows
BODMAS (Brackets() Off(*) Division(/) Multiplication(*) Addition(+) Subtraction(-)) rule.
CONGRATULATIONS !!!! Now you know about
Arithmetic operators in Python and the order in which they are evaluated by
Python.
0 Comments
Welcome to the comments section, this is where you can contact me personally for any doubts or feedback