## 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.

Python offers several types of operators which are,

## Arithmetic Operators

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

 + Addition Adds Both Operands x + y – Subtraction Subtracts right hand side operand from left hand side operand x – y * Multiplication Multiplies Both operands x * y / Division Divides the left operand by right operand x / y % Modulus Divides the left operand by right operand but returns remainder x % y ** Exponent Computes the power a ** 2 // Floor Division It 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.

 and Logical AND Condition becomes true if both operands are true (2 > x) and (2 < y) or Logical OR Condition becomes true if at least one operand is true (2 > x) or (2 < y) not Logical NOT It 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.

 in in Returns True if Container or sequence contains given value 2 in [1, 2, 3, 4, 5] not in not in Returns 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.

 is Returns True if both variables refers to the same object x is y is not Returns False if both variables refers to the same object x 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 hand x = 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 AND Performs Binary AND operation x & y | Bitwise OR Performs Binary OR operation x | y ^ Bitwise XOR/ Exclusive OR Performs Binary XOR. x ^ y ~ BinaryOnes Complement Unary Operator. Flipps the bits ~x << Binary Left Shift Left hand operand value is changed by moving the number of bits to the left specified on right x << 3 >> Binary Right Shift Left 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 assign x += 1 x = x+1 -= Subtract and Assign x -= 1 x = x -1 *= Multiply and Assign x *= 1 x = x * 1 /= Divide and Assign x /= 1 x = x /1 %= Modulus and Assign x %= 2 x = x % 2 **= Exponent and Assign x **= 2 x = x ** 2 //= Floor Divide and Assign x //=2 x = x // 2 &= Bitwise And and Assign x &= 2 x = x & 2 |= Bitwise OR and Assign x |= 2 x = x | 2 ^= Bitwise XOR and Assign x ^= 2 x = x ^ 2 <<= Binary Left Shift and Assign x <<= 2 x = x << 2 >>= Binary Right Shift and Assign x >> =2 x = 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

 . dot This operators is used to access attributes or methods of object math.sqrt(3) () group These operators are used to group expression to increase it’s precedence (x + y) * z [] subscript / index Used to access elements/ items of containers/sequences xy[‘name’] :: Slice Used on sequences to extract part of the sequence x[::]y[2:] … Ellipsis Special value used in extending slice syntax for user defined containers