Arithmetic Operations in Python

Beginners code Computer Skills Mobile Development Python Web Development

Python allows us to perform complex arithmetic operations as it supports a wide range of arithmetic operators to work on. Python supports operators which are mostly binary type, but it has also got few unary operators and ternary operators.

NOTE:

  • Unary operators are those which work on only one operand.
  • Similarly, binary operators need two operands to perform the arithmetic operation.

For arithmetic operations, Operators in python can be classified under the following categories:

There are many operators which can be used at appropriate places and can allow us to reduce code length and simultaneously make the code efficient. So, we will be exploring most of the operators in a structured tabular format. This will enable us to have a rough overview of which kind of operations fall under which sort of categories mentioned above.

ARITHMETIC OPERATORS

  • These operators allow us to perform simple arithmetic operations.
  • For the following examples in the table,
    • We have considered two variables X and Y.
    • These two variables can be of any numeric datatype.
OPERATOR NAME EXAMPLE ACTION
+ Addition X+Y Adds two numbers
Subtraction X-Y Subtracts two numbers
* Multiplication X*Y Multiplies two numbers
/ Division X/Y Divides X with Y and gives the Quotient
% Modulus X%Y Divides X with Y and gives Remainder.
// Floor Division X//Y Divides X and Y gives the floor of Quotient

ASSIGNMENT OPERATORS

  • Assignment operators are used for condensing the code when an arithmetic operation has to update any variable it is currently working on.
  • In the examples for this table,
    • we have taken two variables X and Y which are associated with numeral datatype.
OPERATOR NAME EXAMPLE ACTION
= Assignment X=3+5 Assign value from right to left variable
+= Addition X+=Y Means X=X+Y
-= Subtraction X-=Y Means X=X-Y
*= Multiplication X*=Y Means X=X*Y
/= Division X/=Y Means X=X/Y
%= Modulus X%=Y Means X=X%Y
//= Floor Division X//=Y Means X=X//Y

LOGICAL OPERATORS

  • Logical operators are used for checking the logical meaning of the given condition in a code.
  • Here in the following examples,
    • the X and Y taken are actually representing some conditions which when verified, can select one right solution out of possible solutions given for a program.
OPERATOR NAME EXAMPLE ACTION
and Logical AND X and Y X and Y conditions are verified and if both are true then output comes “True”
or Logical OR X or Y X and Y conditions are verified and if any of them is true, then output is “True”
not Logical NOT not(X and/or Y) X and Y are operated on and/or operator and whatever the output is, it’s opposite is taken.

 

COMPARISON/RELATIONAL OPERATORS

  • Relational operators are used in decision making.
  • They are used to compare two decisions and according to the output the program will traverse in the selected condition path.
  • The operators basically create an accept-reject situation wherein one condition, out of the provided solutions has to be accepted while the others have to be rejected.
OPERATOR NAME EXAMPLE ACTION
== Equal to X==Y Compares X and Y
> Greater than X>Y Checks if X is greater than Y
>= Greater than equal to X>=Y Checks if X is greater than or equal to Y
< Less than X<Y Checks if X is less than Y
<= Less than equal to X<=Y Check if X is less than or equal to Y
!= Not equal to X!=Y Checks if X is not equal to Y

 

IDENTITY OPERATORS

  • The operators are used mostly from the object-oriented programming point of view.
  • In this case, we have created two instances of any function or class and
  • We have used identity operators to see if these two instances created are the same or not.
OPERATOR NAME EXAMPLE ACTION
is IS operator X is Y Returns true if X and Y are the same objects
is not IS NOT operator X is not Y Returns true if X and Y are not the same objects

 

BITWISE OPERATORS

  • These operators convert the given input of a variable into bits and then process individual bits of the given input.
  • In simpler words, they are also comparing operators, except everything is done in bits.
OPERATOR NAME EXAMPLE ACTION
& AND X&Y Compares X and Y bits and performs AND operation between individual bits
| OR X|Y Compares X and Y bits and performs OR operation between individual bits
^ XOR X^Y Compares X and Y bits and performs XOR operation between individual bits
~ NOT X~Y Compares X and Y bits and performs NOT operation between individual bits

OPERATOR PRECEDENCE

From the above lists, we got to know about the arithmetic operations in python, the different types of operators and their functions individually. Now when these operators are combinedly present in an arithmetic expression, how Python will approach to solve?

To address this issue, Python provides operator precedence which presents all the above operators in decreasing order of importance of execution. Note that this feature is common in any other popular programming languages you know.

The table is as follows:

OPERATOR PRECEDENCE
~
*, /, %, //
+,-
&
^, |
>, >=, <, <=
==, !=
=, %=, /=, //=, -=, +=
is, is not
not, or, and

Using this operator precedence chart, Python sequentially solves every arithmetic equation.

Note that operators in Python are not restricted to this. There are a lot more inbuilt operators which can be used for higher complexity usages. As of now to get started, we need only these many operators to work on.