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.
OPERATORNAMEEXAMPLEACTION
+AdditionX+YAdds two numbers
SubtractionX-YSubtracts two numbers
*MultiplicationX*YMultiplies two numbers
/DivisionX/YDivides X with Y and gives the Quotient
%ModulusX%YDivides X with Y and gives Remainder.
//Floor DivisionX//YDivides 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.
OPERATORNAMEEXAMPLEACTION
=AssignmentX=3+5Assign value from right to left variable
+=AdditionX+=YMeans X=X+Y
-=SubtractionX-=YMeans X=X-Y
*=MultiplicationX*=YMeans X=X*Y
/=DivisionX/=YMeans X=X/Y
%=ModulusX%=YMeans X=X%Y
//=Floor DivisionX//=YMeans 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.
OPERATORNAMEEXAMPLEACTION
andLogical ANDX and YX and Y conditions are verified and if both are true then output comes “True”
orLogical ORX or YX and Y conditions are verified and if any of them is true, then output is “True”
notLogical NOTnot(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.
OPERATORNAMEEXAMPLEACTION
==Equal toX==YCompares X and Y
>Greater thanX>YChecks if X is greater than Y
>=Greater than equal toX>=YChecks if X is greater than or equal to Y
<Less thanX<YChecks if X is less than Y
<=Less than equal toX<=YCheck if X is less than or equal to Y
!=Not equal toX!=YChecks 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.
OPERATORNAMEEXAMPLEACTION
isIS operatorX is YReturns true if X and Y are the same objects
is notIS NOT operatorX is not YReturns 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.
OPERATORNAMEEXAMPLEACTION
&ANDX&YCompares X and Y bits and performs AND operation between individual bits
|ORX|YCompares X and Y bits and performs OR operation between individual bits
^XORX^YCompares X and Y bits and performs XOR operation between individual bits
~NOTX~YCompares 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.