Python Operators

Operators are building blocks of all kind of operation we can do in any programming language. Operators are basically the symbols which operate on at least one or more operands. Operators are surrounded by literals or variables which are called operands.

For Example: If you consider the expression 9 + 3 = 10 . 9, 3 and 10 are called operands. And the symbol + is called operator.

Operator vs Operands
Operator vs Operands

Python offers several types of operators which are,

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

Arithmetic Operators

Python arithmetic operators help to perform mathematical operation like, addition, subtraction, multiplication etc. You will find these similar across different programming languages.

+ AdditionAdds Both Operands x + y
SubtractionSubtracts right hand side operand from left hand side operandx – y
*MultiplicationMultiplies Both operands x * y
/DivisionDivides the left operand by right operand x / y
%ModulusDivides the left operand by right operand but returns remainderx % y
** Exponent Computes the power a ** 2
//Floor DivisionIt is like division operator /. But it returns the floored value of the result. For example 5/2 is 2.5 , but floor division results only 2.x // y

Comparison Operators

Comparison operators are also called as relational operators.  These are building blocks of branching and decision making blocks of code you write. These  operators return Boolean True or False based on the condition satisfied or not 

== Equal to operator
This is operator compares the both left and right operands and return true if equal else flase
x == y
!= Not Equal to This is opposite to equal to operator. Where this operator return if both operands are not equal x != y
<> Not Equal to This is same as not equal to != . Condition becomes true only if both operands are not equal to. This is removed in python 3 x <> y
> Greater than The condition becomes true if left hand operand is greater than right operand x > y
< Less than The condition becomes true only if left hand operand is less than right operand x < y
>= Greater than or equal to The condition becomes true only if left hand operand is greater than or equal to the right hand operand x <= y
<= Less than or equal to The condition becomes true only if left hand operand is less than or equal to the right hand operand

Logical Operators 

Like any other programming languages python offers 3 logical operators. These logical operators can be applied to any literal or variable not just limited to Boolean. Like comparison operators logical operators also return either True or False. These are like conjunction words in programming languages. Logical operators help us to combine multiple conditions to write complex conditionals. These operators are not limited to use with multiple condition they can be used as short hands  or  Short Circuit Evaluations  .  If they are used in short circuiting, they return the value instead of boolean. 

andLogical ANDCondition becomes true if both operands are true(2 > x) and (2 < y)
orLogical ORCondition becomes true if at least one operand is true (2 > x) or (2 < y)
notLogical NOTIt reverses the truth value of it’s operand. Returns True if operand evaluated as False, returns False if operand Evaluated as True not x

Membership Operators 

Membership operators in python are used to check whether an element is present in the container or not. Custom Objects can implement this operator using magic method called __contains__ . This operators can be used on container data types which includes sequence and mapping data types like string,  lists and dictionaries.  It doesn’t work with scalar data types int and float. 

inin
Returns True if Container or sequence contains given value
2 in [1, 2, 3, 4, 5]
not innot inReturns True if Container or Sequence doesn’t contain given value 2 not in [4, 9 , 34]

Identity Operators 

Identity operator compares the id(said to be address) of both operand (objects) instead of their value and return True both are same. Same id  mean both objects are same not only by their value but also by the address they are referring to object in the memory. 

isReturns True if both variables refers to the same objectx is y
is notReturns False if both variables refers to the same objectx is not y

Assignment Operator 

Assignment operator plays a vital role in programming. It is used to assign a value to the variable. 

=Assign value of right hand operand to the variable on the left handx = 2

Bitwise Operators 

As the name says, bitwise operators work on bits. These operations are performed on integers of any number system, but operators perform operation on binary equivalent of given operands. 

&Bitwise ANDPerforms Binary AND operationx & y
|Bitwise ORPerforms Binary OR operationx | y
^ Bitwise XOR/ Exclusive ORPerforms Binary XOR.x ^ y
~BinaryOnes ComplementUnary Operator. Flipps the bits~x
<<Binary Left ShiftLeft hand operand value is changed by moving the number of bits to the left specified on rightx << 3
>>Binary Right ShiftLeft hand operand value is changed by moving the number of bits to the right specified on right x >> 3

Compound Assignment Operator 

If you both relational and assignment operators you get compound assignment operators. Compound assignment Operators are also called short hand operators 

+=Add and assignx += 1x = x+1
-=Subtract and Assignx -= 1x = x -1
*=Multiply and Assignx *= 1x = x * 1
/=Divide and Assignx /= 1x = x /1
%=Modulus and Assignx %= 2x = x % 2
**=Exponent and Assignx **= 2x = x ** 2
//=Floor Divide and Assignx //=2x = x // 2
&=Bitwise And and Assignx &= 2x = x & 2
|=Bitwise OR and Assignx |= 2 x = x | 2
^=Bitwise XOR and Assignx ^= 2x = x ^ 2
<<=Binary Left Shift and Assignx <<= 2x = x << 2
>>= Binary Right Shift and Assignx >> =2x = x>>2

Miscellaneous Operators 

This one included all other operators we use which don’t fall under any one of above category, These operators are

.dotThis operators is used to access attributes or methods of objectmath.sqrt(3)
()groupThese operators are used to group expression to increase it’s precedence(x + y) * z
[]subscript / indexUsed to access elements/ items of containers/sequencesx[1]
y[‘name’]
::SliceUsed on sequences to extract part of the sequencex[::]
y[2:]
EllipsisSpecial value used in extending slice syntax for user defined containers

Leave a Reply

Your email address will not be published. Required fields are marked *