Python Operators

Operators are used to perform operations on any operands. They are used to evaluate different types of expressions and to manipulate the value of operands or variables by performing different type of operations.

There are 7 types of operators:

  1. Arithmetic Operators
  2. Comparison Operators
  3. Assignment Operators
  4. Logical Operators
  5. Bitwise Operators
  6. Membership Operators
  7. Identity Operators

Arithmetic Operators


Arithmetic operators are the operators used to perform arithmetic operations on numeric operands like addition, subtraction, multiplication, etc.
Different types of Arithmetic Operators:

Operator Usage Description
+ a+b Add values on either side of the operator.
- a-b Subtracts right hand side operand from left side of operand.
* a*b Multiple values on either side of the operator.
/ a/b Divide left hand side of operand by right hand side of operand.
% a%b Return remainder by dividing left hand side of operand by right hand side of operand.
** a**b Return exponent - left operand raised to the power of right.
// a//b Floor Division - The division of operands where the result is quotient in which digit after decimal point are removed. But if one of the operand is negative then result is floored.


Let's perform programs on above:

>>>3+2
5
>>>5-2
3
>>>2*3
6
>>>9/8
1.125
>>>5%2
1
>>>2**3
8
>>>9//8
1


Comparison Operators



These operators are used to compare the values of operands on either side of this operator. This operator return true or false Boolean values.
Different Comparison operators:

Operator Usage Description
== a==b Return true if the values on either side are equals otherwise false.
!= a!=b Return true if the values on either side are not equals otherwise false.
> a>b Return true if the value of operand on the left side is greater than the value of operand on right side.
< a Return true if the value of operand on the left side is lesser than the value of operand on right side.
>= a>=b Return true if the value of operand on the left side is greater than or equal to the value of operand on right side.
<= a<=b Return true if the value of operand on the left side is less than or equal to the value of operand on right side.


Let's perform programs on above:

>>>3==2
False
>>>3!=2
True
>>>2<=4
True


Assignment Operator



Assignment operator is used to assign a specific value to a variable.
The equal to (=) is used to assign value to a variable.
If we use arithmetic operators (+, -, /, *, etc) with equal to operator it will perform arithmetic operations result will be accordingly.

Let's see some example,

>>>x=20
>>>x
20
>>>x+=2
>>>x
22
>>>x-=5
>>>x
17


Logical Operator



The logical operators are used to perform logical operations and to combine two or more conditions to provide specific results i.e. true or false.
Different logical operators:

Operator Usage Description
and x and y True if both sides of operator are true.
or x or y True if either sidesof operator are true.
not not x True if operator is false.

Bitwise Operators


Bitwise operator acts on the operands bit by bit.
These operators take one ot two operands.
Some bitwise operators appear to be similar to logical operator but they are not.

Different Bitwise Operator:

Operator Usage Description
& (Binary AND) ( a&b) Operator copies a bit to the result if it coexists in both operands
| (Binary OR) ( a|b)
^ (Binary XOR) ( a ^ b) It copies the bit if it is set in one operand but not both.
~ (1's complement) ( ~ a) It is unary and has the effect of 'flipping' bits.
< < (Binary Left Shift) ( a < < 2) The left operands value is moved left by the number of bits specified by the right operand.
> > (Binary Right Shift) ( a > > 2) The left operands value is moved right by the number of bits specified by the right operand.

Membership Operators


The membership operators are used to test whether a value is in a specific sequence or not like in lists, tuples, string, etc. It returns True or False as output.

Different membership operators are:

Operator Usage Description
in x in y True if the value/operand in the left of the operator is present in the sequence in the right of the operator.
not in x not in y True if the value/operand in the left of the operator is not present in the sequence in the right of the operator.


Identity Operator


The identity operators are used to test whether a variable refers to same value/object or not. It returns True or False as output.

Different Identity Operator:

Operator Usage Description
is x is True True if both the operands refer to the same object.
is not x is not True Evaluates to false if the variables on either side of the operator point to the same object and true otherwise.

Back To Top